Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Private repo support #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions lib/extractTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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;
Expand Down
24 changes: 21 additions & 3 deletions lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,7 +42,7 @@ function ReportGenerator(options, query) {
this.query = query || {};

this.router = new Route({
pattern: 'https://' + this.options.hostname + '/<user>/<repo>/archive/<branch>.zip',
pattern: 'https://' + this.options.apiHostname + '/repos/<user>/<repo>/zipball/<branch>',
defaults: {
branch: 'master'
}
Expand Down Expand Up @@ -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()
]);
})
Expand Down Expand Up @@ -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;
}
};

Expand Down
9 changes: 8 additions & 1 deletion server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions server/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ <h1>PlatoJS Services</h1>
<h3>Plato Reports</h3>
<code>/%github-user%/%repo%/%branch%/</code>
<p>Example <a href="/azproduction/plato-as-service/master/">/azproduction/plato-as-service/master/</a></p>
<p>Add <a href="https://help.github.com/articles/creating-an-access-token-for-command-line-use/">OAuth token</a>
<code>?oauth_token=ab4fe...</code> to access private Github account.
</p>
</div>
</div>
<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion test/test.plato-as-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down