If I wanted to perform a search of Jira using Groovy and ScriptRunner, I might make use of the SearchRequestService.  This class contains many useful methods, most of them having to do with filters. 

A great many of these take a JiraServiceContext as an argument.  For example, these are the parameters of the createFilter() method:

 	createFilter(JiraServiceContext serviceCtx, SearchRequest request)

At this point you might start to wonder what a “Jira Service Context” is, and for good reason. Atlassian has once again failed to document exactly what they want, or how to provide it. There is nothing in any of the documentation about what exactly this argument is.  The arguement has it’s own page in the documentation, but that contains no useful information on actually implementing or understanding this parameter.

This is a recurring theme with Atlassian and their APIs.   Rarely is the official documentation helpful or complete; instead we must rely on the kindness of strangers on the internet who post code snippets.  That’s one of the major reasons I started posting the Groovy code that I wrote for Atlassian products; the vendor sure isn’t providing any help, and hopefully what I write will be of help to someone in the future.

 I searched and re-searched, rephrased and retried to find a result on Google that actually implemented this argument.  I finally found one, two hours and six pages back.

 

import com.atlassian.sal.api.component.ComponentLocator 
import com.atlassian.jira.bc.JiraServiceContextImpl 
import com.atlassian.jira.component.ComponentAccessor 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() 

JiraServiceContextImpl serviceContext = new JiraServiceContextImpl(user);

 

On it’s own this snippet doesn’t do much. But it does create the JiraServiceContext that is required to make use of the SearchRequestService.    All the JiraServiceContext does is tell the SearchRequestService which user to run the search as. That’s ALL IT DOES.   But it took several actual hours of my time to figure this out. 

Big thanks to Chad Larkin and the 10-year-old post of his that actually provided a snippit that used this parameter.

 

 

 

 

 

 

 

 

1 thought on “Jira Search Context

  1. Hello Ken,

    thank you very much for your post! It was really helpful.

    It´s a shame that Atlassian doesn´t document these things well. I´ve had the exact same issue as you had.
    It would help a lot having a better documentation to be able to create groovy scripts in an easier way.

Leave a Reply to Tom Cancel Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>