I have a groovy script that runs as the last post-function of an 'Approve & Close' transition for subtasks.
The script auto-transitions the parent issue if all subtasks are closed and some additional conditions are true.
When I run my script the parent issue is successfully transitioned and a comment is added, however, the Status shown in the parent issue (and in the issue navigator) is not updated.
This seems to be a problem with reindexing, but all my reindexing attempts have not resolved the problem.
I have tried using a different thread to reload/reindex the parent issue, but it is still not working.
I am on JIRA 5.2.11.
Thanks for your help!
import com.atlassian.jira.ComponentManager import com.atlassian.jira.bc.issue.IssueService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.IssueInputParameters import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.comments.CommentManager; import com.atlassian.jira.issue.link.IssueLinkManager; import com.atlassian.jira.security.JiraAuthenticationContext import com.atlassian.jira.util.ImportUtils import com.atlassian.jira.workflow.IssueWorkflowManager import com.atlassian.crowd.embedded.api.User; import com.atlassian.jira.issue.index.IndexException; import org.apache.log4j.Logger; import com.custom.capa.ClassCapaReportApproval; def Logger log = Logger.getLogger("com.onresolve.jira.groovy.PostFunction"); IssueManager issueManager = ComponentAccessor.getIssueManager(); IssueService issueService = ComponentAccessor.getIssueService(); CommentManager commentManager = ComponentAccessor.getCommentManager(); IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager(); IssueWorkflowManager issueWorkflowManager = ComponentManager.getComponentInstanceOfType(IssueWorkflowManager.class); JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext(); def userUtil = ComponentAccessor.getUserUtil(); User adminUser = userUtil.getUser('jiraadmin'); User user = authenticationContext.getLoggedInUser(); issue = (MutableIssue) issue; Issue parentIssue; // Get parent issue issueLinkManager.getInwardLinks(issue.getId()).each{issueLink -> if ("is a report approval for" == issueLink.issueLinkType.inward) { parentIssue = issueLink.sourceObject; } } // verify current subtask issue is Closed int targetActionId; if (issue.statusObject.name == 'Closed') { if (parentIssue != null) { // check if other report approvals for this step are closed and this approval step is done: boolean reportApprovalsDone = ClassCapaReportApproval.checkCapaReportApprovalSubtaskCondition(parentIssue); issueWorkflowManager.getAvailableActions(parentIssue).each{action -> if (action.getName() =~ /Containment Plan Complete|Interim Complete/) { targetActionId = action.getId(); } } String statusName = parentIssue.statusObject.name; if (statusName =~ /CAPA Containment|CAPA Interim/) { if (targetActionId && reportApprovalsDone) { IssueInputParameters issueInputParameters = issueService.newIssueInputParameters(); IssueService.TransitionValidationResult transitionValidationResult = issueService.validateTransition(adminUser,parentIssue.getId(),targetActionId,issueInputParameters); if (transitionValidationResult.isValid()) { IssueService.IssueResult transitionResult = issueService.transition(user, transitionValidationResult); if (!transitionResult.isValid()) { log.error("auto-transition validation passed but doing the auto-transition failed for " + parentIssue.getKey()); } else { // add comment to parent String autoTransitionComment = "Auto-transition triggered by " + user.getDisplayName() + " approving " + issue.getKey(); commentManager.create(parentIssue,adminUser.getName(),autoTransitionComment,true); // reload and then reindex it in a separate thread reindexInNewThread(parentIssue); } } else { log.error("auto-transition validation failed for " + parentIssue.getKey()); } } } } } private void reindexInNewThread(Issue issueToReindex) { Thread thread = new Thread(new Runnable() { void run() { boolean wasIndexing = ImportUtils.isIndexIssues(); ImportUtils.setIndexIssues(true); try { Thread.sleep(2000); ComponentAccessor.getIssueIndexManager().reIndex(ComponentAccessor.getIssueManager().getIssueObject(issueToReindex.getId())); } catch (IndexException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ImportUtils.setIndexIssues(wasIndexing); } }); thread.start(); }
Community moderators have prevented the ability to post new answers.
It is doing both. If I look in the parent issue activity history ('All' or 'Transitions Summary' tab), I see that the Status changed (it was auto-transitioned) and a comment was added: 'Auto-transition triggered by ...' Also, the transition buttons available at the top of the page are updated as expected. However, the Status shown at the top of the page and in the issue navigator is not updated. Hence, it seems to be a problem with reindexing. To resolve it I tried: 1) moving my post-function earlier or later 2) using a separate thread to reindex.
I tried using these alternate reindexing methods, but the result was the same.
private void reindexInSameThread(Issue issueToReindex) { boolean wasIndexing = ImportUtils.isIndexIssues(); ImportUtils.setIndexIssues(true); try { ComponentAccessor.getIssueIndexManager().reIndex(ComponentAccessor.getIssueManager().getIssueObject(issueToReindex.getId())); } catch (IndexException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ImportUtils.setIndexIssues(wasIndexing); } private void reindexInSameThreadNoReload(Issue issueToReindex) { boolean wasIndexing = ImportUtils.isIndexIssues(); ImportUtils.setIndexIssues(true); try { ComponentAccessor.getIssueIndexManager().reIndex(issueToReindex); } catch (IndexException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } ImportUtils.setIndexIssues(wasIndexing); }
Also, I removed the groovy condition script I had in the parentIssue workflow that was being triggered after auto-transition script, but it made no difference.
Interestingly, even when I lock JIRA and re-index it, the issue status doesn't update for these auto-transitioned issues.
Here is what I am seeing in the issue screen.
The Status should show 'CAPA Root Cause & Corrective Action Plan':
wrong_status.JPG
History shows transition was performed and comment:
auto_transition_works.JPG
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If using a separate thread I'd expect you to have this problem. Actually you are only doing that if the transition fails - can you see which path it's taking? Ie, adding the comment or doing the transition.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Originally, I didn't use a thread, but based on your comments here I thought it was worth a try (although this is for a listener): https://answers.atlassian.com/questions/188231/strange-behaviour-with-performing-transitions-programmatically-from-listener
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Why use a separate thread to reindex?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.