Developer Portal for YouTrack and Hub Help

Slack Notifications

This workflow posts a notification to Slack when an issue is created, resolved, or reopened.

Name

@jetbrains/youtrack-workflow-slack-notifications-simple

Auto-attached

no

Modules

Send notification to slack when an issue is reported, resolved, or reopened (on-change rule)

Use Case

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.

Slack notifications setup

Copy this URL and use this as the value for the const 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 issue 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 const 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/ const SLACK_WEBHOOK_URL = 'REPLACE_THIS_WITH_YOUR_WEBHOOK_URL'; const entities = require('@jetbrains/youtrack-scripting-api/entities'); const http = require('@jetbrains/youtrack-scripting-api/http'); exports.rule = entities.Issue.onChange({ title: 'Send notification to slack when an issue is reported, resolved, or reopened', guard: (ctx) => { return ctx.issue.becomesReported || ctx.issue.becomesResolved || ctx.issue.becomesUnresolved; }, action: (ctx) => { const issue = ctx.issue; const issueLink = '<' + issue.url + '|' + issue.id + '>'; let message; let 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; let changedByTitle = ''; let changedByName = ''; if (isNew) { changedByTitle = 'Created By'; changedByName = issue.reporter.fullName; } else { changedByTitle = 'Updated By'; changedByName = issue.updatedBy.fullName; } const 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 } ] }] }; const connection = new http.Connection(SLACK_WEBHOOK_URL, null, 2000); const 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 } } });
Last modified: 22 October 2024