This workflow helps you automate the creation of links between related issues.
This workflow references an issue link type with the name refers to. This is a custom issue link type that is not available in YouTrack by default. To use this workflow, you need to add this issue link type to your system. When you create this issue link type, enter the following values:
Field
Description
Name
Reference (or any other name, this does not affect the workflow)
This on-change rule in this module checks the description and comment for an issue for references to other issues. When one or more references are found, the issues are added as refers to links.
Add "refers to" link to an issue when the issue ID is added to a description or comment
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: 'Add "refers to" link to an issue when the issue ID is added to a description or comment',
guard: (ctx) => {
return !ctx.issue.comments.added.isEmpty() || ctx.issue.isChanged('description');
},
action: (ctx) => {
const issue = ctx.issue;
const startGroup = /(^|[',;.:"\\()?!<>#+|/\[\]\t\n\r ])/;
const endGroup = /([',;.:"\\()?!<>#+|/\[\]\t\n\r ]|$)/;
const issueIdGroup = '(' + issue.project.key.toLowerCase() + '-\\d+)';
const regexp = new RegExp(startGroup.source + issueIdGroup + endGroup.source, 'i');
let text = issue.comments.added.isEmpty() ? issue.description : issue.comments.added.first().text;
let match = regexp.exec(text);
const allMentionedIssues = {};
while (match) {
const matchedIssueId = match[2].trim();
const referringIssue = entities.Issue.findById(matchedIssueId);
if (referringIssue !== null) {
issue.links[ctx.RefersTo.outward].add(referringIssue);
allMentionedIssues[matchedIssueId] = true;
}
text = text.substring(match.index + match[0].length);
match = regexp.exec(text);
}
const allAddedIssues = Object.keys(allMentionedIssues);
if (allAddedIssues.length) {
workflow.message(workflow.i18n('Automatically added \'refers to\' {0} links.', allAddedIssues.join()));
}
},
requirements: {
RefersTo: {
type: entities.IssueLinkPrototype,
name: 'Refers',
outward: 'refers to'
}
}
});