Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Help with Custom script post function (Script Runner)

Deleted user January 9, 2019 edited

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

1 answer

1 accepted

0 votes
Answer accepted
Payne
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 9, 2019

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}")
}
}
}
Deleted user January 9, 2019

Hello @Payne ,

 

Thanks so much for the code. I realized where did I go wrong. Very helpful!

 

Regards,

Swathi

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events