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. 