How to inherit tags in YouTrack from task to subtask

osipovsimon

Simon Osipov

Posted on December 29, 2019

How to inherit tags in YouTrack from task to subtask

If you are using JetBrains YouTrack as your Task tracker, you are probably already familiar with Workflows. This is an event-based code snippet that allows you to perform some routine tasks automatically or on schedule. These workflows are written in JavaScript, so most of the team could easily create routines they need. Because in my team both frontend-developers were very busy with the refactoring of some core module, I decided to write a script on my own.

So, imagine, you have tags in your project and every time you add or remove a tag from a card, you would like to populate this change top-to-bottom and bottoms-up: from task to subtask and from subtask to parent. So, how would you inherit a tag in Youtrack? Here is a code snippet:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
  title: 'Inherit tags',
  guard: function(ctx) {
    return ctx.issue.tags.added.isNotEmpty() || ctx.issue.tags.removed.isNotEmpty();
  },
  action: function(ctx) {
    var issue = ctx.issue;
    issue.links['parent for'].forEach(function(childIssue) {
      issue.tags.added.forEach(function(tag) {
        childIssue.addTag(tag.name);
      });
      issue.tags.removed.forEach(function(tag) {
        childIssue.removeTag(tag.name);
      });
    });
  },
  requirements: {

  }
});
Enter fullscreen mode Exit fullscreen mode

If you would like to copy tag from subtask to parent, here it is:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
  title: 'Populate tags up',
  guard: function(ctx) {
    return ctx.issue.tags.added.isNotEmpty() || ctx.issue.tags.removed.isNotEmpty();
  },
  action: function(ctx) {
    var issue = ctx.issue;
    issue.links["subtask of"].forEach(function(parentIssue) {
      issue.tags.added.forEach(function(tag) {
        parentIssue.addTag(tag.name);
      });
      issue.tags.removed.forEach(function(tag) {
        parentIssue.removeTag(tag.name);
      });
    });
  },
  requirements: {

  }
});
Enter fullscreen mode Exit fullscreen mode

I hope it helps!
Simon Osipov
Web
Twitter
FB
GitHub
LinkedIn
Telegram
Data Engineering TG channel RUS

💖 💪 🙅 🚩
osipovsimon
Simon Osipov

Posted on December 29, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related