Hello,
I am trying to auto close an issue (portfolio parent issue) when ALL the child issues (which are connected through parent link field) are closed/done.
For assumption :
Assume PR-1 (initiative) is parent issue which has 3 children PR-2, PR-3, PR-4. I am trying to auto close PR-1 when each of the children is moved to the Done state. Here is the script I am using :
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.search.SearchProvider
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLink = customFieldManager.getCustomFieldObjectByName('Parent Link') // getting the Parent Link custom field
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def customFieldValue = issue.getCustomFieldValue(parentLink) // Parent Link field value
def parentKey = (String) customFieldValue
def parentIssue = issueManager.getIssueObject(parentKey) // Parent issue object
def jqlSearch = "issuekey in childIssuesOf("+ parentIssue.getKey() + ")" ;
log.warn('JQL: ' + jqlSearch);
def query = jqlQueryParser.parseQuery(jqlSearch)
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())
results.getIssues().each {documentIssue ->
def issue1 = issueManager.getIssueObject(documentIssue.id)
if (issue1.getStatus().name == "Done")
{
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
}
// Validate transitioning the parent issue to "Done"
def validationResult = issueService.validateTransition(user, parentIssue.id, 41, issueInputParameters)
if (validationResult.isValid()) {
log.debug("Validation Result is valid")
// Perform the transition
def issueResult = issueService.transition(user, validationResult)
if (! issueResult.isValid()) {
log.debug("Failed to transition task ${parentIssue.key}, errors: ${issueResult.errorCollection}")
}
}
}
}
This script is working or moving the parent (PR-1) to Done even if one child is marked as Done. It is NOT iterating or waiting for the 3 children. Could anyone let me know what is the issue here?
Thanks,
Swathi
Hi Swathi,
Currently you're iterating through the subtasks, and if any one is "Done", then you are executing the block of code that transitions the parent. I would modify the structure to be something like the following.
First, iterate through all of the subtasks and set allTasksDone to true if they are all "Done" and false if at least one is not "Done".
def allTasksDone = true
results.getIssues().each {documentIssue ->
def issue1 = issueManager.getIssueObject(documentIssue.id)
if (issue1.getStatus().name != "Done")
{
allTasksDone = false;
}
}
Then if allTasksDone is true, execute the block the transitions the parent:
if (allTasksDone == true)
{
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
}
// Validate transitioning the parent issue to "Done"
def validationResult = issueService.validateTransition(user, parentIssue.id, 41, issueInputParameters)
if (validationResult.isValid()) {
log.debug("Validation Result is valid")
// Perform the transition
def issueResult = issueService.transition(user, validationResult)
if (! issueResult.isValid()) {
log.debug("Failed to transition task ${parentIssue.key}, errors: ${issueResult.errorCollection}")
}
}
}
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.