Skip to content

Instantly share code, notes, and snippets.

@nitely
Created July 20, 2016 21:00
Show Gist options
  • Select an option

  • Save nitely/22528db88bfa496455efa8d1db409b57 to your computer and use it in GitHub Desktop.

Select an option

Save nitely/22528db88bfa496455efa8d1db409b57 to your computer and use it in GitHub Desktop.
webtask.io
"use latest";
var Promise = require("bluebird@2.9.26");
var request = Promise.promisifyAll(require("request@2.56.0"));
var validator = require('is-my-json-valid@2.12.0');
const SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/T00000000/B00000000/X00000000000000000";
/*
* GitHub[0] should send this
* within the payload
*
* [0] https://developer.github.com/v3/activity/events/types/#issuecommentevent
* */
var validate = validator({
required: true,
type: 'object',
properties: {
action: {
required: true,
type: 'string'
},
issue: {
required: true,
type: 'object',
properties: {
title: {
required: true,
type: 'string'
}
}
},
comment: {
required: true,
type: 'object',
properties: {
html_url: {
required: true,
type: 'string'
},
body: {
required: true,
type: 'string'
}
}
}
}
});
var mentionGroup = 2;
var mentionsRE = /(^|[\s,\.])@(\w+)/mg;
/*
* Parses all mentions from a text
*
* @txt: A comment or text
* return: An array containing all user-names
* */
var mentionsParser = (txt) => {
var mentions = new Set();
var match;
// It'd be great to have a real markdown parser here,
// but the only one available is marked (sigh!)
while ((match = mentionsRE.exec(txt)) !== null) {
mentions.add(match[mentionGroup]);
}
return Array.from(mentions);
};
var payloadParser = (payload) => {
try {
var body = JSON.parse(payload);
} catch (err) {
throw new Error("GitHub payload is not a valid json");
}
if (!validate(body)) {
console.log(validate.errors);
throw new Error("GitHub payload is not valid");
}
return body;
};
var responseParser = (responses) => {
return responses.map((respObj) => {
var resp = respObj[0].toJSON();
return {
statusCode: resp.statusCode,
body: resp.body
};
});
};
/*
* Send all mentions to slack
*
* @mentions: Array of user-names
* @message: Message to sent to Slack
* return: A promise
* */
var sendRequests = (mentions, message) => {
// Send all requests in parallel
return Promise
.map(mentions, (username) => {
return request.postAsync({
url: SLACK_WEBHOOK_URL,
body: {
username: "github-bot",
channel: `@${username}`,
text: message},
json: true
});
}, {concurrency: 10})
.then((results) => {
console.log(results)
return responseParser(results);
});
};
var handler = (context, callback) => {
try {
var body = payloadParser(context.body_raw);
} catch (err) {
callback(err);
return;
}
if (body.action !== "created")
return;
var mentions = mentionsParser(body.comment.body);
var message = `You have been mentioned at <${body.comment.html_url}|${body.issue.title}>`;
sendRequests(mentions, message)
.then((results) => {
callback(null, {
status: "ok",
results: results
});
}).catch((err) => {
callback(err);
})
};
module.exports = handler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment