Skip to content

Instantly share code, notes, and snippets.

@scottgonzalez
Created November 19, 2012 22:34
Show Gist options
  • Save scottgonzalez/4114537 to your computer and use it in GitHub Desktop.
Save scottgonzalez/4114537 to your computer and use it in GitHub Desktop.
Update jQuery's GitHub IRC notifications to include push, pull_request, issues.
var https = require( "https" ),
async = require( "async" ),
username = "xxx",
password = "xxx";
function getRepos( user, fn ) {
var req = https.request({
host: "api.github.com",
port: 443,
path: "/users/" + user + "/repos",
method: "GET",
headers: {
Authorization: "Basic " + new Buffer( username + ":" + password ).toString( "base64" )
}
}, function( res ) {
var response = "";
res.setEncoding( "utf8" );
res.on( "data", function( chunk ) {
response += chunk;
});
res.on( "end", function() {
fn( JSON.parse( response ) );
});
});
req.end();
}
function getHooks( repo, fn ) {
var req = https.request({
host: "api.github.com",
port: 443,
path: "/repos/jquery/" + repo + "/hooks",
method: "GET",
headers: {
Authorization: "Basic " + new Buffer( username + ":" + password ).toString( "base64" )
}
}, function( res ) {
var response = "";
res.setEncoding( "utf8" );
res.on( "data", function( chunk ) {
response += chunk;
});
res.on( "end", function() {
fn( JSON.parse( response ) );
});
});
req.end();
}
function updateHook( repo, id, settings, fn ) {
var data = JSON.stringify( settings ),
headers = {
"Content-length": data.length
};
headers.Authorization = "Basic " + new Buffer( username + ":" + password ).toString( "base64" );
var req = https.request({
host: "api.github.com",
port: 443,
path: "/repos/jquery/" + repo + "/hooks/" + id,
method: "PATCH",
headers: headers
}, function( res ) {
var response = "";
res.setEncoding( "utf8" );
res.on( "data", function( chunk ) {
response += chunk;
});
res.on( "end", function() {
fn( JSON.parse( response ) );
});
});
req.write( data );
req.end();
}
getRepos( "jquery", function( repos ) {
async.forEachSeries( repos, function( repo, done ) {
console.log( "+ " + repo.name );
getHooks( repo.name, function( hooks ) {
var hook = hooks.filter(function( hook ) {
return hook.name === "irc";
});
if ( !hook.length ) {
console.log( "No IRC hook." );
return done();
}
hook = hook[ 0 ];
updateHook( repo.name, hook.id, {
name: hook.name,
config: hook.config,
events: [ "push", "pull_request", "issues" ]
}, function( hook ) {
console.log( hook );
done();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment