-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from duojs/generators
Using generators internally + refactoring flow
- Loading branch information
Showing
7 changed files
with
283 additions
and
266 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,110 @@ | ||
/* eslint-disable camelcase */ | ||
|
||
var base = 'https://api.github.com/'; | ||
var path = require('path'); | ||
var request = require('request'); | ||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
module.exports = function (opts) { | ||
var token = opts.token || opts.password; | ||
var debug = require('debug')('duo:gh-resolve:github'); | ||
var request = require('co-request'); | ||
var url = require('url'); | ||
|
||
return { | ||
tags: tags.bind(null, token), | ||
branch: branch.bind(null, token) | ||
}; | ||
|
||
/** | ||
* Retrieve the list of tags for a given repository. | ||
* | ||
* @param {String} token | ||
* @param {Object} options | ||
* @return {Object} | ||
*/ | ||
|
||
exports.tags = function* (token, options) { | ||
return yield get(token, [ | ||
'repos', | ||
options.owner, | ||
options.repo, | ||
'tags' | ||
]); | ||
}; | ||
|
||
function tags(token, owner, repo, callback) { | ||
return request({ | ||
url: url('repos', owner, repo, 'tags'), | ||
qs: { access_token: token }, | ||
json: true, | ||
headers: { | ||
'Accept': 'application/vnd.github.quicksilver-preview+json', | ||
'User-Agent': 'duo:gh-resolve' | ||
}, | ||
followRedirects: true | ||
}, handle(callback)); | ||
} | ||
/** | ||
* Retrieve the information about a specific branch for a repository. | ||
* | ||
* @param {String} token | ||
* @param {Object} options | ||
* @return {Object} | ||
*/ | ||
|
||
exports.branch = function* (token, options) { | ||
return yield get(token, [ | ||
'repos', | ||
options.owner, | ||
options.repo, | ||
'branches', | ||
options.branch | ||
]); | ||
}; | ||
|
||
/** | ||
* Helper for performing an HTTP GET against the Github API. | ||
* | ||
* @param {String} token | ||
* @param {Array:String} parts Pieces of the URL | ||
* @return {Response} | ||
*/ | ||
|
||
function* get(token, parts, attempts) { | ||
var uri = url.resolve('https://api.github.com/', parts.join('/')); | ||
debug('--> GET %s', uri); | ||
|
||
function branch(token, owner, repo, branch, callback) { | ||
return request({ | ||
url: url('repos', owner, repo, 'branches', branch), | ||
var res = yield request({ | ||
url: uri, | ||
qs: { access_token: token }, | ||
json: true, | ||
headers: { | ||
'Accept': 'application/vnd.github.quicksilver-preview+json', | ||
'User-Agent': 'duo:gh-resolve' | ||
}, | ||
followRedirects: true | ||
}, handle(callback)); | ||
} | ||
headers: { 'User-Agent': 'duo:gh-resolve' }, | ||
followRedirects: true, | ||
json: true | ||
}); | ||
|
||
function url() { | ||
var parts = [].slice.call(arguments); | ||
return base + path.join.apply(path, parts); | ||
} | ||
debug('<-- %s %s', res.statusCode, uri); | ||
rateLimit(res.headers); | ||
|
||
function handle(callback) { | ||
return function (err, res, data) { | ||
if (err) { | ||
callback(err, res); | ||
} else if (isError(res.statusCode)) { | ||
callback(new Error(data.message)); | ||
if (isError(res.statusCode)) { | ||
var msg = res.body.message; | ||
debug('error', msg); | ||
|
||
if (msg === 'Not Found' || attempts > 5) { | ||
throw new Error(msg); | ||
} else { | ||
callback(null, res, data); | ||
// retry! | ||
return yield get(token, parts, attempts + 1); | ||
} | ||
}; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
/** | ||
* Helper for determining if a status code is an error. | ||
* (ie: 4xx or 5xx) | ||
* | ||
* @param {Number} code | ||
* @return {Boolean} | ||
*/ | ||
|
||
function isError(code) { | ||
var type = Math.floor(code / 100); | ||
return type === 4 || type === 5; | ||
} | ||
|
||
/** | ||
* Outputs rate-limit information via debug. | ||
* | ||
* @param {Object} headers | ||
* @api private | ||
*/ | ||
|
||
function rateLimit(headers) { | ||
var remaining = headers['x-ratelimit-remaining']; | ||
var limit = headers['x-ratelimit-limit']; | ||
var reset = new Date(headers['x-ratelimit-reset'] * 1000); | ||
debug('rate limit status: %d / %d (resets: %s)', remaining, limit, reset); | ||
} |
Oops, something went wrong.