-
Notifications
You must be signed in to change notification settings - Fork 1
ファイル構造のリファクタリング + unitテストの追加 #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
d75362a
9a724d3
3fe08a4
063fefe
55136db
3c58b3e
978f19c
2e565f1
c13f654
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,99 @@ | ||||||||||||||||||||||||||||||||||
| import type { Octokit } from "@octokit/rest"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const runnerName = "openci-runner-beta"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // biome-ignore lint/suspicious/noExplicitAny: <Fill the type later> | ||||||||||||||||||||||||||||||||||
| export function isJobRequired(context: any): boolean { | ||||||||||||||||||||||||||||||||||
| return context.payload.workflow_job.labels.includes(runnerName); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const generateJitConfigPath = | ||||||||||||||||||||||||||||||||||
| "/repos/{owner}/{repo}/actions/runners/generate-jitconfig"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| type JitConfigRequest = { | ||||||||||||||||||||||||||||||||||
| headers: Record<string, string>; | ||||||||||||||||||||||||||||||||||
| labels: string[]; | ||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||
| owner: string; | ||||||||||||||||||||||||||||||||||
| repo: string; | ||||||||||||||||||||||||||||||||||
| runner_group_id: number; | ||||||||||||||||||||||||||||||||||
| work_folder: string; | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export function jitConfigRequestBody( | ||||||||||||||||||||||||||||||||||
| owner: string, | ||||||||||||||||||||||||||||||||||
| repo: string, | ||||||||||||||||||||||||||||||||||
| serverId: number, | ||||||||||||||||||||||||||||||||||
| ): JitConfigRequest { | ||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||||||||||
| "X-GitHub-Api-Version": "2022-11-28", | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| labels: ["openci-runner-beta"], | ||||||||||||||||||||||||||||||||||
| name: `OpenCI ランナー ${serverId}`, | ||||||||||||||||||||||||||||||||||
| owner: `${owner}`, | ||||||||||||||||||||||||||||||||||
| repo: `${repo}`, | ||||||||||||||||||||||||||||||||||
| runner_group_id: 1, | ||||||||||||||||||||||||||||||||||
| work_folder: "_work", | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export async function getJitConfig( | ||||||||||||||||||||||||||||||||||
| octokit: Octokit, | ||||||||||||||||||||||||||||||||||
| owner: string, | ||||||||||||||||||||||||||||||||||
| repo: string, | ||||||||||||||||||||||||||||||||||
| serverId: number, | ||||||||||||||||||||||||||||||||||
| ): Promise<string> { | ||||||||||||||||||||||||||||||||||
| const body = jitConfigRequestBody(owner, repo, serverId); | ||||||||||||||||||||||||||||||||||
| const jitConfigRes = await octokit.request( | ||||||||||||||||||||||||||||||||||
| `POST ${generateJitConfigPath}`, | ||||||||||||||||||||||||||||||||||
| body, | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return jitConfigRes.data.encoded_jit_config; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export enum ActionsRunnerOS { | ||||||||||||||||||||||||||||||||||
| osx = "osx", | ||||||||||||||||||||||||||||||||||
| linux = "linux", | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export enum ActionsRunnerArchitecture { | ||||||||||||||||||||||||||||||||||
| x64 = "x64", | ||||||||||||||||||||||||||||||||||
| arm64 = "arm64", | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const mkdir = "mkdir actions-runner"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const cdActionRunner = "cd actions-runner"; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function downloadRunnerScriptAndUnZip( | ||||||||||||||||||||||||||||||||||
| scriptVersion: string, | ||||||||||||||||||||||||||||||||||
| os: ActionsRunnerOS, | ||||||||||||||||||||||||||||||||||
| architecture: ActionsRunnerArchitecture, | ||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||
| const fileName = `actions-runner-${os}-${architecture}-${scriptVersion}.tar.gz`; | ||||||||||||||||||||||||||||||||||
| return `curl -o ${fileName} -L https://github.com/actions/runner/releases/download/v${scriptVersion}/${fileName} && tar xzf ./${fileName}`; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+70
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curlコマンドにエラーハンドリングとチェックサム検証を追加してください。 現在の実装には以下の問題があります:
以下の修正を推奨します: function downloadRunnerScriptAndUnZip(
scriptVersion: string,
os: ActionsRunnerOS,
architecture: ActionsRunnerArchitecture,
) {
const fileName = `actions-runner-${os}-${architecture}-${scriptVersion}.tar.gz`;
- return `curl -o ${fileName} -L https://github.com/actions/runner/releases/download/v${scriptVersion}/${fileName} && tar xzf ./${fileName}`;
+ return `curl -fsSL -o ${fileName} https://github.com/actions/runner/releases/download/v${scriptVersion}/${fileName} && tar xzf ./${fileName}`;
}さらに、チェックサム検証を追加することを強く推奨します。GitHubはランナーリリースと共にSHA256チェックサムを提供しています。 📝 Committable suggestion
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @coderabbitai issueを作成して。github。
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: Length of output: 47 curlコマンドのエラーハンドリングとチェックサム検証の追加に関する詳細を記載し、あなたにアサインしました。 |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function runGHAScript(runnerConfig: string) { | ||||||||||||||||||||||||||||||||||
| return `tmux new -d -s runner "RUNNER_ALLOW_RUNASROOT=true ./run.sh --jitconfig ${runnerConfig}"`; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export function initRunner( | ||||||||||||||||||||||||||||||||||
| scriptVersion: string, | ||||||||||||||||||||||||||||||||||
| os: ActionsRunnerOS, | ||||||||||||||||||||||||||||||||||
| architecture: ActionsRunnerArchitecture, | ||||||||||||||||||||||||||||||||||
| ): string { | ||||||||||||||||||||||||||||||||||
| const command = [ | ||||||||||||||||||||||||||||||||||
| mkdir, | ||||||||||||||||||||||||||||||||||
| cdActionRunner, | ||||||||||||||||||||||||||||||||||
| downloadRunnerScriptAndUnZip(scriptVersion, os, architecture), | ||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||
| return command.join("&&"); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export function startRunner(runnerConfig: string) { | ||||||||||||||||||||||||||||||||||
| const command = [cdActionRunner, runGHAScript(runnerConfig)]; | ||||||||||||||||||||||||||||||||||
| return command.join("&&"); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,19 @@ | |||||||||||||||||||||||||||||||||
| token: string; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export interface HetznerResponse { | ||||||||||||||||||||||||||||||||||
| serverId: number; | ||||||||||||||||||||||||||||||||||
| ipv4: string; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export interface HetznerServerSpec { | ||||||||||||||||||||||||||||||||||
| image: string; | ||||||||||||||||||||||||||||||||||
| location: string; | ||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||
| server_type: string; | ||||||||||||||||||||||||||||||||||
| ssh_keys: [string]; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export async function deleteServer(id: string, apiKey: string) { | ||||||||||||||||||||||||||||||||||
| await fetch(`${baseUrl}/${id}`, { | ||||||||||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||||||||||
|
|
@@ -13,22 +26,28 @@ | |||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export type HetznerResponse = { | ||||||||||||||||||||||||||||||||||
| serverId: number; | ||||||||||||||||||||||||||||||||||
| ipv4: string; | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export async function createServer(apiKey: string): Promise<HetznerResponse> { | ||||||||||||||||||||||||||||||||||
| const body = { | ||||||||||||||||||||||||||||||||||
| image: "ubuntu-24.04", | ||||||||||||||||||||||||||||||||||
| location: "fsn1", | ||||||||||||||||||||||||||||||||||
| name: crypto.randomUUID(), | ||||||||||||||||||||||||||||||||||
| server_type: "cpx41", | ||||||||||||||||||||||||||||||||||
| ssh_keys: ["openci-runner-probot"], | ||||||||||||||||||||||||||||||||||
| export function createServerSpec( | ||||||||||||||||||||||||||||||||||
| image: string = "ubuntu-24.04", | ||||||||||||||||||||||||||||||||||
| location: string = "fsn1", | ||||||||||||||||||||||||||||||||||
| name: string = crypto.randomUUID(), | ||||||||||||||||||||||||||||||||||
| serverType: string = "cpx41", | ||||||||||||||||||||||||||||||||||
| sshKeyName: string = "openci-runner-probot", | ||||||||||||||||||||||||||||||||||
| ): HetznerServerSpec { | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
+35
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
TS でグローバル +import { randomUUID } from "node:crypto";
...
-export function createServerSpec(
+export function createServerSpec(
image: string = "ubuntu-24.04",
location: string = "fsn1",
- name: string = crypto.randomUUID(),
+ name: string = randomUUID(),
serverType: string = "cpx41",
sshKeyName: string = "openci-runner-probot",
): HetznerServerSpec {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. どういう場合に、cryptoが解決されませんか?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: Length of output: 404 🏁 Script executed: Length of output: 9 🏁 Script executed: Length of output: 41 @mafreud さん、申し訳ございません。私の以前の指摘は不正確でした。 実際にコードをチェックした結果、現在の設定では
したがって、 私の以前のレビューコメントは解決(resolve)させていただきます。ご迷惑をおかけして申し訳ございませんでした。 🧬 Code Graph Analysis Resultsopenci-runner/firebase/functions/src/probot/hetzner.ts: createServerSpec (lines 29-43) Summary:
|
||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||
| image: image, | ||||||||||||||||||||||||||||||||||
| location: location, | ||||||||||||||||||||||||||||||||||
| name: name, | ||||||||||||||||||||||||||||||||||
| server_type: serverType, | ||||||||||||||||||||||||||||||||||
| ssh_keys: [sshKeyName], | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export async function createServer( | ||||||||||||||||||||||||||||||||||
| apiKey: string, | ||||||||||||||||||||||||||||||||||
| serverSpec: HetznerServerSpec, | ||||||||||||||||||||||||||||||||||
| ): Promise<HetznerResponse> { | ||||||||||||||||||||||||||||||||||
| const response = await fetch(baseUrl, { | ||||||||||||||||||||||||||||||||||
| body: JSON.stringify(body), | ||||||||||||||||||||||||||||||||||
| body: JSON.stringify(serverSpec), | ||||||||||||||||||||||||||||||||||
| headers: { | ||||||||||||||||||||||||||||||||||
| Authorization: `Bearer ${apiKey}`, | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
|
|
@@ -58,16 +77,5 @@ | |||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| const jsonRes = await _response.json(); | ||||||||||||||||||||||||||||||||||
| return jsonRes.server.status; | ||||||||||||||||||||||||||||||||||
|
Check failure on line 80 in openci-runner/firebase/functions/src/probot/hetzner.ts
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| export function initRunner(runnerConfig: string): string { | ||||||||||||||||||||||||||||||||||
| const command = ` | ||||||||||||||||||||||||||||||||||
| mkdir actions-runner | ||||||||||||||||||||||||||||||||||
| cd actions-runner | ||||||||||||||||||||||||||||||||||
| curl -o actions-runner-linux-x64-2.328.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.328.0/actions-runner-linux-x64-2.328.0.tar.gz | ||||||||||||||||||||||||||||||||||
| tar xzf ./actions-runner-linux-x64-2.328.0.tar.gz | ||||||||||||||||||||||||||||||||||
| tmux new -d -s runner "RUNNER_ALLOW_RUNASROOT=true ./run.sh --jitconfig ${runnerConfig}" | ||||||||||||||||||||||||||||||||||
| `; | ||||||||||||||||||||||||||||||||||
| return command; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
labelsの null チェックを追加してくださいworkflow_job.labelsがundefinedまたはnullの場合、ランタイムエラーが発生します。includesを呼ぶ前に存在チェックを追加してください。以下の diff を適用して修正してください:
export function isJobRequired(context: any): boolean { - return context.payload.workflow_job.labels.includes(runnerName); + return context.payload.workflow_job.labels?.includes(runnerName) ?? false; }📝 Committable suggestion
🤖 Prompt for AI Agents