diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ad6e91e..70ee88c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,9 +5,18 @@ on: branches: [main] pull_request: +# main の push は実行ごとに分ける。グループを共有すると待機中の push まで置き換わる。 +concurrency: + group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + permissions: contents: read +env: + # PR は base と checkout 済みのマージ結果、main は push 前後のツリーを比較する。 + CI_DIFF_BASE: ${{ github.event.pull_request.base.sha || github.event.before }} + jobs: # README は node 24 以上を求めているので、その下限で実際に全チェックを回す。 # @@ -17,6 +26,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 + with: + fetch-depth: 2 - uses: actions/setup-node@v7 with: @@ -35,7 +46,7 @@ jobs: if [ "$(cut -d. -f1 .node-version)" = "$(node -p 'process.versions.node.split(".")[0]')" ]; then ./scripts/check.sh --fast else - ./scripts/check.sh + ./scripts/check.sh --ci "$CI_DIFF_BASE" fi # シェルスクリプトの振る舞いを両 OS で確かめる。Linux で通って macOS(bash 3.2)で落ちる bash 4 構文と、 @@ -48,6 +59,8 @@ jobs: runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v7 + with: + fetch-depth: 2 - uses: actions/setup-node@v7 with: @@ -64,7 +77,7 @@ jobs: - name: 全チェック run: | node --check scripts/lib/merge-settings.mjs - ./scripts/check.sh + ./scripts/check.sh --ci "$CI_DIFF_BASE" # echo ではなく printf。shell によっては JSON 文字列の \n を展開して hook に壊れた JSON を渡し、 # hook が正しく無視する結果、間違った理由でテストが通ってしまう。 @@ -229,7 +242,7 @@ jobs: if (!c.delegate_reason) { console.error(`::error::${c.id}: 理由なしで委譲されている`); bad = 1; } continue; } - if (coveredByCheckSh(c.cmd)) { console.log("skip " + c.id + "(check.sh の段として shell ジョブで実行済み)"); continue; } + if (coveredByCheckSh(c.cmd)) { console.log("skip " + c.id + "(shell ジョブの check.sh が担当)"); continue; } try { execSync(c.cmd, { stdio: "pipe" }); console.log("pass " + c.id); } catch (e) { console.error(`::error::プロファイルのチェック "${c.id}" がクリーンなチェックアウトで落ちる`); bad = 1; } } diff --git a/profiles/dotagents.json b/profiles/dotagents.json index 47f95c2..d8600f8 100644 --- a/profiles/dotagents.json +++ b/profiles/dotagents.json @@ -192,6 +192,14 @@ "hooks/**" ] }, + { + "id": "ci-loop-selection", + "cmd": "node scripts/test-ci-loop.mjs", + "gate": true, + "agent_may_run": true, + "scope": "all", + "paths": [".github/workflows/ci.yml", "scripts/check.sh", "scripts/lib/pr-templates-only.sh", "scripts/test-ci-loop.mjs"] + }, { "id": "loop-tests", "cmd": "./scripts/test-loop.sh", diff --git a/scripts/check.sh b/scripts/check.sh index 0438402..ba5dd54 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -3,6 +3,7 @@ # # scripts/check.sh 全部を実行 # scripts/check.sh --fast 振る舞いのスイートを飛ばす(構文と lint だけ) +# scripts/check.sh --ci <比較元 SHA> PR テンプレートだけの差分ではループ検査を省く # # 検査器が複数あるのは、確かめる対象も落ちる理由も違うため。覚えるものを 1 つにするためにこれがある。 @@ -121,7 +122,12 @@ if (( ! FAST )); then step "検証の固定課題と故障注入" ./scripts/test-verification-evidence.sh step "lint-hook と lint の範囲" ./scripts/test-lint-hook.sh step "インストーラの振る舞い" ./scripts/test-setup.sh - step "ループ駆動の振る舞い" ./scripts/test-loop.sh + step "CI がループ検査を省く差分の判定" node scripts/test-ci-loop.mjs + if [[ "${1:-}" == "--ci" ]] && bash scripts/lib/pr-templates-only.sh "${2:-}"; then + echo "省略: ループ駆動の振る舞い(PR テンプレートのみ変更)" + else + step "ループ駆動の振る舞い" ./scripts/test-loop.sh + fi step "人の入力を待たない" ./scripts/test-non-interactive.sh tree_clean() { [[ "$(git status --porcelain 2>/dev/null)" == "$tree_before" ]]; } diff --git a/scripts/lib/pr-templates-only.sh b/scripts/lib/pr-templates-only.sh new file mode 100644 index 0000000..1ab0659 --- /dev/null +++ b/scripts/lib/pr-templates-only.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# 終了 0 は、比較した差分が PR テンプレートだけだった場合に限る。 +set -u +base=${1:-} +[[ "$base" =~ ^[0-9a-f]{40}$ && ! "$base" =~ ^0+$ ]] || exit 1 + +# depth=2 で通常の push と PR のマージ結果を比較できる。複数コミットの push では比較元だけを取得する。 +if ! git cat-file -e "${base}^{commit}" 2>/dev/null; then + git fetch --quiet --no-tags --depth=1 origin "$base" || exit 1 +fi +files=$(git diff --name-only --no-renames --ignore-submodules=none "$base" HEAD --) || exit 1 +[[ -n "$files" ]] || exit 1 +while IFS= read -r path; do + case "$path" in + .github/PULL_REQUEST_TEMPLATE.md|skills/da-pr/reference/pr-template.md) ;; + *) exit 1 ;; + esac +done <<< "$files" diff --git a/scripts/test-ci-loop.mjs b/scripts/test-ci-loop.mjs new file mode 100644 index 0000000..246c325 --- /dev/null +++ b/scripts/test-ci-loop.mjs @@ -0,0 +1,72 @@ +// 一時リポジトリのコミット間を比較し、CI がループ検査を省略できる差分を確かめる。 +import assert from 'node:assert/strict'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { mkdirSync, mkdtempSync, renameSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; + +const root = mkdtempSync(join(tmpdir(), 'dotagents-ci-loop-')); +const repo = join(root, 'repo'); +const checker = join(import.meta.dirname, 'lib/pr-templates-only.sh'); +const template = '.github/PULL_REQUEST_TEMPLATE.md'; +const reference = 'skills/da-pr/reference/pr-template.md'; +const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_'))); +Object.assign(env, { GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' }); +function git(...args) { + return execFileSync('git', args, { cwd: repo, env, encoding: 'utf8' }).trim(); +} +function write(file, value) { + mkdirSync(dirname(join(repo, file)), { recursive: true }); + writeFileSync(join(repo, file), value); +} +function commit() { + git('add', '-A'); + git('commit', '-qm', '検証'); + return git('rev-parse', 'HEAD'); +} +function check(label, base, expected, cwd = repo) { + const result = spawnSync('bash', [checker, base], { cwd, env, encoding: 'utf8' }); + assert.equal(result.status, expected, label + '\n' + result.stderr); + console.log('✓ ' + label); +} + +try { + mkdirSync(repo); + git('init', '-q'); + git('config', 'user.name', 'CI'); + git('config', 'user.email', 'ci@example.invalid'); + write('README.md', '説明'); + const initial = commit(); + write(template, 'テンプレート'); + const first = commit(); + check('PR テンプレート単独', initial, 0); + write(reference, '本文テンプレート'); + const templates = commit(); + check('da-pr のテンプレート単独', first, 0); + check('複数コミットで両方のテンプレートを変更', initial, 0); + const shallow = join(root, 'shallow'); + git('clone', '-q', '--depth=2', 'file://' + repo, shallow); + check('浅い checkout にない push 前のコミットも比較する', initial, 0, shallow); + check('差分なし', git('rev-parse', 'HEAD'), 1); + check('比較元がない初回 push', '0'.repeat(40), 1); + check('比較元を取得できない', '1'.repeat(40), 1); + + write('README.md', '更新'); + commit(); + write(template, '追記'); + commit(); + check('最後のコミットだけがテンプレートでも PR 全体を検査する', templates, 1); + git('checkout', '-qb', 'rename', initial); + mkdirSync(dirname(join(repo, template)), { recursive: true }); + renameSync(join(repo, 'README.md'), join(repo, template)); + commit(); + check('対象外ファイルをテンプレートに改名', initial, 1); + + git('checkout', '-qb', 'base', initial); + write('README.md', '先に main で入った変更'); + const advancedBase = commit(); + git('merge', '-q', '--no-edit', templates); + check('進んだ base と PR のマージ結果を比較する', advancedBase, 0); +} finally { + rmSync(root, { recursive: true, force: true }); +}