Skip to content

fix(bin): accept a green merge-result pipeline built for the current head in fm-pr-merge - #61

Merged
alandomio merged 2 commits into
mainfrom
fm/fm-merge-mr-pipeline
Oct 14, 2026
Merged

alandomio merged 2 commits into
mainfrom
fm/fm-merge-mr-pipeline

Conversation

@alandomio

Copy link
Copy Markdown
Owner

Intent

Fix bin/fm-pr-merge.sh so a GitLab merge request whose project runs only merge-result pipelines can be merged when that merge-result pipeline is green and current.

Problem: the GitLab guard refused unless the head pipeline ran at the exact current head commit ("the head pipeline ran at ..., not at the current head ..."). platform-infra and devops/observability run only merge-result pipelines, so the pipeline SHA is always a synthetic merge commit and every guarded merge there was refused. Receipt: platform-infra !1884 on 2026-10-14, approved twice, mergeable, merge-result pipeline success on 0536d43d, head 4995446e.

Acceptance (from the task):

  • A green merge-result pipeline that was built for the current head is accepted (verify the exact GitLab fields, do not guess). A merge-result pipeline built for an older head, a failed or running one, and a branch pipeline at a stale commit stay refused, and every failing condition is still reported.
  • Reuse the classification bin/fm-pr-status.sh already does ("badge is merge-result (success on )") instead of a second implementation, if reusable.
  • Behavioural tests colocated with the script cover: merge-result green and current (accepted), merge-result for a stale head (refused), merge-result failed (refused), branch pipeline at head (unchanged, accepted).
  • The script header documents the new accepted shape.
  • Firstmate's prior warning: a merge-result badge can sit on an untested head, so the merge-result run must be proven built for the current head, not merely green.

Decisions made:

  • "Built for the current head" is proven from the GitLab API: GET projects/:id/repository/commits/<head_pipeline.sha> returns parent_ids [target tip, MR source head] for a merge-result commit. Verified live, read-only, on platform-infra !1884: commit 0536d43d has parent_ids [0b8f6697 (target), 4995446e (live head)]. The pipelines API and before_sha do not expose the source head. The guard requires exactly two parents with parent_ids[1] == live head; an unreadable commit or any other shape refuses.
  • A merge-result pipeline is recognised only with source merge_request_event and ref exactly refs/merge-requests//merge; another merge request's merge ref is refused as a stale run.
  • Deliberate extra guard: when accepting via a merge-result run, the MR pipeline list is read and the newest non-external, non-merge-result pipeline at the live head (if any) must be success, because a recorded incident (platform-infra !1591) showed a green merge-result badge over a FAILED branch-head run; an unreadable list refuses. External (third-party commit status) entries are ignored, as fm-pr-status does.
  • The parent reads happen only for a green merge-result run; a failed/running merge-result run is refused on its status alone. The branch-pipeline-at-head path is unchanged and makes no extra API calls.
  • The first parent (target tip the run was built on) is intentionally not compared, matching how a branch pipeline at head is not re-run when the target moves.
  • Reuse: fm-pr-status's classification was embedded Python inside its row printer, not reusable as-is. The merge-result ref rule moved into a shared bash predicate fm_pr_gitlab_ref_is_merge_result in bin/fm-pr-lib.sh, used by fm-pr-merge and by fm-pr-status for its badge kind; fm-pr-status's per-run Python filter keeps an identical regex with a comment pointing to the shared predicate.
  • No agent co-author on commits (repo rule). Delivery: PR to this repo's origin; merge needs Alan's explicit word.

What Changed

  • bin/fm-pr-merge.sh: the GitLab guard now accepts a green merge-result pipeline (source merge_request_event, ref refs/merge-requests/<iid>/merge) only if its commit has exactly two parents and the second parent is the live head. It also requires the newest non-external branch pipeline at the head, if there is one, to be green. It still refuses merge-result runs that failed, are still running, were built for an older head or for another MR, and it reports every failing condition. The branch-pipeline-at-head path is unchanged, and the script header now documents the newly accepted shape.
  • bin/fm-pr-lib.sh: new shared predicate fm_pr_gitlab_ref_is_merge_result. Both fm-pr-merge.sh and bin/fm-pr-status.sh now use it to classify the badge kind instead of each having its own copy.
  • tests/fm-pr-merge.test.sh adds tests for these cases: merge-result green and current (accepted), stale head (refused), failed (refused), and branch pipeline at head (accepted). docs/architecture.md now mentions that merge-result pipelines are accepted.

