How to convert issue links into Parent/Child links

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

   

The information on this page relates to custom development via a third-party app in Jira Applications. Consequently, Atlassian Support cannot guarantee the provision of any support for the steps described on this page as custom development and third-party apps are not covered under Atlassian Support Offerings. Please be aware that this material is provided for your information only and that you use it at your own risk.

If you need any help with altering the script or are running into execution issue, we advise to reach out to the Atlassian Community.


 

Purpose

As part of the adoption of Advanced Roadmaps, projects that were using issue links to define hierarchy relations will have to use the Parent Link field so they become nicely visible in Advanced Roadmaps plans. 

Solution

Execute this Groovy script in the ScriptRunner's development console. Note that the JQL and issue links names will have to be changed accordingly. Follow the code comments for more info. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def issueManager = ComponentAccessor.getIssueManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// edit this query to suit
def query = jqlQueryParser.parseQuery("project = PORTPC and type =\"Program Epic\" and issueLinkType = \"breakes down into\"")
def searchService = ComponentAccessor.getComponent(SearchService.class)

def results = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

//log.debug("Total issues: ${results.total}")

results.getResults().each {documentIssue ->
	log.debug(documentIssue.key)

	def issue = issueManager.getIssueObject(documentIssue.id)

	// Get issue links
	issueLinkManager.getInwardLinks(issue.id).each {issueLink ->
		// Edit issue link name here
		if (issueLink.issueLinkType.name == "Break Down") {
			def linkedIssue = (MutableIssue) issueLink.sourceObject
			def parentLinkField = customFieldManager.getCustomFieldObjectByName("Parent Link")
			if(linkedIssue.issueType.name == "Epic" && linkedIssue.getCustomFieldValue(parentLinkField) == null)

			{
    			log.warn(issue.key + " is linked to " + linkedIssue.key) 
    			def parentLinkFieldType = parentLinkField.getCustomFieldType()
    			def newParentLink = parentLinkFieldType.getSingularObjectFromString(issue.key)
				linkedIssue.setCustomFieldValue(parentLinkField, newParentLink)
    			issueManager.updateIssue(user, linkedIssue, EventDispatchOption.ISSUE_UPDATED, false) 
			}
		}
	}
}




Last modified on Jul 16, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.