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 :)
Did you tried without credentials?
Maybe it will recognize that a user already loged in and will use the session automatically.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Nir Haimov
I want to retrive all plugins with evaluation license with scriptrunner.
Is it possible without REST API ?
Best Regards,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.