From d797551ce3aa7479555afb000e128fcbf939613f Mon Sep 17 00:00:00 2001 From: Negrescu Alexandru <124818050+negoro26@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:26:14 +0200 Subject: [PATCH 1/2] fix(upstream-sync): approve the held bot-PR run before merging --- .github/workflows/upstream-sync.yml | 25 ++++++++++++++++- PORTING.md | 12 ++++++-- omp-port/test-upstream-sync.sh | 43 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/.github/workflows/upstream-sync.yml b/.github/workflows/upstream-sync.yml index 40afd23..c1c79a1 100644 --- a/.github/workflows/upstream-sync.yml +++ b/.github/workflows/upstream-sync.yml @@ -170,12 +170,35 @@ jobs: [ -n "$id" ] || { echo "no gate run appeared for $SHA on $BRANCH" >&2; exit 1; } gh run watch "$id" --repo "$GITHUB_REPOSITORY" --exit-status --interval 30 + # GitHub holds the pull_request run of a PR this workflow opened until someone with write access + # approves it, and the held run blocks the main ruleset even though the gate job above already + # passed on the same commit. The bot approves its own held run and waits for it. If GitHub refuses + # that approval, it cancels the run and tries the merge anyway; a refused merge leaves the PR open. merge: needs: [sync, gate] runs-on: ubuntu-latest + permissions: + actions: write + contents: write + pull-requests: write env: GH_TOKEN: ${{ github.token }} PR: ${{ needs.sync.outputs.pr }} SHA: ${{ needs.sync.outputs.sha }} + BRANCH: ${{ needs.sync.outputs.branch }} steps: - - run: gh pr merge "$PR" --repo "$GITHUB_REPOSITORY" --squash --delete-branch --match-head-commit "$SHA" + - id: land + run: | + held=$(gh run list --repo "$GITHUB_REPOSITORY" --workflow gate.yml --event pull_request \ + --branch "$BRANCH" --json databaseId,headSha,conclusion \ + --jq "[.[] | select(.headSha == \"$SHA\" and .conclusion == \"action_required\")][].databaseId") + for id in $held; do + if gh api -X POST "repos/$GITHUB_REPOSITORY/actions/runs/$id/approve" >/dev/null; then + echo "approved held run $id" + gh run watch "$id" --repo "$GITHUB_REPOSITORY" --exit-status --interval 30 + else + echo "could not approve held run $id, cancelling it" + gh run cancel "$id" --repo "$GITHUB_REPOSITORY" || echo "could not cancel held run $id" + fi + done + gh pr merge "$PR" --repo "$GITHUB_REPOSITORY" --squash --delete-branch --match-head-commit "$SHA" diff --git a/PORTING.md b/PORTING.md index 4947314..5265181 100644 --- a/PORTING.md +++ b/PORTING.md @@ -215,9 +215,15 @@ only after that run passes. A dispatch, unlike a reusable call, reports the chec `smoke`, and `statechart` on the sync commit. The `main` ruleset requires exactly those three, and a personal repository cannot exempt the GitHub Actions bot from a ruleset, so a reusable call's `gate / gate` names would leave every bot merge blocked. The ruleset also requires a pull request -for every change to `main` and forbids force-pushes and deletion. A sync that fails the build, -`check-port.sh`, or `gate.yml` stays open for a human, and later runs stop at the open-PR guard -until someone resolves it. +for every change to `main` and forbids force-pushes and deletion. + +GitHub holds the `pull_request` run of a PR that `github.token` opened until a user with write access +approves it, and that held run blocks the ruleset even when the dispatched checks have passed on the +same commit. This stalled sync #60. Before it merges, the `merge` job therefore approves the held +`gate.yml` run at the sync commit and waits for it to pass. If GitHub refuses the bot's approval, it +cancels the run and tries the merge anyway. A sync that fails the build, `check-port.sh`, `gate.yml`, +or that merge stays open for a human, and later runs stop at the open-PR guard until someone +resolves it. Never substitute the `backnotprop/pstack` mirror for upstream. diff --git a/omp-port/test-upstream-sync.sh b/omp-port/test-upstream-sync.sh index 0e27ab4..82c1f31 100755 --- a/omp-port/test-upstream-sync.sh +++ b/omp-port/test-upstream-sync.sh @@ -37,6 +37,8 @@ for id in plan build; do bun -e "$EXTRACT" -- "$WF" sync "$id" "$WORK/$id.sh" >"$WORK/$id.shell" || { echo "FAIL extracting sync.$id from $WF" >&2; exit 1; } done +bun -e "$EXTRACT" -- "$WF" merge land "$WORK/land.sh" >"$WORK/land.shell" || + { echo "FAIL extracting merge.land from $WF" >&2; exit 1; } mkdir -p "$WORK/bin" "$WORK/port" cat >"$WORK/bin/gh" <<'EOF' @@ -57,6 +59,13 @@ while [ $# -gt 0 ]; do shift done case $sub in +"run list") + jq -r "$filter" "$FIX/runs.json" + ;; +"api -X") + [ ! -e "$FIX/approve-fails" ] || { echo "HTTP 403: Resource not accessible by integration" >&2; exit 1; } + ;; +"run watch" | "run cancel" | "pr merge") ;; "pr list") jq -c --arg s "$state" --arg h "$head" \ '[.[] | select($s == "all" or (.state | ascii_downcase) == $s) | select($h == "" or .headRefName == $h)]' \ @@ -263,5 +272,39 @@ expect "exit 0" exited 0 expect "lists drift" [ -s "$C/stdout" ] expect "the target is the first commit listed" [ "$(cut -c1-7 "$C/target" 2>/dev/null)" = "$(head -n1 "$C/stdout" | cut -f1)" ] +held_runs() { + jq -n --arg s "$1" '[{databaseId: 41, headSha: $s, conclusion: "action_required"}, + {databaseId: 42, headSha: $s, conclusion: "success"}, + {databaseId: 40, headSha: "0000000000000000000000000000000000000000", conclusion: "action_required"}]' >"$C/runs.json" +} +land() { run_step land PR=https://github.com/negoro26/pstack-omp/pull/99 SHA="$BUILT" BRANCH=sync/upstream-x GITHUB_REPOSITORY=negoro26/pstack-omp; } +called() { grep -q "$1" "$C/gh.log"; } +never_called() { ! called "$1"; } + +new_case land-approves-held-run +held_runs "$BUILT" +land +expect "exit 0" exited 0 +expect "approves the held run at the built commit" called "runs/41/approve" +expect "waits for it" called "run watch 41" +expect "leaves an older commit's run alone" never_called "runs/40/" +expect "leaves a run that already finished alone" never_called "runs/42/" +expect "merges" called "pr merge" + +new_case land-cancels-when-approval-refused +held_runs "$BUILT" +touch "$C/approve-fails" +land +expect "exit 0" exited 0 +expect "cancels the run it could not approve" called "run cancel 41" +expect "still tries the merge" called "pr merge" + +new_case land-no-held-run +echo '[]' >"$C/runs.json" +land +expect "exit 0" exited 0 +expect "approves nothing" never_called "approve" +expect "merges" called "pr merge" + [ "$fail" -eq 0 ] && echo "PASS upstream-sync workflow steps and check" exit "$fail" From c04cf540a5c3c3cb12ea671bcfbc0fbe147213db Mon Sep 17 00:00:00 2001 From: Negrescu Alexandru <124818050+negoro26@users.noreply.github.com> Date: Wed, 2 Dec 2026 13:35:28 +0200 Subject: [PATCH 2/2] fix(upstream-sync): drop the inert cancel, wait for the approved run to start, pin the step's guarantees --- .github/workflows/upstream-sync.yml | 17 ++++++++++------- PORTING.md | 9 +++++---- omp-port/test-upstream-sync.sh | 26 ++++++++++++++++++++------ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.github/workflows/upstream-sync.yml b/.github/workflows/upstream-sync.yml index c1c79a1..47d84e9 100644 --- a/.github/workflows/upstream-sync.yml +++ b/.github/workflows/upstream-sync.yml @@ -173,7 +173,7 @@ jobs: # GitHub holds the pull_request run of a PR this workflow opened until someone with write access # approves it, and the held run blocks the main ruleset even though the gate job above already # passed on the same commit. The bot approves its own held run and waits for it. If GitHub refuses - # that approval, it cancels the run and tries the merge anyway; a refused merge leaves the PR open. + # that approval, the merge fails as before and the PR stays open for a human. merge: needs: [sync, gate] runs-on: ubuntu-latest @@ -193,12 +193,15 @@ jobs: --branch "$BRANCH" --json databaseId,headSha,conclusion \ --jq "[.[] | select(.headSha == \"$SHA\" and .conclusion == \"action_required\")][].databaseId") for id in $held; do - if gh api -X POST "repos/$GITHUB_REPOSITORY/actions/runs/$id/approve" >/dev/null; then - echo "approved held run $id" - gh run watch "$id" --repo "$GITHUB_REPOSITORY" --exit-status --interval 30 - else - echo "could not approve held run $id, cancelling it" - gh run cancel "$id" --repo "$GITHUB_REPOSITORY" || echo "could not cancel held run $id" + if ! gh api -X POST "repos/$GITHUB_REPOSITORY/actions/runs/$id/approve" >/dev/null; then + echo "could not approve held run $id; approve it on the PR, then merge" >&2 + continue fi + echo "approved held run $id" + for _ in $(seq 1 30); do + [ "$(gh run view "$id" --repo "$GITHUB_REPOSITORY" --json status --jq .status)" != completed ] && break + sleep 10 + done + gh run watch "$id" --repo "$GITHUB_REPOSITORY" --exit-status --interval 30 done gh pr merge "$PR" --repo "$GITHUB_REPOSITORY" --squash --delete-branch --match-head-commit "$SHA" diff --git a/PORTING.md b/PORTING.md index 5265181..df01380 100644 --- a/PORTING.md +++ b/PORTING.md @@ -220,10 +220,11 @@ for every change to `main` and forbids force-pushes and deletion. GitHub holds the `pull_request` run of a PR that `github.token` opened until a user with write access approves it, and that held run blocks the ruleset even when the dispatched checks have passed on the same commit. This stalled sync #60. Before it merges, the `merge` job therefore approves the held -`gate.yml` run at the sync commit and waits for it to pass. If GitHub refuses the bot's approval, it -cancels the run and tries the merge anyway. A sync that fails the build, `check-port.sh`, `gate.yml`, -or that merge stays open for a human, and later runs stop at the open-PR guard until someone -resolves it. +`gate.yml` run at the sync commit and waits for it to pass. Whether GitHub accepts that approval from +the workflow's own token is not documented. If it refuses, the job says so, the merge fails as before, +and the PR waits for a human to approve the run. A sync that fails the build, `check-port.sh`, +`gate.yml`, or that merge stays open for a human, and later runs stop at the open-PR guard until +someone resolves it. Never substitute the `backnotprop/pstack` mirror for upstream. diff --git a/omp-port/test-upstream-sync.sh b/omp-port/test-upstream-sync.sh index 82c1f31..3f95f21 100755 --- a/omp-port/test-upstream-sync.sh +++ b/omp-port/test-upstream-sync.sh @@ -65,7 +65,13 @@ case $sub in "api -X") [ ! -e "$FIX/approve-fails" ] || { echo "HTTP 403: Resource not accessible by integration" >&2; exit 1; } ;; -"run watch" | "run cancel" | "pr merge") ;; +"run view") + echo in_progress + ;; +"run watch") + [ ! -e "$FIX/watch-fails" ] || { echo "run 41 failed" >&2; exit 1; } + ;; +"pr merge") ;; "pr list") jq -c --arg s "$state" --arg h "$head" \ '[.[] | select($s == "all" or (.state | ascii_downcase) == $s) | select($h == "" or .headRefName == $h)]' \ @@ -287,17 +293,25 @@ land expect "exit 0" exited 0 expect "approves the held run at the built commit" called "runs/41/approve" expect "waits for it" called "run watch 41" +expect "lists only the gate's pull_request runs on the sync branch" called "run list --repo negoro26/pstack-omp --workflow gate.yml --event pull_request --branch sync/upstream-x" expect "leaves an older commit's run alone" never_called "runs/40/" expect "leaves a run that already finished alone" never_called "runs/42/" -expect "merges" called "pr merge" +expect "merges after the approval" [ "$(grep -n 'pr merge' "$C/gh.log" | cut -d: -f1)" -gt "$(grep -n 'approve' "$C/gh.log" | cut -d: -f1)" ] + +new_case land-failed-held-run-blocks-merge +held_runs "$BUILT" +touch "$C/watch-fails" +land +expect "nonzero exit" failed +expect "no merge" never_called "pr merge" -new_case land-cancels-when-approval-refused +new_case land-approval-refused held_runs "$BUILT" touch "$C/approve-fails" land -expect "exit 0" exited 0 -expect "cancels the run it could not approve" called "run cancel 41" -expect "still tries the merge" called "pr merge" +expect "says the run needs a human" logged "could not approve held run 41" +expect "does not watch a run it could not approve" never_called "run watch" +expect "still tries the merge, which GitHub refuses while the run is held" called "pr merge" new_case land-no-held-run echo '[]' >"$C/runs.json"