It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

273 views 1 2
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you