Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Creating issue links with Scriptrunner Console

Will C
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 24, 2019

Hi there,

 

I have been having some issues creating issuelinks from newly cloned issues, I will insert my script for reference

 

The problem comes when I try issueLinkManager.createIssueLink() if I use the clonedissue.id first then the link wont write and I get a nullpointerexception, but if I put the clonedissue.id second then it will write and the script works fine but my link is obviously the wrong way round.

any ideas as to why this is happening and how I can fix it?

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
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.issue.Issue
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
IssueService issueService = ComponentAccessor.getIssueService()
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def projectTo = ComponentAccessor.getProjectManager().getProjectByCurrentKey("***")
def projSubTask = issueManager.getIssueObject("******")
def issueQuery = jqlQueryParser.parseQuery("Project = *** AND issuetype = story")
def issueResults = searchProvider.search(issueQuery, user, PagerFilter.getUnlimitedFilter())

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

issueResults.getIssues().each { documentIssue ->
def issue = issueManager.getIssueObject(documentIssue.id)
def subtasks = issue.getSubTaskObjects()
def toClone = issueFactory.cloneIssueWithAllFields(issue)
def ClonedProject = toClone.setProjectObject(projectTo)
def Cloned = issueManager.createIssueObject(user, toClone)
def clonedIssue = issueManager.getIssueObject(Cloned.key)
issueLinkManager.createIssueLink(clonedIssue.id as long, projSubTask.id as long, 10304 as long, 1, user )
if (subtasks) {
subtasks.each { subTask ->
def subTaskParent = Cloned
def subTaskToClone = issueFactory.cloneIssueWithAllFields(subTask)
def subTaskProject = subTaskToClone.setProjectObject(projectTo)
def subTaskCloned = issueManager.createIssueObject(user, subTaskToClone)
subTaskManager.createSubTaskIssueLink(subTaskParent, subTaskCloned, user)
}
}
}

 

2 answers

1 accepted

0 votes
Answer accepted
Will C
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 25, 2019

This is strange, seems to be only doing this if I try to link it to a particular issue, I have tried with several other issues and it works fine. this is a dev instance though so it could be something to do with the environment.

0 votes
Antoine Berry
Community Champion
April 24, 2019

Hi @Will C ,

Your script has been working fine with me. I tried linking both ways with blocks linking. I do not think I have changed muched, I added some logs to check the execution, maybe you could provide some logs ?

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
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.issue.Issue
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
IssueService issueService = ComponentAccessor.getIssueService()
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager()
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def projectTo = ComponentAccessor.getProjectManager().getProjectByCurrentKey("BPIW")
def projSubTask = issueManager.getIssueByCurrentKey("IBT-26")
def issueQuery = jqlQueryParser.parseQuery("key = IBT-23")
def issueResults = searchProvider.search(issueQuery, user, PagerFilter.getUnlimitedFilter())

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

issueResults.getIssues().each { documentIssue ->
def issue = issueManager.getIssueObject(documentIssue.id)
log.error("issue: " + issue)
def subtasks = issue.getSubTaskObjects()
def toClone = issueFactory.cloneIssueWithAllFields(issue)
log.error("toClone : " + toClone)
toClone.setProjectObject(projectTo)
def Cloned = issueManager.createIssueObject(user, toClone)
log.error("Cloned : " + Cloned)
def clonedIssue = issueManager.getIssueObject(Cloned.key)
log.error("clonedIssue : " + clonedIssue)
issueLinkManager.createIssueLink(clonedIssue.id as long, projSubTask.id as long, 10000 as long, 1, user )
if (subtasks) {
subtasks.each { subTask ->
def subTaskParent = Cloned
def subTaskToClone = issueFactory.cloneIssueWithAllFields(subTask)
def subTaskProject = subTaskToClone.setProjectObject(projectTo)
def subTaskCloned = issueManager.createIssueObject(user, subTaskToClone)
subTaskManager.createSubTaskIssueLink(subTaskParent, subTaskCloned, user)
}
}
}

Antoine

Will C
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 24, 2019

Hi Antoine, thanks for the reply, I've tried with some logs, it creates the first issue and then fails, Im sure its failing at the point of creating the issuelink as if you take the createissuelink out then it runs fine.

Capture.JPGCapture2.JPG

I will experiment with different link types and different issues to see if its that? although I would have thought this should run with any?

Will C
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 25, 2019

This is strange, seems to be only doing this if I try to link it to a particular issue, I have tried with several other issues and it works fine. this is a dev instance though so it could be something to do with the environment.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events