Youtrack 检查用户是否有权限

Youtrack check if user has permissions

我正在尝试创建一个 Youtrack 工作流,其中当当前问题处于积压状态时,只允许特定角色将看板状态编辑为准备拉动。我不太能够让它正常工作,不断抛出异常,但我无法阅读完整的异常。

我尝试创建当前工作流代码:

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

其中大部分是来自看板更改工作流的副本,当看板状态未设置为 "Ready-to-Pull" 时,它会阻止将问题移动到新阶段。我基本上想要完全相同,但我只想允许项目管理员在当前阶段为 "Backlog" 时将看板状态更改为 "ready-to-pull"。目前的代码目前只检查权限,但我已经开始卡在那里了。

要实现这个任务,我建议你使用workflow.check方法,例如:

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

希望对您有所帮助。

鉴于我们目前的情况,我们只需要禁用一个人就无法在设置新工单时更改看板状态,我们有以下解决方案:

exports.rule = entities.Issue.onChange({
    title: workflow.i18n('Block change in Kanban stage for issues in backlog stage'),
    guard: function(ctx) {
    var issue = ctx.issue;
    return issue.fields.isChanged(ctx.KanbanState);//Runs when Kanban state changes
  },
  action: function(ctx) {
    var issue = ctx.issue;
    //Check if user has changed the kanban state to ready to pull while the current stage is backlog.

    if (issue.fields.Stage.name == 'Backlog') {
      //Current stage is backlog
      if (issue.fields.KanbanState.name === ctx.KanbanState.ReadyToPull.name) {
        //Kanban state was changed to ready to pull;

        var target = '<useremail>';

        workflow.check(ctx.currentUser.email == target,
          workflow.i18n('No permissions to change the Kanban state.'));
        issue.fields.KanbanState = ctx.KanbanState.Blocked;
      }
    }
  },
  requirements: {
    Stage: {
      type: entities.State.fieldType,
      Backlog: {},
      Development: {}
    },
    KanbanState: {
      name: 'Kanban State',
      type: entities.EnumField.fieldType,
      ReadyToPull: {
        name: 'Ready to pull'
      },
      Blocked: {}
    }
  }
});

但是我检查了@Oleg Larshun 的答案,他的答案也有效。将电子邮件部分替换为:

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