It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage
Highlighted

Making your Jira7-Scripts Jira8 compatible Edited

Leonard Chew Community Leader 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
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you