Automatically add Watchers to Newly Created Issues

Overview

Watching helps users stay up-to-date with the progress of an issue from the point of discovery to resolution. Enter a user ID into this script to automatically add the user as a watcher when a new issue is created.

Example

I am a project manager, and I want to ensure the technical lead is watching all issues in their sprint so they can keep informed throughout an issues progress. I can use this script to automatically add that user as a watcher on all new issues created.

Good to Know

  • This script can bet set as a listener for "Issue created" event.
  • Add as many user IDs as required by adding them to accountIds.
  • Users are only added if they are assignable to the issue.
  • To retrieve the user ID:
    • Navigate to the People section (https://********.atlassian.net/people/) and select a user. The ID is displayed in the navigator bar.
    • Also, it can be retrieved via REST API.

Requirements

Jira Jira

    
final issueKey = issue.key def result = get('/rest/api/2/user/assignable/search') .queryString('issueKey', "${issueKey}") .header('Content-Type', 'application/json') .asObject(Map) assert result.status == 200 def usersAssignableToIssue = result.body*.accountId as List // A valid user ID and an invalid one will try to be added def accountIds = ['557058:98debc0e-06ba-4e71-baea-bbf1b7fd69ab', 'not-valid-accountId'] accountIds.forEach { accountId -> if (usersAssignableToIssue.contains(accountId)) { def watcherResp = post("/rest/api/2/issue/${issueKey}/watchers") .header('Content-Type', 'application/json') .body("\"${accountId}\"") .asString() if (watcherResp.status == 204) { logger.info("Successfully added ${accountId} as watcher of ${issueKey}") } else { logger.error("Error adding watcher: ${watcherResp}") } } else { logger.error("Not valid accountId ${accountId}") } }
Discovered an issue? Report it here

Suggested for you