Skip to content

Instantly share code, notes, and snippets.

@okjake
Created July 11, 2016 11:38
Show Gist options
  • Select an option

  • Save okjake/70f42a461585ca3ff3064c0b467b8283 to your computer and use it in GitHub Desktop.

Select an option

Save okjake/70f42a461585ca3ff3064c0b467b8283 to your computer and use it in GitHub Desktop.
GitHub digest webtask
"use latest";
const _ = require('lodash');
const request = require('superagent');
const moment = require('moment');
const mandrillApi = require('mandrill-api/mandrill');
/*
* utilites
*/
function time(timestamp) {
return moment(timestamp).format('MMMM Do, HH:mm:ss');
}
function format(str, n) {
const truncated = (str.length > n) ? `${str.substr(0, n-1)}...` : str;
return truncated.split('\n')[0];
}
/*
* templating functions
* the following functions construct a string containing the full email
*/
function main(data) {
let ret = `
Digest for ${data.repo}
${time(data.from)} - ${time(data.to)}
`;
_.each(data.events, event => {
ret += event;
});
return ret;
}
function def(event) {
const name = event.actor.display_login;
return `
${time(event.created_at)}: ${name} performed ${event.type}
`;
}
function push(event) {
const count = event.payload.commits.length;
const name = event.actor.display_login;
const commitStr = count === 1 ? 'commit' : 'commits';
let ret = `
${time(event.created_at)}: ${name} pushed ${count} ${commitStr}
`;
_.each(event.payload.commits, (commit) => {
ret += `
- ${format(commit.message, 80)}
${commit.url}
`;
});
return ret;
}
function issues(event) {
const name = event.actor.display_login;
const verb = event.payload.action;
const title = event.payload.issue.title;
const issue = event.payload.issue.body;
return `
${time(event.created_at)}: ${name} ${verb} ${title}
- ${format(issue, 80)}
`;
}
function comment(event) {
const name = event.actor.display_login;
const title = event.payload.issue.title;
const comment = (event.payload.comment.body);
return `
${time(event.created_at)}: ${name} commented on ${title}
- ${format(comment, 80)}
`;
}
/*
* map github events to tpl funcs
*/
const eventTpl = {
PushEvent: push,
IssuesEvent: issues,
IssueCommentEvent: comment,
};
module.exports = (ctx, done) => {
ctx.storage.get((err, data) => {
if (err) return done(err);
const mandrill = new mandrillApi.Mandrill(ctx.data.MANDRILL_API_KEY);
const lastTimestamp = (data && data.lastTimestamp) ?
data.lastTimestamp :
moment().subtract(1, 'days').toDate();
request.get(
`https://api.github.com/repos/${ctx.params.repository}/events`
).end((err, res) => {
if (err || !res.body.length) return done(err);
// filter out events we've already seen and pass them through the templates
const events = _.filter(res.body, event => new Date(event.created_at).getTime() > new Date(lastTimestamp).getTime())
.map(event => eventTpl[event.type] ? eventTpl[event.type](event) : def(event));
if (events.length === 0) {
console.log('Nothing new to send');
return done(null);
}
const mail = main({
repo: ctx.params.repository,
from: lastTimestamp,
to: new Date().getTime(),
events,
});
const message = {
text: mail,
subject: `Digest for ${ctx.params.repository}`,
from_email: ctx.params.from,
from_name: 'Hub Digest',
to: [
{
email: ctx.params.to,
type: 'to',
}
]
};
mandrill.messages.send({
message
}, (result) => {
console.log(`Sent ${events.length} events`, result);
ctx.storage.set({ lastTimestamp: events[0].created_at }, done);
}, done
);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment