Jira Plugin Classes and Create Linked Issues with Groovy

It is the same like @withplugin in Scriptrunner. Accessing classes from plugins.

A request in Jira Service Desk. At an issue transition, a post function is triggered that creates a new issue in a Kanban project. The post function also adds the request type to the summary field of the linked issue.

Request type of the service desk issue.

Service Desk Request Type

The linked Kanban issue. Summary becomes „<copy of request type>: <copy of summary>“

Linked Issue Summary

Source Code

We get access to classes from other plugins through a classloader of the related plugin.

def serviceDeskClassLoader = ComponentAccessor.getPluginAccessor().getEnabledPlugin("com.atlassian.servicedesk").classLoader

def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceDeskClassLoader.loadClass('com.atlassian.servicedesk.api.requesttype.RequestTypeService'))

Beneath the entire code. You will get an error while editing that a request service is not available. Ignore it. You shouldn’t see any errors at runtime.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.plugin.PluginAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import org.apache.log4j.Level
import org.apache.log4j.Logger
 
def myLog = Logger.getLogger("WF Linked Issue Creation")
myLog.setLevel(Level.DEBUG)
 
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueService = ComponentAccessor.getIssueService()
def projectManager = ComponentAccessor.getProjectManager()
def pluginAccessor = ComponentAccessor.getPluginAccessor()
 
def issueDescription = issue.getDescription()
 
def serviceDeskClassLoader = ComponentAccessor.getPluginAccessor().getEnabledPlugin("com.atlassian.servicedesk").classLoader
def requestTypeService = ComponentAccessor.getOSGiComponentInstanceOfType(serviceDeskClassLoader.loadClass('com.atlassian.servicedesk.api.requesttype.RequestTypeService'))
 
def reqQ = requestTypeService.newQueryBuilder().issue(issue.id).build()
def reqT = requestTypeService.getRequestTypes(user, reqQ)
def requestType = reqT.findFirst().get().getName()
 
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.setProjectId(projectManager.getProjectByCurrentKey("HRK").getId())
issueInputParameters.setIssueTypeId("10002")
issueInputParameters.setSummary(requestType + ": " + issue.getSummary())
issueInputParameters.setDescription(issueDescription)
issueInputParameters.setPriorityId(issue.getPriority().getId())
issueInputParameters.setReporterId(user.getUsername())
 
def CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters)
def IssueResult createResult
if (createValidationResult.isValid())
{
createResult = issueService.create(user, createValidationResult);
if (!createResult.isValid())
{
log.error("Error while linked issue creation: " + issue)
}
} else {
log.error("Error while linked issue creation validity check: " + issue)
}
 
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
try {
issueLinkManager.createIssueLink(issue.getId(), createResult.getIssue().getId(), 10300L, 1L, user)
} catch(Exception error) {
log.error("Error while link creation: " + error + " - in issue" + issue)
}

Install myGroovy

It is free, and an extremely versatile add-on.

https://marketplace.atlassian.com/apps/1218755/mygroovy?hosting=server&tab=overview

Use it together with JSIncluder to inject JavaScript in Jira issues. JSIncluder is from the same group of developers like myGroovy. And it is free, as well.

https://marketplace.atlassian.com/apps/1211670/jsincluder?hosting=server&tab=overview

Further Information

https://github.com/mailru/jira-scripts/tree/master/JIRA%20groovy%20templates

https://gist.github.com/chuikoaleksandr

Support

Send me an email for any kind of support related to Atlassian.

More about Groovy in Jira…