Skip to content

Instantly share code, notes, and snippets.

@rushimusmaximus
Last active April 28, 2020 08:23
Show Gist options
  • Save rushimusmaximus/4bf91bd3cc757ffaade81677c4511f97 to your computer and use it in GitHub Desktop.
Save rushimusmaximus/4bf91bd3cc757ffaade81677c4511f97 to your computer and use it in GitHub Desktop.
YouTrack JavaScript Workflow example: Post to Slack on specific issue state changes
var entities = require('v1/entities');
var http = require('v1/http');
exports.rule = entities.Issue.onChange({
title: 'Notify-slack',
action: function(ctx) {
var issue = ctx.issue;
if (issue.becomesReported || issue.becomesResolved || issue.becomesUnresolved) {
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;
//todo palette seems like it should go in a "custom script" to allow reuse.
//And, is there no way to get the color from the API??
var paletteString = "#FFFFFF,#8d5100,#ce6700,#409600,#0070e4,#900052,#0050a1,#2f9890,#8e1600," +
"#dc0083,#7dbd36,#ff7123,#ff7bc3,#fed74a,#b7e281,#d8f7f3,#e6e6e6,#e6f6cf,#ffee9c,#ffc8ea,#e30000," +
"#e0f1fb,#fce5f1,#f7e9c1,#92e1d5,#a6e0fc,#e0c378,#bababa,#25beb2,#42a3df,#878787,#4d4d4d,#246512,#00665e,#553000,#1a1a1a";
var palette = paletteString.split(",");
var color = palette[issue.Priority.colorIndex];
if (!color) {
color = "#edb431";
}
var milestone = '',
priority = '',
state = '',
assignee = '',
changedByTitle = '',
changedByName = '';
if (issue.fields.Milestone) milestone = issue.fields.Milestone.name;
if (issue.Priority) priority = issue.Priority.name;
if (issue.State) state = issue.State.name;
if (issue.Assignee) assignee = issue.Assignee.fullName;
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": color,
"fields": [{
"title": "Milestone",
"value": milestone,
"short": true
},
{
"title": "State",
"value": state,
"short": true
},
{
"title": "Priority",
"value": priority,
"short": true
},
{
"title": "Assignee",
"value": assignee,
"short": true
},
{
"title": changedByTitle,
"value": changedByName,
"short": true
}
]
}]
};
var SLACK_KEY = 'YOUR_SLACK_KEY_HERE';
var connection = new http.Connection('https://hooks.slack.com/services/');
var response = connection.postSync(SLACK_KEY, '', JSON.stringify(payload));
}
},
requirements: {
//
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment