Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script listener re-indexing is not working consistently.

nallu February 13, 2019 edited

Hi, 

I have a scenario where I need to re-index the issue whenever a linked issue is updated. 

Here is the details.

Actually, We are linking a epic with issue type feature through a custom field (issue picker). This feature issue has some scripted fields which is calculating data based on the data in linked epics. But, These data doesn't get updated correctly in JQL filter as indexing is not happening on the feature issue whenever script field is getting updated. 

So, I have written a script listener to re-index the feature issue whenever the issue type epic is created/updated.

Here is the code,

import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.index.IssueIndexingService;

//import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.web.bean.PagerFilter;

Issue issue = event.issue

//Get issue key
def currKey = issue.getKey();

//Query for all features where "Linked Epics" ~ current key
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchProvider = ComponentAccessor.getComponent(SearchProvider);
def issueManager = ComponentAccessor.getIssueManager();
def user = ComponentAccessor.getJiraAuthenticationContext().getUser();

def queryString = 'issuetype = Feature AND "Linked Epics" ~ ' + currKey;
def query = jqlQueryParser.parseQuery(queryString);
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());

//log.warn("Reindex issue ${issue.key} ${issue.id} ${currKey} ${results.getIssues().size()}+++++++++++++++")

if (results)
{
results.getIssues().each { documentIssue ->
boolean isIndex = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
IssueIndexingService IssueIndexingService = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.class);
IssueIndexingService.reIndex(documentIssue);

ImportUtils.setIndexIssues(isIndex);
}
}

 

It looks like, this script listener works fine. But still JQL result is not getting updated correctly and its inconsistent. So, I had to manually re-index the project on a frequent basis. 

 

Can anyone tell me whats wrong with the script?  OR is there any other issue? 

 

Any help would be greatly appreciated. 

1 answer

0 votes
nallu February 13, 2019 edited

Ok. My script is getting triggered for all the issue types. So, I just added a one more condition to check for the issue type. 

Here is the updated code,

/**
* File Information:
* This script is to perform an auto re-indexing on feature issues.
* When:
* 1) 'Feature Link' field is set, while creating epics.
* 2) The epic which is alreay being linked with feature issues, is updated.
*
* This script is associated with script listener which is created in 'JIRA ADMINISTRATION (Gear) -> Issues -> Script Listener' in Jira.
*
* @since 26th Sep, 2018
* @version 1.0
*
* Additional Informations:
** These informations are provided in JIRA User Interface while creating Script Listener:
* @Note This Listener is to perform reindexing on 'Feature' issues which is linked with an epics whenever linked epics are updated
* @Project(s) All
* @Events Issue Created, Issue Updated
**/

import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.index.IssueIndexingService;

//import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.jql.parser.JqlQueryParser;
import com.atlassian.jira.web.bean.PagerFilter;

Issue issue = event.issue

if(issue.issueTypeObject.name == 'Epic')
{
//Get issue key
def currKey = issue.getKey();

//Query for all features where "Linked Epics" ~ current key
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchProvider = ComponentAccessor.getComponent(SearchProvider);
def issueManager = ComponentAccessor.getIssueManager();
def user = ComponentAccessor.getJiraAuthenticationContext().getUser();

def queryString = 'issuetype = Feature AND "Linked Epics" ~ ' + currKey;
def query = jqlQueryParser.parseQuery(queryString);
def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter());

log.warn("Reindex issue ${issue.key} ${issue.id} ${currKey} ${results.getIssues().size()}+++++++++++++++")

if (results)
{
results.getIssues().each { documentIssue ->
boolean isIndex = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
IssueIndexingService IssueIndexingService = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.class);
IssueIndexingService.reIndex(documentIssue);

ImportUtils.setIndexIssues(isIndex);
}
}
}

 

Not sure this would fix it or not. If anyone find anything wrong with script, Please do let me know. 

Suggest an answer

Log in or Sign up to answer