Search code examples
youtrack

Youtrack Workflow to set issue due date based on issue start date + estimate


I am trying to set up a workflow on youtrack where it sets automatically the end date based on the start date + estimates.

For example, my issue start date is 2022/10/01 and it has an estimate of 10d (10 days, for example). I want that the end date to be set of 2022/10/10.

I couldn't figure out how to set this rule as I couldn't user the workflow constructor for it.

Thanks


Solution

  • Here is an example of a similar workflow that automatically adds the Planned time value to Start date field and writes the result in the Due date field:

    const entities = require('@jetbrains/youtrack-scripting-api/entities');
    
    exports.rule = entities.Issue.onChange({
      title: 'End date',
      guard: (ctx) => {
        return (ctx.issue.fields.isChanged(ctx.Plan) || ctx.issue.fields.isChanged(ctx.StartDate)) && ctx.issue.fields.Plan != null && ctx.issue.fields.StartDate != null;
      },
      action: (ctx) => {
        const issue = ctx.issue;
        var periodestimate = issue.fields.Plan;
        var minutesestimate = !periodestimate ? 0 : (periodestimate.getMinutes() + 60 * (periodestimate.getHours() + 24 * (periodestimate.getDays() + 7 * periodestimate.getWeeks())));
        ctx.issue.fields.EndDate = issue.fields.StartDate + (minutesestimate * 60000);
      },
      requirements: {
          Plan: {
          name: "Planned time",
          type: entities.Field.periodType
        },
        EndDate: {
          name: "Due Date",
          type: entities.Field.dateType
        },
        StartDate: {
        name: "Start Date",
        type: entities.Field.dateType
        }
      }
    });