2016-07-29 36 views
1

因此,我正在開發一個JIRA項目,我需要做的事情之一是在初始問題創建中基於選定的複選框自動創建子任務。所有的值都得到了正確的輸入,但是子任務不會被創建,並且沒有錯誤需要檢查。我把錯誤集合放在子任務創建驗證結果返回false,但它沒有報告任何錯誤,當我運行的代碼。JIRA - 試圖根據複選框自動創建子任務。 Subtask不會被創建,但不會收集錯誤

我錯過了什麼?

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
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也有isValidgetErrorCollection方法。您應該使用這些來檢查在創建問題期間是否發生錯誤。