clone an issue with all sub tasks

Hi,
Usually, I create a swim lane with 4 or 5 subtasks related to issue. From the issues page if I select the feature with all subtasks and try to clone them all the subtasks are getting created as subtasks of the original swim lane. Is there a way to clone a swim lane with its subtasks as one set or any other way to copy them as a I set.
thanks,
VK
0
28 comments
Hi VK,

Unfortunately, it's not possible at the moment. I guess it can be implemented in future as an additional option, like 'Clone Tree' or similar.

Thank you for your suggestion!
0
Avatar
Permanently deleted user
Thank you for the reply! i will look for it in the future versions :)
0

Hi! Has the situation changed? I'd also find cloning a template issue tree useful.

0

Hi! 

No, it has not changed, I'm afraid. Here's the related feature request: https://youtrack.jetbrains.com/issue/JT-34291 So far, it's not gotten much attention (votes), though it's not the only factor, it still matters. Feel free to vote for it, as it increases its priority and subscribes you to the email notifications. To do so, sign in to Jetbrains Youtrack: https://take.ms/41YJh (screenshot) 

0

I'm new to YouTrack and immediately ran into this issue as well.  

I created a custom workflow implementation that does this.  See https://youtrack.jetbrains.com/issue/JT-34291 for details.  My implementation is recursive, so it'll clone a whole tree of subtasks, plus it'll add "(Clone)" to the summary of the top-level issue.

The only thing I didn't created as a "cloned" link (like you might have in JIRA).  I haven't learned how to detect whether or not a certain link type exists or not, but if I did, I'd check to see if "Cloned" exists and if so, then add a link for the top-level parent.

0

Hi Trev, you can use `IssueLinkPrototype.findByName(name)` method as described here 

0

Cool Anna Zhdan, thanks for the pointer.   I tried using that, but kept getting an error and I'm not sure what I did wrong.

I created a link type called "Clone" with outward name = "cloned to" and inward name = "cloned from".

Then I added the following logic to my workflow:

// Add cloned link if Clone link types exist

if (workflow.IssueLinkPrototype.findByName('cloned to') !== null) {
issue.links['cloned to'].add(newIssue);
}
When I tried using my subtask w/clone workflow, it generates a "bad_request" error.  The error isn't very clear about exactly what's wrong.  I tried a few variations of this using findByName('Clone') and issue.links['clone'].
0

Here's the full workflow I was trying in case that helps:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.action({
  title: workflow.i18n('Clone (w/Subtasks)'),
  command: 'clone-with-subtasks',
  guard: function(ctx) {
    return ctx.issue.isReported;
  },
  action: function(ctx) {
    var issue = ctx.issue;
    
    var newIssue = issue.copy();
    newIssue.summary += ' (Clone)';
    
    var copySubtask = function(newParent, subtask) {
      var newSubtask = subtask.copy();
      newParent.links['parent for'].add(newSubtask);

      // Recurse
      subtask.links['parent for'].forEach(function (subtask) { return copySubtask(newSubtask, subtask); });
    };
    
    // Copy subtasks
    issue.links['parent for'].forEach(function (subtask) { return copySubtask(newIssue, subtask); });
    
    // Add cloned link if Clone link types exist
    if (workflow.IssueLinkPrototype.findByName('cloned to') !== null) {
issue.links['cloned to'].add(newIssue);
    }
    
    workflow.message(workflow.i18n('A clone of the current issue has been created: <a href="{0}">{1}</a>', newIssue.url, newIssue.id));
  },
  requirements: {
    // TODO: add requirements
  }
});

 

0

Trev could you please attach logs of this workflow rule? They can be found in the lower panel in workflow editing UI. Could you please also attach a screenshot of your Issue Links Settings page?

 

0

Anna Zhdan, ah, I didn't see the workflow console.  That's useful.  The message I got there is pretty clear:

Processing issue SALT-4:
TypeError: Cannot read property 'findByName' of undefined

I could attack the stack trace, but we probably don't need it, we already know exactly where that's happening.  

Strange that it's not finding IssueLinkPrototype because the editor found that it was available via workflow.  I'm still a little new to javascript, so maybe I'm missing an important piece of syntax that's not obvious.

Here's a screen shot of my Issue Link Types settings page:

 

0

Trev

I'm sorry I didn't notice it before: it should be

entities.IssueLinkPrototype

instead of 

workflow.IssueLinkPrototype
0

That did the trick, thanks!   I had to make one other tweak, which was to refer to the clone link type in the look up and refer to the outgoing link name in the adding of the link.  All works now, thanks!

    // Add cloned link if Clone link types exist
    if (entities.IssueLinkPrototype.findByName('Clone') !== null) {
issue.links['cloned to'].add(newIssue);
    }

 

0

Hi!

