diff --git a/README.md b/README.md index 5edf3e6..5554802 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ npm install plato-as-service var server = require('plato-as-service'); server({ - hostname: 'github.com', + apiHostname: 'api.github.com', badgeService: 'img.shields.io', reports: __dirname + '/reports', maxConcurrent: 50, // max concurrent unzip processes diff --git a/example/index.js b/example/index.js index a3f0432..b15fbb2 100644 --- a/example/index.js +++ b/example/index.js @@ -2,7 +2,7 @@ var server = require('../server'), path = require('path'); server({ - hostname: 'github.com', + apiHostname: 'api.github.com', badgeService: 'img.shields.io', maxConcurrent: 50, maxConcurrentQueue: Infinity, diff --git a/lib/extractTo.js b/lib/extractTo.js index 93f327b..e276300 100644 --- a/lib/extractTo.js +++ b/lib/extractTo.js @@ -4,11 +4,13 @@ var vow = require('vow'), Extract = require('unzip').Extract; /** - * @param {String} zipUrl source url + * @param {Object} options + * @param {String} options.url source url + * @param {Object} options.headers * @param {String} path extract path * @returns {Promise} fulfill(path: String) */ -module.exports = function (zipUrl, path) { +module.exports = function (options, path) { var promise = vow.promise(); var unzip = new Extract({ @@ -20,9 +22,9 @@ module.exports = function (zipUrl, path) { }); unzip.on('error', promise.reject.bind(promise)); - notifyNextTick(promise, 'Downloading zip ' + zipUrl); + notifyNextTick(promise, 'Downloading zip ' + options.url); - var zip = request(zipUrl); + var zip = request(options); var length = 0; zip.on('data', function (chunk) { length += chunk.length; diff --git a/lib/report.js b/lib/report.js index 5005c06..c45d337 100644 --- a/lib/report.js +++ b/lib/report.js @@ -28,11 +28,12 @@ var AVAILABLE_BADGES = { * @param {String} query.user * @param {String} query.repo * @param {String} query.branch + * @param {String} [query.oauth_token] * @constructor */ function ReportGenerator(options, query) { this.options = _.defaults(options || {}, { - hostname: 'github.com', + apiHostname: 'api.github.com', badgeService: 'img.shields.io', reports: join(__dirname, '..', 'reports'), ttl: 60 * 15 * 1000 @@ -41,7 +42,7 @@ function ReportGenerator(options, query) { this.query = query || {}; this.router = new Route({ - pattern: 'https://' + this.options.hostname + '///archive/.zip', + pattern: 'https://' + this.options.apiHostname + '/repos///zipball/', defaults: { branch: 'master' } @@ -111,8 +112,13 @@ ReportGenerator.prototype = { return tmpDir() .then(function (tmpDir) { + var downloadZipAndExtract = extractTo({ + url: self.buildZipUrl(), + headers: self.getRequestHeaders() + }, tmpDir); + return all([ - extractTo(self.buildZipUrl(), tmpDir), + downloadZipAndExtract, self.makeResultsDir() ]); }) @@ -203,6 +209,18 @@ ReportGenerator.prototype = { }, buildZipUrl: function () { return this.router.build(this.query); + }, + getRequestHeaders: function() { + var headers = { + // Header required by api.github.com + 'User-Agent': 'es-analysis/plato-as-service' + }; + + if (this.query.oauth_token) { + headers['Authorization'] = 'token ' + String(this.query.oauth_token); + } + + return headers; } }; diff --git a/server/controllers/index.js b/server/controllers/index.js index 3560003..754445a 100644 --- a/server/controllers/index.js +++ b/server/controllers/index.js @@ -26,7 +26,14 @@ var badgeImages = Object.keys(ReportGenerator.AVAILABLE_BADGES) var messages = new BufferedMessageChannel(); exports.index = function (req, res, next) { - var reportGenerator = new ReportGenerator(res.app.locals.reportSettings, req.params), + var query = { + user: req.params.user, + repo: req.params.repo, + branch: req.params.branch, + oauth_token: req.query.oauth_token + }; + + var reportGenerator = new ReportGenerator(res.app.locals.reportSettings, query), channelId = reportGenerator.cacheKey(), isPending = reportGenerator.isPending(), requestQueue = res.app.locals.requestQueue; diff --git a/server/views/index.html b/server/views/index.html index 3076a03..43317ea 100644 --- a/server/views/index.html +++ b/server/views/index.html @@ -27,6 +27,9 @@

PlatoJS Services

Plato Reports

/%github-user%/%repo%/%branch%/

Example /azproduction/plato-as-service/master/

+

Add OAuth token + ?oauth_token=ab4fe... to access private Github account. +

diff --git a/test/test.plato-as-service.js b/test/test.plato-as-service.js index a21ce9f..ffd11fe 100644 --- a/test/test.plato-as-service.js +++ b/test/test.plato-as-service.js @@ -14,7 +14,7 @@ describe('plato-as-service', function () { beforeEach(function () { app = server({ - hostname: 'github.com', + apiHostname: 'api.github.com', badgeService: 'img.shields.io', maxConcurrent: 50, maxConcurrentQueue: Infinity,