Search code examples
javascriptworkflowyoutrack

How to add group or role based restriction to some action in YouTrack workflow?


I have created a workflow to restrict the changes in the estimation once it has been added. However, I want to give this permission to the Admins but restrict all other groups such as Developers.

I have tried isInGroup function but it's not working correctly and I always get an exception.

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

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

exports.rule = entities.Issue.onChange({

  title: 'Protect_estimations',

  guard: function(ctx) {

  if(!ctx.current.isInGroup('Admin')) {
      return (ctx.issue.fields.oldValue(ctx.Estimation) !== null && ctx.issue.fields.isChanged(ctx.Estimation) === true); }
      return false;     },

  action: function(ctx) {
     workflow.check(false,'Sorry, you cannot update estimates');   },

  requirements: {
    Estimation: {
      type: entities.Field.periodType,
      name: 'Estimation'
    }   } });

The condition !ctx.current.isInGroup('Admin') does not work whereas rest of the code is running perfect. Do I need to add any rule in the 'requirements' section in order to get this done? Please suggest.


Solution

  • To resolve the issue, I suggest you use the currentUser property instead:

    if(!ctx.currentUser.isInGroup('Admin'))...
    

    I hope this helps.