github-webhook 3.0.49 → 3.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/github-webhook.js +37 -35
- package/package.json +1 -1
- package/test.js +49 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## [3.0.51](https://github.com/rvagg/github-webhook/compare/v3.0.50...v3.0.51) (2026-11-27)
|
|
2
|
+
|
|
3
|
+
### Bug Fixes
|
|
4
|
+
|
|
5
|
+
* rerun queued rules with the newest event, preserve array exec ([#98](https://github.com/rvagg/github-webhook/issues/98)) ([098ed87](https://github.com/rvagg/github-webhook/commit/098ed877e0149ca5b63a37cef13266ee9998bb9f)), closes [#13](https://github.com/rvagg/github-webhook/issues/13)
|
|
6
|
+
|
|
7
|
+
## [3.0.50](https://github.com/rvagg/github-webhook/compare/v3.0.49...v3.0.50) (2026-11-02)
|
|
8
|
+
|
|
9
|
+
### Trivial Changes
|
|
10
|
+
|
|
11
|
+
* **deps:** bump the npm-minor-patch group with 2 updates ([#96](https://github.com/rvagg/github-webhook/issues/96)) ([c341feb](https://github.com/rvagg/github-webhook/commit/c341feb51386cdde1fd337867f9351cc0a856ab7))
|
|
12
|
+
|
|
1
13
|
## [3.0.49](https://github.com/rvagg/github-webhook/compare/v3.0.48...v3.0.49) (2026-11-02)
|
|
2
14
|
|
|
3
15
|
### Trivial Changes
|
package/github-webhook.js
CHANGED
|
@@ -205,51 +205,53 @@ function envFromPayload (payload, prefix, env) {
|
|
|
205
205
|
return env
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
-
function
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
rule.executing = true
|
|
216
|
-
|
|
217
|
-
const startTs = Date.now()
|
|
218
|
-
const eventStr = `event="${rule.event}", match="${rule.match}", exec="${rule.exec}"`
|
|
219
|
-
const exec = Array.isArray(rule.exec) ? rule.exec : ['sh', '-c', rule.exec]
|
|
208
|
+
function executeRule (logStream, rule, event) {
|
|
209
|
+
if (rule.executing === true) {
|
|
210
|
+
// coalesce runs requested while busy into one, using the newest event
|
|
211
|
+
rule.pending = event
|
|
212
|
+
return
|
|
213
|
+
}
|
|
220
214
|
|
|
221
|
-
|
|
215
|
+
rule.executing = true
|
|
222
216
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
217
|
+
const startTs = Date.now()
|
|
218
|
+
const eventStr = `event="${rule.event}", match="${rule.match}", exec="${rule.exec}"`
|
|
219
|
+
const [cmd, ...args] = Array.isArray(rule.exec) ? rule.exec : ['sh', '-c', rule.exec]
|
|
226
220
|
|
|
227
|
-
|
|
228
|
-
return eventsDebug('Error executing command [%s]: %s', rule.exec, err.message)
|
|
229
|
-
})
|
|
221
|
+
eventsDebug('Matched rule for %s', eventStr)
|
|
230
222
|
|
|
231
|
-
|
|
232
|
-
|
|
223
|
+
const cp = spawn(cmd, args, {
|
|
224
|
+
env: Object.assign(envFromPayload(event.payload, 'gh_'), process.env)
|
|
225
|
+
})
|
|
233
226
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
logStream.write('Took ' + (Date.now() - startTs) + ' ms\n')
|
|
238
|
-
}
|
|
227
|
+
cp.on('error', (err) => {
|
|
228
|
+
return eventsDebug('Error executing command [%s]: %s', rule.exec, err.message)
|
|
229
|
+
})
|
|
239
230
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
rule.queued = false
|
|
243
|
-
executeRule(rule) // do it again!
|
|
244
|
-
}
|
|
245
|
-
})
|
|
231
|
+
cp.on('close', (code) => {
|
|
232
|
+
eventsDebug('Executed command [%s] exited with [%d]', rule.exec, code)
|
|
246
233
|
|
|
247
234
|
if (logStream) {
|
|
248
|
-
|
|
249
|
-
|
|
235
|
+
logStream.write(eventStr + '\n')
|
|
236
|
+
logStream.write(new Date() + '\n')
|
|
237
|
+
logStream.write('Took ' + (Date.now() - startTs) + ' ms\n')
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
rule.executing = false
|
|
241
|
+
const pending = rule.pending
|
|
242
|
+
if (pending) {
|
|
243
|
+
rule.pending = null
|
|
244
|
+
executeRule(logStream, rule, pending)
|
|
250
245
|
}
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
if (logStream) {
|
|
249
|
+
prefixStream(cp.stdout, 'stdout: ').pipe(logStream, { end: false })
|
|
250
|
+
prefixStream(cp.stderr, 'stderr: ').pipe(logStream, { end: false })
|
|
251
251
|
}
|
|
252
|
+
}
|
|
252
253
|
|
|
254
|
+
function handleRules (logStream, rules, event) {
|
|
253
255
|
rules.forEach((rule) => {
|
|
254
256
|
if (rule.event !== '*' && rule.event !== event.event) {
|
|
255
257
|
return
|
|
@@ -259,7 +261,7 @@ function handleRules (logStream, rules, event) {
|
|
|
259
261
|
return
|
|
260
262
|
}
|
|
261
263
|
|
|
262
|
-
executeRule(rule)
|
|
264
|
+
executeRule(logStream, rule, event)
|
|
263
265
|
})
|
|
264
266
|
}
|
|
265
267
|
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs'
|
|
2
|
+
import os from 'node:os'
|
|
2
3
|
import path from 'node:path'
|
|
3
4
|
import crypto from 'node:crypto'
|
|
4
5
|
import { test } from 'node:test'
|
|
@@ -173,3 +174,51 @@ test('valid request triggers rule', { skip: process.platform === 'win32' }, asyn
|
|
|
173
174
|
// Cleanup
|
|
174
175
|
fs.unlinkSync(tmpfile)
|
|
175
176
|
})
|
|
177
|
+
|
|
178
|
+
async function waitForLines (file, count, timeout = 5000) {
|
|
179
|
+
const deadline = Date.now() + timeout
|
|
180
|
+
for (;;) {
|
|
181
|
+
const lines = fs.existsSync(file) ? fs.readFileSync(file, 'utf8').split('\n').filter(Boolean) : []
|
|
182
|
+
if (lines.length >= count || Date.now() > deadline) {
|
|
183
|
+
return lines
|
|
184
|
+
}
|
|
185
|
+
await new Promise((resolve) => setTimeout(resolve, 20))
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
test('events arriving during a run coalesce into one rerun with the newest payload', { skip: process.platform === 'win32' }, async (t) => {
|
|
190
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-webhook-'))
|
|
191
|
+
t.after(() => fs.rmSync(dir, { recursive: true }))
|
|
192
|
+
const out = path.join(dir, 'out')
|
|
193
|
+
const server = webhook({
|
|
194
|
+
port: 0,
|
|
195
|
+
path: '/webhook',
|
|
196
|
+
secret: 'foofaa',
|
|
197
|
+
rules: [{ event: 'push', match: 'true', exec: `sleep 0.2; echo "$gh_n" >> ${out}` }]
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
for (const n of [1, 2, 3]) {
|
|
201
|
+
server.webhookHandler.emit('*', { event: 'push', payload: { n } })
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
assert.deepStrictEqual(await waitForLines(out, 2), ['1', '3'])
|
|
205
|
+
await new Promise((resolve) => setTimeout(resolve, 400))
|
|
206
|
+
assert.deepStrictEqual(await waitForLines(out, 3, 0), ['1', '3'])
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
test('array exec rule runs repeatedly', { skip: process.platform === 'win32' }, async (t) => {
|
|
210
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-webhook-'))
|
|
211
|
+
t.after(() => fs.rmSync(dir, { recursive: true }))
|
|
212
|
+
const out = path.join(dir, 'out')
|
|
213
|
+
const server = webhook({
|
|
214
|
+
port: 0,
|
|
215
|
+
path: '/webhook',
|
|
216
|
+
secret: 'foofaa',
|
|
217
|
+
rules: [{ event: 'push', match: 'true', exec: ['sh', '-c', `echo "$gh_n" >> ${out}`] }]
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
server.webhookHandler.emit('*', { event: 'push', payload: { n: 1 } })
|
|
221
|
+
assert.deepStrictEqual(await waitForLines(out, 1), ['1'])
|
|
222
|
+
server.webhookHandler.emit('*', { event: 'push', payload: { n: 2 } })
|
|
223
|
+
assert.deepStrictEqual(await waitForLines(out, 2), ['1', '2'])
|
|
224
|
+
})
|