1

I created an "Agile-Board" in youtrack and I want every ticket that is moved to the column (which is mapped to the field Status) "In Produktivsetzung" to be automatically assigned to my user.

Like this:

  1. Agile-Board Lanes
  2. enter image description here

How can this be done?

Cold_Class
  • 3,214
  • 4
  • 39
  • 82

3 Answers3

2

One can set it up with a custom workflow script as follows

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

exports.rule = entities.Issue.onChange({
  title: 'Set logged-in user as an assignee when they move it to In Produktivsetzung state',
  guard: function(ctx) {
    var issue = ctx.issue;
    return issue.isReported &&
        issue.fields.Assignee === null &&
        issue.fields.becomes(ctx.State, ctx.State.InProgress) &&
        !issue.fields.isChanged("project");
  },
  action: function(ctx) {
    var isCurrentUserAssignee = false;
    ctx.Assignee.values.forEach(function(it) {
      if (it.login == ctx.currentUser.login) {
        isCurrentUserAssignee = true;
      }
    });
    if (isCurrentUserAssignee) {
      ctx.issue.Assignee = ctx.currentUser;
    }
  },
  requirements: {
    Assignee: {
      type: entities.User.fieldType
    },
    State: {
      type: entities.State.fieldType,
      InProgress: {
        name: 'In Produktivsetzung'
      }
    }
  }
});
Jk1
  • 11,233
  • 9
  • 54
  • 64
1

I want to set assignee on every state change. After a couple hours trial & error (the documentation is really not that good) I had success:

var entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
  title: 'Assign issue to current user when state changes',
  guard: function(ctx) {
    return ctx.issue.fields.isChanged(ctx.State);
  },
  action: (ctx) => {
    ctx.issue.fields.Assignee = ctx.currentUser;
  },
  requirements: {
    Assignee: {
      type: entities.User.fieldType
    },
    State: {
      type: entities.State.fieldType
    }
  }
});

I don't really understand why I have to use a "guard function" - I could just use a conditional statement in the action and the whole "requirements" section doesn't make any sense to me but if it is necessary... I don't care. Finally works as expected... I hope that it works some years longer than the "legacy scripts" - I don't want to touch it again.

The incredible Jan
  • 741
  • 10
  • 17
  • This looks good - so the 'action' method will be called every time the State is changed and then I could do a `switch(ctx.State)` and have it assigned to different users depending on the sate I guess (?) - that sounds like a smoother solution, than what I have currently - thanks! :) – Cold_Class Feb 02 '22 at 11:12
  • We have this script running in production environment since yesterday and it did exactly that: every state change switches user all other changes of an issue don't. I don't know if switch works but maybe you have to add every (non default) case to the requirements. – The incredible Jan Feb 03 '22 at 09:11
0

Based on the answer this is what I'm using now, I created multiple modules where I just had to change the two variables at the top of my code:

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

var assigneeLogin = '<some.login>';
var stateName = '<Some Statename, see possible values in console.log(ctx.State)>';

exports.rule = entities.Issue.onChange({
  title: 'Set ' + assigneeLogin + ' as the assignee when ticket is moved to "'+ stateName + '"',
  guard: function(ctx) {
    var issue = ctx.issue;
    return issue.fields.becomes(ctx.State, ctx.State.InProgress);
  },
  action: function(ctx) {
    ctx.Assignee.values.forEach(function(it) {
      if (it.login === assigneeLogin) {
        ctx.issue.Assignee = it;
      }
    });
  },
  requirements: {
    Assignee: {
      type: entities.User.fieldType
    },
    State: {
      type: entities.State.fieldType,
      InProgress: {
        name: stateName
      }
    }
  }
});
Cold_Class
  • 3,214
  • 4
  • 39
  • 82
  • And how does this work with multiple states? And why should you hard code assignee names? Don't you occasionally have personnel changes in your company? In my opinion it can't be correct to use multiple modules. – The incredible Jan Jan 19 '22 at 09:46
  • I copied the code and just changed the `stateName` and `assignee` variables. This is how it works for me with different states. It's not the best solution but one that works well for my personal use case. Feel free to post the "correct" solution in your opinion - you may help others with a similar problem ;) – Cold_Class Jan 19 '22 at 11:22
  • If I had a correct solution I would be here. ;) It really sucks that JetBrains stopped the support of "legacy scripts". We had a perfectly working assignee script for many years and now they forced the upgrade by the Log4j vulnerability and "nothing" works anymore... – The incredible Jan Feb 02 '22 at 07:55