Forums

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

Can I call a rest api as a specific user in a scripted field?

Matt Leary
Contributor
September 27, 2017

Hi,

I'm trying to get git tag information for a ticket and surface that information for JQL queries using scriptrunner scripted fields. My JIRA instance has the add-on "git integration for JIRA" which provides an api to query the tags:

/rest/gitplugin/1.0/issuegitdetails/issue/{issueKey}/tag

I would like to be able to call this as a specific user on the system, but am not sure if this can be done within a scripted field.

What I have so far:

import org.apache.http.HttpResponse
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

def http = new HTTPBuilder("https://my-jira-test-system.com")
try {
http.get( path : "/rest/gitplugin/1.0/issuegitdetails/issue/${issue.key}/tag" ) { response, reader ->
assert response instanceof HttpResponse
log.warn "response status: ${response.statusLine}"
log.warn "body: ${reader}"
}
}
catch (e) {
log.warn e
}

return "git tags here"

How can I update the code above to call the api as a specific user? 

Can I cache the result until an event is fired (a specific workflow transition)?

Thanks,

Matt

1 answer

0 votes
somethingblue
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 28, 2017

Hi Matt,

I would start with Adaptavists documentation on Jelly Migration that has a couple examples where users were authenticated in code.

Next, there are a couple Community posts where I have seen some examples in the past of using the user in the body request:

def body_req = [
 "user": user,
 "jiraTicket":issueID,
    "route":"/resources/text",
 "targetLang":languagesCodes[targetLanguage],
 "reqText":textToTranslate
]

In addition, I have also seen an example in the post Solved: Unable to run a groovy script as a service using script runner:

To run as a script (that saved a a file) in JIRA serivce, you need to authenticate the user that's running the service:
 
def AuthContext=ComponentAccessor.getJiraAuthenticationContext()
AuthContext.setLoggedInUser(user)
 
Good thing is, you don't need to supply the user's password.

Take a look at both of the resources and see which one works for you. 

Cheers,

Branden

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira product discovery, product operations, product ops, linkedin live, atlassian events, cross-functional collaboration, product management, engineering collaboration, design collaboration, atlassian learning, agile teams, product strategy

Product Ops Unpacked: 4️⃣ tips every product pro needs to know ✍️

Join PM Evangelist, Axel Sooriah, & Product Ops. Consultant & Advisor, Jenny Wanger, as they unpack the role of product ops and discuss key takeaways in Atlassian’s ‘The Product Ops Mission’ guide. They’ll answer your questions live on May 6 at 9:00am PT.

Register here ⬇️
AUG Leaders

Atlassian Community Events