尝试根据复选框自动创建子任务。子任务不会被创建但是没有收集错误

时间:2016-07-29 11:00:08

标签: java groovy jira

我正在开发一个JIRA项目,我需要做的一件事是根据初始问题创建中的选定复选框自动创建子任务。所有值都正确输入,但是不会生成子任务并且没有要检查的错误。我输入错误集合是子任务创建验证结果返回false,但是当我运行代码时它没有报告任何错误。

我错过了什么?

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.project.Project
import java.lang.Boolean;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.config.SubTaskManager

def customFieldManager = ComponentAccessor.getCustomFieldManager() 
def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager() 
def cfsubTasks = customFieldManager.getCustomFieldObjectByName("CRCs Needed")
def checkboxTypeIDs = ['Endpoint':"11309", "IP/DNS Blocklist":"11311", "Network Signature":"11310", "PCAP Pull":"11306", "Splunk Dashboard":"11305"]
def summaryMap = ['Endpoint':"Endpoint Countermeasure", "IP/DNS Blocklist":"IP/DNS Countermeasure", "Network Signature":"Signature Countermeasure", "PCAP Pull":"PCAP Pull", "Splunk Dashboard":"Splunk Dashboard"]
def subTasks = issue.getCustomFieldValue(cfsubTasks);

SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
IssueService issueService = ComponentAccessor.getIssueService();
Project project = issue.getProjectObject();
Long issueId = issue.getId()
JiraAuthenticationContext authenticationContext = ComponentAccessor.getOSGiComponentInstanceOfType(JiraAuthenticationContext.class);
Object userObject = authenticationContext.getUser()
User user = userObject instanceof ApplicationUser ? ((ApplicationUser) userObject).getDirectoryUser() : (User) userObject;

subTasks.each(){
    def key = it.toString()
    IssueInputParameters inputParameters = issueService.newIssueInputParameters()
    inputParameters.setProjectId(project.getId())
    inputParameters.setSummary(summaryMap[key])
    inputParameters.setIssueTypeId(checkboxTypeIDs[key])
    inputParameters.setReporterId(user.getName())
    inputParameters.setApplyDefaultValuesWhenParameterNotProvided(true)

    CreateValidationResult createSubTaskValidationResult = issueService.validateSubTaskCreate(userObject, issueId, inputParameters)
    if (!createSubTaskValidationResult.isValid()) {
            Collection<String> errors = createSubTaskValidationResult.getErrorCollection().getErrorMessages();
            log.error("Sub-task cannot be created due to following reasons: " + errors);
            return;
    } else {
            IssueResult issueResult = issueService.create(userObject, createSubTaskValidationResult)
            Issue subTask = issueResult.getIssue()
            subTaskManager.createSubTaskIssueLink(issue, subTask, user)
            log.debug("Sub-task " + subTask + " for issue " + issue + " is created");
    }
}

1 个答案:

答案 0 :(得分:1)

else {
        IssueResult issueResult = issueService.create(userObject, createSubTaskValidationResult)
        Issue subTask = issueResult.getIssue()
        subTaskManager.createSubTaskIssueLink(issue, subTask, user)
        log.debug("Sub-task " + subTask + " for issue " + issue + " is created");
}

在这段代码中,当您尝试创建问题时,您会收到 IssueResult 实例。 IssueResult 还具有 isValid getErrorCollection 方法。您应该使用它们来检查在创建问题期间是否发生任何错误。