From 064cd26c98c550b880628a3ea6527f9dda631ebe Mon Sep 17 00:00:00 2001 From: HyuuNyackman <107097515+HyuuNyackman@users.noreply.github.com> Date: Sun, 29 Nov 2026 23:01:55 +0900 Subject: [PATCH] =?UTF-8?q?[xcl-196]=20feat:=20=E3=83=9E=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E5=BE=8C=E3=81=AE=E3=83=96=E3=83=A9=E3=83=B3=E3=83=81=E3=81=AE?= =?UTF-8?q?=E5=89=8A=E9=99=A4=E3=82=92=E8=A8=AD=E5=AE=9A=E3=83=95=E3=82=A1?= =?UTF-8?q?=E3=82=A4=E3=83=AB=E3=81=A7=E5=88=87=E3=82=8C=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/SETUP.md | 2 +- scripts/ci/branch-protection.test.mjs | 23 ++++++++++++++++++++++ skills/branch-protection/SKILL.md | 6 ++++-- skills/branch-protection/scripts/apply.mjs | 15 ++++++++++---- templates/.github/branch-protection.json | 3 ++- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/SETUP.md b/docs/SETUP.md index 5314d63..d589298 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -83,7 +83,7 @@ dependencies: - `develop`: PR 必須、必須チェック、force push と削除の禁止。承認の人数は 0 (一人の運用で、自分の PR は承認できないため)。 - `main`: 上に加えて、管理者にも必須チェックを強制する (`develop` では、管理者は迂回できる)。`develop → main` の PR は人がマージする。 - `agent/*`・`feature/*` は保護しない。 -- リポジトリの設定で、自動マージを許可し、マージの後にブランチを消す。 +- リポジトリの設定で、自動マージを許可し、マージの後にブランチを消す。消したくないときは、設定ファイルで `"delete_branch_on_merge": false` にする (例: CI が後片付けを済ませてから、自分でブランチを消す)。 適用は、スキルの `branch-protection` で行う ([`skills/branch-protection/SKILL.md`](../skills/branch-protection/SKILL.md))。`apm.yml` に `HyuuNyackman/agent-dev-kit/skills/branch-protection` を足して `apm install` し、必須チェックの名前を `.github/branch-protection.json` (例: `templates/.github/branch-protection.json`) に書いて、リポジトリの管理者が手元で実行する。CI からは実行しない。 diff --git a/scripts/ci/branch-protection.test.mjs b/scripts/ci/branch-protection.test.mjs index feaa4b6..da3ca6c 100644 --- a/scripts/ci/branch-protection.test.mjs +++ b/scripts/ci/branch-protection.test.mjs @@ -118,6 +118,24 @@ test('実行: dry-run と同じ JSON を gh api に渡す。2 回実行しても assert.equal(normalize(second.ghLog), expected + expected); }); +test('--dry-run: delete_branch_on_merge を省くと true、false ならリポジトリの設定に false を送る', () => { + for (const [value, expected] of [ + [undefined, true], + [true, true], + [false, false], + ]) { + const config = { required_checks: CHECKS }; + if (value !== undefined) config.delete_branch_on_merge = value; + const env = setup(config); + const r = run(env, ['--dry-run', '--repo', 'owner/repo', env.configPath]); + assert.equal(r.status, 0, r.stderr); + assert.deepEqual(requests(r.stdout)[2].body, { + allow_auto_merge: true, + delete_branch_on_merge: expected, + }); + } +}); + test('テンプレートの設定ファイルは、そのまま通る', () => { const env = setup(); const r = run(env, ['--dry-run', '--repo', 'owner/repo', TEMPLATE]); @@ -141,6 +159,11 @@ const bad = [ ['required_checks に重複', { required_checks: ['a', 'a'] }, /同じ名前が 2 回/], ['知らないキー', { required_checks: CHECKS, required_check: [] }, /知らないキー: required_check/], ['ブランチの名前が空', { required_checks: CHECKS, main_branch: '' }, /main_branch は/], + [ + 'delete_branch_on_merge が文字列', + { required_checks: CHECKS, delete_branch_on_merge: 'false' }, + /delete_branch_on_merge は/, + ], ['ブランチが同じ', { required_checks: CHECKS, develop_branch: 'main' }, /同じ/], ]; diff --git a/skills/branch-protection/SKILL.md b/skills/branch-protection/SKILL.md index cf2d7c9..10113de 100644 --- a/skills/branch-protection/SKILL.md +++ b/skills/branch-protection/SKILL.md @@ -25,7 +25,7 @@ AI にマージさせないための本体 (`docs/SETUP.md` の §5) を、GitHu |---|---| | develop | PR 必須、必須チェック (設定ファイルの一覧)、承認は 0 人、force push と削除の禁止。管理者は迂回できる (必須チェックがどうしても通らない PR を、管理者が確かめてマージする逃げ道) | | main | develop と同じに加えて、管理者にも強制する | -| リポジトリ | 自動マージを許可する (`allow_auto_merge`)、マージの後にブランチを消す (`delete_branch_on_merge`) | +| リポジトリ | 自動マージを許可する (`allow_auto_merge`)、マージの後にブランチを消す (`delete_branch_on_merge`。設定ファイルで切れる) | 承認が 0 人なのは、一人の運用では自分の PR を承認できないため。`agent/*`・`feature/*` は保護しない。 保護は丸ごと置き換えるので、GitHub の画面で足した保護の設定 (承認の人数など) は、実行すると消える。変えたいときは、このスキルを直す。 @@ -38,7 +38,8 @@ AI にマージさせないための本体 (`docs/SETUP.md` の §5) を、GitHu { "required_checks": ["test / test", "ci / claude-review"], "develop_branch": "develop", - "main_branch": "main" + "main_branch": "main", + "delete_branch_on_merge": true } ``` @@ -47,6 +48,7 @@ AI にマージさせないための本体 (`docs/SETUP.md` の §5) を、GitHu | `required_checks` | はい | 必須チェックの名前の配列。空にしない。同じ名前を 2 回書かない | | `develop_branch` | いいえ | 統合のブランチ。省くと `develop` | | `main_branch` | いいえ | リリースのブランチ。省くと `main`。`develop_branch` と違う名前にする | +| `delete_branch_on_merge` | いいえ | マージの後に、GitHub が head のブランチを消すか (真偽値)。省くと `true`。`false` にする例: CI が PR の後片付け (Preview の削除など) を済ませてから、自分でブランチを消す (消せなければブランチが残り、気づける) | ほかのキーがあると、書き間違いとみなして止まる。 diff --git a/skills/branch-protection/scripts/apply.mjs b/skills/branch-protection/scripts/apply.mjs index b34dabb..c86b7cf 100644 --- a/skills/branch-protection/scripts/apply.mjs +++ b/skills/branch-protection/scripts/apply.mjs @@ -4,7 +4,7 @@ import { spawnSync } from 'node:child_process'; import { readFileSync, statSync } from 'node:fs'; const USAGE = '使い方: apply.sh [--dry-run] [--repo ] <設定ファイル>'; -const KEYS = ['required_checks', 'develop_branch', 'main_branch']; +const KEYS = ['required_checks', 'develop_branch', 'main_branch', 'delete_branch_on_merge']; function usage(message) { if (message) console.error(`エラー: ${message}`); @@ -56,6 +56,9 @@ function validate(config) { for (const k of ['develop_branch', 'main_branch']) { if (k in config && !nonEmptyString(config[k])) errors.push(`${k} は、空でない文字列にする`); } + if ('delete_branch_on_merge' in config && typeof config.delete_branch_on_merge !== 'boolean') { + errors.push('delete_branch_on_merge は、true か false にする'); + } if ((config.develop_branch ?? 'develop') === (config.main_branch ?? 'main')) { errors.push('develop_branch と main_branch が同じ'); } @@ -67,7 +70,8 @@ function validate(config) { // - 管理者は、必須チェックを迂回できる。必須チェックがどうしても通らない PR (例: ワークフローのファイルを // 変えて、AI のレビューが走らない) を、管理者が確かめてマージするための逃げ道。 // main: 本番への反映の入口。管理者にも強制する。承認は 0 人 (リリースの PR を自分でマージするため)。 -// 最後に、自動マージを許可し、マージの後にブランチを消す。 +// 最後に、自動マージを許可し、マージの後にブランチを消す (delete_branch_on_merge が false なら消さない。 +// 例: CI が後片付けを済ませてから、自分でブランチを消すとき)。 function requests(repo, config) { const protection = (enforceAdmins) => ({ required_status_checks: { strict: false, contexts: config.required_checks }, @@ -79,6 +83,7 @@ function requests(repo, config) { }); const develop = config.develop_branch ?? 'develop'; const main = config.main_branch ?? 'main'; + const deleteBranch = config.delete_branch_on_merge ?? true; return [ { method: 'PUT', @@ -95,8 +100,10 @@ function requests(repo, config) { { method: 'PATCH', path: `repos/${repo}`, - body: { allow_auto_merge: true, delete_branch_on_merge: true }, - done: `自動マージを許可し、マージの後にブランチを消すようにした: ${repo}`, + body: { allow_auto_merge: true, delete_branch_on_merge: deleteBranch }, + done: deleteBranch + ? `自動マージを許可し、マージの後にブランチを消すようにした: ${repo}` + : `自動マージを許可し、マージの後にブランチを消さないようにした: ${repo}`, }, ]; } diff --git a/templates/.github/branch-protection.json b/templates/.github/branch-protection.json index 29115c6..522e4a9 100644 --- a/templates/.github/branch-protection.json +++ b/templates/.github/branch-protection.json @@ -1,5 +1,6 @@ { "required_checks": ["test / test", "ci / claude-review"], "develop_branch": "develop", - "main_branch": "main" + "main_branch": "main", + "delete_branch_on_merge": true }