Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Code Block
languagejava
linenumberstrue
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.web.bean.PagerFilter;

import java.util.List;



        //需要校正的数据 查询 出来
        String jql  = " 需求编号 is not Empty ";

        try {
            IssueManager issueManager = ComponentAccessor.getIssueManager();
            CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

            ApplicationUser loginUser = ComponentAccessor.getComponent(JiraAuthenticationContext.class).getLoggedInUser();
            SearchService searchService = ComponentAccessor.getComponentOfType(SearchService.class);
            SearchService.ParseResult parseResult = searchService.parseQuery(loginUser, jql);

            //需要处理的两个字段
            CustomField customFieldText = customFieldManager.getCustomFieldObject(11801L);//需求编号,文本类 
            CustomField customFieldScript = customFieldManager.getCustomFieldObject(11800L);//产品需求编号,issue picker 类

            if (parseResult.isValid()){
                SearchResults searchResults =searchService.search(loginUser,parseResult.getQuery(), PagerFilter.getUnlimitedFilter());
                List<Issue> issueList = searchResults.getResults();

                log.info("需要处理的数据数为:"+issueList.size())
                for (int i = 0; i <issueList.size() ; i++) {
                    MutableIssue mutableIssue = issueManager.getIssueObject(issueList.get(i).getId());
                    String fieldTextValue = (String) mutableIssue.getCustomFieldValue(customFieldText);
                    Issue pickerIssue = issueManager.getIssueObject(fieldTextValue);
                    mutableIssue.setCustomFieldValue(customFieldScript, pickerIssue);
                    issueManager.updateIssue(loginUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, true);

                    log.info("更新问题"+mutableIssue.getKey()+":"+fieldTextValue);
                    issueManager.updateIssue(loginUser,mutableIssue, EventDispatchOption.DO_NOT_DISPATCH,true);
                     log.info("更新问题"+mutableIssue.getKey()+":success ");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }