diff --git a/.github/scripts/ci-safety.test.cjs b/.github/scripts/ci-safety.test.cjs index 359c345049..43f5b8abfd 100644 --- a/.github/scripts/ci-safety.test.cjs +++ b/.github/scripts/ci-safety.test.cjs @@ -77,10 +77,12 @@ if (args[0] === 'api') { } else if (route.includes('/actions/runs?')) value = { workflow_runs: data.workflowRuns }; else if (route.includes('/check-runs?')) value = { check_runs: data.checks }; else if (route.endsWith('/status')) value = { total_count: 0, state: 'pending' }; + else if (route.endsWith('/comments')) value = data.comments ?? []; else if (route.includes('/compare/')) value = route.endsWith('...' + data.devHead) ? { status: data.devAncestry } : { behind_by: data.behind }; else throw new Error('Unexpected API route: ' + route); } else if (args[0] === 'pr' && args[1] === 'merge') value = 'merged'; +else if (args[0] === 'pr' && args[1] === 'comment') value = 'commented'; else throw new Error('Unexpected gh command: ' + args.join(' ')); const queryIndex = args.indexOf('--jq'); if (queryIndex >= 0) { @@ -259,15 +261,71 @@ for (const status of ['queued', 'in_progress']) { } for (const conclusion of ['failure', 'timed_out', 'cancelled']) { - test(`dev ${conclusion} cannot fall back to an older green run`, t => { + test(`dev ${conclusion} blocks a stale PR even with an older green run`, t => { + const s = fixture(t, { behind: 1, devRuns: [ + { head_sha: 'dev-head', status: 'completed', conclusion }, + olderGreen, + ] }); + s.execute(); assert.equal(s.merged(), false); + }); +} + +test('dev recovery asks a stale PR to update its branch once per head', t => { + const devRuns = [{ head_sha: 'dev-head', status: 'completed', conclusion: 'failure' }, olderGreen]; + const comments = s => s.calls().filter(args => args[0] === 'pr' && args[1] === 'comment'); + const first = fixture(t, { behind: 1, devRuns }); + first.execute(); + assert.equal(comments(first).length, 1); + const repeat = fixture(t, { behind: 1, devRuns, + comments: [{ body: 'x y' }] }); + repeat.execute(); + assert.equal(comments(repeat).length, 0); +}); + +for (const conclusion of ['failure', 'timed_out', 'cancelled']) { + test(`dev ${conclusion} permits recovery from an up-to-date, fully green PR`, t => { const s = fixture(t, { devRuns: [ { head_sha: 'dev-head', status: 'completed', conclusion }, olderGreen, ] }); + s.execute(); assert.equal(s.merged(), true); + }); +} + +const failedDev = [{ head_sha: 'dev-head', status: 'completed', conclusion: 'failure' }, olderGreen]; + +for (const conclusion of ['skipped', 'failure', null]) { + test(`dev recovery rejects a PR with CI result ${conclusion ?? 'pending'}`, t => { + const s = fixture(t, { devRuns: failedDev, checks: [check('CI result', 10, conclusion)] }); s.execute(); assert.equal(s.merged(), false); }); } +test('dev recovery still requires every other check to pass', t => { + const s = fixture(t, { devRuns: failedDev, checks: [check('CI result', 10), check('Analyze', 10, 'failure')] }); + s.execute(); assert.equal(s.merged(), false); +}); + +test('dev recovery cannot merge a PR targeting a different base', t => { + const s = fixture(t, { devRuns: failedDev, pr: { + state: 'open', draft: false, labels: [{ name: 'automerge' }, { name: 'run-ci' }], + head: { sha: 'pr-head' }, base: { ref: 'main' }, + } }); + s.execute(); assert.equal(s.merged(), false); +}); + +test('dev recovery rejects failed CI from unrelated history', t => { + const s = fixture(t, { devRuns: [ + { head_sha: 'unrelated-dev', status: 'completed', conclusion: 'failure' }, + ], devAncestry: 'diverged' }); + s.execute(); assert.equal(s.merged(), false); +}); + +test('a dev update during recovery checks prevents a stale-base merge', t => { + const s = fixture(t, { devRuns: failedDev, devHeads: ['dev-head', 'new-dev'] }); + s.execute(); assert.equal(s.merged(), false); +}); + test('a newer retry of dev CI blocks merges until it finishes successfully', t => { const s = fixture(t, { devRuns: [ { head_sha: 'dev-head', status: 'in_progress', conclusion: null, run_attempt: 2 }, @@ -276,8 +334,8 @@ test('a newer retry of dev CI blocks merges until it finishes successfully', t = s.execute(); assert.equal(s.merged(), false); }); -test('a pending first attempt does not hide a newer known dev failure', t => { - const s = fixture(t, { devRuns: [ +test('a pending first attempt does not hide a newer known dev failure for a stale PR', t => { + const s = fixture(t, { behind: 1, devRuns: [ { head_sha: 'dev-head', status: 'queued', conclusion: null, run_attempt: 1 }, { head_sha: 'failed-dev', status: 'completed', conclusion: 'failure' }, olderGreen, diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml index 7f5946e750..cd4e093c6e 100644 --- a/.github/workflows/automerge.yml +++ b/.github/workflows/automerge.yml @@ -25,8 +25,8 @@ jobs: github.event_name == 'push' || (github.event_name == 'pull_request_target' && contains(github.event.pull_request.labels.*.name, 'automerge')) || - (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success' && - (github.event.workflow_run.event == 'pull_request' || + (github.event_name == 'workflow_run' && + ((github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success') || (github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'dev' && github.event.workflow_run.name == 'Flutter Tests'))) runs-on: ubuntu-latest @@ -107,27 +107,35 @@ jobs: head=$(jq -r .head.sha <<< "$pr") base=$(jq -r .base.ref <<< "$pr") - # ה-CI האחרון שהסתיים חייב להיות ירוק. ריצה חדשה על dev אינה חוסמת - # PR שכבר עבר CI, אבל כשל ידוע או הרצה חוזרת חדשה יותר כן חוסמים. - # הקומיט הירוק חייב להיות אב קדמון של dev הנוכחי (גם אחרי force push). + # כש-dev שבור ממוזג רק PR שמכיל את dev הנוכחי ועבר CI מלא, בלי ריבייס שלא נבדק. + # גם ריצה שנכשלה חייבת להשתייך להיסטוריה הנוכחית של dev. + dev_recovery=false dev_head=$(gh api "repos/$R/git/ref/heads/dev" --jq .object.sha) dev_runs=$(gh api "repos/$R/actions/workflows/flutter_tests.yml/runs?branch=dev&event=push&per_page=100" --jq '.workflow_runs') dev_ci=$(jq -c 'first(.[] | select(.status == "completed")) // empty' <<< "$dev_runs") - if [ -z "$dev_ci" ] || [ "$(jq -r .conclusion <<< "$dev_ci")" != success ]; then - echo "Waiting for a successful completed CI run on dev" - exit 0 - fi + [ -n "$dev_ci" ] || { echo "Waiting for a completed CI run on dev"; exit 0; } + case "$(jq -r .conclusion <<< "$dev_ci")" in + success) ;; + failure|timed_out|cancelled) + [ "$base" = dev ] || { echo "Waiting for green dev CI"; exit 0; } + dev_recovery=true ;; + *) echo "Waiting for a successful completed CI run on dev"; exit 0 ;; + esac if jq -e '(map(.status == "completed") | index(true)) as $i | any(.[:$i][]; (.run_attempt // 1) > 1)' <<< "$dev_runs" > /dev/null; then echo "Waiting for a newer dev CI retry to finish" exit 0 fi - green_head=$(jq -r '.head_sha // empty' <<< "$dev_ci") - [ -n "$green_head" ] || { echo "No verified green dev commit"; exit 0; } - if [ "$green_head" != "$dev_head" ]; then - relation=$(gh api "repos/$R/compare/$green_head...$dev_head" --jq .status) - [ "$relation" = ahead ] || { echo "Last green dev commit is not an ancestor of current dev"; exit 0; } + ci_head=$(jq -r '.head_sha // empty' <<< "$dev_ci") + [ -n "$ci_head" ] || { echo "No verified dev CI commit"; exit 0; } + if [ "$ci_head" != "$dev_head" ]; then + relation=$(gh api "repos/$R/compare/$ci_head...$dev_head" --jq .status) + [ "$relation" = ahead ] || { echo "Last completed dev CI commit is not an ancestor of current dev"; exit 0; } + fi + if [ "$dev_recovery" = true ]; then + echo "Dev recovery: require full PR CI and the current dev commit in the tested head" + else + echo "Continuing with green dev baseline $ci_head; current dev $dev_head" fi - echo "Continuing with green dev baseline $green_head; current dev $dev_head" base_head=$dev_head if [ "$base" != dev ]; then base_head=$(gh api "repos/$R/git/ref/heads/$base" --jq .object.sha) @@ -180,6 +188,15 @@ jobs: fi behind=$(gh api "repos/$R/compare/$base_head...$head" --jq .behind_by) + if [ "$dev_recovery" = true ] && [ "$behind" != 0 ]; then + echo "Dev recovery requires an up-to-date PR with successful CI; no automatic rebase" + marker="" + posted=$(gh api --paginate "repos/$R/issues/$PR/comments" --jq '.[].body') + if [[ "$posted" != *"$marker"* ]]; then + comment "⏳ המיזוג האוטומטי ממתין לעדכון הענף" "ה-CI של \`dev\` נכשל, ולכן ממוזג כעת רק ענף שמכיל את \`dev\` העדכני ועבר עליו CI מלא. עדכנו את הענף מול \`dev\` והוסיפו שוב את התווית \`automerge\`, או המתינו ש-\`dev\` יחזור לירוק, ואז הריבייס יתבצע אוטומטית."$'\n\n'"$marker" + fi + exit 0 + fi if [ "$behind" -gt 0 ]; then head_repo=$(jq -r .head.repo.full_name <<< "$pr") head_ref=$(jq -r .head.ref <<< "$pr") diff --git a/AGENTS.md b/AGENTS.md index 7ca055a3dd..9499f5f117 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -566,7 +566,7 @@ A pull request runs only Analyze and the guards (Lockfile, Goldens, UI Baseline, A failed run is retried once (except Analyze and the guards); a job that then passes is logged in the open `flaky-ci` issue, and a repeated failure moves the PR to draft. Flutter Tests also runs on every push to `dev`: a commit that fails twice there, after a green one, gets a revert PR from Otzaria Bot. A merge commit reverts its whole PR; a single-parent commit (including squash or rebase) reverts only that commit, never an inferred range based on the original PR's commit count. -The `automerge` label (added by someone with write access) merges the PR with a merge commit as Otzaria Bot once every check passed, including `CI result`. Merges share one queue and require successful CI on the current `dev` SHA, including after a retry. Checks from different workflows remain distinct; only superseded runs of the same workflow and event are replaced. The bot first rebases a branch that is behind, then merges it in the same run without waiting for CI on the rebased head (CI already passed on the PR's own head). Any push by a person removes `automerge`. +The `automerge` label (added by someone with write access) merges the PR with a merge commit as Otzaria Bot once every check passed, including `CI result`. Merges share one queue and normally require a successful completed CI run on an ancestor of the current `dev` SHA; a newer retry still blocks merging. If the latest completed `dev` CI failed, timed out or was cancelled, an approved PR targeting `dev` may recover it only when its fully tested head contains the current `dev` SHA, with no automatic rebase. Checks from different workflows remain distinct; only superseded runs of the same workflow and event are replaced. The bot first rebases a branch that is behind, then merges it in the same run without waiting for CI on the rebased head (CI already passed on the PR's own head). Any push by a person removes `automerge`. ## Keeping a Branch Up to Date