Need groovy code to clone and link issues between projects

kk4065896 April 12, 2024

I have three projects A, B, and C. Project A has 100 standard issues. So project C is masters project. So, I need a groovy code to clone 100 Standard issues from Project A to Project B and then link those 100 cloned issues to each issue being created in project C.

 

Appreciate your help and Thank you!

1 answer

0 votes
Matt Parks
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.
April 12, 2024

This is code that I wrote to do something similar. Our Information Security team has a number of different applications that they perform maintenance on on a scheduled basis (weekly, monthly, quarterly). They have created template epics with stories underneath.

The different template epics are hard-coded into the script. The script goes through each template epic, creates a new epic (copying certain fields from the template epic) and makes separate copies of all of the stories under the template epic and links them to the newly created epic.

You would basically need to get a list of the 100 standard issues from Project C. Then cycle through those issues, creating one story under Project A and another under Project B and linking them together (I think you can do the link on the second story after creating the first).

Obviously, my code doesn't do EXACTLY what you need, but I think it should be pretty easy to make a few updates to get it to copy the fields you care about and create the link (you'll need to use the IssueLinkManager class to create the link, I believe).

 

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.issue.Issue
import groovy.transform.Field
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

@Field SearchService searchService = ComponentAccessor.getComponent(SearchService.class)

Collection<String> epicKeyList = ["{epic key 1}", "{epic key 2}"]

def issueMan = ComponentAccessor.issueManager
def userMan = ComponentAccessor.userManager
def issueFactory = ComponentAccessor.issueFactory
ApplicationUser user = userMan.getUserByName("ITWorkTracking") as ApplicationUser
def teamField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName('Team').first()
def epicNameField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName('Epic Name').first()
def epicLinkField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName('Epic Link').first()

epicKeyList.each

{
  MutableIssue epicIssue = issueMan.getIssueByCurrentKey(it) as MutableIssue
    def teamFieldValue = epicIssue.getCustomFieldValue(teamField)
    def epicLinkValue = epicIssue.summary
    MutableIssue newEpic = issueFactory.getIssue()
    newEpic.setSummary(epicIssue.summary)
    newEpic.setDescription(epicIssue.description)
    newEpic.setProjectObject(epicIssue.getProjectObject())
    newEpic.setIssueTypeId(epicIssue.getIssueTypeId())
    newEpic.setCustomFieldValue(teamField, teamFieldValue)
    newEpic.setPriority(epicIssue.getPriority())
    newEpic.setLabels(epicIssue.getLabels())
    newEpic.setCustomFieldValue(teamField, teamFieldValue)
    newEpic.setCustomFieldValue(epicNameField, epicLinkValue)
    issueMan.createIssueObject(user, newEpic)

   
def issues = getTemplateEpicIssues(it, user)  //get a list of issues from the template Epic that will be cloned

    issues.each

    {
    teamFieldValue = it.getCustomFieldValue(teamField)
        MutableIssue newStory = issueFactory.getIssue()
        newStory.setSummary(it.summary)
        newStory.setDescription(it.description)
        newStory.setProjectObject(it.getProjectObject())
        newStory.setIssueTypeId(it.getIssueTypeId())
        newStory.setCustomFieldValue(teamField, teamFieldValue)
        newStory.setCustomFieldValue(epicLinkField, newEpic)
        newStory.setPriority(it.getPriority())
        issueMan.createIssueObject(user, newStory)    

    }    

}

List<Issue> getTemplateEpicIssues(String epicKey, ApplicationUser user)

{

    def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
    def childrenQuery = jqlQueryParser.parseQuery(""" issuefunction in issuesInEpics("key = ${epicKey}") ORDER BY Created ASC""")
    def children = searchService.search(user, childrenQuery, PagerFilter.getUnlimitedFilter())
    return children.getResults()
}  

Suggest an answer

Log in or Sign up to answer