Cold_Class
Cold_Class

Reputation: 3484

Automatically assign assignee when changing status of ticket

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?

Upvotes: 1

Views: 2121

Answers (3)

The incredible Jan
The incredible Jan

Reputation: 906

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. 🙂

Upvotes: 1

Cold_Class
Cold_Class

Reputation: 3484

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
      }
    }
  }
});

Upvotes: 0

Jk1
Jk1

Reputation: 11443

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'
      }
    }
  }
});

Upvotes: 3

Related Questions