Created
November 19, 2012 22:34
-
-
Save scottgonzalez/4114537 to your computer and use it in GitHub Desktop.
Update jQuery's GitHub IRC notifications to include push, pull_request, issues.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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