Forums

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

Jira Scriptrunner - REST endpoint that will set an issue property

Richard Phillips November 3, 2023

Using Jira Data Center- 

I have created a Refresh button (web item) on the Jira issue details page, when pressed I would like to update a property of the currently displayed issue.

I'm having trouble creating a REST endpoint that will do this. 

Use Case:  Issue "CLI-888" is being displayed and when I click the Refresh button I would like to create or update a property called "requestRefresh" to have the boolean value True.

That's all, but I feel like I'm going down a rabbit hole with groovy and all of the java and atlassian-specific classes, and I haven't yet figured out how to reference the current issue from within scriptrunner.

Seems like this should be trivial, something like "issue.setProperty("requestRefresh", true).  

How should I set this property?

1 answer

1 accepted

1 vote
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 3, 2023

Here is some sample code for setting an issue property value using the IssuePropertyService.

Note that issue properties can only be strings.

 

import com.atlassian.jira.bc.issue.properties.IssuePropertyService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.entity.property.EntityPropertyService

def issuePropertyService = ComponentAccessor.getComponent(IssuePropertyService)
def currentUser= ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issue=ComponentAccessor.issueManager.getIssueObject('CLI-888')
def input = new EntityPropertyService.PropertyInput(true.toString(), 'requestRefresh')

def validationResult = issuePropertyService.validateSetProperty(currentUser,issue.id, input)
if(!validationResult.valid){
log.error validationResult.errorCollection.errorMessages
return
}
issuePropertyService.setProperty(currentUser, validationResult)

After doing that, you can view the properties using a rest call:

<jira base url>/rest/api/latest/issue/CLI-888/properties

Or using the same API:

issuePropertyService.getProperty(currentUser, issue.id, 'requestRefresh').entityProperty.get().value
Richard Phillips November 6, 2023

Thank you so much for this. 

In my question I specified the key 'CLI-888', is there a way to get the key of the currently displayed issue instead of using a fixed string?

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 6, 2023

In your Web Item fragment, you can include the issue key in the "link" field".

It should look something like this

link:
/rest/scriptrunner/latest/custom/yourEndPoint?issueKey=${issue.key}

Then, in your rest endpoint, simply extract the issue Key from the queryParams

 

yourEndPoint (httpMethod: 'POST', groups: []) { MultivaluedMap queryParams, String body ->
def issueIssueKey = queryParams.getFirst('issueKey')
def
issue=ComponentAccessor.issueManager.getIssueObject(issueIssueKey)
/*...*/
}
Like • Richard Phillips likes this
Richard Phillips November 6, 2023

Thank you again, now I also understand the queryParams part. Bonus!

Cheers

Suggest an answer

Log in or Sign up to answer