Skip to content

Commit

Permalink
Merge pull request #23 from duojs/generators
Browse files Browse the repository at this point in the history
Using generators internally + refactoring flow
  • Loading branch information
dominicbarnes committed Jul 10, 2015
2 parents 58ce452 + 0841cd4 commit 8927dd5
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 266 deletions.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

NODE ?= node
NODE_FLAGS ?= $(shell $(NODE) --v8-options | grep generators | cut -d ' ' -f 3)

BIN := ./node_modules/.bin
MOCHA := $(BIN)/mocha
MOCHA := $(BIN)/_mocha
ESLINT := $(BIN)/eslint

test: node_modules
@$(MOCHA)
@$(NODE) $(NODE_FLAGS) $(MOCHA)

node_modules: package.json
@npm install
Expand Down
137 changes: 0 additions & 137 deletions index.js

This file was deleted.

136 changes: 92 additions & 44 deletions lib/github.js
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);
}
Loading

0 comments on commit 8927dd5

Please sign in to comment.