Forgotten Attachment
This workflow reminds users to add an attachment to an issue if an attachment is mentioned in a description or comment.
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.
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var utils = require('./attach-file-utils');
exports.rule = entities.Issue.onChange({
title: 'Check description for reference to attachment',
guard: function(ctx) {
return ctx.issue.becomesReported || ctx.issue.isChanged('description'); // optimize blob (description) read
},
action: function(ctx) {
var issue = ctx.issue;
var description = issue.description;
if (!description) {
return;
}
if (issue.becomesReported) {
utils.findAndShowMessage(description);
return;
}
if (issue.isChanged('description')) {
var oldDescription = issue.oldValue('description') || '';
var 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);
}
}
}
});
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.
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var utils = require('./attach-file-utils');
exports.rule = entities.Issue.onChange({
title: 'Check comment for reference to attachment',
guard: function(ctx) {
return !ctx.issue.comments.added.isEmpty();
},
action: function(ctx) {
ctx.issue.comments.added.forEach(function(comment) {
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.
var workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.words = 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) {
var found = '';
if (exports.words.some(function(word) {
if (text.indexOf(word) > -1) {
found = word;
return true;
}
return false;
})) {
exports.showMessage(found);
}
};
Last modified: 16 March 2020