Creating multiple issues from filter results

Robert Strauch October 3, 2016

Hello,

I often need to create similar tickets which are based on a query result.

Example:

  • A filter returns 20 issues.
  • For each of these issues I need to create another issue in another project.
  • Each of the newly created issue should take the original issues' key and summary and prepend some text for the summary.
  • Original ticket
    • Key: MYKEY-45
    • Summary: Installation of release 1.2
  • New ticket:
    • Summary: Test MYKEY45 - Installation of release 1.2

 

Is there an existing plugin for such a task?

2 answers

1 vote
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.
October 4, 2016

Hi Robert,

If you are willing to user a 3rd party plugin, what you are asking is doable using ScriptRunner for JIRA (link to documentation) There is also a free version 3.1.4 - which is only available for JIRA v6 (check version history). So what you could do with the tool is to navigate to script console and run the script below.

import com.atlassian.jira.bc.issue.IssueService
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.IssueInputParameters
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter

def jqlSearch = """
project = "Source Project"
"""
def PROJECT_KEY_TO_COPY_TO = "TP"

def issueManager = ComponentAccessor.getIssueManager()
def searchService = ComponentAccessor.getComponent(SearchService)
def targetProject = ComponentAccessor.getProjectManager().getProjectObjByKey(PROJECT_KEY_TO_COPY_TO)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issues = []

SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
    def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    issues = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
}

//iterate through the issues returned from the JQL and create clone issues to project with key PROJECT_KEY_TO_COPY_TO
issues.each { it ->
    createIssue(targetProject, it, user)
}

/**
 * A function to create the target issue based on target project and properties extracted by the source issue.
 * You can build the target issue in the way you want (issueType, assignee, reporter, customFields etc..)
 * but for the example, for the mandatory fields (issueType, project) and for the highly recommended
 * (reporter, priority) we copy the source issue's ones
 * @param targetProject A target Project with key PROJECT_KEY_TO_COPY_TO
 * @param sourceIssue The source issue where from the summary (and any other properties) will be copied
 */
def createIssue(Project targetProject, Issue sourceIssue, ApplicationUser user) {
    def issueService = ComponentAccessor.getIssueService()

    // build the target issue's summary
    def targetIssueSummary = "Test ${sourceIssue.key} - ${sourceIssue.summary}"

    IssueInputParameters issueInputParameters = new IssueInputParametersImpl()
    issueInputParameters
        .setProjectId(targetProject.id) //use the mandatory target project id
        .setSummary(targetIssueSummary)//use the mandatory issue summary
        .setIssueTypeId(sourceIssue.issueTypeId) //use the mandatory source issue's issueType (make sure that the target project has this issue type configured)
        .setReporterId(sourceIssue.reporterId) //use the source issue's reporter
        .setPriorityId(sourceIssue.priority.id) //use the source issue's priority
        //set any other fields you wish for the new issue

    IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters)
    if (createValidationResult.isValid()) {
        def targetIssue = issueService.create(user, createValidationResult).issue
        log.debug "Target issue ${targetIssue.key} created with summary ${targetIssue.summary}"
    }
}

The sciprt above is tested in a JIRA v7 instance, if you need it tfor a JIRA v6 then there should be a couple of trivial changes

regards, Thanos

Suggest an answer

Log in or Sign up to answer