diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5177ebc..a4e46bf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -82,7 +82,8 @@ jobs: BASE_SHA: ${{ github.event.pull_request.base.sha }} run: | set -euo pipefail - if git diff --name-only "${BASE_SHA}...HEAD" | grep -qE '^(buf\.gen\.yaml$|go\.(mod|sum)$|mise\.toml$|proto/|openshell/|e2e/|hack/automation/(start|stop)-gateway\.sh$|\.github/workflows/e2e\.yml$)'; then + changed_paths=$(git diff --name-only "${BASE_SHA}...HEAD") + if grep -qE '^(buf\.gen\.yaml$|go\.(mod|sum)$|mise\.toml$|proto/|openshell/|e2e/|hack/automation/(start|stop)-gateway\.sh$|\.github/workflows/e2e\.yml$)' <<<"$changed_paths"; then echo "e2e=true" >>"$GITHUB_OUTPUT" else echo "e2e=false" >>"$GITHUB_OUTPUT" @@ -93,3 +94,22 @@ jobs: needs: changes if: needs.changes.outputs.e2e == 'true' uses: ./.github/workflows/e2e.yml + + e2e-gate: + name: e2e-gate + needs: [changes, e2e] + if: ${{ always() && github.event_name == 'pull_request' }} + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Check E2E result + env: + CHANGES_RESULT: ${{ needs.changes.result }} + E2E_REQUIRED: ${{ needs.changes.outputs.e2e }} + E2E_RESULT: ${{ needs.e2e.result }} + run: | + case "$CHANGES_RESULT:$E2E_REQUIRED:$E2E_RESULT" in + success:true:success | success:false:skipped) exit 0 ;; + esac + echo "E2E gate failed (changes=$CHANGES_RESULT, required=$E2E_REQUIRED, e2e=$E2E_RESULT)." >&2 + exit 1 diff --git a/.github/workflows/core-watch.yml b/.github/workflows/core-watch.yml index 15658b9..ac762a0 100644 --- a/.github/workflows/core-watch.yml +++ b/.github/workflows/core-watch.yml @@ -1,8 +1,10 @@ # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -# Opens one update proposal per new core release tag, then reconciles the RPC -# coverage issues against the newest core tag (hack/automation/). +# Opens one update proposal per new core release tag and reconciles the RPC +# coverage issues against the newest core tag; when auto-merge is on, a +# separate job merges stable proposals that change no proto +# (hack/automation/). name: core-watch on: @@ -43,6 +45,7 @@ jobs: permission-contents: write permission-pull-requests: write permission-issues: write + permission-checks: ${{ vars.CORE_AUTO_MERGE == 'on' && 'write' || '' }} - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -71,6 +74,7 @@ jobs: env: GH_TOKEN: ${{ steps.app-token.outputs.token }} INPUT_TAG: ${{ inputs.tag }} + CORE_AUTO_MERGE: ${{ vars.CORE_AUTO_MERGE }} run: | set -euo pipefail if [ -n "$INPUT_TAG" ]; then @@ -103,3 +107,53 @@ jobs: GH_TOKEN: ${{ steps.app-token.outputs.token }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: hack/automation/report-failure.sh core-watch "$RUN_URL" + + # Merges stable proposals that change no proto and whose checks went green + # (hack/automation/merge-bump-pr.sh). Only when the CORE_AUTO_MERGE + # repository variable is on. A separate job with a separate App: only the + # merge App is a bypass actor on main-integrity, the bump App that pushes + # core/* branches is not, and this job runs no generator or other code + # from core. + merge: + name: merge + needs: watch + if: ${{ !cancelled() && vars.CORE_AUTO_MERGE == 'on' }} + environment: sdk-bot + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - id: app-token + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + client-id: ${{ secrets.SDK_MERGE_CLIENT_ID }} + private-key: ${{ secrets.SDK_MERGE_PRIVATE_KEY }} + permission-contents: write + permission-pull-requests: write + permission-checks: read + permission-statuses: read + permission-issues: write + + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - uses: jdx/mise-action@236a21c3b91f5db6d4e174b348674a0f0caac06d # v5.2.9 + with: + install: false + + - name: Install tools + run: mise install --locked yq + + - name: Merge proposals that change no proto + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + CORE_AUTO_MERGE: ${{ vars.CORE_AUTO_MERGE }} + run: hack/automation/merge-bump-pr.sh + + - name: Report failure + if: failure() + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + run: hack/automation/report-failure.sh core-watch-merge "$RUN_URL" diff --git a/hack/automation/README.md b/hack/automation/README.md index 80abd68..969857c 100644 --- a/hack/automation/README.md +++ b/hack/automation/README.md @@ -10,6 +10,9 @@ Workflows run them with a token of the `ai-openshell-sdk-go` GitHub App (`ai-ope | `open-bump-pr.sh [--dry-run]` | `core:bump` | Open the update proposal for one core tag: branch `core/`, pin change, regenerated bindings, proto diff in the body, labels `core-bump` plus `stable` or `prerelease`. Closes older pre-release proposals. | | `bump-pr-body.md` | | PR body template for `open-bump-pr.sh`. | | `core-registry.sh` | | Sourced by `start-gateway.sh` and `open-bump-pr.sh`: `core_registry ` prints the registry that holds a core release's images, `quay.io/ai-openshell` for releases since the move and `ghcr.io/nvidia/openshell` for older ones. | +| `merge-bump-pr.sh [--dry-run] [--auto-merge on\|off]` | `core:merge` | Merge stable proposals that change no proto: one commit with a provenance check from the bump App, exactly the pin bump, green checks, no `hold` label. Off unless `CORE_AUTO_MERGE` is `on`. | +| `pin-bump.sh` | | Sourced by `open-bump-pr.sh` and `merge-bump-pr.sh`: `pin_bump_edit ` makes the pin and README edits of a bump, `pin_bump_tree ` prints the tree a bump commit with only those edits has, `older ` compares release tags. | +| `merge-bump-pr-test.sh` | | Offline tests for `pin-bump.sh` and `merge-bump-pr.sh` in scratch Git repositories, with the gh stub in `testdata/merge-bump/bin/`; part of `workflows:check`. | | `tag-release.sh [--dry-run]` | `release:tag` | After a stable pin lands on `main`: create the lockstep tag `vX.Y.Z` and the GitHub release, then ask the Go module proxy for the version. | | `drift-check.sh --out ` | `core:drift` | Compare core `main` against the pin: `buf breaking`, regenerate and build, uncovered RPCs. Writes the markdown report. | | `update-drift-issue.sh ` | | Create, update, or close the single `Core main drift` issue from a report; uncovered RPCs link to their open RPC coverage issues. | @@ -28,7 +31,7 @@ Workflows run them with a token of the `ai-openshell-sdk-go` GitHub App (`ai-ope | Workflow | Script | |----------|--------| -| `core-watch.yml` (hourly) | `core-tags.sh`, then `open-bump-pr.sh` per tag, then `rpc-issues.sh` | +| `core-watch.yml` (hourly) | job `watch`: `core-tags.sh`, then `open-bump-pr.sh` per tag, then `rpc-issues.sh`; job `merge` (only when `CORE_AUTO_MERGE` is `on`): `merge-bump-pr.sh` | | `release-tag.yml` (push to `main`) | `tag-release.sh` | | `core-drift.yml` (nightly) | `drift-check.sh`, then `update-drift-issue.sh` | | `e2e.yml` (reusable) | `start-gateway.sh`, `mise run e2e`, `stop-gateway.sh` | @@ -52,3 +55,30 @@ Workflows run them with a token of the `ai-openshell-sdk-go` GitHub App (`ai-ope If they are expected, run core-watch by hand with the input `max_new_issues`, or locally `mise run core:rpc-issues -- --max-new `. - **Preview**: `mise run core:rpc-issues -- --dry-run` prints every planned action with its body and changes nothing (it reads GitHub, so `gh` must be logged in). Without `--dry-run`, the script applies the plan only with an explicit `BOT_LOGIN` and the bot's token (core-watch passes both); under a person's `gh` login it stops before any write, because later runs would not manage the issues it opened. + +## Auto-merge for stable pins + +`merge-bump-pr.sh` is off until the repository variable `CORE_AUTO_MERGE` is `on`. +It merges a stable proposal only when the release changes no proto: the regenerated bindings under `proto/` are identical, so the SDK needs no work and can be tagged right away. +A release that changes the protos always waits for review; the SDK release may lag until the new features are wrapped and usable (see the RPC coverage issues). + +The proposal must be exactly a pin bump. +`pin_bump_tree` rebuilds the expected tree from the merge base with the same edits `open-bump-pr.sh` makes (the pin in `buf.gen.yaml` and the README line), and the bump commit's tree has to match it. +Any other change sends the proposal to review, including one inside `buf.gen.yaml` such as a plugin entry, a changed conformance fixture, or a second commit. +When auto-merge is on, the bump App records a `core-bump/provenance` check on the stable commit it generated. The merge script verifies the App ID and the exact PR head SHA. A new head without that check goes through review, including proposals opened before auto-merge was enabled. +The proposal also goes back to review when a check fails or someone adds the `hold` label. +It waits for review, too, when its tag is not newer than the pin on `main`, or when `main` moved the pin since the proposal was cut (after an earlier proposal merged, a second one from the same base would only conflict). +If GitHub refuses a merge, the script leaves that proposal for review, goes on with the others, and fails the run so the failure is reported; the comment explaining an unreviewed merge is posted only after the merge succeeds. +Merges happen on the hourly run after the checks turn green, so a stable SDK release follows the core release within about two hours. + +The merge runs in its own core-watch job, `merge`, with the token of a second GitHub App that only merges (secrets `SDK_MERGE_CLIENT_ID` and `SDK_MERGE_PRIVATE_KEY` in `sdk-bot`). +That job runs no generator or other code from core. +The script checks the proposal; who may write a `core/*` branch and who may skip review are set by repository rules. +Before an admin sets `CORE_AUTO_MERGE` to `on`: + +1. A ruleset on `core/*` lets only the `ai-openshell-sdk-go` App and maintainers create, update, or delete those branches. +2. A ruleset on `main` requires the GitHub Actions `ci` and `e2e-gate` checks with no bypass actors, and the separate DCO rule stays in force. Auto-merge also requires an actual `e2e / e2e` pass on the proposal. +3. The bump App installation has checks write so `core-watch` can record provenance when auto-merge is enabled. +4. The merge App is installed on this repository with contents and pull requests write, checks and statuses read, and issues write (for failure reports), and its secrets are in `sdk-bot`. +5. The merge App is a PR-only bypass actor for `main-integrity` on this repository, so its merge can skip the code owner review. +6. `mise run core:merge -- --dry-run --auto-merge on` with the merge App's token lists the open stable proposals it would merge, and each skip reason matches what you expect. diff --git a/hack/automation/bump-pr-body.md b/hack/automation/bump-pr-body.md index 2120bb9..7064c61 100644 --- a/hack/automation/bump-pr-body.md +++ b/hack/automation/bump-pr-body.md @@ -14,3 +14,4 @@ Move the core pin from `{{OLD}}` to `{{NEW}}` ({{KIND}}). - CI and E2E must pass. If the SDK needs changes for this core version, push them to this branch. - Merging a stable pin tags the SDK release `{{NEW}}`. Pre-release pins are not tagged; a newer pre-release proposal replaces this one. +- When auto-merge is on (repository variable `CORE_AUTO_MERGE`), core-watch merges a stable proposal without review if the bump App recorded provenance for its single commit, it changes nothing but the pin and the README line, and its checks are green. Add the `hold` label to keep it for review. diff --git a/hack/automation/merge-bump-pr-test.sh b/hack/automation/merge-bump-pr-test.sh new file mode 100755 index 0000000..67cbf6e --- /dev/null +++ b/hack/automation/merge-bump-pr-test.sh @@ -0,0 +1,219 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# merge-bump-pr-test.sh: offline tests for the stable pin auto-merge. +# +# Usage: merge-bump-pr-test.sh +# +# Run from the root of an sdk-go clone. Checks pin_bump_tree and older +# (pin-bump.sh) in a scratch Git repository, then runs merge-bump-pr.sh +# against scratch repositories with a gh stub (testdata/merge-bump/bin/gh) +# that serves PR lists, check runs and checks from files and squash-merges +# into a local bare origin. Exits 1 on any failed case. +set -euo pipefail + +here=$(pwd -P)/hack/automation +stub_bin=$here/testdata/merge-bump/bin +# shellcheck source=hack/automation/pin-bump.sh +. "$here/pin-bump.sh" + +die() { echo "ERROR: $*" >&2; exit 1; } +[ "$(git rev-parse --show-toplevel)" = "$(pwd -P)" ] || die "run this from the root of the sdk-go clone" + +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +failed=0 +fail() { echo "FAIL $1"; failed=1; } +pass() { echo "ok $1"; } + +export GIT_AUTHOR_NAME=test GIT_AUTHOR_EMAIL=test@example.com +export GIT_COMMITTER_NAME=test GIT_COMMITTER_EMAIL=test@example.com +export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 + +# new_case : a bare origin whose main pins core v0.1.10, and a clone +# of it in $repo. Sets $case_dir, $repo and $state (the gh stub's state). +new_case() { + case_dir=$work/$1 + state=$case_dir/state + repo=$case_dir/repo + mkdir -p "$state" "$repo" + git init --quiet --bare -b main "$state/origin.git" + git -C "$repo" init --quiet -b main + git -C "$repo" remote add origin "$state/origin.git" + cat >"$repo/buf.gen.yaml" <<'EOF' +version: v2 +plugins: + - remote: buf.build/protocolbuffers/go + out: proto +inputs: + - git_repo: https://github.com/ai-openshell/core + tag: v0.1.10 +EOF + printf '# SDK\n\nThe bindings are generated from OpenShell core v0.1.10.\n' >"$repo/README.md" + mkdir -p "$repo/proto" + echo "package proto" >"$repo/proto/api.pb.go" + git -C "$repo" add -A + git -C "$repo" commit --quiet -m init + git -C "$repo" push --quiet origin main + echo '[]' >"$state/prs.json" + echo '[{"name":"ci","bucket":"pass"},{"name":"e2e / e2e","bucket":"pass"},{"name":"DCO","bucket":"pass"}]' >"$state/checks.json" + : >"$state/events" +} + +# propose [...]: commit a pin bump to on +# core/ from origin/main, run the optional extra edit in the clone +# before committing, push it, and list it as PR (labels: $labels, +# draft: $draft, fork: $fork). Sets $sha. +propose() { + local n=$1 tag=$2 kind=stable + shift 2 + case $tag in *-pre.*) kind=prerelease ;; esac + git -C "$repo" checkout --quiet -B "core/$tag" origin/main + (cd "$repo" && pin_bump_edit "$tag" "$kind" && if [ $# -gt 0 ]; then "$@"; fi) + git -C "$repo" commit --quiet -am "chore(core): track $tag" + git -C "$repo" push --quiet origin "core/$tag" + sha=$(git -C "$repo" rev-parse HEAD) + git -C "$repo" checkout --quiet --detach origin/main + jq --argjson n "$n" --arg head "core/$tag" --arg sha "$sha" \ + --argjson draft "${draft:-false}" --argjson fork "${fork:-false}" --arg labels "${labels:-core-bump,stable}" \ + '. + [{number: $n, headRefName: $head, headRefOid: $sha, baseRefName: "main", + isDraft: $draft, isCrossRepository: $fork, + labels: ($labels | split(",") | map({name: .}))}]' \ + "$state/prs.json" >"$state/prs.tmp" + mv "$state/prs.tmp" "$state/prs.json" +} + +# run_merge: run merge-bump-pr.sh in the clone; output in $out, status in $rc. +run_merge() { + rc=0 + out=$(cd "$repo" && PATH="$stub_bin:$PATH" GH_STUB_STATE="$state" \ + "$here/merge-bump-pr.sh" --auto-merge on 2>&1) || rc=$? +} + +main_pin() { git -C "$state/origin.git" show main:buf.gen.yaml | yq -r '.inputs[0].tag'; } +events() { paste -sd, "$state/events"; } + +# expect +expect() { + local name=$1 what=$2 + shift 2 + if "$@"; then pass "$name: $what"; else + fail "$name: $what" + printf ' output:\n%s\n events: %s\n' "$(sed 's/^/ /' <<<"$out")" "$(events)" + fi +} +says() { grep -qF -- "$1" <<<"$out"; } + +# --- older --------------------------------------------------------------- +for pair in "v0.1.9 v0.1.10" "v0.1.10-pre.2 v0.1.10" "v0.1.10-pre.2 v0.1.10-pre.10" "v0.9.0 v1.0.0"; do + # shellcheck disable=SC2086 # two words on purpose + if older $pair; then pass "older $pair"; else fail "older $pair"; fi +done +for pair in "v0.1.10 v0.1.10" "v0.1.10 v0.1.9" "v0.1.10 v0.1.10-pre.2"; do + # shellcheck disable=SC2086 + if older $pair; then fail "not older $pair"; else pass "not older $pair"; fi +done + +# --- pin_bump_tree ------------------------------------------------------- +new_case tree +base=$(git -C "$repo" rev-parse HEAD) +propose 1 v0.1.11 +expect tree "stable bump commit matches the rebuilt tree" \ + test "$(cd "$repo" && pin_bump_tree "$base" v0.1.11 stable)" = "$(git -C "$repo" rev-parse "$sha^{tree}")" +expect tree "the caller's index and worktree stay clean" \ + test -z "$(git -C "$repo" status --porcelain)" +propose 2 v0.1.12 yq -i '.plugins += [{"local": "protoc-gen-evil", "out": "proto"}]' buf.gen.yaml +expect tree "an extra Buf plugin does not match" \ + test "$(cd "$repo" && pin_bump_tree "$base" v0.1.12 stable)" != "$(git -C "$repo" rev-parse "$sha^{tree}")" +propose 3 v0.1.13 sh -c 'echo "// changed" >>proto/api.pb.go' +expect tree "a change under proto/ does not match" \ + test "$(cd "$repo" && pin_bump_tree "$base" v0.1.13 stable)" != "$(git -C "$repo" rev-parse "$sha^{tree}")" +pre_tree=$(cd "$repo" && pin_bump_tree "$base" v0.1.11-pre.1 prerelease) +expect tree "a pre-release bump leaves README.md unchanged" \ + test "$(git -C "$repo" rev-parse "$pre_tree:README.md")" = "$(git -C "$repo" rev-parse "$base:README.md")" +expect tree "a pre-release bump sets the pin" \ + test "$(git -C "$repo" show "$pre_tree:buf.gen.yaml" | yq -r '.inputs[0].tag')" = v0.1.11-pre.1 + +# --- merge-bump-pr.sh ---------------------------------------------------- +# gh pr list returns the newest proposal first. +new_case two-proposals +propose 2 v0.1.12 +propose 1 v0.1.11 +run_merge +expect two-proposals "exits 0" test "$rc" = 0 +expect two-proposals "merges the newest, then comments" test "$(events)" = "merge #2,comment #2" +expect two-proposals "main pins v0.1.12" test "$(main_pin)" = v0.1.12 +expect two-proposals "leaves the older proposal for review" says "#1: main moved the pin to v0.1.12 since this proposal was cut" +run_merge +expect two-proposals "a second run leaves main alone" test "$(events)" = "merge #2,comment #2" + +new_case refused +propose 2 v0.1.12 +propose 1 v0.1.11 +touch "$state/refuse-2" +run_merge +expect refused "exits 1" test "$rc" = 1 +expect refused "posts no comment on the refused proposal" test "$(events)" = "merge #1,comment #1" +expect refused "reports the refused merge" says "ERROR: #2: merging v0.1.12 failed; leaving it for review." +expect refused "goes on and merges the next proposal" test "$(main_pin)" = v0.1.11 + +new_case downgrade +propose 1 v0.1.9 +run_merge +expect downgrade "does not merge an older tag" test "$(events)" = "" +expect downgrade "says why" says "#1: v0.1.9 is not newer than the pin v0.1.10 on main" + +new_case provenance +propose 1 v0.1.11 +jq -n --arg sha "$sha" '{check_runs: [{name: "core-bump/provenance", app: {id: 42}, head_sha: $sha, conclusion: "success"}]}' >"$state/runs-$sha.json" +propose 2 v0.1.12 +jq -n --arg sha "$sha" '{check_runs: [{name: "core-bump/provenance", app: {id: 5613773}, head_sha: $sha, conclusion: "failure"}]}' >"$state/runs-$sha.json" +propose 3 v0.1.13 +jq -n '{check_runs: [{name: "core-bump/provenance", app: {id: 5613773}, head_sha: "0000000000000000000000000000000000000000", conclusion: "success"}]}' >"$state/runs-$sha.json" +propose 4 v0.1.14 +echo '{"check_runs": []}' >"$state/runs-$sha.json" +run_merge +expect provenance "merges nothing" test "$(events)" = "" +for n in 1 2 3 4; do + expect provenance "#$n skipped for provenance" says "#$n: no successful bump App provenance" +done + +new_case tree-mismatch +propose 1 v0.1.11 yq -i '.plugins += [{"local": "protoc-gen-evil", "out": "proto"}]' buf.gen.yaml +propose 2 v0.1.12 sh -c 'echo "// changed" >>proto/api.pb.go' +run_merge +expect tree-mismatch "merges nothing" test "$(events)" = "" +expect tree-mismatch "a plugin edit goes to review" says "#1: differs from the pin bump to v0.1.11 in: buf.gen.yaml" +expect tree-mismatch "a proto change goes to review" says "#2: the bindings change (1 files under proto/)" + +new_case checks +propose 1 v0.1.11 +echo '[{"name":"ci","bucket":"pass"},{"name":"e2e / e2e","bucket":"pass"}]' >"$state/checks-1.json" +propose 2 v0.1.12 +echo '[{"name":"ci","bucket":"pass"},{"name":"e2e / e2e","bucket":"skipping"},{"name":"DCO","bucket":"pass"}]' >"$state/checks-2.json" +propose 3 v0.1.13 +echo '[{"name":"ci","bucket":"pass"},{"name":"e2e / e2e","bucket":"pass"},{"name":"DCO","bucket":"pass"},{"name":"lint","bucket":"pending"}]' >"$state/checks-3.json" +propose 4 v0.1.14 +echo '[{"name":"ci","bucket":"pass"},{"name":"ci","bucket":"fail"},{"name":"e2e / e2e","bucket":"pass"},{"name":"DCO","bucket":"pass"}]' >"$state/checks-4.json" +run_merge +expect checks "merges nothing" test "$(events)" = "" +expect checks "a missing required check blocks" says "#1: required checks not passed: 'DCO'" +expect checks "a skipped required check blocks" says "#2: required checks not passed: 'e2e / e2e'" +expect checks "a pending check blocks" says "#3: checks not green: lint (pending)" +expect checks "a failed run of a required check blocks" says "#4: checks not green: ci (fail)" + +new_case filters +labels=core-bump,stable,hold propose 1 v0.1.11 +draft=true propose 2 v0.1.12 +fork=true propose 3 v0.1.13 +propose 4 v0.1.14-pre.1 +run_merge +expect filters "merges nothing" test "$(events)" = "" +expect filters "hold" says "#1: labeled hold" +expect filters "draft" says "#2: draft" +expect filters "fork" says "#3: head is in a fork" +expect filters "pre-release branch" says "#4: branch core/v0.1.14-pre.1 is not core/vX.Y.Z" + +[ "$failed" -eq 0 ] || { echo "merge-bump-pr-test: FAILED" >&2; exit 1; } +echo "merge-bump-pr-test: all cases passed." diff --git a/hack/automation/merge-bump-pr.sh b/hack/automation/merge-bump-pr.sh new file mode 100755 index 0000000..f606872 --- /dev/null +++ b/hack/automation/merge-bump-pr.sh @@ -0,0 +1,150 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# merge-bump-pr.sh: merge stable core-bump proposals that change no proto. +# +# Usage: merge-bump-pr.sh [--dry-run] [--auto-merge on|off] +# +# A stable core release whose bindings are unchanged needs no SDK work, so +# its proposal can merge, and release-tag.yml tags the SDK release, without +# waiting for a review. A release that changes the protos waits for review: +# the SDK release may lag until the new features are wrapped and usable. +# On or off comes from --auto-merge or CORE_AUTO_MERGE (default off). +# +# Every condition is checked again on each run; labels and the PR body are +# not trusted. A proposal is merged only when all of these hold: +# - open, not a draft, from branch core/vX.Y.Z of this repository into main +# - no "hold" label (a maintainer's way to keep it for review) +# - main still pins the tag the proposal was cut from, and the branch's tag +# is newer: after one proposal merges, an older one sharing its base +# would only conflict, and a downgrade always waits for review +# - exactly one commit since main, with a successful provenance check from +# the bump App on that exact SHA +# - the commit's tree is exactly the tree pin_bump_tree (pin-bump.sh) +# rebuilds from the merge base: the pin in buf.gen.yaml set to the +# branch's tag and the README line updated, nothing else. No file under +# proto/ changed (the generated bindings are identical), no conformance +# fixture changed, and no other edit to buf.gen.yaml, such as a plugin. +# - the checks in required_checks passed and none failed or is pending +# A refused merge leaves that proposal for review and the run goes on with +# the others; the script exits non-zero at the end so the failure is reported. +# Repository rules limit writes to core/*, enforce required checks without +# bypass, and let the merge App bypass code owner review on pull requests. +# Run from the root of an sdk-go clone. +set -euo pipefail + +sdk_repo=${SDK_REPO:-ai-openshell/sdk-go} +bump_app_id=5613773 # ai-openshell-sdk-go GitHub App +provenance_check=core-bump/provenance +required_checks=("ci" "e2e / e2e" "DCO") +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck source=hack/automation/pin-bump.sh +. "$here/pin-bump.sh" + +dry=0 +mode=${CORE_AUTO_MERGE:-off} +while [ $# -gt 0 ]; do + case $1 in + --dry-run) dry=1; shift ;; + --auto-merge) mode=${2:?--auto-merge needs on or off}; shift 2 ;; + *) echo "usage: $0 [--dry-run] [--auto-merge on|off]" >&2; exit 2 ;; + esac +done +case $mode in +off | "") echo "Auto-merge is off (CORE_AUTO_MERGE=${mode:-unset})."; exit 0 ;; +on) ;; +*) echo "ERROR: CORE_AUTO_MERGE must be on or off, not '$mode'" >&2; exit 2 ;; +esac + +die() { echo "ERROR: $*" >&2; exit 1; } +[ "$(git rev-parse --show-toplevel)" = "$(pwd -P)" ] || die "run this from the root of the sdk-go clone" + +prs=$(gh pr list -R "$sdk_repo" --label core-bump --label stable --state open \ + --json number,headRefName,headRefOid,baseRefName,isDraft,isCrossRepository,labels \ + --jq '.[] | [.number, .headRefName, .headRefOid, .baseRefName, .isDraft, .isCrossRepository, + ([.labels[].name] | index("hold") != null)] | @tsv') +[ -n "$prs" ] || { echo "No open stable core-bump proposals."; exit 0; } + +failed=0 +while IFS=$'\t' read -r n head sha base draft cross held; do + skip() { echo "#$n: $*; leaving it for review."; } + tag=${head#core/} + + [ "$base" = main ] || { skip "base is $base, not main"; continue; } + [ "$draft" = false ] || { skip "draft"; continue; } + [ "$cross" = false ] || { skip "head is in a fork"; continue; } + [ "$held" = false ] || { skip "labeled hold"; continue; } + [[ "$head" =~ ^core/v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { skip "branch $head is not core/vX.Y.Z"; continue; } + + # Fetch main for each proposal: an earlier one in this run may have moved + # the pin. + git fetch --quiet origin "+refs/heads/main:refs/remotes/origin/main" \ + "+refs/heads/$head:refs/remotes/origin/$head" + [ "$(git rev-parse "origin/$head")" = "$sha" ] || { skip "head moved during the run"; continue; } + mb=$(git merge-base origin/main "$sha") + + main_pin=$(git show origin/main:buf.gen.yaml | yq -r '.inputs[0].tag') + mb_pin=$(git show "$mb:buf.gen.yaml" | yq -r '.inputs[0].tag') + [ "$main_pin" = "$mb_pin" ] || { skip "main moved the pin to $main_pin since this proposal was cut"; continue; } + older "$main_pin" "$tag" || { skip "$tag is not newer than the pin $main_pin on main"; continue; } + + count=$(git rev-list --count "$mb..$sha") + [ "$count" = 1 ] || { skip "$count commits, expected 1"; continue; } + + if ! gh api --method GET "repos/$sdk_repo/commits/$sha/check-runs" \ + -f check_name="$provenance_check" -F app_id="$bump_app_id" | + jq -e --arg name "$provenance_check" --arg sha "$sha" --argjson app "$bump_app_id" \ + 'any(.check_runs[]; .name == $name and .app.id == $app and .head_sha == $sha and .conclusion == "success")' >/dev/null; then + skip "no successful bump App provenance for $sha" + continue + fi + + # Compare whole trees rather than file names: an edit inside an allowed + # file (a Buf plugin entry in buf.gen.yaml, say) must not pass. + expected=$(pin_bump_tree "$mb" "$tag" stable) + if [ "$expected" != "$(git rev-parse "$sha^{tree}")" ]; then + diff=$(git diff --name-only "$expected" "$sha") + if grep -q '^proto/' <<<"$diff"; then + skip "the bindings change ($(grep -c '^proto/' <<<"$diff") files under proto/), so the new core API needs review" + else + skip "differs from the pin bump to $tag in: $(tr '\n' ' ' <<<"$diff")" + fi + continue + fi + + # gh pr checks exits non-zero while checks fail or are pending; read the + # buckets instead. Each required check must be present and pass, and no + # other check may fail or be pending. + checks=$(gh pr checks "$n" -R "$sdk_repo" --json name,bucket 2>/dev/null || true) + [ -n "$checks" ] || { skip "no checks reported yet"; continue; } + missing= + for c in "${required_checks[@]}"; do + jq -e --arg c "$c" 'any(.[]; .name == $c and .bucket == "pass")' <<<"$checks" >/dev/null || missing+="'$c' " + done + [ -z "$missing" ] || { skip "required checks not passed: $missing"; continue; } + bad=$(jq -r '[.[] | select(.bucket != "pass" and .bucket != "skipping") | "\(.name) (\(.bucket))"] | join(", ")' <<<"$checks") + [ -z "$bad" ] || { skip "checks not green: $bad"; continue; } + + comment=$(printf 'Merged without review by core-watch: core `%s` changes no proto, the bump App vouched for the head SHA, and required checks passed.\n\nMerging tags the SDK release `%s`.' \ + "$tag" "$tag") + + if [ "$dry" -eq 1 ]; then + echo "dry-run: would merge #$n ($tag) at $sha with comment:" + sed 's/^/ | /' <<<"$comment" + continue + fi + # --match-head-commit refuses the merge if someone pushed since the checks. + # --admin uses the merge App's ruleset bypass for the code owner review. + # Comment only after the merge, so a refused merge leaves no false claim. + if ! gh pr merge "$n" -R "$sdk_repo" --squash --admin --match-head-commit "$sha"; then + echo "ERROR: #$n: merging $tag failed; leaving it for review." >&2 + failed=1 + continue + fi + echo "#$n: merged ($tag)." + gh pr comment "$n" -R "$sdk_repo" --body "$comment" || + echo "WARNING: #$n merged, but the comment explaining why failed." >&2 +done <<<"$prs" + +exit "$failed" diff --git a/hack/automation/open-bump-pr.sh b/hack/automation/open-bump-pr.sh index 5096b1e..990267c 100755 --- a/hack/automation/open-bump-pr.sh +++ b/hack/automation/open-bump-pr.sh @@ -46,17 +46,6 @@ case $tag in *-pre.*) kind=prerelease ;; *) kind=stable ;; esac die() { echo "ERROR: $*" >&2; exit 1; } -# older : true when release tag a sorts below b (vX.Y.Z-pre.N < vX.Y.Z). -older() { - awk -v a="$1" -v b="$2" ' - function key(t, s, pre, rank, v) { - s = t; sub(/^v/, "", s); pre = 0; rank = 1 - if (index(s, "-pre.")) { pre = substr(s, index(s, "-pre.") + 5) + 0; s = substr(s, 1, index(s, "-pre.") - 1); rank = 0 } - split(s, v, ".") - return sprintf("%06d%06d%06d%d%06d", v[1], v[2], v[3], rank, pre) - } - BEGIN { exit !(key(a) < key(b)) }' -} run() { if [ "$dry" -eq 1 ]; then echo "dry-run: $*"; else "$@"; fi; } [ "$(git rev-parse --show-toplevel)" = "$(pwd -P)" ] || die "run this from the root of the sdk-go clone" @@ -104,9 +93,14 @@ cleanup() { git worktree remove --force "$work/wt" >/dev/null 2>&1 || true; rm - trap cleanup EXIT git worktree add --quiet -b "$branch" "$work/wt" "$base" +# shellcheck source=hack/automation/pin-bump.sh +. "$here/pin-bump.sh" + ( cd "$work/wt" - yq -i ".inputs[0].tag = \"$tag\"" buf.gen.yaml + # The pin and the README line; merge-bump-pr.sh rebuilds the same edits + # with pin_bump_tree to check that a proposal is a pin bump only. + pin_bump_edit "$tag" "$kind" mise run --quiet proto:gen >/dev/null # Refresh vendored fixtures; one not in the tag yet is left as is. @@ -127,13 +121,6 @@ git worktree add --quiet -b "$branch" "$work/wt" "$base" esac done - # Keep the README's "generated from OpenShell core vX.Y.Z" line in step - # with stable pins. Match any version: after a merged pre-release - # proposal, the line still names the last stable tag, not the old pin. - if [ "$kind" = stable ]; then - sed -E -i.bak "s/generated from OpenShell core v[0-9]+\.[0-9]+\.[0-9]+(-pre\.[0-9]+)?/generated from OpenShell core ${tag}/" README.md && rm -f README.md.bak - fi - git add -A git commit --quiet --signoff -m "chore(core): track $tag" -m "Move the core pin from $old to $tag and regenerate the bindings." ) @@ -198,6 +185,16 @@ url=$(gh pr create -R "$sdk_repo" --base main --head "$branch" \ echo "Opened $url" new=${url##*/} +if [ "${CORE_AUTO_MERGE:-off}" = on ] && [ "$kind" = stable ]; then + # Use the local branch we generated. The PR head may already have changed. + sha=$(git rev-parse "refs/heads/$branch") + if ! gh api --method POST "repos/$sdk_repo/check-runs" --silent \ + -f name=core-bump/provenance -f head_sha="$sha" \ + -f status=completed -f conclusion=success; then + echo "WARNING: could not record bump provenance for $sha; $url needs review." >&2 + fi +fi + # Close open pre-release proposals for older tags; this one supersedes them. gh pr list -R "$sdk_repo" --label core-bump --label prerelease --state open \ --json number,headRefName --jq '.[] | "\(.number) \(.headRefName)"' | diff --git a/hack/automation/pin-bump.sh b/hack/automation/pin-bump.sh new file mode 100644 index 0000000..eb0b4b4 --- /dev/null +++ b/hack/automation/pin-bump.sh @@ -0,0 +1,57 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# shellcheck shell=bash + +# pin-bump.sh: the edits a core bump makes besides the regenerated bindings. +# Sourced, not run. open-bump-pr.sh applies them; merge-bump-pr.sh rebuilds +# them to check that a proposal is exactly a pin bump and nothing else. + +# older : true when release tag a sorts below b (vX.Y.Z-pre.N < vX.Y.Z). +older() { + awk -v a="$1" -v b="$2" ' + function key(t, s, pre, rank, v) { + s = t; sub(/^v/, "", s); pre = 0; rank = 1 + if (index(s, "-pre.")) { pre = substr(s, index(s, "-pre.") + 5) + 0; s = substr(s, 1, index(s, "-pre.") - 1); rank = 0 } + split(s, v, ".") + return sprintf("%06d%06d%06d%d%06d", v[1], v[2], v[3], rank, pre) + } + BEGIN { exit !(key(a) < key(b)) }' +} + +# pin_bump_edit : in the current directory, set the pin in +# buf.gen.yaml to and, for a stable , keep the README's +# "generated from OpenShell core vX.Y.Z" line in step. The README line may +# still name the last stable tag after a merged pre-release proposal, so any +# version matches. +pin_bump_edit() { + local tag=$1 kind=$2 + yq -i ".inputs[0].tag = \"$tag\"" buf.gen.yaml + if [ "$kind" = stable ]; then + sed -E -i.bak "s/generated from OpenShell core v[0-9]+\.[0-9]+\.[0-9]+(-pre\.[0-9]+)?/generated from OpenShell core ${tag}/" README.md + rm -f README.md.bak + fi +} + +# pin_bump_tree : print the tree a bump commit on +# must have when it changes nothing but the pin edits: 's tree with +# pin_bump_edit applied to buf.gen.yaml and README.md. No worktree, no +# checkout, no generated code; the blobs are written to the object store. +pin_bump_tree() ( + set -euo pipefail + base=$1 tag=$2 kind=$3 + tmp=$(mktemp -d) + trap 'rm -rf "$tmp"' EXIT + for f in buf.gen.yaml README.md; do + git show "$base:$f" >"$tmp/$f" + done + (cd "$tmp" && pin_bump_edit "$tag" "$kind") + # A private index, so the caller's index and worktree stay untouched. + export GIT_INDEX_FILE="$tmp/index" + git read-tree "$base" + for f in buf.gen.yaml README.md; do + mode=$(git ls-tree "$base" -- "$f" | cut -d' ' -f1) + blob=$(git hash-object -w "$tmp/$f") + git update-index --cacheinfo "$mode,$blob,$f" + done + git write-tree +) diff --git a/hack/automation/testdata/merge-bump/bin/gh b/hack/automation/testdata/merge-bump/bin/gh new file mode 100755 index 0000000..de9a7b8 --- /dev/null +++ b/hack/automation/testdata/merge-bump/bin/gh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +# gh stub for merge-bump-pr-test.sh. Answers the calls merge-bump-pr.sh +# makes from the files in $GH_STUB_STATE: +# prs.json what `gh pr list --json ...` returns (before --jq) +# runs-.json check runs for ; default: one successful +# core-bump/provenance run from the bump App +# checks-.json `gh pr checks --json name,bucket`; default checks.json +# refuse- `gh pr merge ` fails +# origin.git bare repository a merge squashes the PR branch into +# Merges and comments are appended to $GH_STUB_STATE/events, in order. +set -euo pipefail + +state=${GH_STUB_STATE:?GH_STUB_STATE is not set} +jq_expr= +args=() +while [ $# -gt 0 ]; do + case $1 in + --jq) jq_expr=$2; shift 2 ;; + *) args+=("$1"); shift ;; + esac +done +set -- "${args[@]}" + +case "$1 $2" in +"pr list") + if [ -n "$jq_expr" ]; then jq -r "$jq_expr" "$state/prs.json"; else cat "$state/prs.json"; fi + ;; +"api --method") + path=$4 sha=${4#*/commits/} + sha=${sha%/check-runs} + [ "$path" != "$sha" ] || { echo "gh stub: unexpected api path $path" >&2; exit 2; } + if [ -f "$state/runs-$sha.json" ]; then + cat "$state/runs-$sha.json" + else + jq -n --arg sha "$sha" \ + '{check_runs: [{name: "core-bump/provenance", app: {id: 5613773}, head_sha: $sha, conclusion: "success"}]}' + fi + ;; +"pr checks") + f="$state/checks-$3.json" + [ -f "$f" ] || f="$state/checks.json" + cat "$f" + ;; +"pr comment") + echo "comment #$3" >>"$state/events" + ;; +"pr merge") + n=$3 + [ ! -f "$state/refuse-$n" ] || { echo "gh stub: merge of #$n refused" >&2; exit 1; } + branch=$(jq -r --argjson n "$n" '.[] | select(.number == $n) | .headRefName' "$state/prs.json") + tmp=$(mktemp -d) + trap 'rm -rf "$tmp"' EXIT + git clone --quiet "$state/origin.git" "$tmp/m" + cd "$tmp/m" + if ! git merge --quiet --squash "origin/$branch" >/dev/null 2>&1; then + echo "gh stub: #$n is not mergeable (conflict)" >&2 + exit 1 + fi + git -c user.name=stub -c user.email=stub@example.com commit --quiet -m "Merge #$n" + git push --quiet origin HEAD:main + echo "merge #$n" >>"$state/events" + ;; +*) + echo "gh stub: unexpected call: gh $*" >&2 + exit 2 + ;; +esac diff --git a/mise.toml b/mise.toml index dfa7a62..437ae2e 100644 --- a/mise.toml +++ b/mise.toml @@ -227,12 +227,14 @@ echo "Proto check passed: generated files match core $(yq -r '.inputs[0].tag' bu """ [tasks."workflows:check"] -description = "Lint GitHub workflows (actionlint with shellcheck, zizmor) and the hack/ scripts (shellcheck), run the RPC coverage issue tests" +description = "Lint GitHub workflows (actionlint with shellcheck, zizmor) and the hack/ scripts (shellcheck), run the RPC coverage issue and core auto-merge tests" run = [ "actionlint", "zizmor --offline --min-severity medium .github", "shellcheck -x -S warning hack/automation/*.sh hack/migration/*.sh", + "shellcheck -x -S warning hack/automation/testdata/merge-bump/bin/gh", "hack/automation/rpc-issues-test.sh", + "hack/automation/merge-bump-pr-test.sh", ] [tasks."docs:build"] @@ -251,6 +253,10 @@ run = "hack/automation/open-bump-pr.sh" description = "Tag the lockstep SDK release for a stable pin (--dry-run prints what it would do)" run = "hack/automation/tag-release.sh" +[tasks."core:merge"] +description = "Merge stable core proposals that change no proto (--dry-run, --auto-merge on|off)" +run = "hack/automation/merge-bump-pr.sh" + [tasks."core:drift"] description = "Compare core main with the pin: mise run core:drift -- --out " run = "hack/automation/drift-check.sh"