Groovy script - Issue search

I'm having a groovy sript to search the issues while creating an issue and update the number custom field value with the count of the issues from the search result. Using below script for this,

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService .class);
JqlClauseBuilder builder = JqlQueryBuilder.newClauseBuilder();

CustomField cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Application Name");
Option cf1Value=null;
if( cf1 !=null )
cf1Value=(Option)issue.getCustomFieldValue(cf1);

CustomField cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Issue Category");
Option cf2Value=null
if( cf2 !=null )
cf2Value=(Option)issue.getCustomFieldValue(cf2);

CustomField cf3 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("QA Environment");
Option cf3Value=null
if( cf3 !=null )
cf3Value=(Option)issue.getCustomFieldValue(cf3);

int totalIssues=0;

if( cf1Value != null && cf2Value != null && cf3Value != null ){

//JIRA query

Query query=builder.project().eq("DEMO Project").and().customField(cf1.getIdAsLong()).eq(cf1Value.getValue()).and().customField(cf2.getIdAsLong()).eq(cf2Value.getValue()).and().customField(cf3.getIdAsLong()).eq(cf3Value.getValue()).and().created().gtEqFunc("startOfWeek").buildQuery();

        SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);
        SearchResults searchResults = searchProvider.search(query, authenticationContext.getLoggedInUser(), PagerFilter.getUnlimitedFilter());
        List<Issue> searchIssues = searchResults.getIssues();
        totalIssues = searchIssues.size();
}

//Update custom field

IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager();
def MeD =cfm.getCustomFieldObjectByName("Deployments this week");
issue.setCustomFieldValue(MeD, Integer.toString(totalIssues)as Double);
issueManager.updateIssue(authenticationContext.getLoggedInUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);

In this we have three different custom fields,

1. Application Name - select list field

2. Issue category - select list field

3. QA Environment - select list

I'm planning to modify the jql search to with a custom date field, means want to filter only the issues which are in current week of that custom date picker field value. Is that possible?? Now it is working with created date current week.

Please suggest.

Many thanks in advance. :)

Sanu P Soman

2 answers

Any suggestions?

0 vote

This question is too vague, you should not post masses of code, just the interesting bit. Your other question is better and seems like it replaces this one.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

152 views 1 17
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you