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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Making your Jira7-Scripts Jira8 compatible

Edited
Leonard Chew
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.
Nov 21, 2019

Hi there

We have upgraded our Jira Development System from Jira 7.x to the Enterprise Release Jira 8.5.
I had noticed that the Jira API has breaking changes in the Class com.atlassian.jira.issue.search.SearchProvider, which results in several of my Scriptrunner Scripts being broken.

As we have an important Plugin that is not yet compatible with Jira 8.5, I am stuck with Jira 7 in Production and Jira 8 in our Development environment.

I have tweaked my systems, so that both Jira Versions 7 and 8 can run the same Scriptrunner Code.
I would like to share this with you here.

  • The Problem:
    Every Code Snippet that fetches Items by JQL is broken and needs to be rewritten.

  • Solution:
    I have written a Wrapper Class for Jira 7 and one for Jira 8 to get Items by JQL and have rewritten the code to use the wrapper class.

 

  • Wrapper Class for Jira 7
    Create a file BisonHelper_V7.groovy in $JIRA_HOME/scripts
/*
BisonHelper for Jira 7
*/

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

public class BisonHelper {

public static com.atlassian.jira.issue.Issue[] getIssuesByJQL(String jqlQuery) {
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchProvider = ComponentAccessor.getComponent(SearchProvider);
def query = jqlQueryParser.parseQuery(jqlQuery);
def results = searchProvider.searchOverrideSecurity(query, user, PagerFilter.getUnlimitedFilter(),null)
com.atlassian.jira.issue.Issue[] returnVal = []
return returnVal = results.getIssues()
}
}

 

  • Wrapper Class for Jira 8
    Create a file BisonHelper_V8.groovy in $JIRA_HOME/scripts

/*
BisonHelper for Jira 8
*/

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
import com.atlassian.jira.issue.search.SearchQuery


public class BisonHelper {

public static com.atlassian.jira.issue.Issue[] getIssuesByJQL(String jqlQuery) {
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser);
def searchProvider = ComponentAccessor.getComponent(SearchProvider);
def query = jqlQueryParser.parseQuery(jqlQuery);
def searchQuery = SearchQuery.create(query, user).overrideSecurity(true);
def results = searchProvider.search(searchQuery, PagerFilter.getUnlimitedFilter())

com.atlassian.jira.issue.Issue[] returnVal = []
return returnVal = results.getResults()*.document.collect{ComponentAccessor.issueManager.getIssueObject(it.getField('key').stringValue())}
}
}

 

  • Activation of the classes:
    Create the symbolic link BisonHelper.groovy that points to BisonHelper_V7.groovy on your Jira 7 System or to BisonHelper_V8.groovy on your Jira 8 System.


BisonHelper.png

 

Now you can rewrite your code using the BisonHelper Class so that it is compatible with both systems.

 

  • Usage
    Example Code for the Script console that works on both Jira 7 and Jira 8:
def issues = BisonHelper.getIssuesByJQL('project=TEC')

for (it in issues) {
log.warn('found issue: ' + it.key)
}

 

After the productive upgrade, all you need to do is re-link your BisonHelper.groovy link to the BisonHelper_V8.groovy file and all will work.

A very beneficial side-efect:
The wrapper is much easier to use than building the native code.
To get issues in a script you dont need to figure out how to build the query result anymore and there is no need bothering which classes you need to import!

0 comments

Comment

Log in or Sign up to comment