In Jira Create issue in another project with Script Runner

I have few projects, A,B,C,D,E.

A is the high level project. B,C,D,E are low level project.
I need to create a postfunction on Project A, that will create a new issue on Project, based on a drop down list value in A.

For example:

drop-down list A: with value 1, 2, 3,4,5,6

If 1 was selected, after click save issue create button, clone save issue to project B, and link these two issues.

If 2 was selected, after click save issue create button, clone save issue to project C, and link these two issues.

If 3 was selected, after click save issue create button, clone save issue to project B, and link these two issues.

If 4 was selected, after click save issue create button, clone save issue to project D, and link these two issues.

If 5 was selected, after click save issue create button, clone save issue to project B, and link these two issues.

If 6 was selected, after click save issue create button, clone save issue to project E, and link these two issues.

Is there a way I can do that with script runner?

Any insight on the matter is highly appreaciated.

Kind regards

2 answers

0 vote
Mizan Community Champion Oct 12, 2017 • edited

You can do it via script runner. However, a cleaner and better way to do this is using the Bob swift add-on create on transition .

I cannot provide you a complete working script however, you can refer and try working on the below code snippet

def public CreateLinkedIssue(String ProjectName,String IssueType, String Summary, String Description){

// importing the required managers
def issueFactory = ComponentAccessor.getIssueFactory();
def issueManager = ComponentAccessor.getIssueManager();
def projectMgr = ComponentAccessor.getProjectManager();
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
def issueMgr = ComponentAccessor.getIssueManager();
def issueLinkManager = ComponentAccessor.getIssueLinkManager();

// create new issue from factory
def issueObject = issueFactory.getIssue();

//set values
issueObject.setProjectObject(projectMgr.getProjectObjByName(ProjectName));
issueObject.setIssueTypeId("10000");
issueObject.setSummary("Child :"+issue.getSummary());
issueObject.setDescription(issue.getDescription());
issueObject.setPriority(issue.getPriority());
issueObject.setReporter(issue.getReporter());

//create actual issue
def child_issue= issueManager.createIssueObject(authenticationContext.getLoggedInUser(), issueObject)
Long lid=10003;
Long seq =1;
//link the issue
issueLinkManager.createIssueLink(issue.getId(),issueObject.getId(),lid,seq,issue.getAssignee());

}

 

Hi Mizan,

I have post function code as below, will you help me take a look?

import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.atlassian.crowd.embedded.api.User;
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.IssueFactory;

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.util.UserManager

class TaskVersionListener extends AbstractIssueEventListener {
Category log = Category.getInstance(TaskVersionListener.class)

@Override
void workflowEvent(IssueEvent event) {
log.setLevel(org.apache.log4j.Level.DEBUG)
def issue = event.getIssue()
if ( issue.getIssueTypeId() == "XXXXX" ) { // The Issue Type to catch in the Listener
def projectName = "YYYYY" //Target project name

def issueMgr = ComponentAccessor.getIssueManager()
def projectMgr = ComponentAccessor.getProjectManager()
String currentUser = event.getUser().getName();
User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentManager componentManager=ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
log.debug "IF ISSUE IS TYPE XXXXX CLONE ISSUE TO PROJECT YYYYY WITH SAME FIELDS, ETC"

def issueFactory = ComponentAccessor.getIssueFactory()
def newissue = issueFactory.getIssue()
newissue.setSummary (issue.summary)
newissue.setProject (projectMgr.getProjectByName(projectName))
newissue.setIssueTypeId("JJJJ") //The ID of the target Issue Type
newissue.description = issue.description
newissue.reporter = issue.getReporter()

//Set the Project Lead of the target project as assignee of the new ticket
def project = projectMgr.getProjectObjByName(projectName)
newissue.setAssigneeId(project.getLeadUserKey())

//Copy the fields that you want
def cf = null;
//ZZZZZ (list)
cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'ZZZZZ'}
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
//YYYYYY (date) another way to obtain the custom field
cf = customFieldManager.getCustomFieldObject("customfield_NNNNN");
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))
//XXXXXX (text, numeric, ...)
cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'XXXXX'}
newissue.setCustomFieldValue(cf, issue.getCustomFieldValue(cf))


def params = ["issue":newissue]
def subTask = issueMgr.createIssue(currentUserObj, params)


def linkMgr = ComponentManager.getInstance().getIssueLinkManager()
linkMgr.createIssueLink (newissue.id, issue.id, Long.parseLong("ZZZZZ"),Long.valueOf(1), currentUserObj) //ZZZZZ is the id of the type of link
}
}
}

 

Mizan 

If using post function Clones an issue and links , how to get new clone issue id and original issue id?

I did try issue.getid() , but no luck.

Mizan Community Champion Oct 15, 2017

The object for new cloned issue will be sourceIssue

I hope you are using getId() and not getid() .

Try to use as much logging as possible it will be helpful

@Mizan

After working around, I got code below, but looks like getId() is still null. code as below:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.workflow.TransitionOptions
/*
MutableIssue issue = issue
def id=issue.getId()
def pid = sourceIssue.getId()
def creatorid = sourceIssue.getCreatorId()
*/
def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'ECUlist'}
issue.setCustomFieldValue(cf, sourceIssue.getCustomFieldValue(cf))
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
//used for debug
log.debug "I will transit the subtask " + issue.key
log.debug "I will transit the subtask " + issue.id
log.debug "I will transit the subtask " + issue.getId()
//issue.summary = 'NEW Cloned Issue: ' + sourceIssue.summary
//issue.summary = "NEW Cloned Issue: " + "line 0 summary " + "sourceIssue.summary " + sourceIssue.summary

/*
doAfterCreate = {

def cwdUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.setSkipScreenCheck(true)
log.debug "I will transit the subtask " + issue.key
issue.summary = "NEW Cloned Issue: " + "line 1 summary " + "sourceIssue.summary " + sourceIssue.summary

}
*/

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def issueManager = ComponentAccessor.getIssueManager()

doAfterCreate = {
def issueToLinkTo = issueManager.getIssueByCurrentKey(sourceIssue.getId().toString())
def duplicateLinkTypeName = "Duplicate"

issue.summary = "NEW Cloned Issue: " + "line 1 summary " + "sourceIssue.summary " + sourceIssue.summary

// issue.summary = "NEW Cloned Issue: " + "line 2 summary" + "sourceIssue.summary " + sourceIssue.summary
// issue.summary = "NEW Cloned Issue: " + "line 3 summary" + "sourceIssue.summary " + sourceIssue.summary + "sourceIssue id: " + sourceIssue.getId()
// issue.summary = "NEW Cloned Issue: " + "line 4 summary" + "sourceIssue.summary " + sourceIssue.summary + "sourceIssue id: " + sourceIssue.getId() + sourceIssue.id

def duplicateLinkType = issueLinkTypeManager.getIssueLinkTypesByName(duplicateLinkTypeName)

if (!duplicateLinkType) {
log.warn ("Issue link type with name: ${duplicateLinkTypeName} does not exist")
}
else {
issue.summary = 'NEW Cloned Issue: ' + sourceIssue.summary + ' sourceIssue.id: ' + sourceIssue.id.toString() + ' Issue.id: ' + sourceIssue.getId().toString()+ ' Issue.id: ' + issue.getId().toString()+ ' Issue.id: ' + issue.id.toString()
// issueLinkManager.createIssueLink(issue.getId(), issueToLinkTo.id, duplicateLinkType[0].id, 1, currentUser)
issueLinkManager.createIssueLink(issue.getId(), sourceIssue.getId(), new Long(12000), null, authenticationContext.getLoggedInUser())
}
}

 

Mizan Community Champion Oct 16, 2017

Which post function are you using ? 

Custom script or Clone issue ? 

AND

This is not correct 

def issueToLinkTo = issueManager.getIssueByCurrentKey(sourceIssue.getId().toString())

issue id eg: 10001 , issue key eg: EX-109 .

@Mizan

ScriptRunner workflow function - Clones an issue and links (condition & additional actions apply).

@Mizan

Is there anyway we can setup a webex meeting?

Mizan Community Champion Oct 16, 2017

Sorry, that wont be possible.

Why you need any scripting if you are using clone issue function ?

Did you even try this and checked how it works ?

All you need to do is have multiple post functions for cloning the issue and linking. Each post function will have a condition which will check the drop down list value if it passes it will execute else return false. 

@Mizan

Thanks for point out,

After I change togetKey(), still have same issue.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

362 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you