Forums

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

Trouble adding watchers

Jeff Tillett
Community Champion
December 21, 2018

Howdy all!

I am trying to add a watcher to a ticket that is being created inside of a scripted post function. 

Right now, I've got my code creating the issue, and I can programatically get the issue ID of the issue being created, however I cannot get the doAfterCreate function to work as shown in the Scriptrunner docs. I have actually never gotten that function to work in any script I've ever tried to use it in, so I think I am not understanding it's use correctly.

My code is here:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.project.Project
import com.atlassian.jira.bc.project.component.DefaultProjectComponentManager
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentImpl
import com.atlassian.jira.bc.project.component.ProjectComponentManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def compon = "gtm-notification"

// create ComponentAccessor manager/service objects so we can create issues
IssueService issueService = ComponentAccessor.getIssueService()

// get info needed for script to create issues in project
ApplicationUser itautomation = ComponentAccessor.getUserManager().getUserByKey('itautomation')


// issue type ids
String story = "6"
String task = "3"

// component defs
def componentManager = ComponentAccessor.getProjectComponentManager()



// user defs
String reporter = "reporter"

// gtm notification stories
createTask(issueService, 17300, story, "DOCS GTM Notification - ", reporter, compon)
createTask(issueService, 17602, task, "Education GTM Notification - ", reporter, compon)


