Add project role members as watchers on Create
This tutorial describes how a group's members can be added as watchers on a transition, such as in this example Create Issue.
Assumption
There is a project role called Develops assigned to the project TESTB.
In my example MR.Bot and Rick are in the Developers role as members.
Steps
- Create a new Action by clicking CREATE button
- Enter a name: Get Developers members and click CREATE
- Select Method: GET and URL: <your_base_url>rest/api/2/project/TESTB/role/10100
In our instance the project key is TESTB and the ID for Developers role is 10100 this may vary in your instance.
From the Atlassian REST API we can read that to fetch members of a role we need to call GET /rest/api/2/project/{projectIdOrKey}/role/{id} - Select Authentication method: Basic Auth, specify an admin user that can read the project in JIRA.
- Add request headers, key: Content-Type with value application/json
- Click Triggers tab
- Select ISSUE CREATE event
- On JQL Condition. When to execute the event(s) write Project = TESTB to limit the trigger to the TESTB project
- Click SAVE
- (Optional) Click Test button (no issue key is needed) to and click Send, Then go Log Tab.
Each result will have a response like the one below:
{ "self": "https://support.rixter.se/rest/api/2/project/11100/role/10100", "name": "Developers", "id": 10100, "actors": [ { "id": 12204, "displayName": "Mr Bot", "type": "atlassian-user-role-actor", "name": "mr.bot@rixter.se", "avatarUrl": "https://support.rixter.se/secure/useravatar?size=xsmall&ownerId=mr.bot%40rixter.se&avatarId=10601" }, { "id": 12205, "displayName": "Rickard Atthem", "type": "atlassian-user-role-actor", "name": "rick", "avatarUrl": "https://support.rixter.se/secure/useravatar?size=xsmall&ownerId=rick&avatarId=10600" } ] }
Next steps are to add the users as watchers
- Create a new Action by clicking CREATE button
Enter a name: Add Developers members as Watchers and note on the Select parent action choose Get Developers members which we created in part 1 and click CREATE
This will create a chained action that will be triggered directly after the response from the parent action is received. The response will be used as input variables this call using JSON Path expressions.- A new section called Variables is now displayed. On this section enter the Variable name: watchers and the JSON Path: $.actors[*].name
The JSON path will return a list of the user keys, in this example [rick,mr.bot@rixter.se] - Select Method: POST and URL: <your_base_url>/rest/api/2/issue/{{issue.key}}/watchers
note that we use double curly brackets {{issue.key}} to operate on the issue. Within the brackets, we can access any issue property.
From the Atlassian REST API we can read that to add watchers we need to call POST /rest/api/2/issue/{issueIdOrKey}/watchers - Select Authentication method: Basic Auth, specify an admin user that can read groups in JIRA.
- Add request headers, key: Content-Type with value application/json
In Body section add the following payload body
{{#watchers}} "{{.}}" {{/watchers}} {{^watchers}} "{{.}}" {{/watchers}}
Since we get an array of usernames as a result from the JSON Path$.actors[*].name value [rick,mr.bot@rixter.s] and the API endpoint requires a comma-separated list of strings we need to render that in the body using the Mustache template. Accessing the watchers variable with double brackets and then using the iteration method to walk through the list. Therefore the # and / syntax. The ^ and / removes the last comma.
- Click SAVE button
- (Optional) Click Test button (no issue key is needed) to and click Send, Then go Log Tab