mirror of
https://github.com/zhigang1992/probot.git
synced 2026-06-15 02:18:58 +08:00
Merge remote-tracking branch 'origin/master' into greenkeeper/initial
* origin/master: Remove workflow demo Stick with PRIVATE_KEY Look for private-key.pem Default to short log output probot run Update plugin docs Still support PRIVATE_KEY env var Use commander for CLI
This commit is contained in:
7
bin/probot
Executable file
7
bin/probot
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('commander')
|
||||
.version(require('../package').version)
|
||||
.usage('<command> [options]')
|
||||
.command('run', 'run the bot')
|
||||
.parse(process.argv)
|
||||
59
bin/probot-run
Executable file
59
bin/probot-run
Executable file
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('dotenv').config();
|
||||
|
||||
const program = require('commander');
|
||||
|
||||
program
|
||||
.usage('[options] <plugins...>')
|
||||
.option('-i, --integration <id>', 'ID of the GitHub Integration', process.env.INTEGRATION_ID)
|
||||
.option('-s, --secret <secret>', 'Webhook secret of the GitHub Integration', process.env.WEBHOOK_SECRET || 'development')
|
||||
.option('-p, --port <n>', 'Port to start the server on', process.env.PORT || 3000)
|
||||
.option('-P, --private-key <file>', 'Path to certificate of the GitHub Integration', path => {
|
||||
try {
|
||||
return require('fs').readFileSync(path);
|
||||
} catch (err) {
|
||||
console.warn(err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}, process.env.PRIVATE_KEY)
|
||||
.parse(process.argv);
|
||||
|
||||
if(!program.integration) {
|
||||
console.warn("Missing GitHub Integration ID.\nUse --integration flag or set INTEGRATION_ID environment variable.");
|
||||
program.help();
|
||||
}
|
||||
|
||||
if(!program.privateKey) {
|
||||
try {
|
||||
program.privateKey = require('fs').readFileSync('private-key.pem');
|
||||
} catch (err) {
|
||||
console.warn("Missing GitHub Integration private key.\nUse --private-key flag or set PRIVATE_KEY environment variable.");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const pkgConf = require('pkg-conf');
|
||||
const resolve = require('resolve').sync;
|
||||
const createProbot = require('../');
|
||||
|
||||
const probot = createProbot({
|
||||
id: program.integration,
|
||||
secret: program.secret,
|
||||
cert: program.privateKey,
|
||||
port: program.port
|
||||
});
|
||||
|
||||
pkgConf('probot').then(pkg => {
|
||||
program.args.concat(pkg.plugins || []).map(plugin => {
|
||||
try {
|
||||
const path = resolve(plugin, {basedir: process.cwd()});
|
||||
probot.load(require(path))
|
||||
} catch(err) {
|
||||
console.warn(err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
probot.start();
|
||||
});
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
try {
|
||||
require('dotenv-safe').load();
|
||||
} catch (err) {
|
||||
console.log(err.message);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const resolve = require('resolve');
|
||||
const pkgConf = require('pkg-conf');
|
||||
const probot = require('../index.js');
|
||||
|
||||
function loadPlugins(plugins) {
|
||||
plugins.forEach(plugin => {
|
||||
resolve(plugin, {basedir: process.cwd()}, (err, path) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
} else {
|
||||
probot.log.debug('loading plugin %s', path);
|
||||
require(path)(probot);
|
||||
return path;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
loadPlugins(process.argv.slice(2));
|
||||
pkgConf('probot').then(pkg => loadPlugins(pkg.plugins || []));
|
||||
19
docs/demo.js
19
docs/demo.js
@@ -1,19 +0,0 @@
|
||||
// Delete :+1: comments
|
||||
const singleEmoji = /^\W*(:[\w-\+]+:|[\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])\W*$/g;
|
||||
on('issue_comment.created')
|
||||
.filter(event => event.payload.comment.body.match(singleEmoji))
|
||||
.deleteComment();
|
||||
|
||||
function isDeleted(event) {
|
||||
return event.payload.action === 'deleted' &&
|
||||
event.payload.sender.type !== 'Bot';
|
||||
}
|
||||
|
||||
// restore deleted comments but the one deleted by PRobot
|
||||
on('issue_comment', 'commit_comment', 'pull_request_review_comment')
|
||||
.filter(isDeleted)
|
||||
.comment(`
|
||||
Deleted comment from @{{ comment.user.login }} at {{ comment.updated_at }}
|
||||
---
|
||||
{{ comment.body }}
|
||||
`);
|
||||
@@ -89,7 +89,7 @@ TODO: link to docs on getting running in development
|
||||
```
|
||||
$ npm install -g https://github.com/probot/probot
|
||||
|
||||
$ probot autoresponder.js
|
||||
$ probot run ./autoresponder.js
|
||||
Listening on http://localhost:3000
|
||||
```
|
||||
|
||||
|
||||
52
index.js
52
index.js
@@ -1,33 +1,39 @@
|
||||
const fs = require('fs');
|
||||
const createWebhook = require('github-webhook-handler');
|
||||
const createIntegration = require('github-integration');
|
||||
|
||||
const createRobot = require('./lib/robot');
|
||||
const createServer = require('./lib/server');
|
||||
|
||||
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET || 'development';
|
||||
const PORT = process.env.PORT || 3000;
|
||||
module.exports = options => {
|
||||
const webhook = createWebhook({path: '/', secret: options.secret});
|
||||
const integration = createIntegration({
|
||||
id: options.id,
|
||||
cert: options.cert,
|
||||
debug: process.env.LOG_LEVEL === 'trace'
|
||||
});
|
||||
const server = createServer(webhook);
|
||||
const robot = createRobot(integration, webhook);
|
||||
|
||||
const webhook = createWebhook({path: '/', secret: WEBHOOK_SECRET});
|
||||
const integration = createIntegration({
|
||||
id: process.env.INTEGRATION_ID,
|
||||
cert: process.env.PRIVATE_KEY || fs.readFileSync('private-key.pem'),
|
||||
debug: process.env.LOG_LEVEL === 'trace'
|
||||
});
|
||||
const server = createServer(webhook);
|
||||
const robot = createRobot(integration, webhook);
|
||||
// Show trace for any unhandled rejections
|
||||
process.on('unhandledRejection', reason => {
|
||||
robot.log.error(reason);
|
||||
});
|
||||
|
||||
server.listen(PORT);
|
||||
// Handle case when webhook creation fails
|
||||
webhook.on('error', err => {
|
||||
robot.log.error(err);
|
||||
});
|
||||
|
||||
console.log('Listening on http://localhost:' + PORT);
|
||||
return {
|
||||
server,
|
||||
robot,
|
||||
|
||||
// Show trace for any unhandled rejections
|
||||
process.on('unhandledRejection', reason => {
|
||||
robot.log.error(reason);
|
||||
});
|
||||
start() {
|
||||
server.listen(options.port);
|
||||
console.log('Listening on http://localhost:' + options.port);
|
||||
},
|
||||
|
||||
// Handle case when webhook creation fails
|
||||
webhook.on('error', err => {
|
||||
robot.log.error(err);
|
||||
});
|
||||
module.exports = robot;
|
||||
load(plugin) {
|
||||
plugin(robot);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
const bunyan = require('bunyan');
|
||||
const bunyanFormat = require('bunyan-format');
|
||||
const Context = require('./context');
|
||||
|
||||
const logger = bunyan.createLogger({
|
||||
name: 'PRobot',
|
||||
level: process.env.LOG_LEVEL || 'debug'
|
||||
level: process.env.LOG_LEVEL || 'debug',
|
||||
stream: bunyanFormat({outputMode: process.env.LOG_FORMAT || 'short'})
|
||||
});
|
||||
|
||||
class Robot {
|
||||
|
||||
@@ -5,17 +5,19 @@
|
||||
"repository": "https://github.com/probot/probot",
|
||||
"main": "index.js",
|
||||
"bin": {
|
||||
"probot": "./bin/probot.js"
|
||||
"probot": "./bin/probot"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "probot",
|
||||
"start": "probot run",
|
||||
"test": "mocha && xo"
|
||||
},
|
||||
"author": "Brandon Keepers",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"bunyan": "^1.8.5",
|
||||
"dotenv-safe": "^4.0.3",
|
||||
"bunyan-format": "^0.2.1",
|
||||
"commander": "^2.9.0",
|
||||
"dotenv": "~4.0.0",
|
||||
"github-integration": "^1.0.0",
|
||||
"github-webhook-handler": "^0.6.0",
|
||||
"pkg-conf": "^2.0.0",
|
||||
|
||||
Reference in New Issue
Block a user