Adds a new option called `GITHUB_ANONYMOUS` to signal that you want to public repositories that don't require any authentication. If you don't set this to true, Nuts will continue to throw an error if you don't provide authentication credentials. This uses the `getenv` package, which handles env var values like "true" and "1" as bash would.
93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
var _ = require('lodash');
|
|
var Q = require('q');
|
|
var util = require('util');
|
|
var destroy = require('destroy');
|
|
var GitHub = require('octocat');
|
|
var request = require('request');
|
|
var Buffer = require('buffer').Buffer;
|
|
var githubWebhook = require('github-webhook-handler');
|
|
|
|
var Backend = require('./backend');
|
|
|
|
function GitHubBackend() {
|
|
var that = this;
|
|
Backend.apply(this, arguments);
|
|
|
|
this.opts = _.defaults(this.opts || {}, {
|
|
proxyAssets: true
|
|
});
|
|
|
|
if ((!this.opts.username || !this.opts.password) && (!this.opts.token) && (!this.opts.anonymous)) {
|
|
throw new Error('GitHub backend requires either the "username" and "password, "token", or "anonymous" option');
|
|
}
|
|
|
|
this.client = new GitHub({
|
|
token: this.opts.token,
|
|
username: this.opts.username,
|
|
password: this.opts.password
|
|
});
|
|
|
|
this.ghrepo = this.client.repo(this.opts.repository);
|
|
this.releases = this.memoize(this._releases);
|
|
|
|
// GitHub webhook to refresh list of versions
|
|
this.webhookHandler = githubWebhook({
|
|
path: '/refresh',
|
|
secret: this.opts.refreshSecret
|
|
});
|
|
|
|
// Webhook from GitHub
|
|
this.webhookHandler.on('release', function(event) {
|
|
that.onRelease();
|
|
that.versions.list();
|
|
});
|
|
this.nuts.router.use(this.webhookHandler);
|
|
}
|
|
util.inherits(GitHubBackend, Backend);
|
|
|
|
// List all releases for this repository
|
|
GitHubBackend.prototype._releases = function() {
|
|
return this.ghrepo.releases()
|
|
.then(function(page) {
|
|
return page.all();
|
|
});
|
|
};
|
|
|
|
// Return stream for an asset
|
|
GitHubBackend.prototype.serveAsset = function(asset, req, res) {
|
|
if (!this.opts.proxyAssets) {
|
|
res.redirect(asset.raw.browser_download_url);
|
|
} else {
|
|
return Backend.prototype.serveAsset.apply(this, arguments);
|
|
}
|
|
};
|
|
|
|
// Return stream for an asset
|
|
GitHubBackend.prototype.getAssetStream = function(asset) {
|
|
var headers = {
|
|
'User-Agent': 'nuts',
|
|
'Accept': 'application/octet-stream'
|
|
};
|
|
var httpAuth;
|
|
|
|
if (this.opts.token) {
|
|
headers['Authorization'] = 'token '+this.opts.token;
|
|
} else if (this.opts.username) {
|
|
httpAuth = {
|
|
user: this.opts.username,
|
|
pass: this.opts.password,
|
|
sendImmediately: true
|
|
};
|
|
}
|
|
|
|
return Q(request({
|
|
uri: asset.raw.url,
|
|
method: 'get',
|
|
headers: headers,
|
|
auth: httpAuth
|
|
}));
|
|
};
|
|
|
|
|
|
module.exports = GitHubBackend;
|