Skip to content

Commit 90b7c48

Browse files
authored
Merge pull request #2 from badsyntax/feature/error-handling
Better error handling
2 parents 010e990 + ca33c33 commit 90b7c48

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules
22
npm-debug.log
33
build
4+
.env
45
.vscode
56
.git
67
.gitignore

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ curl --header "Content-Type: application/json" \
2121

2222
```bash
2323
docker build -t ghcr.io/badsyntax/strapi-webhook-actions-proxy:latest .
24-
docker run --publish 5000:5000 ghcr.io/badsyntax/strapi-webhook-actions-proxy:latest
24+
docker run --publish 5000:5000 --env-file .env ghcr.io/badsyntax/strapi-webhook-actions-proxy:latest
2525
docker push ghcr.io/badsyntax/strapi-webhook-actions-proxy:latest
2626
```

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
A super simple & lightweight Node.js proxy to send `repository_dispatch` events to GitHub from a Strapi Webhook.
44

5+
Useful when you want to run a GitHub Actions workflow when changes are made in strapi.
6+
7+
## Background
8+
9+
You can't point strapi webhooks to the [repository dispatch event endpoint](https://docs.github.com/en/rest/reference/repos#create-a-repository-dispatch-event) as the webook request body is not compatible with the dispatches endpoint, thus a proxy is required.
10+
511
## Usage
612

713
Ensure your GitHub Actions workflow file handles the "repository_dispatch" event with your custom type:
@@ -16,7 +22,7 @@ on:
1622
Deploy the service to your server, for example:
1723
1824
```bash
19-
docker run --publish 5000:5000 ghcr.io/badsyntax/strapi-webhook-actions-proxy:latest
25+
docker run --publish 5000:5000 --env GITHUB_TOKEN=YOURTOKEN ghcr.io/badsyntax/strapi-webhook-actions-proxy:latest
2026
```
2127

2228
TODO: env

src/routes/api/index.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,31 @@ indexRoute.post(
1515
if (!repo) {
1616
return res.status(400).send('repo param missing');
1717
}
18+
1819
try {
19-
await fetch(`https://api.github.com/repos/${repo}/dispatches`, {
20-
method: 'post',
21-
headers: new Headers({
22-
Accept: 'application/vnd.github.v3+json',
23-
Authorization: `token ${process.env.GITHUB_TOKEN}`,
24-
ContentType: 'application/json',
25-
}),
26-
body: JSON.stringify({ event_type: eventType }),
20+
const response = await fetch(
21+
`https://api.github.com/repos/${repo}/dispatches`,
22+
{
23+
method: 'post',
24+
headers: new Headers({
25+
Accept: 'application/vnd.github.v3+json',
26+
Authorization: `token ${process.env.GITHUB_TOKEN}`,
27+
ContentType: 'application/json',
28+
}),
29+
body: JSON.stringify({
30+
event_type: eventType,
31+
client_payload: req.body,
32+
}),
33+
}
34+
).then((response) => {
35+
if (!response.ok) {
36+
throw new Error(response.statusText);
37+
}
2738
});
2839
res.status(200).send('Success');
2940
} catch (e) {
3041
const msg = 'Error calling actions endpoint';
31-
console.error(msg, e.message);
42+
console.error(`${msg}:`, e.message);
3243
res.status(500).send(msg);
3344
}
3445
}

0 commit comments

Comments
 (0)