YouTrack Standalone 2021.1 Help

Forgotten Attachment

This workflow reminds users to add an attachment to an issue if an attachment is mentioned in a description or comment.

Name@jetbrains/youtrack-workflow-forgotten-attachment
Auto-attachedno
ModulesCheck description for reference to attachment (on-change rule)
Check comment for reference to attachment (on-change rule)
attach-file-utils.js (custom script)

Use Case

This workflow helps to make sure that users remember to attach a file to an issue when they reference an attachment in a description or comment.

Modules

This workflow contains two modules that can be attached as rules to a project and a third module that contains a custom script. This script contains common functions that are used in both rules.

Check description for reference to attachment

The first module contains a rule that scans the description for references to an attachment. If a reference is found, the user is reminded to attach a file to the issue.

const entities = require('@jetbrains/youtrack-scripting-api/entities'); const utils = require('./attach-file-utils'); exports.rule = entities.Issue.onChange({ title: 'Check description for reference to attachment', guard: (ctx) => { return ctx.issue.becomesReported || ctx.issue.isChanged('description'); // optimize blob (description) read }, action: (ctx) => { const issue = ctx.issue; const description = issue.description; if (!description) { return; } if (issue.becomesReported && ctx.issue.attachments.isEmpty()) { utils.findAndShowMessage(description); return; } if (issue.isChanged('description')) { const oldDescription = issue.oldValue('description') || ''; let found = ''; if (utils.words().some(function(word) { if ((description.indexOf(word) > -1) && (oldDescription.indexOf(word) === -1)) { found = word; return true; } return false; })) { utils.showMessage(found); } } } });

Check comment for reference to attachment

The second module contains a rule that scans new comments for references to an attachment. If a reference is found, the user is reminded to attach a file to the issue.

const entities = require('@jetbrains/youtrack-scripting-api/entities'); const utils = require('./attach-file-utils'); exports.rule = entities.Issue.onChange({ title: 'Check comment for reference to attachment', guard: (ctx) => { return !ctx.issue.comments.added.isEmpty(); }, action: (ctx) => { ctx.issue.comments.added.forEach(function (comment) { if (comment.attachments.isEmpty()) { utils.findAndShowMessage(comment.text); } }); } });

attach-file-utils.js

The last module contains the code that both rules use to determine whether a description or comment contains a reference to attachment. This script also contains the code that displays the message to the user.

const workflow = require('@jetbrains/youtrack-scripting-api/workflow'); exports.words = function() { return workflow.i18n('attachments, attachment, attached, attaches, attach, attaching').split(', '); }; exports.showMessage = function(what) { workflow.message(workflow.i18n('You have mentioned the "{0}" word, don\'t forget to attach it (them).', what)); }; exports.findAndShowMessage = function(text) { let found = ''; if (exports.words().some(function(word) { if (text.indexOf(word) > -1) { found = word; return true; } return false; })) { exports.showMessage(found); } };
Last modified: 08 March 2021