如何添加新值以动态地或使用插件在jira中选择列表字段

时间:2014-04-06 10:03:27

标签: jira jira-plugin jira-rest-java-api

这是我的用例 1.我有一个"客户名称"文本字段和"所有客户"单选列表。
2.当问题解决后,我想选择"客户名称"并希望添加"所有客户"。

我能够实现这个目标如果要添加的值已经存在于"所有客户"中。但我想填充所有客户"如果字段不存在则具有新值,以便将来可供选择。 这该怎么做?是否有可能这样做?

1 个答案:

答案 0 :(得分:5)

我找到了答案。这是完整的代码。它的jira插件可以解析已解析的事件并从一个自定义字段获取值并填充另一个自定义字段。希望能帮助其他人。

package com.spmsoftware.plugin.listeners;

import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.event.type.EventType;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;
import java.util.Map;

/**
 * User: adnan
 * Date: 5/4/14
 * Time: 4:49 PM
 */
public class IssueUpdateListener  implements InitializingBean, DisposableBean {

    private static final Logger LOGGER = Logger.getLogger(IssueUpdateListener.class);

    private final EventPublisher eventPublisher;
   // private final JiraAuthenticationContext authenticationContext;

    public IssueUpdateListener(EventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
       // this.authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        eventPublisher.register(this);
    }

    @Override
    public void destroy() throws Exception {
        eventPublisher.unregister(this);
    }

    @EventListener
    public void onIssueEvent(IssueEvent issueEvent) {
        Long eventTypeId = issueEvent.getEventTypeId();
        Issue issue = issueEvent.getIssue();
        if (eventTypeId.equals(EventType.ISSUE_RESOLVED_ID)) {
            MutableIssue mutableIssue = getMutableIssue(issue);
            CustomField customerNameCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Customer Name");
            CustomField allCustomersCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("All Customers");
            Object customerNameVal = mutableIssue.getCustomFieldValue(customerNameCF);
            Option newOptions = addOptionToCustomField(allCustomersCF, customerNameVal.toString());
            LOGGER.info("New updated option {}" + newOptions);
        }
    }

    private MutableIssue getMutableIssue(Issue issue) {
        MutableIssue mutableIssue;
        if (issue instanceof MutableIssue)   {
            mutableIssue = (MutableIssue)issue;
        } else {
            mutableIssue = ComponentAccessor.getIssueManager().getIssueObject(issue.getKey());
        }
        return mutableIssue;
    }


    public Option addOptionToCustomField(CustomField customField, String value) {
        Option newOption = null;
        if (customField != null) {
            List<FieldConfigScheme> schemes = customField.getConfigurationSchemes();
            if (schemes != null && !schemes.isEmpty()) {
                FieldConfigScheme sc = schemes.get(0);
                Map configs = sc.getConfigsByConfig();
                if (configs != null && !configs.isEmpty()) {
                    FieldConfig config = (FieldConfig) configs.keySet().iterator().next();
                    OptionsManager optionsManager = ComponentAccessor.getOptionsManager();
                    Options l = optionsManager.getOptions(config);
                    int nextSequence = l.isEmpty() ? 1 : l.getRootOptions().size() + 1;
                    newOption = optionsManager.createOption(config, null, (long) nextSequence, value);
                }
            }
        }

        return newOption;
    }

}