Skip to content

Instantly share code, notes, and snippets.

@anaisbetts
Created February 19, 2016 22:17
Show Gist options
  • Save anaisbetts/3e76b7088fe20c8d0715 to your computer and use it in GitHub Desktop.
Save anaisbetts/3e76b7088fe20c8d0715 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict';
var main = function () {
var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() {
var testServer = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
var testRepo = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var testCmdWithArgs = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var cmdWithArgs, repo, server, _nwo, jobs, refInfo, nwo, surfUrl, fetchRefs, buildMonitor;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
cmdWithArgs = testCmdWithArgs || argv._;
repo = testRepo || argv.r;
server = testServer || argv.s;
if (cmdWithArgs.length < 1) {
console.log("Command to run not specified, defaulting to 'surf-build'");
cmdWithArgs = ['surf-build', '-n', 'surf'];
}
if (repo) {
_context2.next = 19;
break;
}
_context2.prev = 5;
_context2.next = 8;
return (0, _gitApi.getOriginForRepo)('.');
case 8:
_context2.t0 = _context2.sent;
repo = (0, _githubApi.getSanitizedRepoUrl)(_context2.t0);
console.error('Repository not specified, using current directory: ' + repo);
_context2.next = 19;
break;
case 13:
_context2.prev = 13;
_context2.t1 = _context2['catch'](5);
console.error("Repository not specified and current directory is not a Git repo");
d(_context2.t1.stack);
yargs.showHelp();
process.exit(-1);
case 19:
if (!server) {
console.error('\n**** Becoming a Surf Server ****\n\nIf you\'re only setting up a single build client, this is probably what you want.\nIf you\'re setting up more than one, you\'ll want to run \'surf-server\' somewhere,\nthen pass \'-s\' to all of your build clients.');
_nwo = (0, _githubApi.getNwoFromRepoUrl)(repo);
(0, _refServerApi2.default)([_nwo]);
server = 'http://localhost:' + (process.env.SURF_PORT || 3000);
}
jobs = parseInt(argv.j || '2');
if (argv.j && (jobs < 1 || jobs > 64)) {
console.error("--jobs must be an integer");
yargs.showHelp();
process.exit(-1);
}
// Do an initial fetch to get our initial state
refInfo = null;
nwo = (0, _githubApi.getNwoFromRepoUrl)(repo);
surfUrl = server + '/info/' + nwo;
fetchRefs = function () {
var _this = this;
var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 3;
return (0, _requestPromise2.default)({
uri: surfUrl,
json: true
});
case 3:
return _context.abrupt('return', _context.sent);
case 6:
_context.prev = 6;
_context.t0 = _context['catch'](0);
console.log('Failed to fetch from ' + surfUrl + ': ' + _context.t0.message);
d(_context.t0.stack);
process.exit(-1);
case 11:
case 'end':
return _context.stop();
}
}
}, _callee, _this, [[0, 6]]);
}));
return function fetchRefs() {
return ref.apply(this, arguments);
};
}();
_context2.next = 28;
return fetchRefs();
case 28:
refInfo = _context2.sent;
// TODO: figure out a way to trap Ctrl-C and dispose stop
console.log('Watching ' + repo + ', will run \'' + cmdWithArgs.join(' ') + '\'\n');
buildMonitor = new _buildMonitor2.default(cmdWithArgs, repo, jobs, fetchRefs, refInfo);
buildMonitor.start();
return _context2.abrupt('return', new Promise(function () {}));
case 33:
case 'end':
return _context2.stop();
}
}
}, _callee2, this, [[5, 13]]);
}));
return function main() {
return ref.apply(this, arguments);
};
}();
require('./babel-maybefill');
var _requestPromise = require('request-promise');
var _requestPromise2 = _interopRequireDefault(_requestPromise);
var _gitApi = require('./git-api');
var _githubApi = require('./github-api');
var _refServerApi = require('./ref-server-api');
var _refServerApi2 = _interopRequireDefault(_refServerApi);
var _buildMonitor = require('./build-monitor');
var _buildMonitor2 = _interopRequireDefault(_buildMonitor);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
var d = require('debug')('surf:run-on-every-ref');
var yargs = require('yargs').usage('Usage: surf-client -r https://github.com/some/repo -- command arg1 arg2 arg3...\nMonitors a GitHub repo and runs a command for each changed branch / PR.').help('h').alias('s', 'server').describe('s', 'The Surf server to connect to for multi-machine builds').alias('r', 'repo').describe('r', 'The URL of the repository to monitor. Defaults to the repo in the current directory').alias('j', 'jobs').describe('j', 'The number of concurrent jobs to run. Defaults to 2').alias('h', 'help').epilog('\nSome useful environment variables:\n\nGITHUB_ENTERPRISE_URL - the GitHub Enterprise URL to use instead of .com.\nGITHUB_TOKEN - the GitHub (.com or Enterprise) API token to use. Must be provided.');
var argv = yargs.argv;
main().catch(function (e) {
console.error(e.message);
d(e.stack);
process.exit(-1);
});
#!/usr/bin/env node
import './babel-maybefill';
import request from 'request-promise';
import {getOriginForRepo} from './git-api';
import {getSanitizedRepoUrl, getNwoFromRepoUrl} from './github-api';
import createRefServer from './ref-server-api';
import BuildMonitor from './build-monitor';
const d = require('debug')('surf:run-on-every-ref');
const yargs = require('yargs')
.usage(`Usage: surf-client -r https://github.com/some/repo -- command arg1 arg2 arg3...
Monitors a GitHub repo and runs a command for each changed branch / PR.`)
.help('h')
.alias('s', 'server')
.describe('s', 'The Surf server to connect to for multi-machine builds')
.alias('r', 'repo')
.describe('r', 'The URL of the repository to monitor. Defaults to the repo in the current directory')
.alias('j', 'jobs')
.describe('j', 'The number of concurrent jobs to run. Defaults to 2')
.alias('h', 'help')
.epilog(`
Some useful environment variables:
GITHUB_ENTERPRISE_URL - the GitHub Enterprise URL to use instead of .com.
GITHUB_TOKEN - the GitHub (.com or Enterprise) API token to use. Must be provided.`);
const argv = yargs.argv;
async function main(testServer=null, testRepo=null, testCmdWithArgs=null) {
let cmdWithArgs = testCmdWithArgs || argv._;
let repo = testRepo || argv.r;
let server = testServer || argv.s;
if (cmdWithArgs.length < 1) {
console.log("Command to run not specified, defaulting to 'surf-build'");
cmdWithArgs = ['surf-build', '-n', 'surf'];
}
if (!repo) {
try {
repo = getSanitizedRepoUrl(await getOriginForRepo('.'));
console.error(`Repository not specified, using current directory: ${repo}`);
} catch (e) {
console.error("Repository not specified and current directory is not a Git repo");
d(e.stack);
yargs.showHelp();
process.exit(-1);
}
}
if (!server) {
console.error(`
**** Becoming a Surf Server ****
If you're only setting up a single build client, this is probably what you want.
If you're setting up more than one, you'll want to run 'surf-server' somewhere,
then pass '-s' to all of your build clients.`);
let nwo = getNwoFromRepoUrl(repo);
createRefServer([nwo]);
server = `http://localhost:${process.env.SURF_PORT || 3000}`;
}
let jobs = parseInt(argv.j || '2');
if (argv.j && (jobs < 1 || jobs > 64)) {
console.error("--jobs must be an integer");
yargs.showHelp();
process.exit(-1);
}
// Do an initial fetch to get our initial state
let refInfo = null;
let nwo = getNwoFromRepoUrl(repo);
let surfUrl = `${server}/info/${nwo}`;
const fetchRefs = async () => {
try {
return await request({
uri: surfUrl,
json: true
});
} catch (e) {
console.log(`Failed to fetch from ${surfUrl}: ${e.message}`);
d(e.stack);
process.exit(-1);
}
};
refInfo = await fetchRefs();
// TODO: figure out a way to trap Ctrl-C and dispose stop
console.log(`Watching ${repo}, will run '${cmdWithArgs.join(' ')}'\n`);
let buildMonitor = new BuildMonitor(cmdWithArgs, repo, jobs, fetchRefs, refInfo);
buildMonitor.start();
return new Promise(() => {});
}
main()
.catch((e) => {
console.error(e.message);
d(e.stack);
process.exit(-1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment