Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Reindex not working in auto transition post function groovy script

Scott McCormick July 7, 2015

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();
}

7 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Scott McCormick August 18, 2015

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.

0 votes
Scott McCormick August 18, 2015

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

0 votes
JamieA
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.
August 18, 2015

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.

0 votes
Scott McCormick August 18, 2015

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

0 votes
JamieA
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.
August 18, 2015

Why use a separate thread to reindex?

0 votes
Scott McCormick August 12, 2015

@Jamie Echlin [Adaptavist] Maybe you can help?

0 votes
Scott McCormick July 13, 2015

Any issue indexing experts out there?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events