Skip to content

Instantly share code, notes, and snippets.

@mrchnk
Last active June 12, 2023 12:45
Show Gist options
  • Save mrchnk/e8d6e95d71ed56dba35e85d5138bba11 to your computer and use it in GitHub Desktop.
Save mrchnk/e8d6e95d71ed56dba35e85d5138bba11 to your computer and use it in GitHub Desktop.
Example youtrack workflow. It notifies some user about new mentions in comments.
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var http = require('@jetbrains/youtrack-scripting-api/http');
var slackIdByUsername = {}
slackIdByUsername['@person1'] = '{person1_slack_id}';
slackIdByUsername['@person2'] = '{person2_slack_id}';
slackIdByUsername['@person3'] = '{person3_slack_id}';
var webhookUrl = 'https://{slack_webhook_url}';
var webhookTimeout = 2000;
exports.rule = entities.Issue.onChange({
title: 'Send notification to slack when one of users is mentioned in comment',
guard: function (ctx) {
return ctx.issue.comments.added.isNotEmpty()
},
action: function (ctx) {
ctx.issue.comments.added.forEach(function (comment) {
var text = comment.text;
var mentioned = [];
for (var username in slackIdByUsername) {
if (text.indexOf(username) !== -1) {
mentioned.push(username)
}
}
if (mentioned.length === 0) {
return;
}
var mentionedForSlack = mentioned.map(function (username) {
return '<@' + slackIdByUsername[username] + '>';
});
var issue = ctx.issue;
var pretext = "New mention on issue <" + issue.url + "|" + issue.id + ">";
var slackText = "Hey, " + mentionedForSlack.join(", ") + "!\nThere is a new mention in comment:\n" + text;
var payload = {
"attachments": [{
"fallback": "New mention on issue " + issue.id,
"pretext": pretext,
"text": slackText,
"color": issue.fields.Priority.backgroundColor
}]
};
var webhook = new http.Connection(webhookUrl, null, webhookTimeout);
var response = webhook.postSync('', null, JSON.stringify(payload));
if (!response.isSuccess) {
console.warn('Failed to post notification to Slack. Details: ' + response.toString());
} else {
console.log("Comment(" + comment.url + ") notified to slack by WebHook(" + webhookUrl + ")");
}
});
},
requirements: {}
});
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var http = require('@jetbrains/youtrack-scripting-api/http');
var username = '@person';
var slackId = '{person_slack_id}';
var webhookUrl = 'https://{slack_webhook_url}';
var webhookTimeout = 2000;
exports.rule = entities.Issue.onChange({
title: 'Send notification to slack when ' + username + ' is mentioned in comment',
guard: function (ctx) {
return ctx.issue.comments.added.isNotEmpty()
},
action: function (ctx) {
ctx.issue.comments.added.forEach(function (comment) {
var text = comment.text;
if (text.indexOf(username) === -1) {
return;
}
var issue = ctx.issue;
var pretext = "New mention on issue <" + issue.url + "|" + issue.id + ">";
var slackText = "Hey, <@" + slackId + ">!\nThere is a new mention in comment:\n" + text;
var payload = {
"attachments": [{
"fallback": "New mention on issue " + issue.id,
"pretext": pretext,
"text": slackText,
"color": issue.fields.Priority.backgroundColor
}]
};
var webhook = new http.Connection(webhookUrl, null, webhookTimeout);
var response = webhook.postSync('', null, JSON.stringify(payload));
if (!response.isSuccess) {
console.warn('Failed to post notification to Slack. Details: ' + response.toString());
} else {
console.log("Comment(" + comment.url + ") notified to slack by WebHook(" + webhookUrl + ")");
}
});
},
requirements: {}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment