Automation For Jira - How to create a rule that adds to the watcher list of an issue the members of a project role

Still need help?

The Atlassian Community is here for you.

Ask the community

robotsnoindex


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

    

Summary

This article describes how to create an Automation Rule using the add-on Automation For Jira (for Jira Server/Data Center) that automatically adds the members of a project role to the watching list of a Jira issue.

As explained in the feature request https://jira.atlassian.com/browse/JIRAAUTOSERVER-826, the Manage Watchers action has some limitations:

  • It is only possible to add watchers by:
    • either listing specific users
    • or copying users from a user picker field via a smart value
  • However, it is not possible to add to the watcher list:
    • users that belong to a group
    • users that belong to a project role

This article provides some suggestions on how to work around such limitation.

Environment

Jira Server/Data Center 8.0.0 and above, along with Automation for Jira 7.4.0 and above.

Solution

Let's assume that you are trying to configure a rule that:

  • is triggered when a new Jira issue is created
  • automatically adds the members of the Developers role, if the assignee is also part of this role

There are 2 ways to implement an automation rule that will work for this use case:

Solution 1 - Without the ScriptRunner add-on

Preliminary steps

Creating a new Custom Field

  • Go to ⚙ > Issues > Custom Fields
  • Create a new field of type User Picker (multiple user)
  • Make sure that
    • The field context is configured so that the field is available in the project where you wish to configure the rule
    • The field is added to the Edit Issue Screen used by all the issue types in the project where you wish to configure the rule

Getting the project Role ID

  • Open the URL below in a new tab of the browser, while making sure that you are already logged into Jira as an Admin User, after replacing <JIRA_BASE_URL> with the Jira Base URL, and <PROJECT_KEY> with the project key:

    <JIRA_BASE_URL>/rest/api/2/project/<PROJECT_KEY>/role/


  • Check the output, and get the ID located at the end of the URL next to the role you are interested in
    • In the example below, the ID is 10102 for the Developers role

Creating a PAT (Personal Access Token)

    • Going to your user profile (by clicking on your avatar)
    • Clicking on the Personal Access Tokens menu
  • (warning) Make sure to set an expiry date that's far enough in the future, because the automation rule will need this token to function properly

Rule configuration

The rule needs to be configured as follows:

  • Add the Issue Created trigger
  • Add the User Condition with the parameters below
    • User: Assignee
    • Check to perform: is in role
    • Criteria: Choose the role of your choice (for example: Developers)
  • Add the Send Web Request action with the parameters below:
    • Webhook URL: copy the URL below after replacing replacing <JIRA_BASE_URL> with the Jira Base URL, <PROJECT_KEY> with the project key, and <ROLE_ID> with the ID identified in the preliminary steps:

      <JIRA_BASE_URL>/rest/api/2/project/<PROJECT_KEY>/role/<ROLE_ID>


    • Headers: Add 1 header as follows, after replacing <PAT> with the Personal Access Token generated during the preliminary steps:
      • Name: Authorization
      • Value: Bearer <PAT>
    • HTTP Method: GET
    • Wait for response: Tick this option. (warning) Ticking this option is necessary for the rule to works properly.
  • Add the Edit Issue  action with the parameters below
    • Choose fields to set...: Select the multi user picker field created during the preliminary steps
    • Value: copy the smart value below:

      {{webhookResponse.body.actors.name}}
  • Add the Re-fetch issue data action
    • (warning) This action is necessary for the rule to work, because it will re-load the new value of the multi user picker field
  • Add the Manage Watchers action with the parameters below
    • Add these watchers
      • Add the smart value below, after replacing <MULTI_USER_PICKER_FIELD_NAME> with the name of the field created during the preliminary steps

        {{issue.<MULTI_USER_PICKER_FIELD_NAME>}}
      • For example, if the field name is 

        {{issue.Multi user picker list}}

Screenshot showing the rule

Solution 2 - With the ScriptRunner add-on


This solution is not supported by Atlassian, as it involves the usage of the 3rd party add-on ScriptRunner for Jira, and is only provided as is, in this KB article.

Rule configuration

  • Add the Issue Created trigger
  • Add the User Condition with the parameters below
    • User: Assignee
    • Check to perform: is in role
    • Criteria: Choose the role of your choice (for example: Developers)
  • Add the Execute a ScriptRunner script action with the code snippet below, and after replacing <PROJECT_ROLE> with the name of the project role:

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.watchers.WatcherManager
    import com.atlassian.jira.security.roles.ProjectRoleManager
    import com.atlassian.jira.security.roles.ProjectRole
    
    WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
    ProjectRoleManager projectRoleManager = ComponentAccessor.getComponentOfType(ProjectRoleManager);
    ProjectRole rolePO = projectRoleManager.getProjectRole( "<PROJECT_ROLE>")
    
    if( projectRoleManager.isUserInProjectRole( issue.getAssigneeUser(), rolePO, issue.getProjectObject() ) ) {
    
        projectRoleManager.getProjectRoleActors( rolePO, issue.getProjectObject())
        .getUsers()
        .each { po ->
            watcherManager.startWatching(po, issue)
        }
    }

Screenshot showing the rule




Last modified on May 30, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.