🤖 Generated with Claude Code

Risk Assessment

✅ Low: The merge guard is relaxed only for a green merge-result run on this merge request's own merge ref, and only after the forge confirms it was built for the live head and no branch run at that head is red; every other shape is still refused, the header documents the new case, and colocated behavioural tests cover the required cases.

Testing

I ran the colocated behavioural suite for fm-pr-merge, which uses a stubbed glab, and it passes. A green merge-result run built for the current head (parent_ids[1] equals the live head) merges and reports the accepted shape. A merge-result run built for a stale head, a failed or running one, a green badge over a failed branch run at the head, and one whose build cannot be proven are all refused. The branch-pipeline-at-head path is unchanged. The fm-pr-status suite also passes after the shared-predicate refactor. Running the new tests against the pre-fix script fails the merge-result acceptance case, which reproduces the !1884 bug. There is no UI surface; the evidence is the CLI test transcripts.

Evidence: fm-pr-merge behavioural test transcript (post-fix)

Source: fm-pr-merge behavioural test transcript (post-fix)

ok - fm-pr-merge records pr= and pr_head= before invoking gh-axi pr merge
ok - fm-pr-merge propagates a real merge failure without silently succeeding
ok - fm-pr-merge forwards extra flags to gh-axi pr merge after the -- separator
ok - fm-pr-merge refuses before merging when task meta is missing
ok - fm-pr-merge refuses malformed PR URLs before calling gh-axi
ok - fm-pr-merge refuses unsafe PR URL segments before recording state
ok - fm-pr-merge refuses repo override args before recording state
ok - fm-pr-merge refuses a bundled short-option repo override and forwards other short flags
ok - fm-pr-merge does not add default --squash when the caller passes an explicit merge method
ok - fm-pr-merge respects --method=<value> as an explicit merge method
ok - fm-pr-merge parses a GitHub PR URL into gh-axi number and --repo arguments
ok - fm-pr-merge leaves GitHub extra-arg handling unchanged, including --sha
ok - fm-pr-merge merges a GitLab merge request through glab instead of refusing it
ok - fm-pr-merge takes the GitLab instance from the URL rather than assuming one
ok - fm-pr-merge imposes no merge method on GitLab, leaving the project's own one
ok - fm-pr-merge forwards extra flags to glab mr merge after the -- separator
ok - fm-pr-merge propagates a real glab merge failure without silently succeeding
ok - fm-pr-merge refuses on each GitLab pre-merge condition independently
ok - fm-pr-merge reports every failing GitLab condition, not only the first
ok - fm-pr-merge reports a stale recorded head and verifies the live one
ok - fm-pr-merge merges a GitLab merge request with no pipeline when jobs_enabled is false
ok - fm-pr-merge treats builds_access_level=disabled the same as jobs_enabled=false
ok - fm-pr-merge still refuses a pipeline-less GitLab merge request when the project can run CI
ok - fm-pr-merge never relaxes a pipeline that ran and failed, even on a CI-disabled project
ok - fm-pr-merge never relaxes a pipeline that succeeded at a stale commit, even on a CI-disabled project
ok - fm-pr-merge does not query project capability when the head pipeline already succeeded
ok - fm-pr-merge merges when a green merge-result pipeline was built for the current head
ok - fm-pr-merge refuses a merge-result pipeline built for an older head
ok - fm-pr-merge refuses a failed or running merge-result pipeline even when built for the head
ok - fm-pr-merge refuses a green merge-result badge over a failed branch run at the head
ok - fm-pr-merge refuses a merge-result pipeline it cannot prove was built for this request's head
ok - fm-pr-merge refuses an unreadable GitLab merge request state rather than merging blind
ok - fm-pr-merge refuses a GitLab head commit it cannot validate
ok - fm-pr-merge refuses before recording anything when glab or jq is absent
ok - fm-pr-merge refuses a GitLab head override before recording state
Evidence: fm-pr-status test transcript

Source: fm-pr-status test transcript

ok - case A: green(head) verdict and zero-approvals-required mergeable status
ok - case B: FAILED(head) verdict and not_approved read from merge status
ok - case C: manual(head) verdict and a real conflict
ok - case D: NO-HEAD-RUN with a failed merge-result badge and a draft note
ok - case E: a green badge on an unverified head is still reported as NO-HEAD-RUN
ok - case F: a merged merge request short-circuits to MERGED
ok - case G: a raw control character in the response does not break parsing
ok - case H: a source branch containing 'merge' is not misread as a merge-result run
ok - case I: a refs/merge-requests/<iid>/head run is recognised as a head run
ok - case J: shas are compared in full, not by their displayed 8-char prefix
ok - case K: an MR with no pipeline reports that, not a phantom merge-result run
ok - case L: an unparseable response reports UNREACHABLE and exits non-zero
ok - case M: a parseable non-MR error body reports UNREACHABLE and exits non-zero
ok - case N: a non-zero glab exit reports UNREACHABLE and exits non-zero
ok - case O: a shortname resolves from an https origin remote of projects/<name>
ok - case P: a shortname resolves from an scp-style origin remote of projects/<name>
ok - case Q: an unresolvable shortname fails clearly and never guesses an org prefix
ok - case R: a full group/project path is used verbatim without resolution
ok - case S: a failing pipelines lookup reports UNVERIFIED, never NO-HEAD-RUN
ok - case T: a non-list pipelines body reports UNVERIFIED, never NO-HEAD-RUN
ok - case U: with head_pipeline null, no arm invents a merge-result badge
ok - case V: a missing head sha is reported as unreachable, not as none(head)
ok - case W: failed external statuses never substitute for a CI verdict
ok - case X: external entries are excluded from the CI verdict but still disclosed
ok - case Y: a failed external status is never dropped on the badge-matches-head path
ok - case Z: a stale push badge is described as a push run, not a merge-result run
ok - case AA: unnamed external entries are identified individually, not by their shared branch
ok - case AB: the head's CI status comes from the newest run, not the first listed
ok - case AC: --help describes the verdicts and columns the tool really emits
ok - case AD: a full URL is read from its own host and reports the target branch
ok - case AE: a GitHub pull request URL is refused without a read
ok - case AF: approvals distinguish approved, not required, still required, and unreadable
ok - case AG: project CI capability and Pipelines must succeed are reported, or ? when unread
ok - case AH: a merged merge request reports its target branch
Evidence: New tests against the pre-fix script (reproduces the refusal)

Source: New tests against the pre-fix script (reproduces the refusal)

not ok - gitlab-merge-result-current: a green merge-result pipeline built for the head should merge: expected exit 0, got 1

ok - fm-pr-merge propagates a real merge failure without silently succeeding
ok - fm-pr-merge propagates a real glab merge failure without silently succeeding
ok - fm-pr-merge reports every failing GitLab condition, not only the first
ok - fm-pr-merge never relaxes a pipeline that ran and failed, even on a CI-disabled project
not ok - gitlab-merge-result-current: a green merge-result pipeline built for the head should merge: expected exit 0, got 1

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • bash tests/fm-pr-merge.test.sh (exit 0; covers (v) current-head merge-result accepted, (w) stale head refused, (x) failed/running refused, (y) green MR badge over failed branch run refused, (z) unprovable build refused, plus unchanged branch-at-head and all earlier GitLab/GitHub cases)
  • bash tests/fm-pr-status.test.sh (exit 0; the shared predicate refactor did not break the fm-pr-status badge classification)
  • Regression repro: ran the new tests/fm-pr-merge.test.sh plus bin/fm-pr-lib.sh against the base commit ac50aa5's fm-pr-merge.sh in a temporary worktree (since removed). gitlab-merge-result-current fails there with exit 1, so the test reproduces the original refusal
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Alan Domio added 2 commits October 14, 2026 12:48
…head in fm-pr-merge

Projects that run only merge-result pipelines (platform-infra,
devops/observability) never have a pipeline whose sha is the merge request
head, so every guarded merge there was refused. The guard now also accepts a
successful merge_request_event pipeline on refs/merge-requests/<iid>/merge
whose synthetic merge commit has the live head as its second parent, provided
no branch pipeline at that head is anything but success. Runs built for an
older head, failed or running runs, and unconfirmable builds stay refused,
with every failing condition reported.

The merge-result ref classification moves into fm-pr-lib.sh so fm-pr-status
and fm-pr-merge share one predicate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant