Skip to content

Instantly share code, notes, and snippets.

@tjfontaine
Created September 27, 2012 15:43
Show Gist options
  • Save tjfontaine/3794720 to your computer and use it in GitHub Desktop.
Save tjfontaine/3794720 to your computer and use it in GitHub Desktop.
quick cia.vc replacement
'use strict';
var domain = require('domain'),
http = require('http'),
querystring = require('querystring'),
util = require('util');
var irc = require('irc');
var config = require('./config');
var clients = {};
var repo_map = {};
var clear_to_send = {};
function gitio(url, cb) {
var data = querystring.stringify({url: url});
var opts = {
host: 'git.io',
port: 80,
path: '/',
method: 'POST',
headers: {
'Content-Length': data.length,
'Content-Type': 'application/x-www-form-urlencoded',
},
};
var req = http.request(opts, function (res) {
res.on('end', function () {
cb(res.headers['location'] || url);
});
});
req.write(data);
req.end();
};
function format_bold(name) {
return '' + name + '';
}
function format_author(name) {
return '3' + name + '';
}
function format_branch(name) {
return '7' + name + '';
}
var fmts = {};
fmts.PushEvent = function (event, cb) {
var name = event.repository.owner.name + '/' + event.repository.name;
var head = event.after;
var before = event.before;
//var compare = 'http://github.com/' + name + '/compare/' + before.slice(0, 7) + '...' + head.slice(0, 7);
var compare = event.compare;
//console.log(event);
gitio(compare, function (url) {
//var files = payload.files.all.join(' ');
var files = '<empty>';
var head_commit = event.head_commit;
var commit_msg = head_commit.message ? head_commit.message.slice(0, 73) : 'no commit';
if (event.commits.length > 1)
commit_msg += ' (+'+ (event.commits.length-1) +' more commits)';
var author = head_commit.author ? head_commit.author.name : 'no author';
var branch = event.ref.replace(/^refs\/heads\//, '')
var msg = [
format_bold(name+':'),
format_author(author),
format_branch(branch),
'*', format_bold(head.slice(0, 7)),
//files.slice(0, 100),
':', commit_msg,
].join(' ').slice(0, 500);
msg += ' - ' + url;
cb(msg);
});
};
fmts.IssuesEvent = function (event, cb) {
gitio(event.issue.html_url, function (url) {
var msg = [
format_bold(event.repository.full_name+':'),
format_author(event.sender.login),
format_branch(event.action),
'issue',
'#'+event.issue.number,
'--', event.issue.title,
].join(' ').slice(0, 500);
msg += ' ' + url;
cb(msg);
});
};
/*
fmts.IssueCommentEvent = function (event, cb) {
gitio(event.payload.comment.html_url, function (url) {
var msg = [
format_bold(event.repo.name+':'),
format_author(event.actor.login),
'commented on issue',
'#' + event.payload.issue.number,
'--', event.payload.comment.body.slice(0, 100),
].join(' ').slice(0, 500);
msg += ' ( ' + url + ' )';
cb(msg);
});
};
*/
fmts.CreateEvent = function (event, cb) {
var url = event.repository.url;
var name = event.repository.owner.name + '/' + event.repository.name;
var action = event.created ? 'created' : 'deleted';
var ref_type = 'branch';
var ref;
if (/^refs\/tags\//.test(event.ref)) {
ref_type = 'tag';
ref = event.ref.replace(/^refs\/tags\//, '');
} else if (/^refs\/heads\//.test(event.ref)) {
ref_type = 'branch';
ref = event.ref.replace(/^refs\/heads\//, '');
}
//console.log(util.inspect(event, true, 10, true));
//switch (event.ref_type) {
// case 'branch':
url = event.compare;
// break;
// case 'tag':
// url += '/tarball/' + event.payload.ref;
// break;
//}
gitio(url, function (url) {
var msg = [
format_bold(name+':'),
format_author(event.pusher.name),
action,
ref_type,
ref,
].join(' ').slice(0, 500);
if (action === 'created') {
msg += ' - ' + url;
}
cb(msg);
});
};
//fmts.DeleteEvent = fmts.CreateEvent;
/*
fmts.CommitCommentEvent = function (event, cb) {
gitio(event.payload.comment.html_url, function (url) {
var msg = [
format_bold(event.repo.name+':'),
format_author(event.actor.login),
'commented on',
event.payload.comment.commit_id.slice(0, 7),
event.payload.comment.path + ':' + event.payload.comment.line,
event.payload.comment.body.slice(0, 100),
].join(' ').slice(0, 500);
msg += ' - ' + url;
cb(msg);
});
};
*/
fmts.PullRequestEvent = function (event, cb) {
var name = event.repository.full_name || event.repository.owner.login + '/' + event.repository.name;
gitio(event.pull_request.html_url, function (url) {
var msg = [
format_bold(name+':'),
format_author(event.sender.login),
event.action,
'pull request',
'#'+event.pull_request.number,
'--', event.pull_request.title,
].join(' ').slice(0, 500);
msg += ' - ' + url;
cb(msg);
})
};
function repo_hook(event) {
var repo = event.repository.full_name || event.repository.owner.name + '/' + event.repository.name;
var sinks = repo_map[repo];
var type;
if (event.created) {
type = 'CreateEvent';
} else if (event.deleted) {
type = 'DeleteEvent';
} else if (event.pull_request) {
type = 'PullRequestEvent';
} else if (event.head_commit) {
type = 'PushEvent';
} else if (event.issue && event.comment) {
type = 'IssueCommentEvent';
} else if (event.issue) {
type = 'IssuesEvent';
} else {
console.log('unknown event type', util.inspect(event, true, 10, true));
return;
}
//console.log(type);
//console.log(type, util.inspect(event, true, 10, true));
var format = fmts[type];
if (!sinks) {
console.log("unknown repo", event.repository.full_name);
return;
}
if (format) {
format(event, function (msg) {
sinks.forEach(function (sink) {
var client = clients[sink.network];
//console.log(client);
if (clear_to_send[sink.channel]) {
client.say(sink.channel, msg.slice(0, 510).replace(/\r/g, '').replace(/\n/g, ' '));
}
});
});
}
};
var server = http.createServer(function (req, res) {
var data = '';
if (req.method === 'POST') {
req.on('data', function (chunk) {
data += chunk;
});
}
req.on('end', function () {
if (data) {
if (/^payload=/.test(data)) {
data = querystring.unescape(data.slice(8));
}
try {
var payload = JSON.parse(data);
repo_hook(payload);
} catch (e) {
console.log(e, data);
}
}
res.writeHead(200, {
'Content-type': 'text/plain',
});
res.end();
})
}).listen(8000);
Object.keys(config).forEach(function (name) {
var conf = config[name];
conf.channels = Object.keys(conf.subscribe).slice(0);
Object.keys(conf.subscribe).forEach(function (channel) {
var chan = conf.subscribe[channel];
chan.forEach(function (repo) {
if (!repo_map[repo]) {
repo_map[repo] = [];
}
repo_map[repo].push({
network: name,
channel: channel,
});
});
});
var client = clients[name] = new irc.Client(name, conf.nick, conf);
client.on('error', function () {
});
client.on('join', function (channel) {
clear_to_send[channel] = true;
})
});
console.log(repo_map);
module.exports = {
'irc.oftc.net': {
port: 6667,
secure: false,
nick: 'github-lurk',
showErrors: true,
floodProtection: true,
selfSigned: true,
subscribe: {
'#native-dns': [
'tjfontaine/node-dns',
],
}
},
};
var gh = require('github')
function update(token, repos) {
var github = new gh({
version: "3.0.0",
//debug: true,
});
github.authenticate({
type: 'oauth',
token: token,
});
repos.forEach(function (repo) {
var s = repo.split('/');
var user = s[0];
var name = s[1];
github.repos.getHooks({
user: user,
repo: name,
}, function (err, result) {
var i, hook, found = [];
for (i in result) {
hook = result[i];
console.log(i, hook);
if (hook.name === 'web' && hook.config.url === 'http://cass.atxconsulting.com:8000') {
found.push(hook.id);
}
}
hook = found.pop();
if (hook) {
console.log('update hook', hook);
github.repos.updateHook({
user: user,
repo: name,
name: 'web',
id: hook,
config: {
url: 'http://cass.atxconsulting.com:8000',
content_type: 'json',
},
events: [
'push',
'issues',
'issue_comment',
'pull_request',
'pull_request_review_comment',
'commit_comment',
],
active: true,
}, function () {
console.log(arguments);
});
}
while (found.length) {
github.repos.deleteHook({
user: user,
repo: name,
id: found.pop()
}, function () {
console.log(arguments);
});
}
if (hook)
return;
github.repos.createHook({
user: user,
repo: name,
name: 'web',
config: {
url: 'http://cass.atxconsulting.com:8000',
content_type: 'json',
},
events: [
'push',
'issues',
'issue_comment',
'pull_request',
'pull_request_review_comment',
'commit_comment',
],
active: true,
}, function () {
console.log(arguments);
});
});
});
}
var repos = [
'joyent/libuv',
'joyent/node',
];
var GITHUBTOKEN = '';
update(GITHUBTOKEN, repos);
/*
var octOAuth = require('octoauth');
var oauth = new octOAuth(require('./ghauth'))
oauth.scopes = ['public_repo'];
oauth.getToken(function (err, token) {
console.log('octoauth token', token);
update(token, repos);
});
*/
{
"name": "cia-replacement",
"version": "0.0.1",
"author": "Timothy J Fontaine <[email protected]> (http://atxconsulting.com)",
"description": "cia-replacement",
"keywords": [ "cia", "github" ],
"main": "bot.js",
"engines": {
"node": ">= 0.8.0"
},
"dependencies": {
"github-firehose": ">= 0.0.1",
"irc": ">= 0.3.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment