Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 からは実行しない。

Expand Down
23 changes: 23 additions & 0 deletions scripts/ci/branch-protection.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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' }, /同じ/],
];

Expand Down
6 changes: 4 additions & 2 deletions skills/branch-protection/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 の画面で足した保護の設定 (承認の人数など) は、実行すると消える。変えたいときは、このスキルを直す。
Expand All @@ -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
}
```

Expand All @@ -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 の削除など) を済ませてから、自分でブランチを消す (消せなければブランチが残り、気づける) |

ほかのキーがあると、書き間違いとみなして止まる。

Expand Down
15 changes: 11 additions & 4 deletions skills/branch-protection/scripts/apply.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { spawnSync } from 'node:child_process';
import { readFileSync, statSync } from 'node:fs';

const USAGE = '使い方: apply.sh [--dry-run] [--repo <owner/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}`);
Expand Down Expand Up @@ -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 が同じ');
}
Expand All @@ -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 },
Expand All @@ -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',
Expand All @@ -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}`,
},
];
}
Expand Down
3 changes: 2 additions & 1 deletion templates/.github/branch-protection.json
Original file line number Diff line number Diff line change
@@ -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
}