Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add GitHub authentication to exceed rate-limiting
  • Loading branch information
rvagg committed Oct 16, 2012
1 parent 696f1f9 commit 299f3fd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
6 changes: 4 additions & 2 deletions build
Expand Up @@ -14,8 +14,10 @@ var config = {
, verbose: process.argv.indexOf('-v') != -1
, quiet: process.argv.indexOf('-q') != -1
}
, ghAuth = require('./lib/ghauth')
, main = require('./lib/main')
, stats = require('./lib/stats')

!(process.argv.indexOf('-s') != -1 ? stats : main).create(config).start()

ghAuth(config, function () {
(process.argv.indexOf('-s') != -1 ? stats : main).create(config).start()
})
21 changes: 21 additions & 0 deletions lib/ghauth.js
@@ -0,0 +1,21 @@
var read = require('read')
, colors = require('colors')

, auth = function (config, callback) {
console.log('To collect GitHub stats we need you to authenticate so we can exceed API rate-limiting.'.bold.green)
console.log('Leave the username field blank to skip stats collection.'.bold.green)
console.log()

read({ prompt: 'GitHub username:'.bold }, function (err, data) {
if (err) return callback(err)
if (data === '') return callback()
config.ghUser = data
read({ prompt: 'GitHub password:'.bold, silent: true, replace: '\u2714' }, function (err, data) {
if (err) return callback(err)
config.ghPass = data
callback()
})
})
}

module.exports = auth
21 changes: 17 additions & 4 deletions lib/gitstats.js → lib/ghstats.js
Expand Up @@ -8,6 +8,8 @@ var GitStats = {
processLibrary: function (lib, callback) {
var match

if (!this.user)
return callback()
if (lib.github)
match = lib.github.match(githubSpecRegex)
if (!match)
Expand All @@ -23,7 +25,10 @@ var GitStats = {

, processRepo: function (lib, user, project, callback) {
request(
GIT_API_REPO_URL.replace('{user}', user).replace('{project}', project)
{
url: GIT_API_REPO_URL.replace('{user}', user).replace('{project}', project)
, headers: { Authorization: "Basic " + new Buffer(this.user + ":" + this.pass).toString('base64') }
}
, function (err, response, body) {
if (err)
return callback('Error requesting repo data from GitHub for ' + user + '/' + project + ': ' + err)
Expand All @@ -39,7 +44,13 @@ var GitStats = {
//, project: project
})
} else {
this.emit('warning', lib, 'Looks like a GitHub project but can\'t get data from GitHub API')
this.emit(
'warning'
, lib
, typeof data.message == 'string'
? 'GitHub says: ' + data.message
: 'Looks like a GitHub project but can\'t get data from GitHub API'
)
callback()
}
}.bind(this)
Expand All @@ -49,8 +60,10 @@ var GitStats = {

GitStats.__proto__ = events.EventEmitter.prototype

module.exports.create = function () {
module.exports.create = function (user, pass) {
var gitStats = Object.create(GitStats)
gitStats.user = user
gitStats.pass = pass
events.EventEmitter.call(gitStats)
return gitStats
}
}
6 changes: 3 additions & 3 deletions lib/main.js
Expand Up @@ -47,7 +47,7 @@ var Main = {
async.parallel(
{
sizes: this.processor.processLibrary.bind(this.processor, lib)
, ghstats: this.gitstats.processLibrary.bind(this.gitstats, lib)
, ghstats: this.ghstats.processLibrary.bind(this.ghstats, lib)
, twstats: this.twitterstats.processLibrary.bind(this.twitterstats, lib)
}
, function (err, data) {
Expand Down Expand Up @@ -152,8 +152,8 @@ module.exports.create = function (config) {
main.output = require('./output').create(config.outFile)
main.processor = require('./processor').create(config)
main.processor.on('warning', main.warn.bind(main))
main.gitstats = require('./gitstats').create()
main.gitstats.on('warning', main.warn.bind(main))
main.ghstats = require('./ghstats').create(config.ghUser, config.ghPass)
main.ghstats.on('warning', main.warn.bind(main))
main.twitterstats = require('./twitterstats').create()
main.twitterstats.on('warning', main.warn.bind(main))
main.atom = require('./atom')
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -3,7 +3,7 @@
, "author": ""
, "version": "1.0.0"
, "private": true
, "devDependencies": {
, "dependencies": {
"async": "*"
, "colors": "*"
, "uglify-js": "*"
Expand All @@ -12,8 +12,8 @@
, "date-utils": "*"
, "node-uuid": "*"
, "errno": "*"
, "read": "*"
}
, "dependencies": {}
, "engines": {
"node": "*"
}
Expand Down

0 comments on commit 299f3fd

Please sign in to comment.