以下代码为 SW Version 自定义字段中选择的每个版本创建3个子任务。这作为后期功能放在工作流转换上并且完美地运行。但是,如果重新访问工作流转换,则会为已选择的版本创建重复的子任务。
实施例: SW Version字段有5个选项: 1,2,3,4,5
如果用户选择了1和2并继续转换,则将为所选的每个版本创建6个子任务3。 (一切都很好)
如果用户将SW Version字段更新为已经预先选择1和2的1,2,3和4,则将创建12个附加子任务,使得总共18个具有6个重复的1和2。
我想检查一下这些副本,所以基本的逻辑是如果已经存在的子任务转到下一个。对于所选的每个版本,请检查子任务是否已存在并继续。我尝试过各种方式并且失败了。已注释掉下面的一些代码以获取子任务的摘要,并与所有现有的子任务摘要进行比较,但它不起作用。
另外,我试过这样做无济于事。
if(issue.getSubTaskObjects()*.summary.equals(summaryText)){
log.info("Subtask already exists")
return;
任何帮助将不胜感激!
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.index.IssueIndexingService
import org.apache.log4j.Logger
import org.apache.log4j.Level
log.info("Processing: " + issue.key);
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager
IssueManager issueManager = ComponentAccessor.getIssueManager();
def cfM119 = customFieldManager.getCustomFieldObjectByName("SW Version")
log.info("cfM119: " + cfM119)
def m119VersionArray = issue.getCustomFieldValue(cfM119) as String[]
def reqAssignee = 'user1'
def swAssignee = 'user2'
def testAssignee = 'user3'
//collecting subtask object
//Collection allsubtasks = issue.getSubTaskObjects()
//for(Issue allsubtask: allsubtasks) {
//def subtaskSummary = allsubtask.getSummary() as String[]
//log.info("Subtask Summary" + subtaskSummary)
//if (subtaskSummary[]){
// log.info("Subtask already exists")}
//else {
m119VersionArray.each{ version ->
createSubTask("", version, "_Approved_REQ", reqAssignee)
createSubTask("", version, "_Approved_SW", swAssignee)
createSubTask("", version, "_Approved_TEST", testAssignee)
}
def createSubTask(String component, version, type, String assignee) {
def Long issueLinkType = new Long (10702)
def Long sequence = new Long (1)
//Issue issue
def summaryText = component + version + " " + type
def issueManager = ComponentAccessor.issueManager
def issueFactory = ComponentAccessor.issueFactory
def subTaskManager = ComponentAccessor.subTaskManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def userManager = ComponentAccessor.userManager
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
if(issue.getSubTaskObjects()*.summary.equals(summaryText)){
log.info("Subtask already exists")
return;
}
// Defining subtask
def newIssue = issueFactory.getIssue()
newIssue.setIssueTypeId("5")
newIssue.setParentId(issue.getId())
newIssue.setProjectObject(issue.getProjectObject())
newIssue.setSummary(summaryText)
newIssue.setAssignee(userManager.getUserByName(assignee))
newIssue.setDescription(issue.getDescription())
log.info("Creating subtask - " + summaryText)
def subTask = issueManager.createIssueObject(authenticationContext.getLoggedInUser(), newIssue)
subTaskManager.createSubTaskIssueLink(issue, subTask, authenticationContext.getLoggedInUser())
issueLinkManager.createIssueLink(issue.getId(), newIssue.getId(), issueLinkType, sequence, authenticationContext.getLoggedInUser())
// reindex
ImportUtils.setIndexIssues(true)
IssueIndexingService issueIndexService =
ComponentAccessor.getComponent(IssueIndexingService.class)
issueIndexService.reIndex(subTask)
ImportUtils.setIndexIssues(false)
}
答案 0 :(得分:0)
它不仅仅因为issue.getSubTaskObjects()*.summary
返回数组,然后尝试将此数组与.equals(summaryText)
中的字符串进行比较而无法工作。但是您已经接近了,如果将这一部分更改为issue.getSubTaskObjects()*.summary.contains(summaryText)
(意味着您在每个数组元素中搜索summaryText
),它将可以很好地工作。
在我看来,下一个代码将更加易于理解和清晰issue.getSubTaskObjects().find{it.getSummary() == summaryText}
(在if
语句中也可以使用)