Trev could you help me? I use your code in a workflow (thank you very much for your works!), but I need to use this feature that clone target and its subtasks, starting from a group of template task which I created specifically by assigning a particular value to a field named Category (Category = Template). When I use your function to clone the target issue with its subtask, I would like to automatically change the value of the Category Field to “Ticket” during the cloning process. Is it possible?

0

Marco Tarantino Hi! You'd need to add this code to the workflow:

newIssue.fields.Category = “Ticket”;

0

Hi Alisa Kasyanova, thanks a lot for your suggestion.

In my case the code is like below, where can I put your instruction to obtain my need?

/**
* This is a template for an action rule. This rule defines a custom command
* and the changes that are applied by the command.
*
* For details, read the Quick Start Guide:
* https://www.jetbrains.com/help/youtrack/incloud/2020.6/Quick-Start-Guide-Workflows-JS.html
*/
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.action({
 title: workflow.i18n('Clone (w/Subtasks)'),
 command: 'clone-with-subtasks',
 guard: function(ctx) {
   return ctx.issue.isReported;
 },
 action: function(ctx) {
   var issue = ctx.issue;
   
   var newIssue = issue.copy();
   newIssue.summary; //+= ' (Clone)';
   
   var copySubtask = function(newParent, subtask) {
     var newSubtask = subtask.copy();
     newParent.links['parent for'].add(newSubtask);
     // Recurse
     subtask.links['parent for'].forEach(function (subtask) { return copySubtask(newSubtask, subtask); });
   };
   
   // Copy subtasks
   issue.links['parent for'].forEach(function (subtask) { return copySubtask(newIssue, subtask); });
   
   workflow.message(workflow.i18n('A clone of the current issue has been created: <a href="{0}">{1}</a>', newIssue.url, newIssue.id));
 },
 requirements: {
   // TODO: add requirements
 }
});
0

Hello Marco Tarantino 

In your code, this function goes through all subtasks of the original issue and copies them:

   var copySubtask = function(newParent, subtask) {
     var newSubtask = subtask.copy();
     newParent.links['parent for'].add(newSubtask);
     // Recurse
     subtask.links['parent for'].forEach(function (subtask) { return copySubtask(newSubtask, subtask); });
   };

So you need to change the field value in the subtasks, you need to add this line after newParent.links… :

newSubtask.fields.Category = “Ticket”;

If you need to change the field value in the parent issue, then here is where you copy an original issue:

   var newIssue = issue.copy();
   newIssue.summary; //+= ' (Clone)';

So you need to add this line after that code block:

newIssue.fields.Category = “Ticket”;
0

Hello Alisa Kasyanova, I follow your suggestions and all works fine!

Thank you very much!

1

Hi, I have one more questions: how can I modify the code that currently works to also change the project when I clone a task with its subtasks?

My goal is to switch the project from the current one, where I create all my template tasks, to another project, which is the actual operational project.

Here is my current code:


/**
 * This is a template for an action rule. This rule defines a custom command
 * and the changes that are applied by the command.
 *
 * For details, read the Quick Start Guide:
 * https://www.jetbrains.com/help/youtrack/incloud/2020.6/Quick-Start-Guide-Workflows-JS.html
 */

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.action({
  title: workflow.i18n('Clone (w/Subtasks)'),
  command: 'clone-with-subtasks',
  guard: function(ctx) {
    return ctx.issue.isReported;
  },
  action: function(ctx) {
    var issue = ctx.issue;
    
    var newIssue = issue.copy();
    newIssue.summary; //+= ' (Clone)';
    newIssue.fields.Category = "Ticket";
    
    var copySubtask = function(newParent, subtask) {
      var newSubtask = subtask.copy();
      newParent.links['parent for'].add(newSubtask);
      newSubtask.fields.Category = "Ticket";

      // Recurse
      subtask.links['parent for'].forEach(function (subtask) { return copySubtask(newSubtask, subtask); });
    };
    
    // Copy subtasks
    issue.links['parent for'].forEach(function (subtask) { return copySubtask(newIssue, subtask); });

    
    workflow.message(workflow.i18n('A clone of the current issue has been created: <a href="{0}">{1}</a>', newIssue.url, newIssue.id));
  },
  requirements: {
    // TODO: add requirements
  }
});
0

Marco Tarantino Add these lines to the code parts where you change the field values:

newIssue.fields.Category = "Ticket";
newIssue.project = entities.Project.findByKey('PRID');

and

newSubtask.fields.Category = "Ticket";
newSubtask.project = entities.Project.findByKey('PRID');

Where PRID - is the needed project's ID.

1

Alisa Kasyanova this one works perfectly too.

Thank you very much indeed

0

Alisa Kasyanova, I need your help again, and I hope you can assist.

I can now clone issues with parent and subtasks, change the project and category—everything works perfectly on that front. However, I've noticed that the Estimate field, which is private, isn't carried over in the cloned tasks. The new task is created perfectly but without the Estimate field value.

