Permalink
Browse files

sethooks progress

  • Loading branch information...
1 parent 71d41c3 commit cee7e3f1ed9d8f1681d039774da222a1ca3fbe34 @arturadib committed Apr 7, 2012
Showing with 207 additions and 79 deletions.
  1. +9 −5 README.md
  2. +11 −6 bin/botio
  3. +1 −0 bootstrap/config.json
  4. +20 −63 lib/bootstrap.js
  5. +1 −2 lib/common.js
  6. +3 −3 lib/server.js
  7. +162 −0 lib/sethooks
View
@@ -40,24 +40,28 @@ Bot.io is written in Node.js and works on both Windows and Unix. It has been bat
## Getting started
-Bot.io depends on [Node.js](https://github.com/joyent/node) and `git`. To get started, create a new dir for your Botio files, bootstrap the necessary files (this also takes care of setting up the necessary Github hooks), and fire up the server:
+Bot.io depends on [Node.js](https://github.com/joyent/node) and `git`. To get started, create a new dir for your Botio files and bootstrap the necessary files for your repo:
```
$ npm install -g botio
$ mkdir botio-files; cd botio-files
-$ botio bootstrap --repo arturadib/pdf.js --user arturadib --pwd password123 --port 8877
-$ botio start --user arturadib --pwd password123
+$ botio bootstrap --repo arturadib/pdf.js
```
-(Of course, replace `--user arturadib` and `--repo arturadib/pdf.js` by your corresponding user and repo names, and set `--port` to the desired port number for the Botio server, making sure the port is open).
+The bootstrapped file `config.json` contains sensible defaults, but you will likely want to double-check and/or modify it at this point. (In particular, make sure `host`, `port`, and `whitelist` are correct). Then let Bot.io set up the necessary Github hooks, and start the server:
+
+```
+$ botio sethooks --user arturadib --pwd password123
+$ botio start --user arturadib --pwd password123
+```
That's it! You can now trigger your first Bot.io job by leaving the following comment on any pull request in your repo:
```
/botio test
```
-The bot should write back a hello world response in the PR discussion. At this point you will probably want to customize your bot scripts, as described below.
+The bot should write back a hello world response in the PR discussion. At this point you will probably want to customize your scripts, as described below.
View
@@ -13,20 +13,25 @@ program
.command('bootstrap')
.description('set up necessary Github hooks, create basic config files in current dir')
.option('-r, --repo <repo>', 'Github repo to watch, e.g. mozilla/pdf.js')
- .option('-o, --port <port>', 'port for the Botio server')
- .option('-u, --user <user>', 'user name of Github repo admin')
- .option('-p, --pwd <pwd>', 'password of Github repo admin')
.option('-f, --force', 'do not prompt before overwriting files')
.action(function(opts){
global.repo = opts.repo;
- global.port = opts.port;
- global.user = opts.user;
- global.pwd = opts.pwd;
global.force = opts.force;
require(__dirname+'/../lib/bootstrap.js');
});
program
+ .command('sethooks')
+ .description('set up necessary Github hooks, create basic config files in current dir')
+ .option('-u, --user <user>', 'user name of Github repo admin')
+ .option('-p, --pwd <pwd>', 'password of Github repo admin')
+ .action(function(opts){
+ global.user = opts.user;
+ global.pwd = opts.pwd;
+ require(__dirname+'/../lib/sethooks.js');
+ });
+
+program
.command('start')
.description('start the Bot.io server')
.option('-u, --user <user>', 'user name of Github repo admin')
View
@@ -3,5 +3,6 @@
"script_timeout": 3600,
"private_dir": "./private",
"public_dir": "./public",
+ "port": 8000,
"use_queue": true
}
View
@@ -8,8 +8,8 @@ var request = require('request'),
shell.silent();
-if (!global.repo || !global.user || !global.pwd || !global.port) {
- console.log('Please specify repo, user credentials, and port number. See --help.');
+if (!global.repo) {
+ console.log('Missing command line parameter. See --help.');
console.log();
process.exit(1);
}
@@ -20,7 +20,8 @@ if (!shell.which('git')) {
process.exit(1);
}
-var githubHooksUrl = 'https://'+global.user+':'+global.pwd+'@api.github.com/repos/'+global.repo+'/hooks';
+// Get params from globals
+config.repo = global.repo;
// Copy bootstrap/ files into current dir
bootstrap.copyFiles = function() {
@@ -56,8 +57,8 @@ bootstrap.copyFiles = function() {
}
console.log('OK');
- config = JSON.parse(shell.cat('./config.json'));
-
+ config = common.extend(config, JSON.parse(shell.cat('./config.json')));
+
// Next step
bootstrap.getIp();
}; // copyFiles()
@@ -72,22 +73,24 @@ bootstrap.getIp = function() {
console.log('Please try again.');
process.exit(1);
}
- console.log(config.ip = ip);
+ console.log(config.host = ip);
- bootstrap.checkHooks();
+ bootstrap.getWhitelist();
});
};
-// Make sure hooks are not already set
-bootstrap.checkHooks = function() {
- process.stdout.write('Getting existing Github hooks... ');
- request.get(githubHooksUrl, function(err, res, body) {
+// Get list of repo collaborators
+bootstrap.getWhitelist = function() {
+ var url = 'https://api.github.com/repos/'+config.repo+'/collaborators';
+
+ process.stdout.write('Getting list of repo collaborators (whitelist)... ');
+ request.get(url, function(err, res, body) {
if (err || res.statusCode !== 200) {
console.log('FAILED');
console.log();
if (res && res.statusCode === 404)
- console.log('Repo does not exist or repo access forbidden. You need admin privileges to setup Botio.');
+ console.log('Repo does not exist (404) or repo access forbidden.');
if (res && res.statusCode === 401)
console.log('Bad Github credentials');
@@ -96,67 +99,21 @@ bootstrap.checkHooks = function() {
}
console.log('OK');
- bootstrap.saveConfig();
+ config.whitelist = [];
- // Check if hooks already exist
body = JSON.parse(body);
- body.forEach(function(hook) {
- if (hook.active &&
- hook.name === 'web' &&
- hook.config && hook.config.url && hook.config.url.indexOf(config.ip) > -1) {
- console.log();
- console.log('Hooks already set up for this IP');
- process.exit(0);
- }
+ body.forEach(function(user) {
+ config.whitelist.push(user.login);
});
- // If here, we need new hooks
- bootstrap.setupHooks();
+ bootstrap.saveConfig();
});
};
// Save config to file
bootstrap.saveConfig = function() {
- config.repo = global.repo;
- config.port = global.port;
- config.whitelist = [global.user];
JSON.stringify(config, null, 2).to('config.json');
-}
-
-// Set up hooks
-bootstrap.setupHooks = function() {
- var payload = {
- name: 'web',
- active: true,
- events: [
- 'push',
- 'pull_request',
- 'issue_comment'
- ],
- config: {
- url: 'http://'+config.ip+':'+global.port,
- content_type: 'json'
- }
- };
-
- process.stdout.write('Setting up new hooks... ');
- request.post({url:githubHooksUrl, json:payload}, function(err, res, body) {
- if (err || res.statusCode !== 201) {
- console.log('FAILED');
- console.log();
-
- if (res && res.statusCode === 404)
- console.log('Repo does not exist or repo access forbidden');
-
- if (res && res.statusCode === 401)
- console.log('Bad Github credentials');
-
- process.exit(1);
- }
-
- console.log('OK');
- process.exit(0);
- }); // request.post
+ process.exit(0);
}
bootstrap.copyFiles();
View
@@ -43,8 +43,7 @@ exports.getConfig = function() {
// extend(target_obj, source_obj1 [, source_obj2 ...])
// Shallow extend, e.g.:
-// aux.extend({a:1}, {b:2}, {c:3})
-// returns {a:1, b:2, c:3}
+// extend({a:1}, {b:2}, {c:3}) returns: {a:1, b:2, c:3}
exports.extend = function(target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function(source) {
View
@@ -297,7 +297,7 @@ app.post('/', function(req, res) {
issue: payload.issue.number,
private_dir: path.resolve(config.private_dir+'/'+id),
public_dir: path.resolve(config.public_dir+'/'+id),
- public_url: 'http://'+config.ip+':'+config.port+'/'+id,
+ public_url: 'http://'+config.host+':'+config.port+'/'+id,
base_url: 'git://github.com/'+config.repo+'.git',
head_url: pull.head.repo.git_url,
head_ref: pull.head.ref,
@@ -350,7 +350,7 @@ app.post('/', function(req, res) {
issue: null,
private_dir: path.resolve(config.private_dir+'/'+id),
public_dir: path.resolve(config.public_dir+'/'+id),
- public_url: 'http://'+config.ip+':'+config.port+'/'+id,
+ public_url: 'http://'+config.host+':'+config.port+'/'+id,
base_url: null,
head_url: 'git://github.com/'+config.repo+'.git',
head_ref: 'master',
@@ -384,7 +384,7 @@ if (global.debug)
// Start things up
//
checkCredentials(function() {
- log('Starting server at:', config.ip+':'+config.port);
+ log('Starting server at:', config.host+':'+config.port);
app.listen(config.port);
});
Oops, something went wrong.

0 comments on commit cee7e3f

Please sign in to comment.