ScriptRunner for Jira Cloud: Add watcher in ScriptRunner Post-Function

VVeider September 12, 2019

What is exact script to add single user as watcher?

3 answers

1 accepted

1 vote
Answer accepted
VVeider September 15, 2019

Thanks, but it was too late.

My working code:

post("/rest/api/3/issue/${issue.key}/watchers")
.header('Content-Type', 'application/json')
.body('"<accountId>"')
.asString()
1 vote
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 13, 2019

Hi VVeider,

Thank you for your question. 

I can confirm that as Randy has said that to add a watcher with ScriptRunner for Jira Cloud that you will need to make a rest API call to the watchers API. 

I can confirm that we have an example code snippet located here which can be run in the Script Console and shows how you can set the watcher on an Issue and that you will be able to take this example and modify it to create the post function that you require.

I can confirm that inside the post function that there is a variable called issue which comes in the binding of the script and you can get the issue key to pass into the rest call by calling issue.key.

Finally, I can advise that if you are creating your post function on the Create transition that the post function should be ordered as the last post function in the list of post functions, so that the issue is created before the post function code is executed. 

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

Srecko Anzic March 29, 2020

Kristian thanks for this post https://bitbucket.org/snippets/Adaptavist/yAdX6X?_ga=2.267944206.120189328.1585505760-1987078914.1573938112

Since October I was trying to solve this problem.

This is my code to automatically add watchers from a certain JIRA group to a newly created Incident issue:

def issueKey = issue.key
def group = get('/rest/api/3/group/member')
.header('Content-Type', 'application/json')
.queryString("groupname":"IT Department")
.asObject(Map)
.body
group.values.accountId.each{accountId->
def result = post('/rest/api/2/issue/' + issueKey + '/watchers')
.header('Content-Type', 'application/json')
.body("\"${accountId}\"")
.asString()
}

0 votes
Randy
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.
September 12, 2019
post("/rest/api/3/issue/{issueKey}/watchers/")
VVeider September 13, 2019

Hi, Randy. Thanks.

 

But:

  1. What should be in POST body and how to pass it to method call?
  2. How to get issue key when creating issue (not modifying one)?
Randy
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.
September 13, 2019

1. you only need to include post body contents if the watcher you're setting is different from the executing user.  

2. This is a different issue from your original question but when you submit a create request the call will return the issuekey.  You cant run post functions on create transition reliably unfortunately, use a listener instead and there will be an issue binding available.

Suggest an answer

Log in or Sign up to answer