Skip to content

Instantly share code, notes, and snippets.

@eisisig
Forked from sirkirby/stash-webhook-to-slack.js
Last active September 7, 2015 17:02
Show Gist options
  • Save eisisig/c97b03eb17475fdd44a8 to your computer and use it in GitHub Desktop.
Save eisisig/c97b03eb17475fdd44a8 to your computer and use it in GitHub Desktop.
process a stash webhook and post to slack
var issues;
exports.post = function(request, response) {
var rep = request.body.repository;
var ref = request.body.refChanges;
var ch = request.body.changesets;
// let me specify the destination and noise level via the query string
var channel = "#devops";
if (request.query.channel != undefined && request.query.channel != null)
channel = request.query.channel;
var alert = false;
if (request.query.alert != undefined && request.query.alert != null)
alert = true;
var Slack = require('slack-node');
var slack = new Slack("[mywebhookskey]", "[myteamname]");
var stash = {
root: "https://mystashurl.com",
key: rep.project.key,
slug: rep.slug,
name: rep.name
};
for (var y = 0; y < ref.length; y++) {
issues = "";
var r = {
type: ref[y].type,
from: ref[y].fromHash,
to: ref[y].toHash,
branch: ref[y].refId.replace("refs/heads/", "").replace("refs/tags/", ""),
branchFull: ref[y].refId
}
var slackMsg = alert ? (channel.indexOf("#") < 0 ? "<!group> " : "<!everyone> ") : "";
if (r.type === "UPDATE") {
slackMsg += "Commit on *" + stash.name + "* to branch" + " *<" + stash.root + "/projects/" + stash.key + "/repos/" + stash.slug + "/commits/" + r.branchFull + "|" + r.branch + ">*";
} else if (r.type == "DELETE") {
slackMsg += "Deleted remote branch *" + r.branch + "* on *<" + stash.root + "/projects/" + stash.key + "/repos/" + stash.slug + "/branches|" + stash.name + ">*";
} else if (r.type == "ADD") {
var bn = "branch";
if (r.branchFull.indexOf("/tags/") > -1) {
bn = "tag";
}
slackMsg += "Created remote " + bn + " *<" + stash.root + "/projects/" + stash.key + "/repos/" + stash.slug + "/browse/?at=" + r.branchFull + "|" + r.branch + ">* on *" + stash.name + "*";
}
var started = false;
var anyCommits = false;
var commitAttachments = [];
for (var x = ch.values.length; x > 0; x--) {
var v = ch.values[x - 1];
var c = {
to: v.toCommit == null ? "" : v.toCommit.id,
toDisplay: v.toCommit == null ? "" : v.toCommit.displayId,
from: v.fromCommit == null ? "" : v.fromCommit.id,
author: v.toCommit == null ? "" : v.toCommit.author,
message: v.toCommit == null ? "" : v.toCommit.message,
mods: v.changes.values.filter(function(e) {
return e.type === "MODIFY";
}).length,
adds: v.changes.values.filter(function(e) {
return e.type === "ADD";
}).length,
parents: v.toCommit.parents.length
};
if (r.type === "ADD") {
if (c.from === r.from) {
started = true;
}
if (started) {
commitAttachments.push(buildAttachment(stash, c));
anyCommits = true;
}
if (c.to === r.to) {
if (!anyCommits && c.parents <= 1) {
commitAttachments.push(buildAttachment(stash, c));
}
started = false;
}
} else {
commitAttachments.push(buildAttachment(stash, c));
}
}
var attach = r.type === "ADD" ? commitAttachments : commitAttachments.reverse();
var trimAttach = attach.slice(0, 3); // there could be 100 commits, lets not post all of that to chat
var more = attach.length - trimAttach.length;
if (more > 0) {
var moreMessage = "_" + more + " more commit(s)_";
trimAttach.push({
"fallback": moreMessage,
"pretext": moreMessage,
"mrkdwn_in": ["pretext", "fallback"]
})
}
slack.webhook({
text: slackMsg,
channel: channel,
attachments: trimAttach
}, function(err, response) {
console.log(response);
});
}
response.send(statusCodes.OK, { message: 'Message Sent' });
};
function buildAttachment(stash, c) {
var almPattern = /[A-Za-z]+\-[0-9]+/g;
var msgFull = c.message.replace(almPattern, buildAlmLink);
// get rid of the line breaks and trim up the message just enough to get the point
var msg = msgFull.replace(/\\n/g, " ").replace(/\n/g, " ").replace(/\\/g, " ").substr(0, 100)
var m = "_" + c.author.name + "_ - " + msg;
m += (c.message.length > 100) ? "..." : "";
m = m.trim();
m += " (<" + stash.root + "/projects/" + stash.key + "/repos/" + stash.slug + "/commits/" + c.to + "|" + c.toDisplay + ">)\n";
var a = {
"fallback": m,
"pretext": m,
"color": "#003366",
"fields": [
{
"title": "Changes",
"value": "*" + c.mods + "* modified, *" + c.adds + "* added",
"short": true
}
],
"mrkdwn_in": ["pretext", "fallback", "fields"]
};
if (issues.length > 0) {
a.fields.push({
"title": "JIRA Issues",
"value": issues,
"short": true
});
a.fields.reverse();
issues = "";
}
return a;
}
function buildAlmLink(match, p1, p2, p3, offset, string) {
// p1 is nondigits, p2 digits, and p3 non-alphanumerics
var link = " <https://myjiraurl.com/browse/" + match.toUpperCase() + "|" + match.toUpperCase() + ">";
issues += link;
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment