Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy script modification - Clone w/ dependency, need project array

Deborah Hansen January 15, 2012

Need to mofify code below so I can apply to a list / array of projectname...

Would also like to copy over custom fields - I have some of that code, but missing something:

for (CustomField cf : customFieldManager.getCustomFieldObjects(issue)) {
    if (issue.getCustomFieldValue(cf)) {
        newissue.setCustomFieldValue(cf,  oneIssue.getCustomFieldValue(cf))
    }
}

Ref:

https://studio.plugins.atlassian.com/wiki/display/GRV/Post+Functions#PostFunctions-Cloneissuewithdependencylink

Current / Working code:

______________________________________


import com.atlassian.core.user.UserUtils import com.atlassian.jira.ComponentManager import com.atlassian.jira.issue.link.IssueLink import com.atlassian.jira.util.ImportUtils import com.opensymphony.user.User import com.opensymphony.workflow.WorkflowContext import org.apache.log4j.Category log = Category.getInstance("com.onresolve.jira.groovy.CreateDependentIssue") // Configurable section def projectName = "PMO-Test" // Name of project you want to create the issue in // for other customisations change the code below issueMgr = ComponentManager.getInstance().getIssueManager() projectMgr = ComponentManager.getInstance().getProjectManager() String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller(); User currentUserObj = UserUtils.getUser(currentUser); def wasIndexing = ImportUtils.indexIssues ImportUtils.indexIssues = true issueFactory = ComponentManager.getInstance().getIssueFactory() newissue = issueFactory.getIssue() newissue.setSummary ("Tier I Common - $issue.summary") newissue.setProject (projectMgr.getProjectByName(projectName)) newissue.setIssueType (issue.getIssueType()) newissue.description = issue.description newissue.reporter = issue.getReporter() params = ["issue":newissue] subTask = issueMgr.createIssue(currentUserObj, params) println subTask.get("key") // get the current list of outwards depends on links to get the sequence number linkMgr = ComponentManager.getInstance().getIssueLinkManager() def sequence = 0 for (IssueLink link in linkMgr.getInwardLinks(issue.id)) { if ("Depends" == link.issueLinkType.name) { sequence++; } } linkMgr = ComponentManager.getInstance().getIssueLinkManager() // TODO: Should check that 10002 is always the link id for Depends linkMgr.createIssueLink (newissue.id, issue.id, 10001, sequence, currentUserObj) ImportUtils.indexIssues = wasIndexing

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
JamieA
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.
January 24, 2012

Does it not work? I can't really see what the question is ;-)

Custom fields I do like this:

newIssueCfs.intersect(customFieldManager.getCustomFieldObjects(issue)).each {CustomField cf ->

To avoid trying to set a custom field that doesn't exist on the target issue. Not sure what happens if you do though, maybe harmless.
projectMgr.getProjectByName("${it}")

Probably better written:

ProjectList.each() {projectName

....

projectMgr.getProjectByName(projectName)

Try to avoid "it" in long loops, gets a bit confusing. Otherwise looks ok at first glance.

1 vote
Jon Sword
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.
January 24, 2012

Hi Deborah,

I have done something very similar. Your custom field copy code is correct - I am using the exact same code. I wonder if your issue is caused by the placement of that code. I have it above the issue creation.

Jon Sword
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.
January 24, 2012

Here is my linking code - this bypasses the 10002 concern

     Collection cloneIssueLinkTypes = issueLinkTypeManager.getIssueLinkTypesByName("Cloners")
     for (Iterator iterator = cloneIssueLinkTypes.iterator(); iterator.hasNext();) {
         IssueLinkType issueLinkType = (IssueLinkType) iterator.next();
         if (issueLinkType.getName() == "Cloners" ) {
             cloneIssueLinkType = issueLinkType.getId();
         }
     }
     linkMgr.createIssueLink (issue.id, newissue.id, cloneIssueLinkType, sequence, currentUserObj)

Jon Sword
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.
January 24, 2012

A stripped down (and somewhat outdated) example of my code is here.

Jon Sword
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.
January 24, 2012

P.S> I notice you use "newissue = issueFactory.getIssue()"

I use "newissue = issueFactory.cloneIssue(issue)"

This means that it automatically sets the reporter, description etc on creation (although you can override it). It makes for a bit less work.

Deborah Hansen January 24, 2012

Thank you Jon! I think I'm running inot havoc because I have the for statement inside an project list array - a sub-loop in my loop.

0 votes
Deborah Hansen January 24, 2012

I have the project array functioning - - custom fields pending...

import com.atlassian.core.user.UserUtils
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.util.ImportUtils
import com.opensymphony.user.User
import com.opensymphony.workflow.WorkflowContext
import org.apache.log4j.Category
 
log = Category.getInstance("com.onresolve.jira.groovy.CreateDependentIssue")
 
// Configurable section
def ProjectList = ["Project A", "Project B"]   //list of projects

ProjectList.each() { 

// for other customisations change the code below
 
issueMgr = ComponentManager.getInstance().getIssueManager()
projectMgr = ComponentManager.getInstance().getProjectManager()
 
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
User currentUserObj = UserUtils.getUser(currentUser);
 
def wasIndexing = ImportUtils.indexIssues
ImportUtils.indexIssues = true
issueFactory = ComponentManager.getInstance().getIssueFactory()
newissue = issueFactory.getIssue()
newissue.setSummary ("Common - $issue.summary")
newissue.setProject (projectMgr.getProjectByName("${it}"))
newissue.setIssueType (issue.getIssueType())
newissue.description = issue.description
newissue.reporter = issue.getReporter()
 
params = ["issue":newissue]
subTask = issueMgr.createIssue(currentUserObj, params)
println subTask.get("key")
 
// get the current list of outwards depends on links to get the sequence number
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
def sequence = 0
for (IssueLink link in linkMgr.getInwardLinks(issue.id)) {
    if ("Depends" == link.issueLinkType.name) {
       sequence++;
    }
}
 
linkMgr = ComponentManager.getInstance().getIssueLinkManager()
// TODO: Should check that 10002 is always the link id for Depends
linkMgr.createIssueLink (newissue.id, issue.id, 10001, sequence, currentUserObj)
ImportUtils.indexIssues = wasIndexing
}

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events