Forums

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

Acces Jira REST API through ScriptRunner with currently logged in user

Basit Rehman
Contributor
February 1, 2019

Hi there,

I have a question regarding the REST API. I have implemented a REST endpoint with ScriptRunner and want to acces the REST API when this endpoint gets called.

Basically I need get the renderedFields by using https://host.com/rest/api/2/issue/{issueIdD}?expand=renderedFields. Everything works fine.

My problem is that I want to use the credentials of the currently logged in user who calls this endpoint. At the moment the username and password for accessing the API are hard coded in the script what I don't really like. Is there any possibility to use the credentials of a logged in user?

Best regards,

Basit

 

@Nir Haimov fyi :)

1 answer

1 accepted

0 votes
Answer accepted
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 2, 2019

Hi @Basit Rehman

Did you tried without credentials?

Maybe it will recognize that a user already loged in and will use the session automatically.

Basit Rehman
Contributor
February 5, 2019

Hi @Nir Haimov

thanks for your reply.

I actually tried to call the API without credentials but I alwas got 401 as response code (unauthorized). But I am not sure if I used everything correctly. This is my approach:

//def login = "username:password"
//def encoded = new String(Base64.getEncoder().encode(login.getBytes()))

def obj = new URL(baseUrl + "/rest/api/2/issue/" + issueId + "?expand=renderedFields")
def urlConnection = (HttpsURLConnection) obj.openConnection()

urlConnection.setRequestMethod("GET")
urlConnection.doOutput = true
//urlConnection.setRequestProperty("Authorization", "Basic " + encoded.toString())
log.info("connection established")
BufferedReader bf = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))

String inputLine
StringBuffer response = new StringBuffer()

while ((inputLine = bf.readLine()) != null) {
response.append(inputLine)
}
bf.close()

log.info(response)

As you can see the login data is commented out. This is the log:

INFO [runner.ScriptRunnerImpl]: connection established 
WARN [common.UserScriptEndpoint]: Script console script failed: java.io.IOException: Server returned HTTP response code: 401 for URL: https://{host}/rest/api/2/issue/{issueId}?expand=renderedFields at Script34.run(Script34.groovy:39)

 Is there anything else I have to consider?

Regards, 

Basit

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2019

Hi @Basit Rehman,

why are you trying to access Jira from inside Jira (scriptrunner) via rest api?

you should access everything directly with the JAVA API and not rest api.

Please tell me what you are trying to do so i will be able to guide you better.

Basit Rehman
Contributor
February 5, 2019

Hi,

basically I created an REST endpoint that has to look for (rendered) comments in a given timeframe. A user presses a certain button in JIRA that opens a dialog. In this dialog you have to enter starting time and end time so the script can print all the relevant comments on a generated html page.

I have also tried to get the comments by using the Jira Java API but it doesn't give me the rendered fields... I have only found the getBody() method in com.atlassian.jira.issue.comments.Comment which doesn't help me really.

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issueId) //list of comments

I hope you understand my problem and would be very glad if you could help me out :D 

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2019

Hi,

Not fully understand :)

When you say "rendered" comment, you mean, you want the comment body as HTML instead wikiMarkup?

If yes, follow this:

With "commentManager" you can get all comments.

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)

comments.created

 By using "comments.created" you can get when the comments created to check your condition as you said.

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def comments = commentManager.getComments(issue)

def rendererManager = ComponentAccessor.getRendererManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fieldLayoutItem = ComponentAccessor.getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(comments[0].body)
def renderer = rendererManager.getRendererForField(fieldLayoutItem)

renderer.render(comments[0].body, null)

 this will render your comment

Basit Rehman
Contributor
February 6, 2019

Hi @Nir Haimov,

thanks a lot for your help. I understand this approach but I experienced some problems while trying this so I thought that the Jira REST API (by calling https://host.com/rest/api/2/issue/{issueIdD}?expand=renderedFields) will be better here.

For example, if a comment contains a picture as attachment this script won't give me the link to this picture (as it is represented on a html page: <a href="..." /a>). What I get looks as follows: 

This is a comment !myPicture.PNG|thumbnail!

My rest endpoint will forward the user to a generated html page that will display the relevant comments. To do that I need the structure of the comment body to be like this:

<p>This is a comment <span class="image-wrap" style=""><a id="12345_thumb" href="https://host.com/secure/attachment/12345/12345_myPicture.PNG" title="myPicture.PNG" file-preview-type="image" file-preview-id="12345" file-preview-title="myPicture.PNG"><img src="https://host.com/secure/thumbnail/12345/_thumb_12345.png" style="border: 0px solid black" /></a></span></p>"

Basically the comment should look like it's displayed in an Jira issue :)

Is there any other possibility to get all the html tags with the Java API?

Best regards,

Basit 

Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2019

Hi @Basit Rehman,

Not that i know of.

At this point, i think going back to the REST API will be best, but you will have to hardcoded your credentials.

Basit Rehman
Contributor
February 6, 2019

Hi @Nir Haimov,

ok then I will have to use this workaround. Again thanks a lot for your help! :)

Seifallah Bellassoued
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.
April 14, 2022

Hello @Nir Haimov

I want to retrive all plugins with evaluation license with scriptrunner.

Is it possible without REST API ?

Best Regards,

Suggest an answer

Log in or Sign up to answer