Add group 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 group called Warehouse, your group can be named anything else as long as you can follow along.

In my example MR.Bot and Rick are in the group as members.

Steps

  1. Create a new Action by clicking CREATE button
  2. Enter a name: Get Warehouse members and click CREATE
  3. Select Method: GET and URL: <your_base_url>/rest/api/2/group/member?groupname=Warehouse.
    From the Atlassian REST API we can read that to fetch members of a group we need to call GET /rest/api/2/group/member
  4. Select Authentication method: Basic Auth, specify an admin user that can read groups in JIRA.
  5. Add request headers, key: Content-Type with value application/json
  6. Click Triggers tab
  7. Select ISSUE CREATE event
  8. On JQL Condition. When to execute the event(s) write Project = TESTB to limit the trigger to the TESTB project
  9. Click SAVE
  10. (Optional) Click Test button (no issue key is needed) to and click Send, Then go 3 - View Execution Log.

Each result will have a response like the one below:

{
	"self": "https://localhost/rest/api/2/group/member?includeInactiveUsers=false&maxResults=50&groupname=Warehouse&startAt=0",
	"maxResults": 50,
	"startAt": 0,
	"total": 2,
	"isLast": true,
	"values": [
		{
			"self": "https://localhost/rest/api/2/user?username=mr.bot%40rixter.se",
			"name": "mr.bot@rixter.se",
			"key": "mr.bot@rixter.se",
			"emailAddress": "mr.bot@rixter.se",
			"avatarUrls": {
				"48x48": "https://localhost/secure/useravatar?ownerId=mr.bot%40rixter.se&avatarId=10601",
				"24x24": "https://localhost/secure/useravatar?size=small&ownerId=mr.bot%40rixter.se&avatarId=10601",
				"16x16": "https://localhost/secure/useravatar?size=xsmall&ownerId=mr.bot%40rixter.se&avatarId=10601",
				"32x32": "https://localhost/secure/useravatar?size=medium&ownerId=mr.bot%40rixter.se&avatarId=10601"
			},
			"displayName": "Mr Bot",
			"active": true,
			"timeZone": "Europe/Stockholm"
		},
		{
			"self": "https://localhost/rest/api/2/user?username=rick",
			"name": "rick",
			"key": "rick",
			"emailAddress": "rick@atthem.se",
			"avatarUrls": {
				"48x48": "https://localhost/secure/useravatar?ownerId=rick&avatarId=10600",
				"24x24": "https://localhost/secure/useravatar?size=small&ownerId=rick&avatarId=10600",
				"16x16": "https://localhost/secure/useravatar?size=xsmall&ownerId=rick&avatarId=10600",
				"32x32": "https://localhost/secure/useravatar?size=medium&ownerId=rick&avatarId=10600"
			},
			"displayName": "Rickard Atthem",
			"active": true,
			"timeZone": "Europe/Stockholm"
		}
	]
}

Next steps are to add the users as watchers

  1. Create a new Action by clicking CREATE button
  2. Enter a name: Add Warehouse members as Watchers and (warning) note on the Select parent action choose Get Warehouse 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.

  3. A new section called Variables is now displayed. On this section enter the Variable name: watchers and the JSON Path$.values[*].key
    The JSON path will return a list of the user keys, in this example [rick,mr.bot@rixter.se]
  4. Select Method: POST and URL: <your_base_url>/rest/api/2/issue/{{issue.key}}/watchers
    (warning) 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
  5. Select Authentication method: Basic Auth, specify an admin user that can read groups in JIRA.
  6. Add request headers, key: Content-Type with value application/json
  7. 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 $.values[*].key value  [rick,mr.bot@rixter.s] and the API endpoint requires a comma seperated 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.

  8. Click SAVE button
  9. (Optional) Click Test button (no issue key is needed) to and click Send, Then go 3 - View Execution Log