From 44dcd194c1559e3f2a6117c4aaecb6aef651b574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Fri, 27 Sep 2019 09:46:17 +0200 Subject: [PATCH 1/3] Add report tool --- README.md | 3 ++- github-webhook.js | 24 ++++++++++++++++++------ package.json | 1 + 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index de3b4af..43ead50 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ You can also specify a `--config ` where *file* is a JSON file containing "rules": [{ "event": "push", "match": "ref == \"refs/heads/master\" && repository.name == \"myrepo\"", - "exec": "echo yay!" + "exec": "echo yay!", + "report": "echo \"${gh_report}\" | mail -s 'Deployed ${gh_repository_name}' \"${gh_pusher_name} <${gh_pusher_email}>\"" }] } ``` diff --git a/github-webhook.js b/github-webhook.js index 98c6d73..fe2a15d 100755 --- a/github-webhook.js +++ b/github-webhook.js @@ -2,12 +2,14 @@ const http = require('http') const fs = require('fs') -const spawn = require('child_process').spawn +const Child = require('child_process') +const PassThrough = require('stream').PassThrough const createHandler = require('github-webhook-handler') const debug = require('debug') const matchme = require('matchme') const split2 = require('split2') const through2 = require('through2') +const getStream = require('get-stream') const argv = require('minimist')(process.argv.slice(2)) const serverDebug = debug('github-webhook:server') const eventsDebug = debug('github-webhook:events') @@ -200,12 +202,14 @@ function handleRules (logStream, rules, event) { const startTs = Date.now() const eventStr = `event="${rule.event}", match="${rule.match}", exec="${rule.exec}"` const exec = Array.isArray(rule.exec) ? rule.exec : ['sh', '-c', rule.exec] + const past = new PassThrough() eventsDebug('Matched rule for %s', eventStr) - - const cp = spawn(exec.shift(), exec, { + const childOpts = { env: Object.assign(envFromPayload(event.payload, 'gh_'), process.env) - }) + } + + const cp = Child.spawn(exec.shift(), exec, childOpts) cp.on('error', (err) => { return eventsDebug('Error executing command [%s]: %s', rule.exec, err.message) @@ -227,10 +231,18 @@ function handleRules (logStream, rules, event) { } }) + cp.stdout.pipe(past) + prefixStream(cp.stderr, '! ').pipe(past) + if (logStream) { - prefixStream(cp.stdout, 'stdout: ').pipe(logStream, { end: false }) - prefixStream(cp.stderr, 'stderr: ').pipe(logStream, { end: false }) + past.pipe(logStream, {end: false}) } + if (rule.report) getStream(past).then(function(str) { + childOpts.env.gh_report = str; + Child.exec(rule.report, childOpts, function(err) { + if (err) eventsDebug('Error executing report [%s]: %s', rule.report, err.message) + }) + }) } rules.forEach((rule) => { diff --git a/package.json b/package.json index c7c74e1..5ff7461 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "debug": "~4.1.1", + "get-stream": "^5.1.0", "github-webhook-handler": "~1.0.0", "matchme": "~2.0.1", "minimist": "~1.1.1", From 13168a88a5f1aa810b1a9fe9abd6bf4ac0f4bce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Mon, 30 Sep 2019 09:31:24 +0200 Subject: [PATCH 2/3] Style fixes, use bl instead of get-stream --- github-webhook.js | 28 ++++++++++++++++++---------- package.json | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) mode change 100755 => 100644 github-webhook.js diff --git a/github-webhook.js b/github-webhook.js old mode 100755 new mode 100644 index fe2a15d..ec8d02b --- a/github-webhook.js +++ b/github-webhook.js @@ -2,14 +2,14 @@ const http = require('http') const fs = require('fs') -const Child = require('child_process') +const childProcess = require('child_process') const PassThrough = require('stream').PassThrough const createHandler = require('github-webhook-handler') const debug = require('debug') const matchme = require('matchme') const split2 = require('split2') const through2 = require('through2') -const getStream = require('get-stream') +const bl = require('bl') const argv = require('minimist')(process.argv.slice(2)) const serverDebug = debug('github-webhook:server') const eventsDebug = debug('github-webhook:events') @@ -209,7 +209,7 @@ function handleRules (logStream, rules, event) { env: Object.assign(envFromPayload(event.payload, 'gh_'), process.env) } - const cp = Child.spawn(exec.shift(), exec, childOpts) + const cp = childProcess.spawn(exec.shift(), exec, childOpts) cp.on('error', (err) => { return eventsDebug('Error executing command [%s]: %s', rule.exec, err.message) @@ -235,14 +235,22 @@ function handleRules (logStream, rules, event) { prefixStream(cp.stderr, '! ').pipe(past) if (logStream) { - past.pipe(logStream, {end: false}) + past.pipe(logStream, { end: false }) + } + if (rule.report) { + past.pipe(bl((err, data) => { + if (err) { + console.error('Cannot buffer executed command output', err) + } else { + childOpts.env.gh_report = data.toString() + childProcess.exec(rule.report, childOpts, function (err) { + if (err) { + eventsDebug('Error executing report [%s]: %s', rule.report, err.message) + } + }) + } + })) } - if (rule.report) getStream(past).then(function(str) { - childOpts.env.gh_report = str; - Child.exec(rule.report, childOpts, function(err) { - if (err) eventsDebug('Error executing report [%s]: %s', rule.report, err.message) - }) - }) } rules.forEach((rule) => { diff --git a/package.json b/package.json index 5ff7461..4a314f4 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,8 @@ "tape": "~4.11.0" }, "dependencies": { + "bl": "~4.0.0", "debug": "~4.1.1", - "get-stream": "^5.1.0", "github-webhook-handler": "~1.0.0", "matchme": "~2.0.1", "minimist": "~1.1.1", From 6c2644dbcb50c7488fba4e6d67d24790bf156452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Lal?= Date: Mon, 30 Sep 2019 09:32:08 +0200 Subject: [PATCH 3/3] Pipe only if needed --- github-webhook.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) mode change 100644 => 100755 github-webhook.js diff --git a/github-webhook.js b/github-webhook.js old mode 100644 new mode 100755 index ec8d02b..8d9d311 --- a/github-webhook.js +++ b/github-webhook.js @@ -231,8 +231,10 @@ function handleRules (logStream, rules, event) { } }) - cp.stdout.pipe(past) - prefixStream(cp.stderr, '! ').pipe(past) + if (logStream || rule.report) { + cp.stdout.pipe(past) + prefixStream(cp.stderr, '! ').pipe(past) + } if (logStream) { past.pipe(logStream, { end: false })