Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Narrow script down to a specific project

Ashley February 2, 2023

I have this script I'm using as a post function for adding a watcher during a workflow transition. I don't know how to write script, I found it, lol. How could I narrow it down to only run when a specific project is using the workflow?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue


def userName = "BM0987" // name of the user
def userManager = ComponentAccessor.getUserManager()
MutableIssue issues = issue as MutableIssue
def user = userManager.getUserByName(userName)

def watcherManager = ComponentAccessor.getWatcherManager()

watcherManager.startWatching(user, issue)

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 5, 2023

Hi @Ashley

For your requirement, you can try something like this:-

import com.atlassian.jira.component.ComponentAccessor

def userManager = ComponentAccessor.userManager
def watcherManager = ComponentAccessor.watcherManager

def project = issue.projectObject
def username = 'BM0987'

if (project.key == 'MOCK') {
def user = userManager.getUserByName(username)
watcherManager.startWatching(user, issue)
}

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to modify it accordingly.

I have added a filter for the project key in the code above. Hence, the watch manager will update the watcher only if an issue belongs to that particular project.

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Ashley February 6, 2023

This is perfect! Thank you so much for taking the time to help me. I am very grateful!

Ashley February 6, 2023

Also, thank you for explaining how it works. That is very helpful.

Suggest an answer

Log in or Sign up to answer