Sometimes we need to create a special ticket for the Versions that we Release (with the subtasks that composes the version listed inside the “Version” ticket, for example as linked issues).

Using Groovy with Adaptavist Scriptrunner and JIRA this task is very easy.

Just we need to configure an inline Script Listener in the Event: VERSION_CREATE_EVENT

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.customfields.*
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IndexException
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.fugue.Option
import org.apache.log4j.Category
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.plugin.PluginAccessor
import com.atlassian.fugue.Option
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IndexException
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Category
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.priority.Priority;

def issueMgr = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def projectMgr = ComponentAccessor.getProjectManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserManager()
//We will execute actions using a jira user with permissions
def currentUserObj = userManager.getUserByName("user_with_permissions")
def linkMgr = ComponentAccessor.getIssueLinkManager()

//We capture the Event: VersionCreateEvent in a XXXX project
Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "Creating Version ticket: " + event.version.name

//On new version created we want a new ticket in VERSIONS project with all related tasks linked to the ticket
def projectName = "VERSIONS"
def newissue =  issueFactory.getIssue();
newissue.setSummary(event.version.name)
newissue.setProjectObject (projectMgr.getProjectByCurrentKey(projectName))
newissue.setIssueTypeId("ID_ISSUE_TYPE")
def project = projectMgr.getProjectByCurrentKey(projectName)
newissue.setAssigneeId(project.getLeadUserKey())

log.debug "Set the user of the create_version action as reporter of the new ticket created \n"
newissue.setReporterId(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getKey())

log.debug "Example of how to set a Combo field\n"
def cf = customFieldManager.getCustomFieldObjects(newissue).find{it.name == "Dropdown field or multiselect"}
def optionsManager = ComponentAccessor.getOptionsManager()
def fieldConfig = cf.getRelevantConfig(newissue)
def value = optionsManager.getOptions(fieldConfig)?.find {
    it.toString() == 'Selected value'
}
log.debug "DEBUG: "+value+"\n"
newissue.setCustomFieldValue(cf, [value]);

log.debug "Example of set Priority \n"
newissue.setPriorityId("ID_OF_PRIORITY");

log.debug "Example of how to set Description \n"
newissue.setDescription(event.version.name)

log.debug "DEBUG: creating issue \n"
newissue = issueMgr.createIssueObject(currentUserObj, newissue)

log.debug "Ticket created: " + newissue.getKey() + "\n"

String version = event.version.name
String proj = event.version.projectObject.name
//Now we can search for the issues pending to link to the VERSION ticket (The tikets will be in YYYY status and in project XXXX)
String jqlSearch = "project = '"+proj+"' and status = YYYY and fixversion is empty"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
log.debug "DEBUG: JQL " + jqlSearch
SearchService.ParseResult parseResult = searchService.parseQuery(currentUserObj, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(currentUserObj, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
def issues = searchResult.issues.collect {issueMgr.getIssueObject(it.id)}
for ( issue2 in issues ){
    log.debug "Linking issues to the version ticket"
    linkMgr.createIssueLink (newissue.id, issue2.id, Long.parseLong("LINK_TYPE_ID"),Long.valueOf(1), currentUserObj)

}
}

We can combine this script with another piece of code to transition this linked issues. See this exampleJIRA Groovy Script to Transition issues from a JQL query using the Transition name and not the Transition ID

By MrAddon

Advertisements
Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s