github-webhook 2.0.2 → 3.0.0
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/.github/dependabot.yml +20 -0
- package/.github/workflows/test-and-release.yml +46 -0
- package/CHANGELOG.md +10 -0
- package/README.md +192 -29
- package/github-webhook.js +33 -25
- package/package.json +103 -11
- package/test.js +122 -80
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: 'github-actions'
|
|
4
|
+
directory: '/'
|
|
5
|
+
schedule:
|
|
6
|
+
interval: 'weekly'
|
|
7
|
+
commit-message:
|
|
8
|
+
prefix: 'chore'
|
|
9
|
+
include: 'scope'
|
|
10
|
+
cooldown:
|
|
11
|
+
default-days: 5
|
|
12
|
+
- package-ecosystem: 'npm'
|
|
13
|
+
directory: '/'
|
|
14
|
+
schedule:
|
|
15
|
+
interval: 'weekly'
|
|
16
|
+
commit-message:
|
|
17
|
+
prefix: 'chore'
|
|
18
|
+
include: 'scope'
|
|
19
|
+
cooldown:
|
|
20
|
+
default-days: 5
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: Test & Maybe Release
|
|
2
|
+
on: [push, pull_request]
|
|
3
|
+
jobs:
|
|
4
|
+
test:
|
|
5
|
+
strategy:
|
|
6
|
+
fail-fast: false
|
|
7
|
+
matrix:
|
|
8
|
+
node: [lts/*, current]
|
|
9
|
+
os: [macos-latest, ubuntu-latest, windows-latest]
|
|
10
|
+
runs-on: ${{ matrix.os }}
|
|
11
|
+
steps:
|
|
12
|
+
- name: Checkout Repository
|
|
13
|
+
uses: actions/checkout@v6
|
|
14
|
+
- name: Use Node.js ${{ matrix.node }}
|
|
15
|
+
uses: actions/setup-node@v6
|
|
16
|
+
with:
|
|
17
|
+
node-version: ${{ matrix.node }}
|
|
18
|
+
- name: Install Dependencies
|
|
19
|
+
run: npm install --no-progress
|
|
20
|
+
- name: Run tests
|
|
21
|
+
run: npm test
|
|
22
|
+
release:
|
|
23
|
+
name: Release
|
|
24
|
+
permissions:
|
|
25
|
+
contents: write
|
|
26
|
+
id-token: write
|
|
27
|
+
needs: test
|
|
28
|
+
runs-on: ubuntu-latest
|
|
29
|
+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
|
|
30
|
+
steps:
|
|
31
|
+
- name: Checkout
|
|
32
|
+
uses: actions/checkout@v6
|
|
33
|
+
with:
|
|
34
|
+
fetch-depth: 0
|
|
35
|
+
- name: Setup Node.js
|
|
36
|
+
uses: actions/setup-node@v6
|
|
37
|
+
with:
|
|
38
|
+
node-version: lts/*
|
|
39
|
+
registry-url: 'https://registry.npmjs.org'
|
|
40
|
+
- name: Install dependencies
|
|
41
|
+
run: npm install --no-progress
|
|
42
|
+
- name: Release
|
|
43
|
+
env:
|
|
44
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
45
|
+
NPM_CONFIG_PROVENANCE: true
|
|
46
|
+
run: npx semantic-release
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
## [3.0.0](https://github.com/rvagg/github-webhook/compare/v2.0.2...v3.0.0) (2026-01-24)
|
|
2
|
+
|
|
3
|
+
### ⚠ BREAKING CHANGES
|
|
4
|
+
|
|
5
|
+
* Requires Node.js 20 or later. Uses ESM imports
|
|
6
|
+
instead of CommonJS require().
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* modernise to ESM with Node.js 20+ ([#22](https://github.com/rvagg/github-webhook/issues/22)) ([09b04ad](https://github.com/rvagg/github-webhook/commit/09b04ad8db951627fc55f865558d887b57435c5e))
|
package/README.md
CHANGED
|
@@ -1,23 +1,44 @@
|
|
|
1
1
|
# github-webhook
|
|
2
2
|
|
|
3
|
-
[](https://nodei.co/npm/github-webhook/)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A lightweight server that listens for GitHub Webhook events and executes commands when specific conditions are matched. Useful for automated deployments, CI triggers, and other GitHub-driven workflows.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Installation
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
```sh
|
|
10
|
+
npm install -g github-webhook
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
github-webhook \
|
|
17
|
+
--port=9999 \
|
|
18
|
+
--path=/webhook \
|
|
19
|
+
--secret=mygithubsecret \
|
|
20
|
+
--rule='push:ref == "refs/heads/master" && repository.name == "myrepo":./deploy.sh'
|
|
21
|
+
```
|
|
10
22
|
|
|
11
|
-
|
|
23
|
+
Then configure your GitHub repository's webhook settings to point to `http://yourserver:9999/webhook` with the matching secret.
|
|
24
|
+
|
|
25
|
+
## Configuration
|
|
26
|
+
|
|
27
|
+
You can configure github-webhook via command-line arguments or a JSON config file (or both - command-line overrides the config file).
|
|
28
|
+
|
|
29
|
+
### Command-line
|
|
30
|
+
|
|
31
|
+
```sh
|
|
12
32
|
github-webhook \
|
|
33
|
+
--config=/etc/github-webhook.json \
|
|
13
34
|
--port=9999 \
|
|
14
35
|
--path=/webhook \
|
|
15
36
|
--secret=mygithubsecret \
|
|
16
37
|
--log=/var/log/webhook.log \
|
|
17
|
-
--rule='push:ref == refs/heads/master
|
|
38
|
+
--rule='push:ref == "refs/heads/master":./deploy.sh'
|
|
18
39
|
```
|
|
19
40
|
|
|
20
|
-
|
|
41
|
+
### Config file
|
|
21
42
|
|
|
22
43
|
```json
|
|
23
44
|
{
|
|
@@ -25,40 +46,182 @@ You can also specify a `--config <file>` where *file* is a JSON file containing
|
|
|
25
46
|
"path": "/webhook",
|
|
26
47
|
"secret": "mygithubsecret",
|
|
27
48
|
"log": "/var/log/webhook.log",
|
|
28
|
-
"rules": [
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
49
|
+
"rules": [
|
|
50
|
+
{
|
|
51
|
+
"event": "push",
|
|
52
|
+
"match": "ref == \"refs/heads/master\" && repository.full_name == \"myuser/myrepo\"",
|
|
53
|
+
"exec": "/var/www/deploy.sh"
|
|
54
|
+
}
|
|
55
|
+
]
|
|
33
56
|
}
|
|
34
57
|
```
|
|
35
58
|
|
|
36
|
-
|
|
59
|
+
### Options
|
|
37
60
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
61
|
+
| Option | Required | Description |
|
|
62
|
+
|--------|----------|-------------|
|
|
63
|
+
| `port` | Yes | Port to listen on (also respects `PORT` env var) |
|
|
64
|
+
| `path` | Yes | URL path to receive webhooks (e.g., `/webhook`) |
|
|
65
|
+
| `secret` | Yes | Webhook secret for payload verification (configure the same value in GitHub) |
|
|
66
|
+
| `host` | No | Bind to a specific host/IP address |
|
|
67
|
+
| `log` | No | Log file path, or `stdout`/`stderr` for console output |
|
|
68
|
+
| `rules` | No | Array of rules to match and execute (see below) |
|
|
44
69
|
|
|
45
|
-
|
|
70
|
+
## Rules
|
|
46
71
|
|
|
47
|
-
|
|
72
|
+
Rules define what commands to execute when specific webhook events are received.
|
|
48
73
|
|
|
49
|
-
|
|
50
|
-
* `"match"`: a basic object matching rule that will be applied against the payload received from GitHub. Should be flexible enough to match very specific parts of the PayLoad. See [matchme](https://github.com/DamonOehlman/matchme) for how this works.
|
|
51
|
-
* `"exec"`: a system command to execute if this rule is matched, should obviously be something related to the event, perhaps a deploy on `"push"` events? **Note**: if you provide a string it will be run with `sh -c "<string>"` (unlikely to be Windows-friendly), however if you provide an array of strings then the first element will be executed with the remaining elements as its arguments.
|
|
74
|
+
### Rule properties
|
|
52
75
|
|
|
53
|
-
|
|
76
|
+
| Property | Description |
|
|
77
|
+
|----------|-------------|
|
|
78
|
+
| `event` | GitHub event type to match (`push`, `pull_request`, `issues`, etc.). Use `*` to match all events. See [GitHub Webhooks documentation](https://docs.github.com/en/webhooks) for available events. |
|
|
79
|
+
| `match` | Expression to match against the webhook payload. Uses [matchme](https://github.com/DamonOehlman/matchme) syntax. |
|
|
80
|
+
| `exec` | Command to execute. A string runs via `sh -c "..."`. An array executes directly (first element is the command, rest are arguments). |
|
|
81
|
+
|
|
82
|
+
### Command-line rule syntax
|
|
83
|
+
|
|
84
|
+
Rules can be specified on the command line as `--rule 'event:match:exec'`:
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
--rule 'push:ref == "refs/heads/master":./deploy.sh'
|
|
88
|
+
```
|
|
54
89
|
|
|
55
|
-
|
|
90
|
+
### Environment variables
|
|
56
91
|
|
|
57
|
-
|
|
92
|
+
Commands receive the entire webhook payload as environment variables with a `gh_` prefix:
|
|
93
|
+
|
|
94
|
+
- `gh_ref` - The git ref (e.g., `refs/heads/master`)
|
|
95
|
+
- `gh_branch` - The branch name (extracted from ref for convenience)
|
|
96
|
+
- `gh_repository_name` - Repository name
|
|
97
|
+
- `gh_repository_full_name` - Full repository name (e.g., `owner/repo`)
|
|
98
|
+
- `gh_sender_login` - Username of the person who triggered the event
|
|
99
|
+
- ... and all other payload fields
|
|
100
|
+
|
|
101
|
+
Nested objects become underscore-separated (e.g., `gh_repository_owner_login`).
|
|
102
|
+
|
|
103
|
+
### Example rules
|
|
104
|
+
|
|
105
|
+
Deploy on push to master:
|
|
106
|
+
```json
|
|
107
|
+
{
|
|
108
|
+
"event": "push",
|
|
109
|
+
"match": "ref == \"refs/heads/master\" && repository.full_name == \"myuser/myrepo\"",
|
|
110
|
+
"exec": "/var/www/deploy.sh"
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Run tests on pull request:
|
|
115
|
+
```json
|
|
116
|
+
{
|
|
117
|
+
"event": "pull_request",
|
|
118
|
+
"match": "action == \"opened\" || action == \"synchronize\"",
|
|
119
|
+
"exec": ["./run-tests.sh", "--pr"]
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Log all events:
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"event": "*",
|
|
127
|
+
"match": "true",
|
|
128
|
+
"exec": "echo \"Received $gh_event\" >> /var/log/webhooks.log"
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Running as a Service
|
|
133
|
+
|
|
134
|
+
### systemd (recommended)
|
|
135
|
+
|
|
136
|
+
Create `/etc/systemd/system/github-webhook.service`:
|
|
137
|
+
|
|
138
|
+
```ini
|
|
139
|
+
[Unit]
|
|
140
|
+
Description=GitHub Webhook Server
|
|
141
|
+
After=network.target
|
|
142
|
+
|
|
143
|
+
[Service]
|
|
144
|
+
Type=simple
|
|
145
|
+
User=www-data
|
|
146
|
+
ExecStart=/usr/bin/github-webhook --config /etc/github-webhook.json
|
|
147
|
+
Restart=on-failure
|
|
148
|
+
RestartSec=5
|
|
149
|
+
|
|
150
|
+
[Install]
|
|
151
|
+
WantedBy=multi-user.target
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Then enable and start:
|
|
155
|
+
|
|
156
|
+
```sh
|
|
157
|
+
sudo systemctl daemon-reload
|
|
158
|
+
sudo systemctl enable github-webhook
|
|
159
|
+
sudo systemctl start github-webhook
|
|
160
|
+
sudo systemctl status github-webhook
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
View logs:
|
|
164
|
+
|
|
165
|
+
```sh
|
|
166
|
+
sudo journalctl -u github-webhook -f
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Running behind a reverse proxy
|
|
170
|
+
|
|
171
|
+
For production, run github-webhook behind nginx or another reverse proxy that handles TLS:
|
|
172
|
+
|
|
173
|
+
```nginx
|
|
174
|
+
location /webhook {
|
|
175
|
+
proxy_pass http://127.0.0.1:9999;
|
|
176
|
+
proxy_set_header Host $host;
|
|
177
|
+
proxy_set_header X-Real-IP $remote_addr;
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Then configure github-webhook with `--host=127.0.0.1` to only accept local connections.
|
|
182
|
+
|
|
183
|
+
## Programmatic Usage
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
import webhook from 'github-webhook'
|
|
187
|
+
|
|
188
|
+
const server = webhook({
|
|
189
|
+
port: 9999,
|
|
190
|
+
path: '/webhook',
|
|
191
|
+
secret: 'mygithubsecret',
|
|
192
|
+
rules: [
|
|
193
|
+
{
|
|
194
|
+
event: 'push',
|
|
195
|
+
match: 'ref == "refs/heads/master"',
|
|
196
|
+
exec: './deploy.sh'
|
|
197
|
+
}
|
|
198
|
+
]
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
server.listen(9999, () => {
|
|
202
|
+
console.log('Webhook server listening on port 9999')
|
|
203
|
+
})
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
The function returns an `http.Server` instance with an attached `webhookHandler` for custom event handling:
|
|
207
|
+
|
|
208
|
+
```js
|
|
209
|
+
server.webhookHandler.on('push', (event) => {
|
|
210
|
+
console.log('Push event:', event.payload)
|
|
211
|
+
})
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Debugging
|
|
215
|
+
|
|
216
|
+
Enable debug output with the `DEBUG` environment variable:
|
|
217
|
+
|
|
218
|
+
```sh
|
|
219
|
+
DEBUG=github-webhook:* github-webhook --config /etc/github-webhook.json
|
|
220
|
+
```
|
|
58
221
|
|
|
59
|
-
## More
|
|
222
|
+
## More Information
|
|
60
223
|
|
|
61
|
-
**github-webhook** is powered by [github-webhook-handler](https://github.com/rvagg/github-webhook-handler)
|
|
224
|
+
**github-webhook** is powered by [github-webhook-handler](https://github.com/rvagg/github-webhook-handler).
|
|
62
225
|
|
|
63
226
|
## License
|
|
64
227
|
|
package/github-webhook.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
import http from 'node:http'
|
|
4
|
+
import fs from 'node:fs'
|
|
5
|
+
import { spawn } from 'node:child_process'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
import createHandler from 'github-webhook-handler'
|
|
8
|
+
import debug from 'debug'
|
|
9
|
+
import matchme from 'matchme'
|
|
10
|
+
import split2 from 'split2'
|
|
11
|
+
import through2 from 'through2'
|
|
12
|
+
import minimist from 'minimist'
|
|
13
|
+
|
|
12
14
|
const serverDebug = debug('github-webhook:server')
|
|
13
15
|
const eventsDebug = debug('github-webhook:events')
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
const isMain = process.argv[1] === fileURLToPath(import.meta.url)
|
|
18
|
+
|
|
19
|
+
if (isMain) {
|
|
20
|
+
const argv = minimist(process.argv.slice(2))
|
|
16
21
|
let config = {}
|
|
17
22
|
|
|
18
23
|
if (typeof argv.config === 'string') {
|
|
@@ -71,7 +76,7 @@ function collectRules (rules) {
|
|
|
71
76
|
return rules.map((rule) => {
|
|
72
77
|
let c = rule.indexOf(':')
|
|
73
78
|
if (c < 0) {
|
|
74
|
-
return
|
|
79
|
+
return null
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
const event = rule.substring(0, c)
|
|
@@ -79,17 +84,17 @@ function collectRules (rules) {
|
|
|
79
84
|
rule = rule.substring(c + 1)
|
|
80
85
|
c = rule.indexOf(':')
|
|
81
86
|
if (c < 0) {
|
|
82
|
-
return
|
|
87
|
+
return null
|
|
83
88
|
}
|
|
84
89
|
|
|
85
90
|
const match = rule.substring(0, c)
|
|
86
91
|
const exec = rule.substring(c + 1)
|
|
87
92
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
match: match,
|
|
91
|
-
exec: exec
|
|
93
|
+
if (!event || !match || !exec) {
|
|
94
|
+
return null
|
|
92
95
|
}
|
|
96
|
+
|
|
97
|
+
return { event, match, exec }
|
|
93
98
|
}).filter(Boolean)
|
|
94
99
|
}
|
|
95
100
|
|
|
@@ -119,13 +124,17 @@ function createServer (options) {
|
|
|
119
124
|
|
|
120
125
|
handler(req, res, (err) => {
|
|
121
126
|
function response (code, msg) {
|
|
127
|
+
if (res.headersSent) {
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
122
131
|
const address = req.socket.address()
|
|
123
132
|
|
|
124
|
-
serverDebug('Response to %s:%s: %d "%s"'
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
,
|
|
128
|
-
|
|
133
|
+
serverDebug('Response to %s:%s: %d "%s"',
|
|
134
|
+
address ? address.address : 'unknown',
|
|
135
|
+
address ? address.port : '??',
|
|
136
|
+
code,
|
|
137
|
+
msg
|
|
129
138
|
)
|
|
130
139
|
|
|
131
140
|
res.writeHead(code, { 'content-type': 'text/json' })
|
|
@@ -163,10 +172,9 @@ function envFromPayload (payload, prefix, env) {
|
|
|
163
172
|
env = {}
|
|
164
173
|
}
|
|
165
174
|
|
|
175
|
+
// Add branch as an env var without mutating the payload
|
|
166
176
|
if (payload.ref && payload.ref.startsWith('refs/heads/')) {
|
|
167
|
-
|
|
168
|
-
} else {
|
|
169
|
-
payload.branch = null
|
|
177
|
+
env[prefix + 'branch'] = payload.ref.substring('refs/heads/'.length)
|
|
170
178
|
}
|
|
171
179
|
|
|
172
180
|
Object.keys(payload).forEach((k) => {
|
|
@@ -246,4 +254,4 @@ function handleRules (logStream, rules, event) {
|
|
|
246
254
|
})
|
|
247
255
|
}
|
|
248
256
|
|
|
249
|
-
|
|
257
|
+
export default createServer
|
package/package.json
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "github-webhook",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "A flexible web server for reacting GitHub Webhooks",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "github-webhook.js",
|
|
7
|
+
"exports": "./github-webhook.js",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=20"
|
|
10
|
+
},
|
|
6
11
|
"scripts": {
|
|
7
|
-
"lint": "standard
|
|
8
|
-
"test": "
|
|
12
|
+
"lint": "standard",
|
|
13
|
+
"test:unit": "node --test test.js",
|
|
14
|
+
"test": "npm run lint && npm run test:unit"
|
|
9
15
|
},
|
|
10
16
|
"bin": {
|
|
11
17
|
"github-webhook": "./github-webhook.js"
|
|
@@ -21,17 +27,103 @@
|
|
|
21
27
|
},
|
|
22
28
|
"license": "MIT",
|
|
23
29
|
"devDependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
30
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
31
|
+
"@semantic-release/commit-analyzer": "^13.0.0",
|
|
32
|
+
"@semantic-release/git": "^10.0.1",
|
|
33
|
+
"@semantic-release/github": "^12.0.0",
|
|
34
|
+
"@semantic-release/npm": "^13.0.0",
|
|
35
|
+
"@semantic-release/release-notes-generator": "^14.0.1",
|
|
36
|
+
"conventional-changelog-conventionalcommits": "^9.0.0",
|
|
37
|
+
"semantic-release": "^25.0.0",
|
|
38
|
+
"standard": "^17.1.2"
|
|
28
39
|
},
|
|
29
40
|
"dependencies": {
|
|
30
|
-
"debug": "^4.
|
|
41
|
+
"debug": "^4.4.3",
|
|
31
42
|
"github-webhook-handler": "^1.0.0",
|
|
32
43
|
"matchme": "^2.0.1",
|
|
33
|
-
"minimist": "^1.2.
|
|
34
|
-
"split2": "^
|
|
35
|
-
"through2": "^
|
|
44
|
+
"minimist": "^1.2.8",
|
|
45
|
+
"split2": "^4.2.0",
|
|
46
|
+
"through2": "^4.0.2"
|
|
47
|
+
},
|
|
48
|
+
"release": {
|
|
49
|
+
"branches": [
|
|
50
|
+
"master"
|
|
51
|
+
],
|
|
52
|
+
"plugins": [
|
|
53
|
+
[
|
|
54
|
+
"@semantic-release/commit-analyzer",
|
|
55
|
+
{
|
|
56
|
+
"preset": "conventionalcommits",
|
|
57
|
+
"releaseRules": [
|
|
58
|
+
{
|
|
59
|
+
"breaking": true,
|
|
60
|
+
"release": "major"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
"revert": true,
|
|
64
|
+
"release": "patch"
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"type": "feat",
|
|
68
|
+
"release": "minor"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"type": "fix",
|
|
72
|
+
"release": "patch"
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
"type": "chore",
|
|
76
|
+
"release": "patch"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
"type": "docs",
|
|
80
|
+
"release": "patch"
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
"type": "test",
|
|
84
|
+
"release": "patch"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"scope": "no-release",
|
|
88
|
+
"release": false
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
}
|
|
92
|
+
],
|
|
93
|
+
[
|
|
94
|
+
"@semantic-release/release-notes-generator",
|
|
95
|
+
{
|
|
96
|
+
"preset": "conventionalcommits",
|
|
97
|
+
"presetConfig": {
|
|
98
|
+
"types": [
|
|
99
|
+
{
|
|
100
|
+
"type": "feat",
|
|
101
|
+
"section": "Features"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"type": "fix",
|
|
105
|
+
"section": "Bug Fixes"
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"type": "chore",
|
|
109
|
+
"section": "Trivial Changes"
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"type": "docs",
|
|
113
|
+
"section": "Trivial Changes"
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"type": "test",
|
|
117
|
+
"section": "Tests"
|
|
118
|
+
}
|
|
119
|
+
]
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
],
|
|
123
|
+
"@semantic-release/changelog",
|
|
124
|
+
"@semantic-release/npm",
|
|
125
|
+
"@semantic-release/github",
|
|
126
|
+
"@semantic-release/git"
|
|
127
|
+
]
|
|
36
128
|
}
|
|
37
129
|
}
|
package/test.js
CHANGED
|
@@ -1,54 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import crypto from 'node:crypto'
|
|
4
|
+
import { test } from 'node:test'
|
|
5
|
+
import assert from 'node:assert'
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
import webhook from './github-webhook.js'
|
|
8
|
+
|
|
9
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
7
10
|
|
|
8
11
|
function signBlob (key, blob) {
|
|
9
12
|
return `sha1=${crypto.createHmac('sha1', key).update(blob).digest('hex')}`
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
async function makeRequest (server, { method = 'POST', path = '/', headers = {}, body = '{}' }) {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
server.listen(0, '127.0.0.1', () => {
|
|
18
|
+
const { port } = server.address()
|
|
19
|
+
const url = new URL(path, `http://127.0.0.1:${port}`)
|
|
20
|
+
|
|
21
|
+
fetch(url, {
|
|
22
|
+
method,
|
|
23
|
+
headers: {
|
|
24
|
+
'Content-Type': 'application/json',
|
|
25
|
+
...headers
|
|
26
|
+
},
|
|
27
|
+
body
|
|
28
|
+
})
|
|
29
|
+
.then(async (res) => {
|
|
30
|
+
const text = await res.text()
|
|
31
|
+
let json
|
|
32
|
+
try {
|
|
33
|
+
json = JSON.parse(text)
|
|
34
|
+
} catch {}
|
|
35
|
+
server.close()
|
|
36
|
+
resolve({ status: res.status, headers: res.headers, text, json })
|
|
37
|
+
})
|
|
38
|
+
.catch((err) => {
|
|
39
|
+
server.close()
|
|
40
|
+
reject(err)
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
}
|
|
14
45
|
|
|
46
|
+
test('invalid url gets 404', async () => {
|
|
15
47
|
const options = { port: 0, path: '/webhook', secret: 'foofaa' }
|
|
16
48
|
const server = webhook(options)
|
|
17
49
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
t.error(err)
|
|
28
|
-
})
|
|
29
|
-
})
|
|
50
|
+
const res = await makeRequest(server, {
|
|
51
|
+
path: '/',
|
|
52
|
+
headers: {
|
|
53
|
+
'X-Hub-Signature': signBlob('foofaa', '{}'),
|
|
54
|
+
'X-Github-Event': 'issues',
|
|
55
|
+
'X-Github-Delivery': '123abc'
|
|
56
|
+
},
|
|
57
|
+
body: '{}'
|
|
58
|
+
})
|
|
30
59
|
|
|
31
|
-
|
|
32
|
-
|
|
60
|
+
assert.strictEqual(res.status, 404)
|
|
61
|
+
assert.ok(res.headers.get('content-type').includes('json'))
|
|
62
|
+
})
|
|
33
63
|
|
|
64
|
+
test('valid url, incomplete data gets 400', async () => {
|
|
34
65
|
const options = { port: 0, path: '/webhook', secret: 'foofaa' }
|
|
35
66
|
const server = webhook(options)
|
|
36
67
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
t.error(err)
|
|
46
|
-
})
|
|
47
|
-
})
|
|
68
|
+
const res = await makeRequest(server, {
|
|
69
|
+
path: '/webhook',
|
|
70
|
+
headers: {
|
|
71
|
+
'X-Github-Event': 'issues',
|
|
72
|
+
'X-Github-Delivery': '123abc'
|
|
73
|
+
},
|
|
74
|
+
body: '{}'
|
|
75
|
+
})
|
|
48
76
|
|
|
49
|
-
|
|
50
|
-
|
|
77
|
+
assert.strictEqual(res.status, 400)
|
|
78
|
+
assert.ok(res.headers.get('content-type').includes('json'))
|
|
79
|
+
})
|
|
51
80
|
|
|
81
|
+
test('valid url, complete data gets 200', async () => {
|
|
52
82
|
const options = { port: 0, path: '/webhook', secret: 'foofaa' }
|
|
53
83
|
const server = webhook(options)
|
|
54
84
|
const obj = { some: 'github', object: 'with', properties: true }
|
|
@@ -56,40 +86,46 @@ test('valid url, complete data gets 200', (t) => {
|
|
|
56
86
|
const id = '123abc'
|
|
57
87
|
const eventType = 'issues'
|
|
58
88
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
89
|
+
const eventPromise = new Promise((resolve) => {
|
|
90
|
+
server.webhookHandler.on(eventType, (event) => {
|
|
91
|
+
delete event.host
|
|
92
|
+
resolve(event)
|
|
93
|
+
})
|
|
62
94
|
})
|
|
63
95
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
96
|
+
const res = await makeRequest(server, {
|
|
97
|
+
path: '/webhook',
|
|
98
|
+
headers: {
|
|
99
|
+
'X-Hub-Signature': signBlob('foofaa', json),
|
|
100
|
+
'X-Github-Event': eventType,
|
|
101
|
+
'X-Github-Delivery': id
|
|
102
|
+
},
|
|
103
|
+
body: json
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
assert.strictEqual(res.status, 200)
|
|
107
|
+
assert.ok(res.headers.get('content-type').includes('json'))
|
|
76
108
|
|
|
77
|
-
|
|
78
|
-
|
|
109
|
+
const event = await eventPromise
|
|
110
|
+
assert.strictEqual(event.event, eventType)
|
|
111
|
+
assert.strictEqual(event.id, id)
|
|
112
|
+
assert.deepStrictEqual(event.payload, obj)
|
|
113
|
+
})
|
|
79
114
|
|
|
80
|
-
|
|
115
|
+
test('valid request triggers rule', { skip: process.platform === 'win32' }, async () => {
|
|
116
|
+
const tmpfile = path.join(__dirname, '__test_data.' + Math.random())
|
|
81
117
|
const eventType = 'issues'
|
|
82
118
|
const options = {
|
|
83
119
|
port: 0,
|
|
84
120
|
path: '/webhook',
|
|
85
121
|
secret: 'foofaa',
|
|
86
122
|
rules: [
|
|
87
|
-
{
|
|
123
|
+
{
|
|
88
124
|
event: eventType,
|
|
89
125
|
match: 'some == xxgithub',
|
|
90
126
|
exec: ['sh', '-c', `echo "w00t!" > ${tmpfile}2`]
|
|
91
127
|
},
|
|
92
|
-
{
|
|
128
|
+
{
|
|
93
129
|
event: eventType,
|
|
94
130
|
match: 'some == github',
|
|
95
131
|
exec: `echo "w00t!" > ${tmpfile}`
|
|
@@ -101,33 +137,39 @@ test('valid request triggers rule', (t) => {
|
|
|
101
137
|
const json = JSON.stringify(obj)
|
|
102
138
|
const id = '123abc'
|
|
103
139
|
|
|
104
|
-
|
|
105
|
-
|
|
140
|
+
const eventPromise = new Promise((resolve) => {
|
|
141
|
+
server.webhookHandler.on(eventType, (event) => {
|
|
142
|
+
delete event.host
|
|
143
|
+
resolve(event)
|
|
144
|
+
})
|
|
106
145
|
})
|
|
107
146
|
|
|
108
|
-
server
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
fs.stat(`${tmpfile}2`, (err) => {
|
|
117
|
-
t.ok(err, 'does not exist, didn\'t trigger second event')
|
|
118
|
-
})
|
|
119
|
-
}, 100)
|
|
147
|
+
const res = await makeRequest(server, {
|
|
148
|
+
path: '/webhook',
|
|
149
|
+
headers: {
|
|
150
|
+
'X-Hub-Signature': signBlob('foofaa', json),
|
|
151
|
+
'X-Github-Event': eventType,
|
|
152
|
+
'X-Github-Delivery': id
|
|
153
|
+
},
|
|
154
|
+
body: json
|
|
120
155
|
})
|
|
121
156
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
157
|
+
assert.strictEqual(res.status, 200)
|
|
158
|
+
|
|
159
|
+
const event = await eventPromise
|
|
160
|
+
assert.strictEqual(event.event, eventType)
|
|
161
|
+
assert.strictEqual(event.id, id)
|
|
162
|
+
assert.deepStrictEqual(event.payload, obj)
|
|
163
|
+
|
|
164
|
+
// Wait for the rule to execute
|
|
165
|
+
await new Promise((resolve) => setTimeout(resolve, 200))
|
|
166
|
+
|
|
167
|
+
const data = fs.readFileSync(tmpfile, 'utf8')
|
|
168
|
+
assert.strictEqual(data, 'w00t!\n')
|
|
169
|
+
|
|
170
|
+
// Verify second rule did not trigger
|
|
171
|
+
assert.throws(() => fs.statSync(`${tmpfile}2`), { code: 'ENOENT' })
|
|
172
|
+
|
|
173
|
+
// Cleanup
|
|
174
|
+
fs.unlinkSync(tmpfile)
|
|
133
175
|
})
|