javascriptyoutrack

How to disable modifying workitems in YouTrack workflows?


I want to prevent users in YouTrack from modifying or adding workitems in the past. They should only add/modify workitems in current day.

In YouTrack workflows i can detect changed Spent time event and prevent users from adding workitem. But I want to get an event when user is modifying workitem in JavaScript workflows. Here is my 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('Disable editing workitems'),

  guard: function(ctx) {
    return ctx.issue.fields.isChanged(ctx.ST);
  },

  action: function(ctx) {
    workflow.check(ctx.issue.workItems.added.isEmpty(), workflow.i18n('You can add/modify workitems only in current day.'));
  },

  requirements: {
    ST: {
      type: entities.Field.periodType,
      name: 'Spent Time'
    }
  }
});

Datetime conditions are omitted...


Solution

  • Not sure if its still relevant, but I have full example:

    /**
     * This is a template for an on-change rule. This rule defines what
     * happens when a change is applied to an issue.
     *
     * For details, read the Quick Start Guide:
     * https://www.jetbrains.com/help/youtrack/server/2022.2/Quick-Start-Guide-Workflows-JS.html
     */
    
    const entities = require('@jetbrains/youtrack-scripting-api/entities');
    const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
    
    exports.rule = entities.Issue.onChange({
      title: 'Prevent-workitem-updates-past',
      guard: (ctx) => {
        const issue = ctx.issue;
        const fs = issue.fields;
        
        // NOT using issue.workItems.added.isEmpty() , because we also want to detect EDITS as well as ADDS to the WorkItems array.
        return fs.isChanged(ctx.ST) && !issue.becomesReported;
      },
      action: (ctx) => {
        const issue = ctx.issue;
       
        function checkWorkItemDate(currentWorkItem, currentEpochTimeStamp){
            workflow.check(currentWorkItem.date == currentEpochTimeStamp, workflow.i18n('You can add/modify workitems only in current day.'));
        }
    
        //const splittedDate = new Date().toISOString().slice(0, 10).split('-');
        //const currentEpoch = new Date(splittedDate[0], splittedDate[1] - 1, splittedDate[2]).valueOf();
    
        const currentDate = new Date();
        const currentEpoch = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate()).valueOf();
         
        // Check newly added elements
        issue.workItems.added.forEach(workItem => {
            checkWorkItemDate(workItem, currentEpoch);
        });
        
        // Check edited elements
        issue.editedWorkItems.forEach(workItem => {
            checkWorkItemDate(workItem, currentEpoch);
        });
        
        workflow.check(true);
      },
      requirements: {
        ST: {
          type: entities.Field.periodType,
          name: 'Spent Time'
        }
      }
    });