Forums

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

REST call as current user

aleksandr_pavlovic November 13, 2019

Hello, my goal is to get all possible information about the current issue and send it by e-mail.

The only thing I came up with is getting it from REST (similar to the way it is done when calling webhook in the post function)
The code below gives me a json wich I can email.

But I dont like to store a user credentials in a script. Can I somehow execute that REST call with a currently logged in user? The way it is done when calling a webhook (I mean that when I call a webhook in a postfunction it done with a current user credentials).

 

import groovy.json.JsonSlurper
import org.apache.commons.io.IOUtils

def user = "user"
def password = "passwd"
def urlConnection = new URL("http://jiratest.dom.com/rest/api/2/issue/ISS-56905").openConnection()
urlConnection.setRequestProperty("Authorization", "Basic " + (user + ":" + password).bytes.encodeBase64().toString())

def jsonString = IOUtils.toString(urlConnection.inputStream)

 Jira 6.2.4

ScriptRunner 3.0.16

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
November 13, 2019

There might be a way, but I don't know it.

But you could get all the information from a jira issue using the JAVA api.

You could start with something like this and refine the output as needed (for example, expand user type fields).

import com.atlassian.jira.component.ComponentAccessor 
def cfm = ComponentAccessor.customFieldManager
def im = ComponentAccessor.issueManager
def issue = im.getIssueObject('JSP-1922')

def issueMap = [key:issue.key, id:issue.id, fields:[]]
issueMap.fields << [summary: issue.summary]
issueMap.fields << [description: issue.description]
issueMap.fields << [assignee: issue.assignee]
issueMap.fields << [reporter: issue.reporter]
cfm.getCustomFieldObjects(issue).findAll{issue.getCustomFieldValue(it)}.each{
issueMap.fields << [(it.name) : issue.getCustomFieldValue(it)]
}
issueMap
aleksandr_pavlovic November 14, 2019

Hello Peter,
Thank you for your help. Your suggestion will do the trick, and I think I'll stick to it for now because it's the best solution for now.

 

The only thing there is a little mistype at "def issueMap" "fields" should be a LinkedHashMap instead of ArrayList. :)

def issueMap = [key:issue.key, id:issue.id, fields:[:]]

 

Thank you :)

PD Sheehan
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.
November 14, 2019

It wasn't a mistype... I just didn't look a the rest output structure close enough.

The array would work took, it just makes finding and accessing individual field items a little harder.

Clearly the map is better.

Suggest an answer

Log in or Sign up to answer