Hi all,
We are experiencing an issue with transitioning an issue using a for loop in a post function script. Somehow when an issue that we want to transition using the script has satisfied the condition we set up, it successfully transitions it. But if there are multiple issues that is needed to be transitioned, what happens is that only the first issue it sees is transitioned and then the script stops. We have checked and verified that the loop works as without the line containing the "issueService.Transition", it completes the loop but without the transition.
Adding our script here for reference:
import com.atlassian.jira.bc.issue.search.SearchService import com.atlassian.jira.bc.issue.IssueService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.search.SearchException import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.link.IssueLink import com.atlassian.jira.issue.link.IssueLinkManager import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.web.bean.PagerFilter import com.atlassian.jira.user.ApplicationUser import com.atlassian.jira.workflow.TransitionOptions
final String jqlSearch = "issue in linkedIssues(${issue})" // the jql query for the linked issue
log.warn("issue: " + issue) def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService) SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
//try {
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter) def parentIssues = results.results // jql query result
log.warn("Number of parent issues: " + parentIssues.size())
log.warn("Checking parent issue(es) and status")
for ( def i = 0; i < parentIssues.size(); i++) {
def parentIssueStatus = parentIssues[i].getStatus().getName()
log.warn("Parent issue and status: " + parentIssues[i].key + " : " + parentIssues[i].getStatus().getName())
log.warn("Checking linked issue(es) and status")
if ( parentIssues[i].issueType.name == "Test-Case" ) {
final String jqlQuery = "issue in linkedIssues(${parentIssues[i].key})" // the jql query for the parent issue
SearchService.ParseResult parseNewResult = searchService.parseQuery(user, jqlQuery)
if (parseNewResult.isValid()) {
def linkedResults = searchService.search(user, parseNewResult.query, PagerFilter.unlimitedFilter)
def linkedIssues = linkedResults.results // jql query result for linked issues on parent issue
for (link in linkedIssues) {
def linkedIssueStatus = link.getStatus().getName()
log.warn("Linked issue and status: " + link.key + " : " + link.getStatus().getName())
}
def statusCheckerDefect = linkedIssues.count { it.getStatus().getName().contains("Closed") } // Number of linked defect issues that has Passed Status
log.warn("Total number of linked issues: " + statusCheckerDefect)
def linkedIssuesCount = linkedIssues.size() - 1 // Number of linked issues
log.warn("Number of linked issues associated with parent ticket: " + linkedIssuesCount)
if ( linkedIssuesCount == statusCheckerDefect) { // this block for transition for blocked to unblocked and failed to for retest
IssueService issueService = ComponentAccessor.getIssueService()
if (parentIssueStatus == "Blocked") {
IssueService.TransitionValidationResult result = issueService.validateTransition(user, parentIssues[i].getId(), 201, issueService.newIssueInputParameters())
if (result.isValid()) {
log.warn("Moving ticket from Blocked to Unblocked")
issueService.transition(user, result)
log.warn("Continuing loop")
} else {
log.warn result.getErrorCollection().getErrors()
}
log.warn("Continuing loop2")
}
if (parentIssueStatus == "Failed") {
IssueService.TransitionValidationResult result = issueService.validateTransition(user, parentIssues[i].getId(), 241, issueService.newIssueInputParameters())
if (result.isValid()) {
log.warn("Moving ticket from Failed to For Retest")
issueService.transition(user, result)
log.warn("Continuing loop")
} else {
log.warn result.getErrorCollection().getErrors()
} log.warn("Continuing loop2")
} log.warn("Continuing loop3")
} log.warn("Continuing loop4")
} log.warn("Continuing loop5")
} log.warn("Continuing loop6")
}
} else {
log.warn("Invalid query")
return null
}