How to remove inactive users as issue watchers with Jira Automation ?

Andreas Safar February 29, 2024

Goal: I want a scheduled automation to remove inactive users as watchers from issues.

 

I can identify the affected issues with the JQL: "watcher in inactiveUsers()".

 

Within "Global automation / Rule Builder" I can select the "Manage Watchers" action, where I can define the users, which should get removed as watchers. Unfortunately, inactiveUsers() is not available and it does not list any inactive users there at all. Only active users and apps can be chosen. 

screen.png

 

Do you have any idea/solution how to automate the removal of inactive users as watchers?

Please note: It is not an option to activate a user, remove as watcher, and then deactivate again.

 

Thank you!

2 answers

1 accepted

0 votes
Answer accepted
Andreas Safar April 17, 2024

All, I was able to solve it. See here

image.png

Bill Sheboy
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.
April 17, 2024

Hi @Andreas Safar 

Would you please explain how that works?

There is no Lookup Issues action in your rule, and so {{lookupIssues}} is null, and so the entire expression is null...meaning it cannot remove any watchers.

Thanks, and kind regards,
Bill

Andreas Safar April 18, 2024

Hi Bill,

I made some further adjustments and missed to insert the right data/screen before.

 

So I`ve decided to follow a two step approach.

 

 

1. step/rule (the easy rule)

My scheduled JQL "watcher in inactiveUsers() AND watchers = 1" gives me list of issues where I can simply use the "remove all existing watchers" flag.

 Bildschirmfoto 2024-04-18 um 13.03.43.png

This rule works fine. At the beginning it runs every month but in future it might be enough the run it once per quarter.

 


 

 

2. step/rule (for issues with more then 1 watcher)

My scheduled JQL here is "watcher in inactiveUsers() AND watchers > 1".

My lookup checks for issues where the watchers.active value is set to "false". Then I use a smart value to remove the users as watchers.

Bildschirmfoto 2024-04-18 um 15.55.32.png 

The rule removes watchers without errors but it does not limit the list of watchers to the inactive users = it removes also active users.

 

Note:

I`ve also tried it with this lookup:

Bildschirmfoto 2024-04-18 um 16.11.59.png

and with changing the content within "Remove watchers", but then it generates different error messages depending on my test changes. E.g.

- Field '5e9d82c24e1a4b0c136ea646' does not exist or you do not have permission to view it

- Error in the JQL Query: A field name cannot be empty.

 

 

Maybe you have a suggestion how to proceed with my second rule/approach?

 

 

Thank you in advanced!

Bill Sheboy
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.
April 18, 2024

You found the correct field, watchers and its attribute active, and to do what you ask adjustments are needed because looking at watchers is a separate REST API call out of the rule to capture the value of active for filtering.

  • trigger: scheduled
  • action: create variable
    • name: varWatchers
    • smart value: {{#issue.watchers}}{{active}}:{{accountId}}{{^last}},{{/}}{{/}}
  • advanced branch: to iterate over the inactive watchers
    • name: varInactiveWatcher
    • smart value: {{varWatchers.split(",").match("(false:.+)").match(".*:(.+)")}}
      • action: manage watcher, removing {{varWatcherAccountId}}

How that works...

  • the create variable action iterates over the values in the watchers, saving their active status and account id values, producing a comma-separated values list
  • the advanced branch's smart value...
    • splits the values apart on the commas
    • uses the match() function with a regular expression to find any that were inactive, with a "false" value
    • then the second match() pulls out the accountId from after the colon separator

 

Please note well: this rule uses a branch over multiple things, and so the loops run in parallel and asynchronously.  Test this rule to confirm it works as you expect as it will be potentially be making multiple updates to the watchers for each issue.

Andreas Safar April 19, 2024

Hi Bill,

thank you for the detailed response! My updated rule ran successfully, but first it did not remove any inactive users.

Within the "manage watcher" action I replaced {{varWatcherAccountId}} with {{varInactiveWatcher}} and since then it removes the inactive users. 

You were are a great help. Thank you very much for your support!

 

Best regards

Andreas

 

 


Summarizing the final rule for reference:

  • trigger: scheduled (JQL e.g. watcher in inactiveUsers() )
  • action: create variable
    • variable name: varWatchers
    • smart value: {{#issue.watchers}}{{active}}:{{accountId}}{{^last}},{{/}}{{/}}
  • advanced branch: to iterate over the inactive watchers
    • smart value: {{varWatchers.split(",").match("(false:.+)").match(".*:(.+)")}}
    • variable name: varInactiveWatcher
    • Then action: manage watcher, removing {{varInactiveWatcher}}
0 votes
Bill Sheboy
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.
February 29, 2024

Hi @Andreas Safar 

First thing, I have not tried what you ask, although I believe it would be possible in a rule...

The {{issue.watchers}} smart value as an {{active}} attribute for each watcher.  And so you could check for a value of false and remove them: https://support.atlassian.com/cloud-automation/docs/jira-smart-values-users/

Your rule's trigger is detecting the issues with users to remove from watchers, and it could then use an advanced branch over the watchers field values, use a condition to check if they are inactive, and then remove them.

Please note: there seems to be a small delay for Jira Cloud before the attribute indicates false for an inactive user.  And so I would not try to run this rule immediately after inactivating a user.

Kind regards,
Bill

Andreas Safar March 4, 2024

Thank you for your idea Bill. I`ve tried it with different variants but had no luck so far. Getting always an error when it runs.

Note: I`ve scheduled it to run monthly over the weekend to avoid any issues with the delay

Bill Sheboy
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.
March 4, 2024

For what you have tried, please post an image of your complete automation rule, images of any relevant actions / conditions / branches, an image of the audit log details showing the rule execution, and explain what is not working as expected.  Those will provide context for the community to offer ideas.  Thanks!

 

Suggest an answer

Log in or Sign up to answer