Forums

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

Groovy Script to Clone an Issue and it's Linked Issues (all fields)

Katrina Thacker March 24, 2020

Hello! I'm trying to use the JMWE "Create/Clone Issues Post Function" capability to clone an issue on a transition AS WELL as clone it's linked issues - all fields including custom fields.  Essentially I want to create nearly an exact duplicate of an issue and it's linked issues.  I'm using the JMWE so I can iterate and create multiples given certain criteria.  Anyone done this before and willing to share how?  I've successfully used this function to clone the 'parent' issue but struggling how to clone the linked issues and ensure they are linked to the clone of the "parent" issue. 

 

Other info: Jira Server v8.5.3

Scriptrunner: 5.6.14.1-p5

JMWE:  6.2.4

 

Thanks in advance for your help!

2 answers

0 votes
Prashant Mali
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.
October 19, 2021

Is this solution works for any one.

 

I have same requirement.

We have parent child issues between two projects. I want to clone complete structure of Parent- Child issues.

Kindly suggest

0 votes
Radhika Vijji _Innovalog_
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.
March 24, 2020

Hi Katrina,

Let's assume on the trigger of the transition "Clone now" you want to clone a parent and issues linked to it through "is related to" link type.

Clone Parent : Add the Create issue post-function to the "Clone now" transition.

  1. Select the destination project from Project
  2. Select the Issue type same as the current parent
  3. Select the link type as "clones"
  4. Using the "Set fields of the new section" set the fields on the new parent (copy or set with new values)
  5. Save the post-function.

Clone linked issues: Add a "Transition Linked Issues" post-function configured as:

  1. Select the Link type as "is cloned by"
  2. Input the transition name as "Clone linked issues" (will be created in the next step)
  3. Add a Delayed execution of 3 seconds
  4. Save the post-function.
  5. Publish the workflow.

Create "Clone linked issues" transition

  1. On the Parent workflow of the destination project (on the same workflow if both parents share the same workflow), create a transition named "Clone linked issues" on the Create transition to the same initial status.
  2. Hide that transition from users by adding a "Hide Transition" Condition

Clone linked issues: Add the Create issue post-function to the "Clone linked issues" transition

  1. Select the Project as "Same as current issue"

  2. Select the Issue Type as Calculated, with value

    {{it.fields.issuetype.id}}
  3. Select the Link to new issue as "is related to"

  4. Input the Summary as:

    {{it.fields.summary}}
  5. Select "Multiple issue creation" option
  6. In the iterator write the following script:

    {{issue | linkedIssues( "clones" ) | first | linkedIssues( "is related to" ) | dump}}

    (note that "clones" here is the name of the reverse direction of the "is cloned by" link type used above.

  7. Save the post-function.
  8. Publish the workflow

Now when you trigger the transition "Clone now" the parent and its linked issues are cloned to the new project. Hope this helps.

Regards,

Radhika

Katrina Thacker March 24, 2020

Thank you so much Radhika...gonna try and set this up now.  Will let you know how it goes. 

Katrina Thacker March 24, 2020

@Radhika Vijji _Innovalog_ your suggestion helped me overcome cloning the linked issues although I'm having issues with specific fields not cloning as expected; I'm working through that.  The bigger issue is that the cloned linked issues are not linking to the cloned issue (they are linking to the original issue).  Let me see if I can describe what I'm trying to achieve more clearly:

 

Project: the cloning is within the same project and all issue types I'm trying to clone are using the same workflow

Scenario: I create issue type = Feature and it has 2 linked issues of issue type Test (will call them Test A & Test B); link from Feature to Test is "tested by"; link from Test to Feature is "tests"

When I transition the Feature to "Ready for Development" I need the following issues to be created:

a) Issue Type "Story" - Clone of Feature with a Platform field = app1 and exact clones of Test A and Test B linked to Story 1 with "tests' link

b) Issue Type "Story" - Clone of Feature with a Platform field = app2 and exact clones of Test A and Test B linked to this Story  with "tests' link

I hope that helps and thanks again for all your help!

Radhika Vijji _Innovalog_
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.
March 24, 2020

Hi Katrina,

My bad. I have provided you the answer for Jira Cloud and not Jira server. Here is what you need to do. Please try and let me know:

Clone Feature: Add the Create issue post-function to the "Ready for Development" transition.

  1. Select the destination project as Same as current issue
  2. Select the Issue type as "Story"
  3. Select the link type as "clones"
  4. Under the "Set fields of the new section"
    1. Select the Platform field.
    2. Select "Set field value to constant or Groovy template"
    3. Input the value 
    4. it
  5. Select the "Multiple Issue Creation" checkbox.
    1. In the iterator, input the following code"
    2. ["app1","app2"]
  6. Save the post-function.