Here is a screenshot of the original task

and this is the one for the cloned task

What's strange is that if I use the clone function on a single issue (without parent or subtasks), the Estimate field value copies over correctly.

Any idea why this might be happening?

0

Marco Tarantino  It is because the default clone action and your workflow use different methods to copy the task. You can add an additional code to copy the value in this field, like that: newIssue.fields.Estimate = issue.fields.Estimate;

0

Alisa Kasyanova I added your instruction but not work: the parent don't have yet the estimate value.

This is my code now:

/**
 * This is a template for an action rule. This rule defines a custom command
 * and the changes that are applied by the command.
 *
 * For details, read the Quick Start Guide:
 * https://www.jetbrains.com/help/youtrack/incloud/2020.6/Quick-Start-Guide-Workflows-JS.html
 */

var entities = require('@jetbrains/youtrack-scripting-api/entities');
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');

exports.rule = entities.Issue.action({
  title: workflow.i18n('Create Ordinary Task (w/Subtasks)'),
  command: 'clone-with-subtasks-change-project',
  guard: function(ctx) {
    return ctx.issue.isReported;
  },
  action: function(ctx) {
    var issue = ctx.issue;
    
    var newIssue = issue.copy();
    newIssue.summary; //+= ' (Clone)';
    newIssue.fields.Category = "Ticket";
    newIssue.project = entities.Project.findByKey('ITG_GAS_AMS');
    newIssue.fields.Estimate = issue.fields.Estimate;
    
    var copySubtask = function(newParent, subtask) {
      var newSubtask = subtask.copy();
      newParent.links['parent for'].add(newSubtask);
      newSubtask.fields.Category = "Ticket";
      newSubtask.project = entities.Project.findByKey('ITG_GAS_AMS');
            
      // Recurse
      subtask.links['parent for'].forEach(function (subtask) { return copySubtask(newSubtask, subtask); });
    };
    
    // Copy subtasks
    issue.links['parent for'].forEach(function (subtask) { return copySubtask(newIssue, subtask); });

    
    workflow.message(workflow.i18n('A clone of the current issue has been created: <a href="{0}">{1}</a>', newIssue.url, newIssue.id));
  },
  requirements: {
    // TODO: add requirements
  }
});
0

Hi Marco Tarantino!

By default, YouTrack sets the estimate of a newly created parent issue to the sum of its subtasks' estimates. Since the estimates are not set for the subtasks of ITG_GAS_TEMP-31, the estimate for ITG_GAS_AMS-76 is missing as well.

You can work around that by, say, tagging cloned parents to then update their estimates within an on-schedule rule. However, it seems that providing estimates for subtasks might be a more straightforward approach.

0

Hi Julia Bernikova! thank you for your explanation.

By your suggestion I set in the template issue the estimate value for all subtask and now the parent show the sum of its subtasks' estimates. Effectively when I clone the parent now I don't have any problems.

If I may, I would make another request.

By viewing my current workflow code, which I report below, I'm using Alisa Kasyanova suggestion to clone the Category fields:

newIssue.fields.Category = "Ticket";

and this for subtask 

newSubtask.fields.Category = "Ticket"

During the use of the workflows, I noticed that the issue has some problems with the Category field. The cloned Category field is set with a wrong value because when I open the cloned task, I see 2 values, as shown in the following image:

Another problem is that the cloned task does not have the Ticket Type field, which was present in the original task Template

In the cloned task, with the errorm, this field appears only if I select the second value “Ticket” for the Category field.

Below:

  • The field configuration for the template issue in the “Template - AMPB ITG Gas AMS” project (where I start cloning): 
  • The field configuration for the cloned issue in the “AMPB ITG Gas - AM” project (where the cloned task ends up):

I tried these instruction in the code to also clone Ticket Type field from the issue template to the cloned Issue, but it doesn't work:

newIssue.fields.TicketType = "Ordinary Task";
newSubtask.fields.TicketType = "Ordinary Task";

 

0

Hi Marco Tarantino,

It seems that the “Ticket” field behavior is caused by the script setting the fields for an issue copy before moving it to a different project. Instead, I'd suggest creating the issue copy in the needed project right away by passing the project as the copy method parameter like this:

var newIssue = issue.copy(entities.Project.findByKey('ITG_GAS_AMS'));

You can then remove the line that sets the newIssue.project property.

To set the “Ticket Type” field, access it by the exact name via newIssue.fields["Ticket Type"]:

newIssue.fields["Ticket Type"] = "Ordinary Task";

Please apply both changes to creating a subtask as well.

 

0

Hi Julia Bernikova, thank you so much for your suggestion. Everything works perfectly now!

0

Thank you for the update, I'm very glad to hear it works now! 😊

0

Please sign in to leave a comment.