Skip to content

Commit ddd1790

Browse files
authored
Issue作成時のProbotにunit testを追加 + 全体のファイルを再度フォーマット (#505)
* feat: enhance GitHub Probot integration with new JIT config and improved server management * fix: correct linter configuration by removing duplicate 'recommended' entry * fix: reorder 'recommended' entry in linter configuration for clarity * fix: update biome.jsonc structure and enhance workspace recommendations
1 parent 1ea5d43 commit ddd1790

File tree

11 files changed

+167
-207
lines changed

11 files changed

+167
-207
lines changed

biome.jsonc

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
{
2+
"assist": {
3+
"actions": {
4+
"source": {
5+
"useSortedKeys": "on"
6+
}
7+
},
8+
"enabled": true
9+
},
10+
"files": {
11+
"includes": [
12+
"**",
13+
"!**/node_modules"
14+
]
15+
},
216
"linter": {
317
"enabled": true,
418
"rules": {
5-
"recommended": true,
619
"correctness": {
720
"noUnusedImports": "error"
8-
}
21+
},
22+
"recommended": true
923
}
1024
}
1125
}

openci-runner/firebase/functions/package-lock.json

Lines changed: 0 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
11
{
2-
"name": "functions",
3-
"scripts": {
4-
"build": "tsc",
5-
"build:watch": "tsc --watch",
6-
"serve": "npm run build && firebase emulators:start --only functions",
7-
"shell": "npm run build && firebase functions:shell",
8-
"start": "npm run build && probot run ./lib/probot/index.js",
9-
"deploy": "firebase deploy --only functions",
10-
"logs": "firebase functions:log",
11-
"test": "vitest run"
12-
},
13-
"engines": {
14-
"node": "22"
15-
},
16-
"main": "lib/src/index.js",
172
"dependencies": {
183
"@octokit/rest": "^22.0.0",
194
"firebase-admin": "^13.5.0",
@@ -23,14 +8,28 @@
238
"probot": "^14.1.0"
249
},
2510
"devDependencies": {
26-
"@types/supertest": "^6.0.3",
2711
"@vitest/coverage-v8": "^3.2.4",
2812
"firebase-functions-test": "^3.1.0",
2913
"smee-client": "^4.3.1",
3014
"supertest": "^7.0.0",
3115
"typescript": "^5.9.3",
3216
"vitest": "^3.2.4"
3317
},
18+
"engines": {
19+
"node": "22"
20+
},
21+
"main": "lib/src/index.js",
22+
"name": "functions",
3423
"private": true,
24+
"scripts": {
25+
"build": "tsc",
26+
"build:watch": "tsc --watch",
27+
"deploy": "firebase deploy --only functions",
28+
"logs": "firebase functions:log",
29+
"serve": "npm run build && firebase emulators:start --only functions",
30+
"shell": "npm run build && firebase functions:shell",
31+
"start": "npm run build && probot run ./lib/probot/index.js",
32+
"test": "vitest run"
33+
},
3534
"type": "module"
3635
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
1+
import type { Octokit } from "@octokit/rest";
2+
13
const runnerName = "openci-runner-beta";
24

35
// biome-ignore lint/suspicious/noExplicitAny: <Fill the type later>
46
export function isJobRequired(context: any): boolean {
57
return context.payload.workflow_job.labels.includes(runnerName);
68
}
9+
10+
export async function getJitConfig(
11+
octokit: Octokit,
12+
owner: string,
13+
repo: string,
14+
serverId: number,
15+
): Promise<string> {
16+
const jitConfigRes = await octokit.request(
17+
`POST /repos/{owner}/{repo}/actions/runners/generate-jitconfig`,
18+
{
19+
headers: {
20+
"X-GitHub-Api-Version": "2022-11-28",
21+
},
22+
labels: ["openci-runner-beta"],
23+
name: `OpenCI ランナー ${serverId}`,
24+
owner: `${owner}`,
25+
repo: `${repo}`,
26+
runner_group_id: 1,
27+
work_folder: "_work",
28+
},
29+
);
30+
31+
return jitConfigRes.data.encoded_jit_config;
32+
}

openci-runner/firebase/functions/probot/hetzner.ts

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Octokit } from "@octokit/rest";
2-
31
const baseUrl = "https://api.hetzner.cloud/v1/servers";
42