Clone linked issues: Add a "Transition Linked Issues" post-function configured as:

  1. On the "Ready for Development" transition add a "Transition related issues" post-function.
  2. Input the transition name as "Clone linked issues" (will be created in the next step) and click on "Add"
  3. Select the "Issue linked to the current issue through the following link type" under "Which issues"
  4. Select "is cloned by" under "Issue Link"
  5. Save the post-function.

Create "Clone linked issues" transition

  1. On the "Create" transition of the workflow create a transition named "Clone linked issues" to the same initial status.
  2. Hide that transition from users by adding a "Hide Transition" Condition

Clone linked issues: Add the "Create issue" post-function to the "Clone linked issues" transition

  1. Select the Project as "Same as current issue"

  2. Select the Issue Type as Calculated, with value

     

     

  3. it.issuetype.id
  4. Select the Link to new issue as "tests"

  5. Input the Summary as:

  6. it.summary
  7. Select "Multiple issue creation" option
  8. In the iterator write the following script. Note that "clones" here is the name of the reverse direction of the "is cloned by" link type used above.

  9. issue.getLinkedIssues("clones").first().getLinkedIssues("tested by")

     

  10. Save the post-function.
  11. Publish the workflow

Now when you trigger the transition "Ready For Development" two cloned Stories will be created for the feature with two linked issues each with the link type as "tests" (from test to Story)

Hope this helps,

Regards,

Radhika

Katrina Thacker March 26, 2020

Hi again @Radhika Vijji _Innovalog_ 

I seem to be much closer thanks to your help.  I'm sure I have made an error so hoping you can walkthrough what I have set up and another set of eyes can find.  Here's the error I am receiving:
Screen Shot 2020-03-26 at 12.01.48 PM.png

Here's the Clone Feature "Ready for Development" transition set up:

Screen Shot 2020-03-26 at 12.08.51 PM-1.jpg

And here's the Cloned Linked AC Test Issues transition set up:

Screen Shot 2020-03-26 at 12.44.00 PM.png

I'm sure I'm doing something wrong here in terms of the iterator statement you provided....I've changed the linked issue type(s) in the statement multiple times using different values and still getting the same error.  Thanks again so much for your help.

Radhika Vijji _Innovalog_
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.
March 26, 2020

Hi Katrina,

I think the Stories itself are not created. Because the iterator script is wrong. It is currently:

["app1,app2"]

Which will try creating one Story with the Summary "app1, app2". But since you are trying to set the Platform field to "app1, app2" which likely is a select field, the option turns to be invalid and hence the issue cannot be created. Please fix the script to:

["app1,"app2"]

save the post-function, publish the workflow and retest.

Regards,

Radhika

Katrina Thacker March 27, 2020

Hi @Radhika Vijji _Innovalog_  the stories are created...that's an edit I did to the screenshot to not show what could be propietary info. That part of the script works fine. 

Radhika Vijji _Innovalog_
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.
March 27, 2020

Hi Katrina,

Ok. 

  1. The Create issue post-function on the "Cloned Linked AC Test Issues" does not see the link issues because the link type specified is incorrect. It needs to be: 
  2. issue.getLinkedIssues("is the parent of").first().getLinkedIssues("tested by")
  3. In the Transition related issue post-function the specified link type should be the link from the Feature to the created Stories, which is the other direction of "is the parent of".

Can you fix these and retry?

Regards,

Radhika

Katrina Thacker April 2, 2020

HI @Radhika Vijji _Innovalog_  hope you are well and healthy.  I'm still working on getting this to work as needed.  I made the changes you recommended and was able to get the Test issue types cloned but they link to the original Feature not the newly created Story issue types.  I'm trying the Link Issues to Current Issue JMWE function but can't get this to work correctly.  The result shows the correct issues that I want to link the Test issue type with but I keep getting an error.  Hoping you can help.  Thanks so much again for all your help!

For some reason I can't attach the screen shot.  Here's how I've tried applying the groovy template for the JQL query:

${issue.getLinkedIssues("tests").first().getLinkedIssues("is the parent of")}

I get this error:

Message:
Errors: [Error in JQL Query: Expecting a field name but got '['. Did you mean 'cf['? (line 1, character 1)], Warnings: []

 I've also tried this:
<%= issue.getLinkedIssues("tests").first().getLinkedIssues("is the parent of") %>

and get the same error.

Suggest an answer

Log in or Sign up to answer