Issue reindex from groovy script?

Susanne Harelius [Riada]
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.
April 22, 2014

We are using a groovy script in our deployment workflow. Setting a reelasedate and a releasedatetime (one will go later). But our problem is that the issues that are updated with this date are not indexed so the JQL will not find them - the field is empty for the issue search. First when another update has been done it is possible to find them. I have inherited the script and is not very fluent in groovy (yet).

You can see that we have tried with issueIndexManager but with a script crash so it does not work. Very very thankful for any input here.

----------------

import com.atlassian.jira.bc.issue.search.SearchService

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.issue.changehistory.ChangeHistoryManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import java.sql.Timestamp

import java.util.Date

import com.atlassian.jira.issue.index.IssueIndexManager

 

def jqlQuery = "issueFunction in linkedIssuesOf(\"issuekey='${issue.getKey()}' and  cf[12130] in ('PROD')\", \"In Release\")"

def result = "Updated issues:"

Date date = new Date()

Timestamp currentDateTime = new Timestamp(date.getTime())

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

//IssueIndexManager issueIndexManager = ComponentAccessor.getIndexManager()

 

getFilterResult(jqlQuery,log).each{ Issue issue ->

        

// Set released custom field to current date

    CustomField releasedField = customFieldManager.getCustomFieldObjects(issue).find { it.name == 'Release Date' }

    releasedField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(releasedField), currentDateTime), new DefaultIssueChangeHolder());

 

    CustomField releasedFieldTime = customFieldManager.getCustomFieldObjects(issue).find { it.name == 'Release DateTime' }

    releasedFieldTime.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(releasedFieldTime), currentDateTime), new DefaultIssueChangeHolder());

 

    result += " ${issue.key}"

    //issueIndexManager.reindex(issue);

 

}

 

return result

List<Issue> getFilterResult(String jqlSearch, log) {

    def searchService = ComponentAccessor.getComponent(SearchService.class);

    def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

    List<Issue> issues = null

    def parseResult =  searchService.parseQuery(user, jqlSearch);

    if (parseResult.isValid()) {

        def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

        issues = searchResult.issues

    } else {

        log.error("Invalid JQL: " + jqlSearch);

    }

    return issues

}

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

5 votes
Answer accepted
Joffrey_Hamman November 25, 2016

I have almost the same problematic.
My goal was to reindex issues whose the scripted field value have changed, in order to sort them in the issues viewer.
If you run a groovy script inside the scripted field, it ends in a infinite loop.
Then, I run my groovy script in an escalation service which runs every minute.

I share with you my code which works well under Jira 7.1.0, maybe it can help you.

Escalation service :

 

import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category

def issueManager = ComponentAccessor.getIssueManager()
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
log.warn("Reindex issue ${issue.key} ${issue.id}")
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);


To test under script Console, you have to define "issue" before :

Issue issue = issueManager.getIssueObject("your issue Key");
Susanne Harelius [Riada]
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.
November 27, 2016

Thanks! 

1 vote
Peter Atthem May 8, 2014

Hi,

This was the solution to the problem:

adding these lines to getFilteredResult function.

boolean wasIndexing = ImportUtils.isIndexIssues()

ImportUtils.setIndexIssues(true)

ComponentAccessor.getIssueIndexManager().reIndex(issue)

ImportUtils.setIndexIssues(wasIndexing)

Also add the import com.atlassian.jira.util.ImportUtils in the header.

BR

Peter

1 vote
Boris Georgiev _Appfire_
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.
April 22, 2014

Where is this script executed ? In a post-function ? Also what is the error message when the index manager crashes ?

There is possibility that the issue object you pass to the index manager is not valid but this depends on the point where the script is executed, so please provide more details.

Susanne Harelius [Riada]
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.
April 23, 2014

Hi Boris!

Thanks for the quick reply. Yes it is from a post-function and the issue triggering this is not the one updated (the script updates linked issues). I will get back when I have the error.

TAGS
AUG Leaders

Atlassian Community Events