Parameterized workflow

I have the code below used to notify the assignee/project lead daily at 8am if a ticket has not been updated in X amount of time. There are 4 configurable parameters. How do I reuse this code so I can apply it to different project, or even different states in the same project with different REMINDER times depending on the state? Obviously I don't want to copy-paste the code for each usage, I would like to parameterize it so that I can just call it multiple times with different parameter values each time.

 

/**
* This will send a daily reminder at 8am for any ticket that has not been updated for 7 days
* Configurable parameters
*
* exports.REMINDER_STATES
* comma-separated list of states we want to send reminders for, e.g. '{In Progress} , {To Verify}'
*
* exports.REMINDER_UNIT
* the units the reminder is measured in, e.g. exports.WEEK, exports.DAY, exports.HOUR, export.MINUTE
*
* exports.REMINDER_UNIT_NAME
* String name representing the units, e.g. 'weeks', 'days', 'hours', 'minutes'
* (developer note: REMINDER_UNIT and REMINDER_UNIT_NAME should be coupled, and we should only have to input one of these fields. I'm not sure what is the best-practice way to do this in the youtrack environment.)
*
* exports.REMINDER_UNIT_COUNT
* the unit multiplier, e.g. 7
* for 7 weeks/days/hours/minutes (depending on the value of REMINDER_UNIT)
*/

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

exports.MINUTE = 60 * 1000;
exports.HOUR = 60 * exports.MINUTE;
exports.DAY = 24 * exports.HOUR;
exports.WEEK = 7 * exports.DAY;

/* START UPDATE VALUES HERE */
exports.REMINDER_STATES = '{In Progress} , {To Verify} '; // what state(s) are we setting the reminder for?
exports.REMINDER_UNIT = exports.DAY; // is the reminder measured in weeks/minutes/hours/days?
exports.REMINDER_UNIT_NAME = 'days'; // name of the reminder unit chosen above
exports.REMINDER_UNIT_COUNT = 7; // how many weeks/minutes/hours/days?
/* END UPDATE VALUES HERE */

exports.REMINDER_TIME = exports.REMINDER_UNIT_COUNT * exports.REMINDER_UNIT;
exports.REMINDER_MESSAGE = ' stuck for ' + exports.REMINDER_UNIT_COUNT + ' ' +
exports.REMINDER_UNIT_NAME;

exports.rule = entities.Issue.onSchedule({
title: 'No movement on ticket',
cron: '0 0 8 * * ?',
search: 'State: ' + exports.REMINDER_STATES,
guard: (ctx) => {
return ctx.issue.updated + exports.REMINDER_TIME < Date.now();
},
action: (ctx) => {
const issue = ctx.issue;
let userToNotify = issue.fields.Assignee;
if (!userToNotify) {
userToNotify = issue.project.leader;
}

const notificationText = workflow.i18n('No update on issue: ') + ' <a href="' + ctx.issue.url + '">' + ctx.issue.summary + '</a> for more than ' + exports.REMINDER_UNIT_COUNT + ' ' + exports.REMINDER_UNIT_NAME;
console.log('Message for user: ' + notificationText);
userToNotify.notify(workflow.i18n('[YouTrack, No update on issue]'), notificationText);
  },
requirements: {
}
});
1 comment
Comment actions Permalink

Hello,

Please excuse me for the delayed reply.

I can suggest specifying `return true;` in the `guard` section of the workflow and using different conditions in the `action` section afterwards (for example, `if (ctx.issue.fields.State == ctx.issue.fields.State.Closed.name && ...)`). In this case, the workflow will be triggered according to its cron, but the action for each issue will differ depending on your conditions.

0

Please sign in to leave a comment.