How to make the post function script to wait until the re-indexing completes

nallu October 14, 2019

Hi All,

I have written a custom post function (Groovy script) to auto-transition an Epic when all the child issues are resolved. I have added that in the right place in the workflow. But, the problem is in my script, I am checking all the child issues are closed through JQL. So, My script has to wait util the indexing gets complete. 

Can anyone please help me how to make my script to wait until the indexing get complete or Is there any other better solution to achieve my requirement?? Please help.

 

Here is my code, 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.util.ImportUtils;

if(issue.getIssueType().getName().toString() != 'Epic') {

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
IssueManager issueManager = ComponentAccessor.getIssueManager();
IssueService issueService = ComponentAccessor.getComponent(IssueService);
CustomField epicLinkField = customFieldManager.getCustomFieldObjectByName('Epic Link');
CustomField enable_auto_transition_parent_issue = customFieldManager.getCustomFieldObject('customfield_17701');

//lookup the corresponding epic via the subtask's parent
MutableIssue epic = issueManager.getIssueObject(
(String)issue.getCustomFieldValue(epicLinkField)
);

if(epic != null) {

//Get JQL query parser and search provider object
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchProvider = ComponentAccessor.getComponent(SearchProvider);

// Get user object of a user to transition an epic
def userManager = ComponentAccessor.getUserManager() as UserManager;
ApplicationUser user = userManager.getUserByKey('svcjir_atadmin');

def queryString = "issue in childIssuesOf(${epic.key}) AND resolutiondate is EMPTY";
def query = jqlQueryParser.parseQuery(queryString);
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());
def no_of_opened_child_issues = results.getIssues().size();

log.error("${results.getIssues().size()}")
log.error("${results.getIssues()[0].key}")
log.error("${results.getIssues()[0].status.name}")

if(issue.getCustomFieldValue(enable_auto_transition_parent_issue).toString() == 'ENABLED' && no_of_opened_child_issues == 0) {

IssueInputParameters issueInputParameters = new IssueInputParametersImpl([:]);

// Get access to the Jira comment and component manager
CommentManager commentManager = ComponentAccessor.getCommentManager();

// 61 is the ID of 'Done' trantion in Non-code Sequenced Workflow which supports Epic issue types
IssueService.TransitionValidationResult validationResult =
issueService.validateTransition(user,
epic.id, 61 as Integer, issueInputParameters)

def errorCollection = validationResult.errorCollection

if (! errorCollection.hasAnyErrors()) {
issueService.transition(user, validationResult)

// Create a comment on the issue
def comment = "Moving this epic to 'Done' since all the child issues are closed!! Please feel free to re-open it if it is still an active epic. ";
commentManager.create(epic, user, comment, true);
}
}
}
}

//End of the scripts;

1 answer

0 votes
fran garcia gomera
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.
October 14, 2019
nallu October 14, 2019

Yeah. I have tried that too. But no luck. The thing is, It is not waiting until the re-indexing thread to get completed. 

Suggest an answer

Log in or Sign up to answer