youtrackyoutrack-api

Youtrack check if user has permissions


I'm trying to create a Youtrack workflow where only a specific role is allowed to edit the Kanban state to ready-to-pull when the current issue is in backlog. I wasn't quite able to get it correctly working, keeps throwing exceptions but I'm unable to read the full exception.

I tried to create the current workflow code:

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

exports.rule = entities.Issue.onChange({
    title: workflow.i18n('Block change in Kanban stage for issues that are in backlog'),
    guard: function(ctx) {
    return ctx.issue.isReported && ctx.issue.fields.isChanged(ctx.KanbanState);
  },
  action: function(ctx) {
    var issue = ctx.issue;

    if (!ctx.user.hasRole('project-admin', ctx.project)) {
        workflow.message('U dont have the correct permissions to do this'); 
        ctx.KanbanState = ctx.KanbanState.Blocked;
    }
  },
  requirements: {
    Stage: {
      type: entities.State.fieldType
    },
    KanbanState: {
      name: 'Kanban State',
      type: entities.EnumField.fieldType,
      ReadyToPull: {
        name: 'Ready to pull'
      },
      Blocked: {}
    }
  }
});

Most of this is a copy from the Kanban change workflow that blocks moving the issue to a new stage when the kanban state isn't set to "Ready-to-Pull". I basically want the exact same, but I want to only allow project admins to change the kanban state to "ready-to-pull" when the current stage is "Backlog". The current code only checks the permissions at the moment, but I started getting stuck there already.


Solution

  • To implement this task, I suggest you use the workflow.check method, for example:

        workflow.check(ctx.user.hasRole('project-admin', ctx.project), 'U dont have the correct permissions to do this'); 
    

    I hope this helps.