53
export interface OctokitToken {
@@ -8,10 +6,10 @@ export interface OctokitToken {
86

97
export async function deleteServer(id: string, apiKey: string) {
108
await fetch(`${baseUrl}/${id}`, {
11-
method: "DELETE",
129
headers: {
1310
Authorization: `Bearer ${apiKey}`,
1411
},
12+
method: "DELETE",
1513
});
1614
}
1715

@@ -22,19 +20,19 @@ export type HetznerResponse = {
2220

2321
export async function createServer(apiKey: string): Promise<HetznerResponse> {
2422
const body = {
25-
name: crypto.randomUUID(),
23+
image: "ubuntu-24.04",
2624
location: "fsn1",
25+
name: crypto.randomUUID(),
2726
server_type: "cpx41",
28-
image: "ubuntu-24.04",
2927
ssh_keys: ["openci-runner-probot"],
3028
};
3129

3230
const response = await fetch(baseUrl, {
33-
method: "POST",
31+
body: JSON.stringify(body),
3432
headers: {
3533
Authorization: `Bearer ${apiKey}`,
3634
},
37-
body: JSON.stringify(body),
35+
method: "POST",
3836
});
3937

4038
const responseJson = await response.json();
@@ -45,8 +43,8 @@ export async function createServer(apiKey: string): Promise<HetznerResponse> {
4543
const serverId = server.id;
4644

4745
return {
48-
serverId: serverId,
4946
ipv4: ipv4,
47+
serverId: serverId,
5048
};
5149
}
5250

@@ -63,30 +61,6 @@ export async function getServerStatusById(
6361
return jsonRes.server.status;
6462
}
6563

66-
export async function getJitConfig(
67-
octokit: Octokit,
68-
owner: string,
69-
repo: string,
70-
serverId: number,
71-
): Promise<string> {
72-
const jitConfigRes = await octokit.request(
73-
`POST /repos/{owner}/{repo}/actions/runners/generate-jitconfig`,
74-
{
75-
owner: `${owner}`,
76-
repo: `${repo}`,
77-
name: `OpenCI ランナー ${serverId}`,
78-
runner_group_id: 1,
79-
labels: ["openci-runner-beta"],
80-
work_folder: "_work",
81-
headers: {
82-
"X-GitHub-Api-Version": "2022-11-28",
83-
},
84-
},
85-
);
86-
87-
return jitConfigRes.data.encoded_jit_config;
88-
}
89-
9064
export function initRunner(runnerConfig: string): string {
9165
const command = `
9266
mkdir actions-runner

openci-runner/firebase/functions/probot/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import { setTimeout } from "node:timers/promises";
12
import { Octokit } from "@octokit/rest";
23
import { NodeSSH } from "node-ssh";
3-
import { setTimeout } from "node:timers/promises";
44
import type { ApplicationFunction, Context, Probot } from "probot";
5-
import { isJobRequired } from "./github.js";
5+
import { getJitConfig, isJobRequired } from "./github.js";
66
import {
77
createServer,
88
deleteServer,
9-
getJitConfig,
109
getServerStatusById,
1110
initRunner,
1211
type OctokitToken,
@@ -19,9 +18,9 @@ export const appFn: ApplicationFunction = (app: Probot) => {
1918
app.log.info("Yay! The app was loaded!");
2019

2120
// This is for debugging.
22-
app.on("issues.reopened", async (context: Context) => {
21+
app.on("issues.opened", async (context: Context) => {
2322
return context.octokit.rest.issues.createComment(
24-
context.issue({ body: "Probot is working!" }),
23+
context.issue({ body: "Hello, World!" }),
2524
);
2625
});
2726

@@ -71,9 +70,9 @@ export const appFn: ApplicationFunction = (app: Probot) => {
7170
try {
7271
const sshResult = await ssh.connect({
7372
host: hetznerResponse.ipv4,
74-
username: "root",
75-
privateKey: process.env.HETZNER_SSH_PRIVATE_KEY,
7673
passphrase: process.env.HETZNER_SSH_PASSPHRASE,
74+
privateKey: process.env.HETZNER_SSH_PRIVATE_KEY,
75+
username: "root",
7776
});
7877

7978
const octokit = new Octokit({

openci-runner/firebase/functions/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const hetznerSshPrivateKey = defineSecret("HETZNER_SSH_PRIVATE_KEY");
1414

1515
export const githubWebhook = onRequest(
1616
{
17+
cpu: 8,
18+
memory: "16GiB",
1719
secrets: [
1820
githubAppId,
1921
githubPrivateKey,
@@ -22,24 +24,22 @@ export const githubWebhook = onRequest(
2224
hetznerSshPassphrase,
2325
hetznerSshPrivateKey,
2426
],
25-
memory: "16GiB",
26-
cpu: 8,
2727
timeoutSeconds: 300,
2828
},
2929
async (req, res) => {
3030
log("githubWebhook has started");
3131
const probot = await createNodeMiddleware(appFn, {
3232
probot: createProbot({
33-
overrides: {
34-
appId: githubAppId.value(),
35-
privateKey: githubPrivateKey.value(),
36-
secret: githubWebhookSecret.value(),
37-
},
3833
env: {
3934
HETZNER_API_KEY: hetznerApiKey.value(),
4035
HETZNER_SSH_PASSPHRASE: hetznerSshPassphrase.value(),
4136
HETZNER_SSH_PRIVATE_KEY: hetznerSshPrivateKey.value(),
4237
},
38+
overrides: {
39+
appId: githubAppId.value(),
40+
privateKey: githubPrivateKey.value(),
41+
secret: githubWebhookSecret.value(),
42+
},
4343
}),
4444
webhooksPath: "/",
4545
});
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
2-
"action": "opened",
3-
"issue": {
4-
"number": 1,
5-
"user": {
6-
"login": "hiimbex"
7-
}
8-
},
9-
"repository": {
10-
"name": "testing-things",
11-
"owner": {
12-
"login": "hiimbex"
13-
}
14-
},
15-
"installation": {
16-
"id": 2
17-
}
2+
"action": "opened",
3+
"installation": {
4+
"id": 2
5+
},
6+
"issue": {
7+
"number": 1,
8+
"user": {
9+
"login": "hiimbex"
10+
}
11+
},
12+
"repository": {
13+
"name": "testing-things",
14+
"owner": {
15+
"login": "hiimbex"
16+
}
17+
}
1818
}

0 commit comments

Comments
 (0)