javascriptworkflowyoutrack

Youtrack workflow for setting Start Date of an issue to the Start Date of the Sprint


I tried the following code snipped, but it looks like there are serveral issues with that. The requirements do not work. The guard is not correct, and I also do not get the correct start date of the sprint. I am fairly new to youtrack and maybe I do not fully understand this api. Kindly ask for help.

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

exports.rule = entities.Issue.onChange({
  title: 'Set StartDate to Sprint Start Date',
  guard: (ctx) => {
    return ctx.issue.fields.isChanged(ctx.Sprints);
  },
  action: (ctx) => {
    const issue = ctx.issue;
    if (issue.fields.Sprints.isNotEmpty()) {
      const sprint = issue.fields.Sprints.iterator().next();
      const startDate = sprint.startDate;
      issue.fields.StartDate = startDate;
      workflow.message('Start date set to {0}', startDate);
    }
  },
  requirements: {
    Sprints: {
      type: entities.EnumField.fieldType,
      name: 'Sprints',
      isMultiValue: true
    },
    StartDate: {
      type: entities.Field.dateTimeType,
      name: 'StartDate'
    }
  }
});

Solution

  • Considering it is not possible to listen to Agile boards changes I can suggest the following approach: set guard to listen to sprints property changes and then apply the start value of the sprint to the desired field.

    A couple more points to consider:

    1. sprints is not a field, but a property of Issue
    2. sprints is a Set

    Having these point in mind and the idea that you don't want to change the field's value if it's already set the script might be the following:

    const entities = require('@jetbrains/youtrack-scripting-api/entities');
    const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
    
    exports.rule = entities.Issue.onChange({
      title: 'Set StartDate to Sprint Start Date',
        guard: (ctx) => {
        return ctx.issue.sprints.added.size > 0 && !Boolean(ctx.issue.fields.StartDate);
      },
      action: (ctx) => {
        const issue = ctx.issue;
        const sprintStartTime = issue.sprints.added.first().start;
        issue.fields.StartDate = sprintStartTime;
        workflow.message('Start date set to {0}', sprintStartTime);
      },
      requirements: {
          StartDate: {
          type: entities.Field.dateTimeType,
          name: 'StartDate'
        }
      }
    });