Forums

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

Post-function script with headers

luanlopes94 July 24, 2019

Hi community,

I've developed a custom rest plugin with routines to automate some of my team tasks.

I am using scripts in post-functions to run the rest API's:

(...)

def issueRest = remote.request(Method.POST,ContentType.JSON) { req ->
uri.path = ("/rest/myrest/latest/mymethod")
headers.'Authorization' = "Basic ${"defaultusers:pass".bytes.encodeBase64().toString()}"
body = [
"skuList" : arrayOfSkus,
"customFieldModelsList" : [
[ "name" : "Customfield 01", "value" : "Value 01" ],
[ "name" : "Customfield 02", "value" : "Value 02" ]
]
]
response.success = { resp6,json ->
log.warn "Success! ${resp6.status}"
}
response.failure = { resp6,json ->
log.warn "Request failed with status ${resp6.status}"
}
}

(...)

The code is working perfectly. But, the changes will always be made with the default user.

I need to change the "defaultuser" to the current user. But, the ApplicationUser (com.atlassian.jira.component.ComponentAccessor.getJiraAuthenticationContext().getUser()) doesn't have the password value or any value related to the session.

How can I do it?

Best Regards.

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
Derek Fields _RightStar_
Community Champion
July 24, 2019

You can't get the password for another user. It would completely nullify all security if you could look up another user's password.

The only way to do this is to make a REST call that can accept the username and then internally has the permission to act on behalf of that user. Several JIRA REST calls do this depending on what you are trying to accomplish.

Your alternative is to perform the update directly in the code. Jira allows you to pass in the user as part of the update call. In that case, the update is recorded as the user that you pass in. You can't do this through REST.

luanlopes94 July 24, 2019

Hi @Derek Fields _RightStar_ ,

Thank you for replying.

I've added the user as parameter in the request body:

(...)

body
= [
"skuList" : arrayOfSkus,
"customFieldModelsList" : [
[ "name" : "Customfield 01", "value" : "Value 01" ],
[ "name" : "Customfield 02", "value" : "Value 02" ]
],
"user" : username
]

(...)

The code executes the method with the received user. This approach solved my problem.

Thank you.

Suggest an answer

Log in or Sign up to answer