def createTask(IssueService issueService,
long projectId,
String issuetype,
String summary,
ApplicationUser reporter,
String component) {
log.info("component: " + component)

// build new issue parameters
def compon = "gtm-notification"
def componentManager = ComponentAccessor.getProjectComponentManager()

if (!componentManager.findByComponentName(projectId,compon)) {
try {
long assgineeType = 0
componentManager.create("gtm-notification","",null,assgineeType,projectId)
} catch (e) {
log.warn(e)
}
}

def gtmcomp = componentManager.findByComponentName(projectId,compon)
IssueInputParameters params = issueService.newIssueInputParameters()
log.warn("params: " + params)
params.setProjectId(projectId)
.setIssueTypeId(issuetype)
.setSummary(summary)
.setReporterId(reporter.getUsername())
.setComponentIds(gtmcomp.getId())

// validate the issue can be created as to not run into any errors
CreateValidationResult validation = issueService.validateCreate(reporter, params)
log.warn("isValid: " + validation.isValid())

// create the issue
if (validation.isValid()) {

IssueResult createResult = issueService.create(reporter, validation)

def newissue = createResult.getIssue()
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()

try {
doAfterCreate = {
def userKeyToAddAsWatcher = "userid"
def watcher = userManager.getUserByKey(userKeyToAddAsWatcher)

if (watcher) {
watcherManager.startWatching(watcher, newissue)
}
else {
log.warn("User with key: ${userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}
}
}
catch(e)
{log.warn(e)}
}
}

 I know I'm probably importing way more than I need, but at this point I'm just getting lost in my code. The main thing giving me fits troubleshooting this myself, is that I cannot get ANY logging statements to print after line 86 when the issue is created no matter what I do.

We are on Jira 7.10.2 with Scriptrunner 5.4.43. 

I've been struggling to get doAfterCreate to work in any fashion since Jira 6 with whatever scriptrunner version was current at the time. :)

4 answers

1 accepted

3 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
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.
December 24, 2018

Hi @Jeff Tillett@Tom Lister

The doAfterCreate makes sense and is used only if you use the CloneIssue build in post functions. 

In your case, where you use a custom script, is not applicable and you don't really need it. 

I rewrote your script, so could you please give it a try and let me know if this does what you need 

import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

def projectManager = ComponentAccessor.projectManager
def userManager = ComponentAccessor.userManager
def allIssueTypes = ComponentAccessor.getComponent(ConstantsManager).allIssueTypeObjects

def story = allIssueTypes.findByName("Story")?.id
def task = allIssueTypes.findByName("Task")?.id

def watcher = userManager.getUserByKey("anuser")
def reporter = userManager.getUserByKey("admin")
def projectA = projectManager.getProjectObjByKey("ATG").id
def projectB = projectManager.getProjectObjByKey("JRA").id

def component = "gtm-notification"

// Create issues
def issueA = createTask(projectA, story, "DOCS GTM Notification - ", reporter, component)
def issueB = createTask(projectB, task, "Education GTM Notification - ", reporter, component)

// Add watcher to issues
[issueA, issueB].findAll { it }.each { ComponentAccessor.watcherManager.startWatching(watcher, it) }

MutableIssue createTask(long projectId, String issueType, String summary, ApplicationUser reporter, String comp) {

def issueService = ComponentAccessor.issueService
def componentManager = ComponentAccessor.getProjectComponentManager()
def component = componentManager.findByComponentName(projectId, comp) ?:
componentManager.create("gtm-notification", "description", "", 0L, projectId)

IssueInputParameters params = issueService.newIssueInputParameters()
params
.setProjectId(projectId)
.setIssueTypeId(issueType)
.setSummary(summary)
.setReporterId(reporter.key)
.setComponentIds(component.id)

CreateValidationResult validation = issueService.validateCreate(reporter, params)

if (!validation.valid) {
log.error validation.errorCollection
return null
}

issueService.create(reporter, validation).issue
}

Kind regards, 

Thanos

Jeff Tillett
Community Champion
January 2, 2019

This script seems pretty hard coded to only account for those 2 issues in 2 projects. This will actually end up being N projects and X issues based on other logic, so I'm needing something that is less hardcoded for specific counts like project a OR b and issue 1 OR 2. Also, I've got about 20 some odd watchers to use. 

 

While this script most likely is perfect for what it does now, it does not appear to be written to scale.

0 votes
Jeff Tillett
Community Champion
January 2, 2019

I want to thank everyone for their replies so far. I'm not really looking for someone to do the work for me, but rather to explain what I am not understanding and point me in a new direction. 

 

Thanos, your explanation of doAfterCreate makes total sense now, thank you!

0 votes
Vasiliy Zverev
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.
December 24, 2018

Dear Jeff, hi!

Try this script

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.user.ApplicationUser

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def compon = "gtm-notification"

// create ComponentAccessor manager/service objects so we can create issues
IssueService issueService = ComponentAccessor.getIssueService()

// get info needed for script to create issues in project
ApplicationUser itautomation = ComponentAccessor.getUserManager().getUserByKey('itautomation')

// issue type ids
String story = "6"
String task = "3"

// component defs
def componentManager = ComponentAccessor.getProjectComponentManager()

// user defs
String reporter = "reporter"

// gtm notification stories
createTask(issueService, 17300, story, "DOCS GTM Notification - ", reporter, compon)
createTask(issueService, 17602, task, "Education GTM Notification - ", reporter, compon)

def createTask(IssueService issueService,
long projectId,
String issuetype,
String summary,
ApplicationUser reporter,
String component) {
log.info("component: " + component)

// build new issue parameters
def compon = "gtm-notification"
ProjectComponentManager componentManager = ComponentAccessor.getProjectComponentManager()

if (!componentManager.findByComponentName(projectId,compon)) {
try {
long assgineeType = 0
componentManager.create("gtm-notification","",null,assgineeType,projectId)
} catch (e) {
log.warn(e)
}
}

def gtmcomp = componentManager.findByComponentName(projectId, compon)
IssueInputParameters params = issueService.newIssueInputParameters()
log.warn("params: " + params)
params.setProjectId(projectId)
.setIssueTypeId(issuetype)
.setSummary(summary)
.setReporterId(reporter.getUsername())
.setComponentIds(gtmcomp.getId())

// validate the issue can be created as to not run into any errors
CreateValidationResult validation = issueService.validateCreate(reporter, params)
log.warn("isValid: " + validation.isValid())

// create the issue
if (validation.isValid()) {

IssueResult createResult = issueService.create(reporter, validation)

Issue newissue = createResult.getIssue()
WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
ApplicationUser userManager = ComponentAccessor.getUserManager()

try {
String userKeyToAddAsWatcher = "userid"
ApplicationUser watcher = userManager.getUserByKey(userKeyToAddAsWatcher)

if (watcher != null) {
watcherManager.startWatching(watcher, newissue)
}
else {
log.warn("User with key: ${userKeyToAddAsWatcher} does not exist therefore would not be added as a watcher")
}

}
catch(e)
{log.warn(e)}
}
}
Jeff Tillett
Community Champion
January 2, 2019

It looks like everything that was there to make sure the new issues are linked to the epic they were created from has been removed. I'm still trying to dig through the rest of this. 

Jeff Tillett
Community Champion
January 2, 2019

@Vasiliy Zverev,

This had no change from my previous script after running. The issues are created and still have no watchers. Also, my exception is still not being written to the logs. 

Also - 

WatcherManager watcherManager = ComponentAccessor.getWatcherManager()

errors out as "Unable to resolve class WatcherManager", so I changed it to a def, and after I did that,

 ApplicationUser userManager = ComponentAccessor.getUserManager()

errored out as "Cannot assign value of type UserManager to variable of type ApplicationUser", so I changed that to a def as well. This cleared all errors, but did not work. 

Tom Lister
Community Champion
January 2, 2019
ApplicationUser userManager = ComponentAccessor.getUserManager()
should be
UserManager userManager = ComponentAccessor.getUserManager()

 

WatcherManager watcherManager = ComponentAccessor.getWatcherManager()
should have worked
0 votes
Tom Lister
Community Champion
December 23, 2018

Hi @Jeff Tillett

I had a go at trying to recreate this with the same results :-(

However I have just noticed that doAfterCreate is no longer mentioned in the latest docs (5.4.45). Last I can see it in is in 5.4.12

Maybe check with Adaptavist if this is still supported. I'd guess not.

Suggest an answer

Log in or Sign up to answer