This workflow helps teams who use Slack as their primary communication hub to stay up to date with their issues in YouTrack.
While the workflow is written to post notifications to Slack when an issue is created, resolved, or reopened, you can easily customize this notification scheme to include other events that are important to you.
To use this workflow, you need to create an Incoming Webhooks Integration in Slack. This gives you the ability to post a message from an external source to a specific channel in Slack. For instructions, refer to the Slack App Directory.
When you add an Incoming Webhook, Slack generates a Webhook URL.
Copy this URL and use this as the value for the var SLACK_WEBHOOK_URL statement in the on-change rule. You might also take a minute to read through the setup instructions on this page in Slack, as it explains exactly how this integration works and can help you identify how to customize this workflow should you want to modify the message content and appearance.
Note that Incoming Webhooks are channel-specific. If you want to send notifications to different channels on a per-project basis, you can add separate modules to this workflow for each project team. You can then configure copies of this rule to send notification to the channels that are used by each team.
This workflow requires that the Priority, State, and Assignee custom fields are available in your project.
Modules
When an is created, resolved, or reopened, this on-change rule posts a notification to Slack. The JSON payload that contains the message is defined in the var payload statement.
If a message is not posted successfully, a warning is posted to the workflow console.
Send notification to slack when an issue is reported, resolved, or reopened
// Thanks to Michael Rush for the original version of this rule (https://software-development.dfstudio.com/youtracks-new-javascript-workflows-make-slack-integration-a-breeze-d3275605d565)
// IMPORTANT: Use a valid Incoming Webhook from Slack. To get one, go to https://my.slack.com/services/new/incoming-webhook/
var SLACK_WEBHOOK_URL = 'REPLACE_THIS_WITH_YOUR_WEBHOOK_URL';
var entities = require('@jetbrains/youtrack-scripting-api/entities');
var http = require('@jetbrains/youtrack-scripting-api/http');
exports.rule = entities.Issue.onChange({
title: 'Send notifications to Slack whenever an issue is reported, resolved, or reopened',
guard: function(ctx) {
return ctx.issue.becomesReported || ctx.issue.becomesResolved || ctx.issue.becomesUnresolved;
},
action: function(ctx) {
var issue = ctx.issue;
var issueLink = '<' + issue.url + "|" + issue.id + '>';
var message, isNew;
if (issue.becomesReported) {
message = "Created: ";
isNew = true;
} else if (issue.becomesResolved) {
message = "Resolved: ";
isNew = false;
} else if (issue.becomesUnresolved) {
message = "Reopened: ";
isNew = false;
}
message += issue.summary;
var changedByTitle = '',
changedByName = '';
if (isNew) {
changedByTitle = "Created By";
changedByName = issue.reporter.fullName;
} else {
changedByTitle = "Updated By";
changedByName = issue.updatedBy.fullName;
}
var payload = {
"attachments": [{
"fallback": message + " (" + issueLink + ")",
"pretext": message + " (" + issueLink + ")",
"color": issue.fields.Priority.backgroundColor || "#edb431",
"fields": [{
"title": "State",
"value": issue.fields.State.name,
"short": true
},
{
"title": "Priority",
"value": issue.fields.Priority.name,
"short": true
},
{
"title": "Assignee",
"value": issue.fields.Assignee ? issue.fields.Assignee.fullName : '',
"short": true
},
{
"title": changedByTitle,
"value": changedByName,
"short": true
}
]
}]
};
var connection = new http.Connection(SLACK_WEBHOOK_URL, null, 2000);
var response = connection.postSync('', null, JSON.stringify(payload));
if (!response.isSuccess) {
console.warn('Failed to post notification to Slack. Details: ' + response.toString());
}
},
requirements: {
Priority: {
type: entities.EnumField.fieldType
},
State: {
type: entities.State.fieldType
},
Assignee: {
type: entities.User.fieldType
}
}
});