跟踪检查用户是否具有权限



我正在尝试创建一个 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: {}
}
}
});

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

要实现此任务,我建议您使用 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拉顺的答案,他的答案也有效。 将电子邮件部分替换为:

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

最新更新