Skip to content

Commit decc44a

Browse files
authored
webhookイベントをgithub app(開発環境)で扱えるように実装 (#556)
* Update dependencies and enhance webhook event handling * Add comments for future implementation of runner creation in webhook handling * Refactor webhook event handling to correctly respond to queued actions and ignore others * Update test description for non-workflow_job event response
1 parent 8ef8c32 commit decc44a

File tree

5 files changed

+85
-11
lines changed

5 files changed

+85
-11
lines changed

openci-runner/package-lock.json

Lines changed: 12 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openci-runner/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"dependencies": {
3-
"@octokit/webhooks-methods": "^6.0.0"
3+
"@octokit/webhooks-methods": "^6.0.0",
4+
"@octokit/webhooks-types": "^7.6.1"
45
},
56
"devDependencies": {
67
"@cloudflare/vitest-pool-workers": "^0.8.19",
78
"@vitest/coverage-istanbul": "^3.2.4",
89
"typescript": "^5.5.2",
910
"vitest": "3.2.4",
10-
"wrangler": "^4.45.2"
11+
"wrangler": "^4.45.3"
1112
},
1213
"name": "openci-runner",
1314
"private": true,

openci-runner/src/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { verify } from "@octokit/webhooks-methods";
2+
import type { WebhookEvent } from "@octokit/webhooks-types";
23

34
export default {
45
async fetch(request, env, _): Promise<Response> {
@@ -18,6 +19,28 @@ export default {
1819
return new Response("Unauthorized", { status: 401 });
1920
}
2021

21-
return new Response("Successfully created OpenCI runner", { status: 201 });
22+
const payload: WebhookEvent = JSON.parse(body);
23+
24+
if ("workflow_job" in payload) {
25+
switch (payload.action) {
26+
case WorkflowJobAction.Queued:
27+
return new Response("Successfully created OpenCI runner", {
28+
status: 201,
29+
});
30+
default:
31+
return new Response(
32+
"Workflow Job but status is not queued. Ignore this event",
33+
{
34+
status: 200,
35+
},
36+
);
37+
}
38+
}
39+
40+
return new Response("Event ignored", { status: 200 });
2241
},
2342
} satisfies ExportedHandler<Env>;
43+
44+
const WorkflowJobAction = {
45+
Queued: "queued",
46+
} as const;

openci-runner/test/index.spec.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("ensure secrets", () => {
2020
});
2121

2222
describe("fetch", () => {
23-
it("responds with 201 when signature is valid", async () => {
23+
it("responds with 200 for non-workflow_job events", async () => {
2424
const webhookSecret = env.GH_APP_WEBHOOK_SECRET;
2525
const body = JSON.stringify({ action: "ping" });
2626
const signature = createSignature(body, webhookSecret);
@@ -35,9 +35,9 @@ describe("fetch", () => {
3535
const ctx = createExecutionContext();
3636
const response = await worker.fetch(request, env, ctx);
3737
await waitOnExecutionContext(ctx);
38-
expect(response.status).toBe(201);
38+
expect(response.status).toBe(200);
3939
await expect(response.text()).resolves.toMatchInlineSnapshot(
40-
`"Successfully created OpenCI runner"`,
40+
`"Event ignored"`,
4141
);
4242
});
4343

@@ -96,4 +96,46 @@ describe("fetch", () => {
9696
"Webhook secret not configured",
9797
);
9898
});
99+
100+
it("return 201 when new runner is created", async () => {
101+
const webhookSecret = env.GH_APP_WEBHOOK_SECRET;
102+
const body = JSON.stringify({ action: "queued", workflow_job: {} });
103+
const signature = createSignature(body, webhookSecret);
104+
const request = new IncomingRequest("http://example.com", {
105+
body,
106+
headers: {
107+
"content-type": "application/json",
108+
"x-hub-signature-256": signature,
109+
},
110+
method: "POST",
111+
});
112+
const ctx = createExecutionContext();
113+
const response = await worker.fetch(request, env, ctx);
114+
await waitOnExecutionContext(ctx);
115+
expect(response.status).toBe(201);
116+
await expect(response.text()).resolves.toMatchInlineSnapshot(
117+
`"Successfully created OpenCI runner"`,
118+
);
119+
});
120+
121+
it("nothing to be processed", async () => {
122+
const webhookSecret = env.GH_APP_WEBHOOK_SECRET;
123+
const body = JSON.stringify({ action: "ping", workflow_job: {} });
124+
const signature = createSignature(body, webhookSecret);
125+
const request = new IncomingRequest("http://example.com", {
126+
body,
127+
headers: {
128+
"content-type": "application/json",
129+
"x-hub-signature-256": signature,
130+
},
131+
method: "POST",
132+
});
133+
const ctx = createExecutionContext();
134+
const response = await worker.fetch(request, env, ctx);
135+
await waitOnExecutionContext(ctx);
136+
expect(response.status).toBe(200);
137+
await expect(response.text()).resolves.toMatchInlineSnapshot(
138+
`"Workflow Job but status is not queued. Ignore this event"`,
139+
);
140+
});
99141
});

openci-runner/test/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"strictNullChecks": true,
34
"types": ["@cloudflare/vitest-pool-workers"]
45
},
56
"exclude": [],

0 commit comments

Comments
 (0)