Can you help me with reIndex problem?

Scott Evans October 11, 2012

I am using JIRA 5.0.6 download and ScriptRunner 2.0.7.

I have the following code that sets a custom field to a set of issues (retrieved from a search). It is a stripped down version of my real code. I wouldn't go through this trouble to do something a bulk edit could do.

After I set the value I do a reIndex of the issue. In the GUI, I can filter on those issues and see that the custom field gets set, but it seems to clear out my "resolved" (closed date) field. This messes up any Created Vs. Resolved graphs.

A manual Index of the DB clears the issue up. Can someone tell me what I am doing wrong? I don't want to do a manula Index after runnig this Listener.

package com.custom
 
import com.atlassian.jira.event.issue.IssueEvent
import org.apache.log4j.Category
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
//import com.atlassian.jira.issue.history
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutManager;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.fields.layout.field.EditableFieldLayout;
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.query.Query

//----------------------------------------------------------------------
// UpdateRT Listener Test
//
// Sets a custom field named ReleaseTrain to the value of "November" if the fixVersion is "s3220-c5"
//     

class UpdateRTTestListener extends AbstractIssueEventListener {
   Category log = Category.getInstance(UpdateRTTestListener.class)
   

    ComponentManager  cm           = ComponentManager.getInstance()
    def               cfm          = cm.getCustomFieldManager()
    def               lm           = cm.getFieldLayoutManager()
    def               fm           = cm.getFieldManager()
    def               chm          = cm.getChangeHistoryManager()
    def               im           = cm.getIndexManager()
    
    @Override
    void workflowEvent(IssueEvent event) {
        MutableIssue issue = event.issue as MutableIssue    
        log.setLevel(org.apache.log4j.Level.DEBUG)
        
        //Get current state of the indexer (usually false/disabled)
        def wasIndexing = ImportUtils.indexIssues;
        ImportUtils.indexIssues = true;

        // Get some values before proceeding

               
        // Find all the tickets with the same fixVersion and set their ReleaseTrain
        JqlQueryBuilder sub_builder = JqlQueryBuilder.newBuilder()
        sub_builder.where().fixVersion("s3220-c5")
        Query sub_query = sub_builder.buildQuery()
        SearchService ipsearchService = cm.getInstance().getSearchService();
        def ticket_results = ipsearchService.search(cm.getJiraAuthenticationContext()?.getUser(), sub_query, PagerFilter.getUnlimitedFilter())
                   
        CustomField       ReleaseTrain  = cfm.getCustomFieldObjectByName("ReleaseTrain")

        // For each ticket found, update the ReleaseTrain value
        for (Issue ip_issue in ticket_results.getIssues()) {
            def  IPReleaseTrain = ip_issue.getCustomFieldValue(ReleaseTrain)
            log.debug "Setting ReleaseTrain on issue "+ip_issue.getKey()+" to (November)"
                       
            ModifiedValue ipsubValue = new ModifiedValue(IPReleaseTrain, "November")
            def ipFieldLayoutItem = lm.getFieldLayout(ip_issue).getFieldLayoutItem(ReleaseTrain);
            ReleaseTrain.updateValue(ipFieldLayoutItem, ip_issue, ipsubValue, new DefaultIssueChangeHolder())
            ReleaseTrain.store()
            im.reIndex(ip_issue)
        }

        //Reset the indexer to what it was before.
        ImportUtils.indexIssues = wasIndexing;
    }
}

2 answers

1 accepted

0 votes
Answer accepted
Scott Evans October 15, 2012

I found a way around my problem or maybe found the right way to do it. My loop code looks like:

for (Issue ip_issue in ticket_results.getIssues()) {
            MutableIssue mip_issue = issueM.getIssueObject(ip_issue.getKey())
            // Set parameters to change
            IssueInputParameters issueInputParameters = new IssueInputParametersImpl();            
            issueInputParameters.addCustomFieldValue(cfReleaseTrain.getId(), "November")
            // Create Issue Service and update issue
            IssueService issueService = ComponentManager.getInstance().getIssueService();
            def updateValidationResult = issueService.validateUpdate(a_user, mip_issue.getLong("id"), issueInputParameters);
            if (updateValidationResult.isValid()) {
                issueService.update(a_user, updateValidationResult);
            }
        }

Using the IssueService also leaves a History statement showing who made the change.

0 votes
lrobertson39
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 11, 2012

Hi George,

As part of your Jira implementation is it set to disallow issues from being edited after they have been closed? If so, maybe this is a bug within Jira that allows a plugin to modify a Closed ticket and then when re-indexed, Jira is unsure of the state due to it being recently edited.

Scott Evans October 11, 2012

Luke,

I checked every state in every workflow. They are all editable. I allow closed tickets to be edited so we can add errata notes later (and change their release date).

George

Suggest an answer

Log in or Sign up to answer