diff --git a/.mise.toml b/.mise.toml index 5706dea7..39bf15a6 100644 --- a/.mise.toml +++ b/.mise.toml @@ -30,6 +30,7 @@ run = [ "uv run --script ./tests/app-key-confinement.py", "bash ./tests/lint-job-modes-contract.sh", "bash ./tests/open-pr-push.sh", + "bash ./tests/open-pr-api-commit.sh", "bash ./tests/open-pr-labels.sh", "bash ./tests/config-sync-ai-toolkit-instructions.sh", "bash ./tests/gitleaks-config.sh", diff --git a/actions/open-pr/README.md b/actions/open-pr/README.md index 09ba028f..d7ea122c 100644 --- a/actions/open-pr/README.md +++ b/actions/open-pr/README.md @@ -4,9 +4,14 @@ Open (or update) a pull request from the current working tree. After your job has produced changes to commit (signed scripts, formatted files, regenerated configs, …), this action stages them, commits them -to a stable working branch, force-pushes, and opens a PR via the -preinstalled `gh` CLI. Repeat runs reuse the same PR rather than -spamming new ones. +as one commit on top of the checked-out commit to a stable working +branch, and opens a PR via the preinstalled `gh` CLI. Repeat runs reuse +the same PR rather than spamming new ones. + +Set `commit-method: api` to commit through the GitHub API instead: then +**GitHub signs the commit** and the PR can merge into a branch that requires +signed commits (see [Signed commits](#signed-commits-commit-method-api)). +The default, `commit-method: git`, pushes an unsigned commit. Designed for jobs that should land via PR review instead of pushing directly to a protected branch — auto-formatting, artifact signing, @@ -20,26 +25,27 @@ tree the caller just modified. A reusable workflow runs on its own runner without access to those uncommitted changes. This action also avoids pulling in third-party `create-pull-request` -actions: it uses only `git` and the `gh` CLI that ship preinstalled on -GitHub-hosted runners. +actions: it uses only `git`, `jq` and the `gh` CLI that ship preinstalled +on GitHub-hosted runners. ## Inputs -| Name | Required | Default | Description | -| ---------------- | -------- | ------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `branch` | yes | — | Working branch to create or force-push to. Use a stable per-purpose name (e.g. `chore/sign-powershell-scripts`) so repeat runs reuse the same PR. | -| `base` | no | `main` | Branch to merge into. | -| `title` | yes | — | PR title (also default commit message). Should be a Conventional Commit subject. | -| `body` | no | `""` | PR body, plain Markdown. | -| `commit-message` | no | `title` | Commit message for the staged changes. Defaults to `title`. | -| `paths` | no | `.` | Newline-delimited pathspecs to `git add`. Globs are forwarded literally. | -| `labels` | no | `""` | Newline-delimited labels to apply on PR creation. | -| `draft` | no | `false` | Open as draft. | -| `signoff` | no | `false` | Add a `Signed-off-by` trailer to the commit. | -| `if-no-changes` | no | `skip` | Behaviour when there's nothing to commit: `skip` (exit 0) or `fail` (exit non-zero). | -| `git-user-name` | no | `github-actions[bot]` | Author name for the commit. | -| `git-user-email` | no | `41898282+github-actions[bot]@users.noreply.github.com` | Author email for the commit. | -| `github-token` | no | `${{ github.token }}` | Token used for the `git push` and `gh` API call. The default `GITHUB_TOKEN` will NOT retrigger workflows on the bot's push, which is what you want inside loops like signing-on-push. Pass a PAT/App token for cross-repo PRs or to retrigger CI on the new branch. | +| Name | Required | Default | Description | +| ---------------- | -------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `branch` | yes | — | Working branch to create or replace. Use a stable per-purpose name (e.g. `chore/sign-powershell-scripts`) so repeat runs reuse the same PR. | +| `base` | no | `main` | Branch to merge into. | +| `title` | yes | — | PR title (also default commit message). Should be a Conventional Commit subject. | +| `body` | no | `""` | PR body, plain Markdown. | +| `commit-message` | no | `title` | Commit message for the staged changes. Defaults to `title`. | +| `paths` | no | `.` | Newline-delimited pathspecs to `git add`. Globs are forwarded literally. | +| `labels` | no | `""` | Newline-delimited labels to apply on PR creation. | +| `draft` | no | `false` | Open as draft. | +| `signoff` | no | `false` | Add a `Signed-off-by` trailer with `git-user-name` and `git-user-email`. | +| `commit-method` | no | `git` | `git`: local commit and leased force-push, authored as `git-user-name` and **unsigned**. `api`: commit through GraphQL `createCommitOnBranch`, signed by GitHub and authored as the token's identity. | +| `if-no-changes` | no | `skip` | Behaviour when there's nothing to commit: `skip` (exit 0) or `fail` (exit non-zero). | +| `git-user-name` | no | `github-actions[bot]` | Author name for `commit-method: git`, and the `signoff` name. | +| `git-user-email` | no | `41898282+github-actions[bot]@users.noreply.github.com` | Author email for `commit-method: git`, and the `signoff` email. | +| `github-token` | no | `${{ github.token }}` | Token for the commit and the `gh` PR calls (needs Contents and Pull requests write). The default `GITHUB_TOKEN` will NOT retrigger workflows on the bot's commit, which is what you want inside loops like signing-on-push. Pass a GitHub App token to retrigger CI on the new branch. | ## Outputs @@ -57,34 +63,96 @@ GitHub-hosted runners. `pull-requests: write`. - Make whatever changes you want committed to the working tree before invoking the action. +- With `commit-method: api`, don't commit locally first: the commit is + built on the checked-out commit, which must exist on GitHub. + `actions/checkout` does not need to persist credentials for this + method; `commit-method: git` still pushes with them. + +## Signed commits (`commit-method: api`) + +This method stages the changes locally, then sends them with the +GraphQL [`createCommitOnBranch`](https://docs.github.com/en/graphql/reference/mutations#createcommitonbranch) +mutation. GitHub signs commits made this way, whichever token makes them +(a GitHub App token, `GITHUB_TOKEN` or a PAT), so the PR passes a +"require signed commits" ruleset without an admin bypass. The commit is +authored as the token's identity: the App's bot with an App token, +`github-actions[bot]` with `GITHUB_TOKEN`. + +```yaml +- uses: DevSecNinja/.github/actions/open-pr@ + with: + branch: chore/config-sync + title: 'chore: sync config files' + commit-method: api + github-token: ${{ steps.app-token.outputs.token }} +``` + +How a run commits: + +1. Stage `paths` and read the changes from the index, including deletions + and binary files (sent base64-encoded). +2. Read the working branch's tip. When it is already one **signed** commit + on the same base with the same files, nothing is written (see below). An + unsigned tip, such as one pushed earlier with `commit-method: git`, is + replaced, so switching a caller to `api` unblocks its open PR. +3. Create a temporary `open-pr-tmp/` branch at the checked-out + commit and commit the changes on it, with that commit as + `expectedHeadOid`. +4. Point the working branch at the new commit and delete the temporary + branch in one atomic `updateRefs` call, leased on the tip from step 2. + +The temporary branch exists because `createCommitOnBranch` can only append +to an existing branch: resetting the working branch to its base first +would briefly leave an open PR with no changes, which can make GitHub +close it. A push to the working branch during the run fails the run +instead of being overwritten, and the temporary branch is deleted on any +failure. + +Limits of the API method; keep `commit-method: git` (unsigned) when one +applies: + +- New executable files are committed as regular files (a warning is + printed). Modified files keep their mode, and a mode or type change on + an existing file (for example `chmod`, or a symlink becoming a file) + fails the run. +- Symlinks and submodules can't be committed (the run fails). +- Changed Git LFS files can't be committed, because the API can't upload + their objects (the run fails). +- The whole change goes in one request. Config-sized changes (a few MB) + work; in testing, requests of 10 to 25 MB of base64 payload failed + intermittently. No file may exceed 100 MiB. +- The checked-out commit must be on GitHub, so jobs that commit locally + before this action need `git`. ## Pitfalls baked in -- **No third-party action dependency.** Only `git` + `gh`, both +- **No third-party action dependency.** Only `git`, `jq` and `gh`, all preinstalled on GitHub-hosted runners. - **Default `GITHUB_TOKEN` does not retrigger workflows on push.** Safe inside loops where the same workflow would otherwise re-run on the bot's commit (e.g. sign-on-push). -- **Force-push leased against the live branch tip.** The action reads the - working branch's current tip with `git ls-remote` and pushes with +- **Every update is leased against the live branch tip.** The API method + leases its `updateRefs` call on the tip it read; the `git` method reads + the tip with `git ls-remote` and pushes with `--force-with-lease=:` (an empty tip means "must not exist - yet"). A concurrent push to the working branch is still never silently + yet"). A concurrent push to the working branch is never silently overwritten, and — unlike a bare `--force-with-lease`, which needs a remote-tracking ref that a fresh checkout does not have — an existing branch (an open PR, or one left behind after merge) no longer fails with `! [rejected] … (stale info)`. Fixed in v3.3.0; callers pinned to an older version can run `git fetch origin "+refs/heads/:refs/remotes/origin/"` before this action as a workaround. -- **No-op pushes are skipped.** When the branch already holds the same +- **No-op updates are skipped.** When the branch already holds the same changes on the same base commit, the action leaves it alone, so an unchanged scheduled run does not re-run the PR's CI. The existing PR is still looked up and reported (`changed=true`, `created=false`). - **PR reuse on conflict.** If a PR already exists for the same - `branch -> base` pair, its contents are updated by the force-push - and the existing PR is reused — no duplicate PRs across runs. -- **Branch protection stays enforced.** This action only ever pushes - to the working branch and opens a PR; merging into the protected - base branch is left to the normal review/automerge flow. + `branch -> base` pair, its branch is replaced with the new commit and + the existing PR is reused — no duplicate PRs across runs. +- **Branch protection stays enforced.** This action only ever writes + to the working branch (and a short-lived temporary branch) and opens a + PR; merging into the protected base branch is left to the normal + review/automerge flow. ## Example — sign artifacts and open a PR diff --git a/actions/open-pr/action.yml b/actions/open-pr/action.yml index e88a8fc2..af0ab389 100644 --- a/actions/open-pr/action.yml +++ b/actions/open-pr/action.yml @@ -3,11 +3,17 @@ name: Open pull request description: | Open (or update) a pull request from the current working tree. - Stages, commits, and force-pushes any uncommitted changes in the - caller's checkout to a dedicated working branch, then opens a PR - against the target branch via the `gh` CLI. If a PR for that branch - already exists (e.g. a previous run that hasn't been merged yet), the - force-push updates its contents and the existing PR is reused. + Stages any uncommitted changes in the caller's checkout, commits them + as a single commit on top of the checked-out commit to a dedicated + working branch, then opens a PR against the target branch via the `gh` + CLI. If a PR for that branch already exists (e.g. a previous run that + hasn't been merged yet), the branch is replaced with the new commit and + the existing PR is reused. + + With `commit-method: api` the commit is created through the GitHub + GraphQL API, so GitHub signs it and the PR passes a "require signed + commits" rule. The default, `commit-method: git`, keeps the original + local commit and force-push, which is unsigned. This is a composite action so it can act on the caller's working tree (a reusable workflow runs in its own job and would not see those @@ -26,7 +32,7 @@ description: | inputs: branch: description: | - Name of the working branch to create or force-push to. Choose a + Name of the working branch to create or replace. Choose a stable per-purpose name (e.g. `chore/sign-powershell-scripts`) so repeat runs reuse the same PR rather than opening duplicates. required: true @@ -70,9 +76,25 @@ inputs: required: false default: "false" signoff: - description: "Add a `Signed-off-by` trailer to the commit." + description: | + Add a `Signed-off-by` trailer to the commit, using `git-user-name` + and `git-user-email`. required: false default: "false" + commit-method: + description: | + How the commit is made: + - `git` (default): local commit authored as `git-user-name` and + a leased force-push. The commit is unsigned, so a base branch + that requires signed commits blocks the PR. + - `api`: GraphQL `createCommitOnBranch` with `github-token`. + GitHub signs the commit and authors it as the token's identity + (the GitHub App bot, or github-actions[bot] for GITHUB_TOKEN). + The executable bit can't be set on new files; mode changes, + symlinks, submodules and Git LFS files can't be committed; and + the checked-out commit must be on GitHub. + required: false + default: git if-no-changes: description: | What to do when there is nothing to commit: @@ -81,22 +103,25 @@ inputs: required: false default: skip git-user-name: - description: "Author name for the commit." + description: | + Author name for `commit-method: git`, and the `signoff` name. The + API method always authors the commit as the token's identity. required: false default: "github-actions[bot]" git-user-email: - description: "Author email for the commit." + description: "Author email for `commit-method: git`, and the `signoff` email." required: false default: "41898282+github-actions[bot]@users.noreply.github.com" github-token: description: | - Token used by `gh` for the PR API call. Defaults to the workflow's - GITHUB_TOKEN, which is enough for same-repo PRs and intentionally - will NOT retrigger workflows on the bot's push (so this action is - safe to use inside loops like sign-on-push). + Token used for the commit (API call or `git push`) and the PR API + calls. Defaults to the workflow's GITHUB_TOKEN, which is enough for + same-repo PRs and intentionally will NOT retrigger workflows on the + bot's commit (so this action is safe to use inside loops like + sign-on-push). With `commit-method: api` it needs Contents: write. - Pass a PAT or App token if you need cross-repo PRs or want the - bot's push to retrigger CI on the new branch. + Pass a GitHub App token if you want the bot's commit to retrigger + CI on the new branch; the commit is then authored as the App. required: false default: ${{ github.token }} @@ -113,8 +138,8 @@ outputs: created: description: | `true` when a brand-new PR was opened by this run, `false` when - an existing PR for `branch` was reused (its contents updated via - force-push), or when changed=false. + an existing PR for `branch` was reused (its branch replaced with + the new commit), or when changed=false. value: ${{ steps.pr.outputs.created }} runs: @@ -129,85 +154,16 @@ runs: BRANCH: ${{ inputs.branch }} BASE: ${{ inputs.base }} COMMIT_MESSAGE: ${{ inputs.commit-message }} + COMMIT_METHOD: ${{ inputs.commit-method }} TITLE: ${{ inputs.title }} PATHS: ${{ inputs.paths }} SIGNOFF: ${{ inputs.signoff }} IF_NO_CHANGES: ${{ inputs.if-no-changes }} - # Use the supplied token for the push so the bot identity is - # consistent with the gh CLI call below. + # The same token commits (API or git push) and opens the PR below, so + # the bot identity is consistent. + GH_TOKEN: ${{ inputs.github-token }} GITHUB_TOKEN: ${{ inputs.github-token }} - run: | - set -euo pipefail - - git config user.name "${GIT_USER_NAME}" - git config user.email "${GIT_USER_EMAIL}" - - # Stash any unstaged changes onto a fresh branch rooted at the - # current commit so each run produces a clean, single-commit PR - # rather than accumulating history on top of a stale branch. - git checkout -B "${BRANCH}" - - # Forward `paths` to `git add` as separate arguments. An empty - # element (e.g. trailing newline) is filtered out. - add_args=() - while IFS= read -r line; do - trimmed="${line#"${line%%[![:space:]]*}"}" - trimmed="${trimmed%"${trimmed##*[![:space:]]}"}" - [ -n "${trimmed}" ] && add_args+=("${trimmed}") - done <<< "${PATHS}" - if [ "${#add_args[@]}" -eq 0 ]; then - add_args=(".") - fi - git add -- "${add_args[@]}" - - if git diff --cached --quiet; then - echo "No staged changes for ${BRANCH}." - echo "changed=false" >> "${GITHUB_OUTPUT}" - if [ "${IF_NO_CHANGES}" = "fail" ]; then - echo "::error::if-no-changes=fail and there is nothing to commit." >&2 - exit 1 - fi - exit 0 - fi - - msg="${COMMIT_MESSAGE:-${TITLE}}" - commit_args=(-m "${msg}") - if [ "${SIGNOFF}" = "true" ]; then - commit_args+=(--signoff) - fi - git commit "${commit_args[@]}" - - # Lease the push against the branch tip as it is on the remote right - # now. A bare --force-with-lease compares against the local - # remote-tracking ref, which a fresh (usually shallow) checkout does - # not have for this branch, so every push to an already existing - # branch was rejected as "stale info". An empty lease value means - # "the branch must not exist yet". Either way, a branch someone moves - # between this lookup and the push is still never overwritten. - remote_ref="refs/heads/${BRANCH}" - remote_sha="$(git ls-remote origin "${remote_ref}" | awk -v ref="${remote_ref}" '$2 == ref { print $1 }')" || { - echo "::error::Could not read ${remote_ref} from origin; refusing to push without a lease." >&2 - exit 1 - } - - # Leave an identical branch alone: same parent and same tree means the - # open PR already carries exactly these changes, and a no-op - # force-push would only re-run its CI. Only deepen history when the - # checkout is already shallow, so a full clone stays complete. - fetch_args=(--quiet --no-tags) - if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then - fetch_args+=(--depth=2) - fi - if [ -n "${remote_sha}" ] && - git fetch "${fetch_args[@]}" origin "${remote_sha}" 2>/dev/null && - [ "$(git rev-parse "${remote_sha}^{tree}")" = "$(git rev-parse 'HEAD^{tree}')" ] && - [ "$(git rev-parse --verify --quiet "${remote_sha}^")" = "$(git rev-parse 'HEAD^')" ]; then - echo "${BRANCH} already has these changes on the same base; skipping push." - else - git push --force-with-lease="${remote_ref}:${remote_sha}" origin "HEAD:${remote_ref}" - fi - - echo "changed=true" >> "${GITHUB_OUTPUT}" + run: bash "${GITHUB_ACTION_PATH}/commit.sh" - name: Open or reuse pull request id: pr @@ -301,5 +257,5 @@ runs: echo "pr-url=${url}" echo "pr-number=${number}" } >> "${GITHUB_OUTPUT}" - echo "Reused existing PR #${number}: ${url} (force-pushed updated contents)." + echo "Reused existing PR #${number}: ${url} (its branch holds the new commit)." fi diff --git a/actions/open-pr/commit.sh b/actions/open-pr/commit.sh new file mode 100755 index 00000000..d57553c1 --- /dev/null +++ b/actions/open-pr/commit.sh @@ -0,0 +1,331 @@ +#!/usr/bin/env bash +# +# Stage the caller's changes and commit them to ${BRANCH} for actions/open-pr. +# +# COMMIT_METHOD=api creates the commit with the GraphQL +# `createCommitOnBranch` mutation. GitHub signs those commits, so the PR passes +# a "require signed commits" rule. The mutation can only append to an existing +# branch, and resetting an open PR's branch to its base could close the PR, +# so the commit is built on a throwaway branch at the base commit and then moved +# to ${BRANCH} with one atomic `updateRefs` call that also deletes the +# throwaway branch. That call is leased on the tip read before the commit, so a +# concurrent push to ${BRANCH} is never overwritten. +# +# COMMIT_METHOD=git (default) keeps the original behaviour: a local commit +# authored as ${GIT_USER_NAME} and a leased force-push. Those commits are +# unsigned. +# +# Inputs (environment): BRANCH, BASE, TITLE, COMMIT_MESSAGE, PATHS, SIGNOFF, +# IF_NO_CHANGES, COMMIT_METHOD, GIT_USER_NAME, GIT_USER_EMAIL, GH_TOKEN and +# GITHUB_OUTPUT. Writes changed=true|false to GITHUB_OUTPUT. + +set -euo pipefail + +method="${COMMIT_METHOD:-git}" +case "${method}" in +api | git) ;; +*) + echo "::error::commit-method must be 'api' or 'git', got '${method}'." >&2 + exit 1 + ;; +esac + +# GitHub rejects files over 100 MiB, whatever the transport. +max_file_bytes="${OPEN_PR_MAX_FILE_BYTES:-104857600}" +zero_oid="0000000000000000000000000000000000000000" + +trim() { + local value="$1" + value="${value#"${value%%[![:space:]]*}"}" + printf '%s' "${value%"${value##*[![:space:]]}"}" +} + +# jq on Windows runners ends its output lines with CRLF; a stray CR breaks +# every comparison below. JSON output escapes CRs inside strings, so only line +# endings are dropped. +jq() { + command jq "$@" | tr -d '\r' +} + +if [ "${method}" = "git" ]; then + git config user.name "${GIT_USER_NAME}" + git config user.email "${GIT_USER_EMAIL}" + # Rebuild the working branch from the current commit so each run produces + # a clean, single-commit PR rather than piling onto a stale branch. + git checkout -B "${BRANCH}" +fi + +# Forward `paths` to `git add` as separate arguments; blank lines are dropped. +add_args=() +while IFS= read -r line; do + trimmed="$(trim "${line}")" + [ -n "${trimmed}" ] && add_args+=("${trimmed}") +done <<<"${PATHS:-.}" +if [ "${#add_args[@]}" -eq 0 ]; then + add_args=(".") +fi +git add -- "${add_args[@]}" + +if git diff --cached --quiet; then + echo "No staged changes for ${BRANCH}." + echo "changed=false" >>"${GITHUB_OUTPUT}" + if [ "${IF_NO_CHANGES:-skip}" = "fail" ]; then + echo "::error::if-no-changes=fail and there is nothing to commit." >&2 + exit 1 + fi + exit 0 +fi + +msg="${COMMIT_MESSAGE:-${TITLE}}" + +commit_with_git() { + echo "::warning title=open-pr commit is unsigned::commit-method=git pushes an unsigned commit as ${GIT_USER_NAME}, so a base branch that requires signed commits blocks this PR. Use commit-method: api to get a commit GitHub signs." + + local commit_args=(-m "${msg}") + if [ "${SIGNOFF:-false}" = "true" ]; then + commit_args+=(--signoff) + fi + git commit "${commit_args[@]}" + + # Lease the push against the branch tip as it is on the remote right now. + # A bare --force-with-lease compares against the local remote-tracking + # ref, which a fresh (usually shallow) checkout does not have for this + # branch, so every push to an existing branch was rejected as "stale + # info". An empty lease value means "the branch must not exist yet". + local remote_ref="refs/heads/${BRANCH}" remote_sha + remote_sha="$(git ls-remote origin "${remote_ref}" | awk -v ref="${remote_ref}" '$2 == ref { print $1 }')" || { + echo "::error::Could not read ${remote_ref} from origin; refusing to push without a lease." >&2 + exit 1 + } + + # Leave an identical branch alone: same parent and same tree means the open + # PR already carries exactly these changes, and a no-op force-push would + # only re-run its CI. Only deepen history when the checkout is already + # shallow, so a full clone stays complete. + local fetch_args=(--quiet --no-tags) + if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then + fetch_args+=(--depth=2) + fi + if [ -n "${remote_sha}" ] && + git fetch "${fetch_args[@]}" origin "${remote_sha}" 2>/dev/null && + [ "$(git rev-parse "${remote_sha}^{tree}")" = "$(git rev-parse 'HEAD^{tree}')" ] && + [ "$(git rev-parse --verify --quiet "${remote_sha}^")" = "$(git rev-parse 'HEAD^')" ]; then + echo "${BRANCH} already has these changes on the same base; skipping push." + else + git push --force-with-lease="${remote_ref}:${remote_sha}" origin "HEAD:${remote_ref}" + fi +} + +work="" +tmp_ref="" +repo="" + +cleanup() { + if [ -n "${tmp_ref}" ]; then + gh api -X DELETE "repos/${repo}/git/${tmp_ref}" >/dev/null 2>&1 || + echo "::warning::Could not delete the temporary branch ${tmp_ref#refs/heads/}; delete it by hand." + fi + if [ -n "${work}" ]; then + rm -rf "${work}" + fi +} + +# Apply a JSON array of RefUpdate objects atomically. +update_refs() { + jq -n --arg id "${repo_id}" --argjson updates "$1" '{ + query: "mutation($input: UpdateRefsInput!) { updateRefs(input: $input) { clientMutationId } }", + variables: { input: { repositoryId: $id, refUpdates: $updates } } + }' >"${work}/update-refs.json" + gh api graphql --input "${work}/update-refs.json" >/dev/null +} + +# True when the remote tree holds the same entries as the local tree: every +# file, symlink and submodule at the same path with the same object. Only the +# executable bit is ignored, because the API can't set it on new files. +same_entries() { + gh api "repos/${repo}/git/trees/$1?recursive=1" >"${work}/remote-tree.json" 2>/dev/null || return 1 + [ "$(jq -r '.truncated' "${work}/remote-tree.json")" = "false" ] || return 1 + jq -r '.tree[] | select(.type != "tree") | "\(.mode | sub("^100755$"; "100644")) \(.sha)\t\(.path)"' \ + "${work}/remote-tree.json" | LC_ALL=C sort >"${work}/remote-entries" + git ls-tree -r -z "${tree}" | tr '\0' '\n' | + awk -F '\t' '{ split($1, meta, " "); mode = meta[1] == "100755" ? "100644" : meta[1]; sub(/^[^\t]*\t/, ""); print mode " " meta[3] "\t" $0 }' | + LC_ALL=C sort >"${work}/local-entries" + cmp -s "${work}/remote-entries" "${work}/local-entries" +} + +commit_with_api() { + work="$(mktemp -d)" + trap cleanup EXIT + + local parent tree + parent="$(git rev-parse HEAD)" + tree="$(git write-tree)" + + # Describe the staged changes as createCommitOnBranch file changes. The + # contents come from the index, which is exactly what `git commit` would + # record, base64-encoded so binary files survive. + : >"${work}/additions.jsonl" + : >"${work}/deletions.jsonl" + git diff --cached --raw -z --no-renames --no-abbrev HEAD -- >"${work}/diff" + local meta path old_mode new_mode new_sha status size + while IFS= read -r -d '' meta && IFS= read -r -d '' path; do + read -r old_mode new_mode _ new_sha status <<<"${meta#:}" + case "${status}" in + D) + jq -cn --arg path "${path}" '{path: $path}' >>"${work}/deletions.jsonl" + continue + ;; + A | M | T) ;; + *) + echo "::error::Unexpected change '${status}' for ${path}." >&2 + exit 1 + ;; + esac + # GitHub keeps an existing file's mode, so a mode or type change + # (e.g. chmod, or a symlink becoming a file) would be silently lost. + if [ "${status}" != "A" ] && [ "${old_mode}" != "${new_mode}" ]; then + echo "::error::${path} changes mode from ${old_mode} to ${new_mode}, which createCommitOnBranch can't commit. Use commit-method: git for this change." >&2 + exit 1 + fi + case "${new_mode}" in + 100644) ;; + 100755) + if [ "${status}" = "A" ]; then + echo "::warning::${path} is executable, but createCommitOnBranch can't set the executable bit; it is committed as a regular file. Use commit-method: git if the bit matters." + fi + ;; + *) + echo "::error::${path} has mode ${new_mode} (a symlink or submodule), which createCommitOnBranch can't commit. Use commit-method: git for this change." >&2 + exit 1 + ;; + esac + size="$(git cat-file -s "${new_sha}")" + if [ "${size}" -gt "${max_file_bytes}" ]; then + echo "::error::${path} is ${size} bytes, over GitHub's ${max_file_bytes}-byte file limit." >&2 + exit 1 + fi + # The API can't upload Git LFS objects, so a changed pointer would + # reference content that only exists on the runner. + if [ "${size}" -lt 1024 ] && + git cat-file blob "${new_sha}" | head -n 1 | grep -Fxq 'version https://git-lfs.github.com/spec/v1'; then + echo "::error::${path} is a Git LFS pointer, and createCommitOnBranch can't upload LFS objects. Use commit-method: git for this change." >&2 + exit 1 + fi + git cat-file blob "${new_sha}" | base64 | tr -d '\n' >"${work}/contents" + jq -cn --arg path "${path}" --rawfile contents "${work}/contents" \ + '{path: $path, contents: $contents}' >>"${work}/additions.jsonl" + done <"${work}/diff" + + local headline="${msg%%$'\n'*}" body="" + if [[ "${msg}" == *$'\n'* ]]; then + body="${msg#*$'\n'}" + body="${body#"${body%%[!$'\n']*}"}" + fi + if [ "${SIGNOFF:-false}" = "true" ]; then + local trailer="Signed-off-by: ${GIT_USER_NAME} <${GIT_USER_EMAIL}>" + if [ -n "${body}" ]; then + body="${body}"$'\n\n'"${trailer}" + else + body="${trailer}" + fi + fi + + # One query for the repository, the working branch tip, and whether the + # base commit exists on GitHub. + local state + # shellcheck disable=SC2016 # GraphQL variables, not shell expansions. + state="$(gh api graphql \ + -F owner='{owner}' -F name='{repo}' \ + -f ref="refs/heads/${BRANCH}" -f parent="${parent}" \ + -f query='query($owner: String!, $name: String!, $ref: String!, $parent: GitObjectID!) { + repository(owner: $owner, name: $name) { + id + nameWithOwner + base: object(oid: $parent) { oid } + ref(qualifiedName: $ref) { + target { + oid + ... on Commit { tree { oid } parents(first: 2) { nodes { oid } } signature { isValid } } + } + } + } + }')" + repo_id="$(jq -r '.data.repository.id' <<<"${state}")" + repo="$(jq -r '.data.repository.nameWithOwner' <<<"${state}")" + if [ "$(jq -r '.data.repository.base.oid // empty' <<<"${state}")" != "${parent}" ]; then + echo "::error::The checked-out commit ${parent} is not on GitHub (the job made local commits?). commit-method: api can only commit on top of a pushed commit; use commit-method: git." >&2 + exit 1 + fi + + local tip tip_tree tip_parents tip_signed + tip="$(jq -r '.data.repository.ref.target.oid // empty' <<<"${state}")" + if [ -n "${tip}" ]; then + tip_tree="$(jq -r '.data.repository.ref.target.tree.oid' <<<"${state}")" + tip_parents="$(jq -r '[.data.repository.ref.target.parents.nodes[].oid] | join(" ")' <<<"${state}")" + tip_signed="$(jq -r '.data.repository.ref.target.signature.isValid // false' <<<"${state}")" + # Leave an identical, signed branch alone, so the PR's CI does not + # re-run. An unsigned one (e.g. pushed by commit-method: git) is + # replaced, so switching a caller to the API unblocks its open PR. + if [ "${tip_parents}" = "${parent}" ] && { [ "${tip_tree}" = "${tree}" ] || same_entries "${tip_tree}"; }; then + if [ "${tip_signed}" = "true" ]; then + echo "${BRANCH} already has these changes on the same base; skipping commit." + return + fi + echo "${BRANCH} already has these changes, but its commit is not signed; replacing it." + fi + fi + + tmp_ref="refs/heads/open-pr-tmp/${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-1}-${RANDOM}${RANDOM}" + update_refs "$(jq -cn --arg name "${tmp_ref}" --arg before "${zero_oid}" --arg after "${parent}" \ + '[{name: $name, beforeOid: $before, afterOid: $after}]')" || { + tmp_ref="" + echo "::error::Could not create a temporary branch for the commit." >&2 + exit 1 + } + + # shellcheck disable=SC2016 # GraphQL variables, not shell expansions. + jq -n \ + --arg repo "${repo}" --arg branch "${tmp_ref#refs/heads/}" --arg expected "${parent}" \ + --arg headline "${headline}" --arg body "${body}" \ + --slurpfile additions "${work}/additions.jsonl" \ + --slurpfile deletions "${work}/deletions.jsonl" '{ + query: "mutation($input: CreateCommitOnBranchInput!) { createCommitOnBranch(input: $input) { commit { oid } } }", + variables: { input: { + branch: { repositoryNameWithOwner: $repo, branchName: $branch }, + expectedHeadOid: $expected, + message: ({ headline: $headline } + (if $body == "" then {} else { body: $body } end)), + fileChanges: { additions: $additions, deletions: $deletions } + } } + }' >"${work}/commit.json" + local commit + commit="$(gh api graphql --input "${work}/commit.json" --jq '.data.createCommitOnBranch.commit.oid')" || { + echo "::error::createCommitOnBranch failed (request body $(wc -c <"${work}/commit.json") bytes). For changes too large for one API request, use commit-method: git." >&2 + exit 1 + } + [ -n "${commit}" ] || { + echo "::error::createCommitOnBranch returned no commit." >&2 + exit 1 + } + + # Move the working branch to the new commit and drop the temporary branch + # in one atomic update, leased on the tip read above (or on the branch not + # existing yet). + if ! update_refs "$(jq -cn \ + --arg branch "refs/heads/${BRANCH}" --arg tip "${tip:-${zero_oid}}" --arg commit "${commit}" \ + --arg tmp "${tmp_ref}" --arg zero "${zero_oid}" \ + '[{name: $branch, beforeOid: $tip, afterOid: $commit, force: true}, + {name: $tmp, beforeOid: $commit, afterOid: $zero}]')"; then + echo "::error::${BRANCH} changed on GitHub while this run was committing; not overwriting it. Re-run the job." >&2 + exit 1 + fi + tmp_ref="" + echo "Committed ${commit} to ${BRANCH} through the GitHub API (signed by GitHub)." +} + +if [ "${method}" = "git" ]; then + commit_with_git +else + commit_with_api +fi + +echo "changed=true" >>"${GITHUB_OUTPUT}" diff --git a/docs/architecture.md b/docs/architecture.md index ade528ee..a659e57f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -704,41 +704,49 @@ for attribution caveats, inputs and docs-site or standalone Pages examples. ### Open PR (`actions/open-pr/`) -Opens (or force-updates) a pull request from the current working tree. +Opens (or updates) a pull request from the current working tree. Use it whenever a job has produced changes that should land via PR review rather than a direct push to a protected branch — signing, formatting, generated-content updates. Key properties: -- Uses only the preinstalled `gh` CLI; no third-party action. +- With `commit-method: api`, commits through the GraphQL + `createCommitOnBranch` API, so GitHub signs the commit and the PR passes + a "require signed commits" ruleset. The default, `commit-method: git`, + keeps the unsigned local commit and force-push. See the action README + for the API method's limits (no new executable bits, symlinks or very + large changes). +- Uses only the preinstalled `git`, `jq` and `gh`; no third-party action. - Default `GITHUB_TOKEN` does not retrigger workflows on the bot's - push, so it is safe inside loops like sign-on-push. -- Reuses the existing PR (force-pushes its branch) when one already - exists for the same `branch -> base` pair, so repeat runs do not - spam duplicate PRs. -- Leases the force-push against the branch tip it reads with - `git ls-remote`, so an existing working branch (an open PR, or a branch - left behind after merge) is updated instead of rejected as "stale info" - (fixed in v3.3.0). When the branch already holds the same changes on the - same base, the push is skipped so the PR's CI does not re-run. + commit, so it is safe inside loops like sign-on-push. +- Reuses the existing PR (replaces its branch with the new commit) when + one already exists for the same `branch -> base` pair, so repeat runs + do not spam duplicate PRs. +- Leases every branch update against the tip it read, so a concurrent + push is never overwritten and an existing working branch (an open PR, + or a branch left behind after merge) is updated instead of rejected as + "stale info" (fixed in v3.3.0). When the branch already holds the same + changes on the same base, nothing is written so the PR's CI does not + re-run. - Branch protection on the base branch stays fully enforced. -| Input | Required | Default | Description | -| ---------------- | -------- | --------------------------- | ------------------------------------------------------------------------ | -| `branch` | yes | — | Working branch to create or force-push to. | -| `title` | yes | — | PR title (also default commit message). Conventional Commit subject. | -| `base` | no | `main` | Branch to merge into. | -| `body` | no | `""` | PR body (Markdown). | -| `commit-message` | no | `title` | Override the commit message. | -| `paths` | no | `.` | Newline-delimited pathspecs to `git add`. | -| `labels` | no | `""` | Newline-delimited labels. | -| `draft` | no | `false` | Open as draft. | -| `signoff` | no | `false` | Add `Signed-off-by` trailer. | -| `if-no-changes` | no | `skip` | `skip` = exit 0; `fail` = error when nothing to commit. | -| `git-user-name` | no | `github-actions[bot]` | Commit author name. | -| `git-user-email` | no | github-actions[bot] noreply | Commit author email. | -| `github-token` | no | `${{ github.token }}` | Token for `git push` and `gh`. Override with PAT/App for cross-repo PRs. | +| Input | Required | Default | Description | +| ---------------- | -------- | --------------------------- | -------------------------------------------------------------------- | +| `branch` | yes | — | Working branch to create or replace. | +| `title` | yes | — | PR title (also default commit message). Conventional Commit subject. | +| `base` | no | `main` | Branch to merge into. | +| `body` | no | `""` | PR body (Markdown). | +| `commit-message` | no | `title` | Override the commit message. | +| `paths` | no | `.` | Newline-delimited pathspecs to `git add`. | +| `labels` | no | `""` | Newline-delimited labels. | +| `draft` | no | `false` | Open as draft. | +| `signoff` | no | `false` | Add `Signed-off-by` trailer. | +| `commit-method` | no | `git` | `git` = unsigned local commit and push; `api` = signed API commit. | +| `if-no-changes` | no | `skip` | `skip` = exit 0; `fail` = error when nothing to commit. | +| `git-user-name` | no | `github-actions[bot]` | Author name for `commit-method: git`, and the sign-off. | +| `git-user-email` | no | github-actions[bot] noreply | Author email for `commit-method: git`, and the sign-off. | +| `github-token` | no | `${{ github.token }}` | Token for the commit and `gh`. Use an App token to retrigger CI. | Outputs: `changed`, `created`, `pr-number`, `pr-url`. diff --git a/tests/open-pr-api-commit.sh b/tests/open-pr-api-commit.sh new file mode 100755 index 00000000..a022f4c3 --- /dev/null +++ b/tests/open-pr-api-commit.sh @@ -0,0 +1,483 @@ +#!/usr/bin/env bash +# +# Functional test for actions/open-pr with commit-method: api. +# +# Runs the action's commit script with a fake `gh` that serves the GraphQL and +# REST calls it makes from a local bare repository, mimicking GitHub: +# createCommitOnBranch needs an existing branch and a matching expectedHeadOid, +# keeps the mode of modified files and commits new files as 100644, and +# updateRefs applies leased updates atomically. Behaviour against real GitHub +# (signed commits, ref deletion in updateRefs) was checked by hand; see +# actions/open-pr/README.md. + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +action="${repo_root}/actions/open-pr/action.yml" +script="${repo_root}/actions/open-pr/commit.sh" +tmp_dir="$(mktemp -d)" +trap 'rm -rf "${tmp_dir}"' EXIT +cd "${tmp_dir}" + +branch="chore/config-sync" + +fail() { + echo "error: $*" >&2 + exit 1 +} + +# shellcheck disable=SC2016 # Literal text in action.yml. +grep -Fq 'run: bash "${GITHUB_ACTION_PATH}/commit.sh"' "${action}" || + fail "${action} no longer runs commit.sh; update this test" +# shellcheck disable=SC2016 # Literal text in action.yml. +grep -Fq 'COMMIT_METHOD: ${{ inputs.commit-method }}' "${action}" || + fail "${action} does not pass commit-method to commit.sh" +awk '/^ commit-method:/ { found = 1 } found && /^ default:/ { print $2; exit }' "${action}" | grep -Fxq git || + fail "commit-method must keep defaulting to git; changing it is a breaking change" + +export GIT_CONFIG_GLOBAL="${tmp_dir}/gitconfig" +export GIT_CONFIG_NOSYSTEM=1 +git config --global init.defaultBranch main +git config --global user.name "test" +git config --global user.email "test@example.com" +git config --global commit.gpgsign false +git config --global protocol.file.allow always + +origin="${tmp_dir}/origin.git" +git init --quiet --bare "${origin}" +git -C "${origin}" config core.logAllRefUpdates always +export FAKE_REMOTE="${origin}" +export FAKE_LOG="${tmp_dir}/gh.log" +FAKE_JQ="$(command -v jq)" +export FAKE_JQ + +mkdir -p "${tmp_dir}/bin" +cat >"${tmp_dir}/bin/gh" <<'FAKE_GH' +#!/usr/bin/env bash +# Fake gh: serves the calls actions/open-pr/commit.sh makes from ${FAKE_REMOTE}. +set -euo pipefail +printf '%s\n' "$*" >>"${FAKE_LOG}" +zero=0000000000000000000000000000000000000000 +g() { git -C "${FAKE_REMOTE}" "$@"; } +jq() { "${FAKE_JQ}" "$@"; } +err() { + echo "gh: $*" >&2 + exit 1 +} +oid_of() { g rev-parse --verify --quiet "$1^{commit}" || true; } + +create_commit() { + local input="$1" ref expected tip index empty tree commit message body i n path mode blob + [ "$(jq -r '.variables.input.branch.repositoryNameWithOwner' "${input}")" = "owner/repo" ] || err "wrong repository" + ref="refs/heads/$(jq -r '.variables.input.branch.branchName' "${input}")" + expected="$(jq -r '.variables.input.expectedHeadOid' "${input}")" + tip="$(oid_of "${ref}")" + [ -n "${tip}" ] || err "A ref named \"${ref}\" does not exist." + [ "${tip}" = "${expected}" ] || err "Expected branch to point to \"${expected}\" but it did not." + index="$(mktemp -u)" + empty="$(mktemp -d)" + export GIT_INDEX_FILE="${index}" GIT_WORK_TREE="${empty}" + g read-tree "${tip}" + n="$(jq '.variables.input.fileChanges.additions | length' "${input}")" + for ((i = 0; i < n; i++)); do + path="$(jq -r --argjson i "${i}" '.variables.input.fileChanges.additions[$i].path' "${input}")" + blob="$(jq -r --argjson i "${i}" '.variables.input.fileChanges.additions[$i].contents' "${input}" | + base64 -d | g hash-object -w --stdin)" + # Like GitHub: a modified file keeps its mode, a new file is 100644. + mode="$(g ls-tree "${tip}" -- "${path}" | awk '{ print $1 }')" + g update-index --add --cacheinfo "${mode:-100644},${blob},${path}" + done + n="$(jq '.variables.input.fileChanges.deletions | length' "${input}")" + for ((i = 0; i < n; i++)); do + path="$(jq -r --argjson i "${i}" '.variables.input.fileChanges.deletions[$i].path' "${input}")" + [ -n "$(g ls-tree "${tip}" -- "${path}")" ] || err "deleting missing file ${path}" + g update-index --force-remove -- "${path}" + done + tree="$(g write-tree)" + message="$(jq -r '.variables.input.message.headline' "${input}")" + body="$(jq -r '.variables.input.message.body // empty' "${input}")" + [ -z "${body}" ] || message="${message}"$'\n\n'"${body}" + commit="$(printf '%s' "${message}" | GIT_AUTHOR_NAME=app GIT_AUTHOR_EMAIL=app@example.com \ + GIT_COMMITTER_NAME=GitHub GIT_COMMITTER_EMAIL=noreply@github.com g commit-tree "${tree}" -p "${tip}")" + g update-ref "${ref}" "${commit}" "${tip}" + # GitHub signs every commit this mutation makes. + echo "${commit}" >>"${FAKE_REMOTE}/signed-commits" + rm -rf "${index}" "${empty}" + unset GIT_INDEX_FILE GIT_WORK_TREE + # Someone pushes to the working branch while the run is committing. + if [ -n "${FAKE_RACE_REF:-}" ]; then + g update-ref "${FAKE_RACE_REF}" "${FAKE_RACE_OID}" + fi + jq -n --arg oid "${commit}" '{data: {createCommitOnBranch: {commit: {oid: $oid}}}}' +} + +update_refs() { + local input="$1" n i name before after force current + [ "$(jq -r '.variables.input.repositoryId' "${input}")" = "R_fake" ] || err "wrong repository id" + n="$(jq '.variables.input.refUpdates | length' "${input}")" + for ((i = 0; i < n; i++)); do + IFS=$'\t' read -r name before after force < <(jq -r --argjson i "${i}" \ + '.variables.input.refUpdates[$i] | [.name, (.beforeOid // "-"), .afterOid, (.force // false)] | @tsv' "${input}") + current="$(oid_of "${name}")" + current="${current:-${zero}}" + if [ "${before}" != "-" ] && [ "${before}" != "${current}" ]; then + err "Something went wrong while executing your query." + fi + if [ "${after}" != "${zero}" ] && [ "${current}" != "${zero}" ] && [ "${force}" != "true" ] && + ! g merge-base --is-ancestor "${current}" "${after}"; then + err "non-fast-forward update of ${name}" + fi + done + for ((i = 0; i < n; i++)); do + IFS=$'\t' read -r name after < <(jq -r --argjson i "${i}" \ + '.variables.input.refUpdates[$i] | [.name, .afterOid] | @tsv' "${input}") + if [ "${after}" = "${zero}" ]; then + g update-ref -d "${name}" + else + g update-ref "${name}" "${after}" + fi + done + echo '{"data":{"updateRefs":{"clientMutationId":null}}}' +} + +state_query() { + local ref="$1" parent="$2" base=null ref_json=null tip parents signature=null + if g cat-file -e "${parent}^{commit}" 2>/dev/null; then + base="$(jq -cn --arg oid "${parent}" '{oid: $oid}')" + fi + tip="$(oid_of "${ref}")" + if [ -n "${tip}" ]; then + parents="$(g rev-list --parents -n 1 "${tip}" | cut -s -d ' ' -f 2- | jq -cR 'split(" ") | map({oid: .})')" + if grep -Fxq "${tip}" "${FAKE_REMOTE}/signed-commits" 2>/dev/null; then + signature='{"isValid":true}' + fi + ref_json="$(jq -cn --arg oid "${tip}" --arg tree "$(g rev-parse "${tip}^{tree}")" --argjson parents "${parents:-[]}" \ + --argjson signature "${signature}" \ + '{target: {oid: $oid, tree: {oid: $tree}, parents: {nodes: $parents}, signature: $signature}}')" + fi + jq -n --argjson base "${base}" --argjson ref "${ref_json}" \ + '{data: {repository: {id: "R_fake", nameWithOwner: "owner/repo", base: $base, ref: $ref}}}' +} + +method=GET +args=() +while [ $# -gt 0 ]; do + case "$1" in + -X) + method="$2" + shift 2 + ;; + *) + args+=("$1") + shift + ;; + esac +done +set -- "${args[@]}" +[ "$1" = "api" ] || err "unexpected command: $*" +shift + +if [ "$1" = "graphql" ]; then + shift + input="" jq_expr="" owner="" name="" ref="" parent="" + while [ $# -gt 0 ]; do + case "$1" in + --input) input="$2" ;; + --jq) jq_expr="$2" ;; + -f | -F) + case "$2" in + owner=*) owner="${2#owner=}" ;; + name=*) name="${2#name=}" ;; + ref=*) ref="${2#ref=}" ;; + parent=*) parent="${2#parent=}" ;; + esac + ;; + esac + shift 2 + done + if [ -z "${input}" ]; then + [ "${owner}" = "{owner}" ] && [ "${name}" = "{repo}" ] || err "repository not taken from the checkout" + out="$(state_query "${ref}" "${parent}")" + elif grep -q 'createCommitOnBranch' "${input}"; then + out="$(create_commit "${input}")" + elif grep -q 'updateRefs' "${input}"; then + out="$(update_refs "${input}")" + else + err "unexpected mutation" + fi + if [ -n "${jq_expr}" ]; then + jq -r "${jq_expr}" <<<"${out}" + else + printf '%s\n' "${out}" + fi + exit 0 +fi + +case "${method} $1" in + "GET repos/owner/repo/git/trees/"*"?recursive=1") + sha="${1#repos/owner/repo/git/trees/}" + sha="${sha%%\?*}" + g ls-tree -r "${sha}" | jq -R 'capture("^(?\\S+) (?\\S+) (?\\S+)\t(?.*)$")' | + jq -s --arg sha "${sha}" '{sha: $sha, truncated: false, tree: .}' + ;; + "DELETE repos/owner/repo/git/refs/heads/"*) + ref="refs/${1#repos/owner/repo/git/refs/}" + [ -n "$(oid_of "${ref}")" ] || err "Reference does not exist (HTTP 422)" + g update-ref -d "${ref}" + ;; + *) err "unexpected call: ${method} $*" ;; +esac +FAKE_GH +chmod +x "${tmp_dir}/bin/gh" +export PATH="${tmp_dir}/bin:${PATH}" + +seed="${tmp_dir}/seed" +git clone --quiet "file://${origin}" "${seed}" 2>/dev/null +echo "base" >"${seed}/config.txt" +echo "old" >"${seed}/obsolete.txt" +printf '#!/bin/sh\necho old\n' >"${seed}/tool.sh" +chmod +x "${seed}/tool.sh" +git -C "${seed}" add . +git -C "${seed}" update-index --chmod=+x tool.sh +git -C "${seed}" commit --quiet -m "initial" +git -C "${seed}" push --quiet origin HEAD:refs/heads/main + +# A fresh shallow checkout of main, as actions/checkout leaves it, plus the +# changes a sync job makes: a modified, an added binary, a new executable, a +# path with a space, a modified executable, and a deleted file. +new_checkout() { + local dir="$1" + git init --quiet "${dir}" + git -C "${dir}" remote add origin "file://${origin}" + git -C "${dir}" fetch --quiet --no-tags --depth=1 origin "+refs/heads/main:refs/remotes/origin/main" + git -C "${dir}" checkout --quiet -B main refs/remotes/origin/main + echo "synced" >"${dir}/config.txt" + printf 'bin\000ary\377\n' >"${dir}/data.bin" + mkdir -p "${dir}/dir with space" + echo "nested" >"${dir}/dir with space/file.txt" + printf '#!/bin/sh\necho new\n' >"${dir}/new-tool.sh" + chmod +x "${dir}/new-tool.sh" + printf '#!/bin/sh\necho updated\n' >"${dir}/tool.sh" + rm "${dir}/obsolete.txt" + git -C "${dir}" update-index --add --chmod=+x new-tool.sh +} + +run_action() { + local dir="$1" output="$2" + shift 2 + ( + cd "${dir}" + env GITHUB_OUTPUT="${output}" \ + GIT_USER_NAME="github-actions[bot]" \ + GIT_USER_EMAIL="41898282+github-actions[bot]@users.noreply.github.com" \ + BRANCH="${branch}" \ + BASE="main" \ + COMMIT_MESSAGE=$'chore: sync config files\n\nFrom the central repo.' \ + TITLE="chore: sync config files" \ + PATHS="." \ + SIGNOFF="true" \ + IF_NO_CHANGES="skip" \ + COMMIT_METHOD="api" \ + GH_TOKEN="fake" \ + "$@" bash "${script}" + ) +} + +remote_tip() { + git -C "${origin}" rev-parse --verify --quiet "refs/heads/$1" || true +} + +no_tmp_refs() { + [ -z "$(git -C "${origin}" for-each-ref 'refs/heads/open-pr-tmp/')" ] || fail "temporary branch left behind: $1" +} + +calls() { + grep -c -- "$1" "${FAKE_LOG}" || true +} + +# 1. A new branch gets one commit on top of main with every change. +work="${tmp_dir}/work" +new_checkout "${work}" +run_action "${work}" "${tmp_dir}/output-1" >"${tmp_dir}/run-1.log" 2>&1 || { + cat "${tmp_dir}/run-1.log" >&2 + fail "API commit to a new branch failed" +} +grep -Fxq "changed=true" "${tmp_dir}/output-1" || fail "changed=true missing after the first commit" +tip="$(remote_tip "${branch}")" +[ -n "${tip}" ] || fail "branch was not created" +[ "$(git -C "${origin}" rev-parse "${tip}^")" = "$(remote_tip main)" ] || fail "commit must sit directly on main" +[ "$(git -C "${origin}" show "${tip}:config.txt")" = "synced" ] || fail "modified file has the wrong content" +cmp -s <(git -C "${origin}" cat-file blob "${tip}:data.bin") "${work}/data.bin" || fail "binary file was corrupted" +[ "$(git -C "${origin}" show "${tip}:dir with space/file.txt")" = "nested" ] || fail "path with a space was lost" +git -C "${origin}" cat-file -e "${tip}:obsolete.txt" 2>/dev/null && fail "deleted file is still in the commit" +[ "$(git -C "${origin}" ls-tree "${tip}" tool.sh | awk '{ print $1 }')" = "100755" ] || + fail "modified executable lost its mode" +grep -Fq "new-tool.sh is executable" "${tmp_dir}/run-1.log" || fail "no warning for the executable bit on a new file" +[ "$(git -C "${origin}" log -1 --format=%B "${tip}")" = "$(printf '%s\n\n%s\n\n%s' "chore: sync config files" \ + "From the central repo." "Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>")" ] || + fail "commit message, body or sign-off is wrong: $(git -C "${origin}" log -1 --format=%B "${tip}")" +[ "$(git -C "${work}" rev-parse --abbrev-ref HEAD)" = "main" ] || fail "the API method must not move the local checkout" +no_tmp_refs "first commit" + +# 2. An identical re-run leaves the branch alone, even though the new file's +# executable bit makes the trees differ. jq output with CRLF line endings, as +# on Windows runners, must not break the comparison. +mkdir -p "${tmp_dir}/crlf-bin" +cat >"${tmp_dir}/crlf-bin/jq" <"${tmp_dir}/run-2.log" 2>&1 || { + cat "${tmp_dir}/run-2.log" >&2 + fail "identical re-run failed" +} +grep -Fq "skipping commit" "${tmp_dir}/run-2.log" || fail "identical re-run should skip the commit" +[ "$(remote_tip "${branch}")" = "${tip}" ] || fail "identical re-run must not rewrite the branch" +[ "$(calls '--input')" = "${before}" ] || fail "identical re-run must not call a mutation" +grep -Fxq "changed=true" "${tmp_dir}/output-2" || fail "identical re-run must still report changed=true so the PR is reused" + +# 2b. Switching a caller from commit-method: git to api replaces its unsigned +# commit even when the files and base are unchanged, so the open PR stops +# being blocked by required signatures. +switch="${tmp_dir}/switch" +new_checkout "${switch}" +run_action "${switch}" "${tmp_dir}/output-2b-git" COMMIT_METHOD=git >/dev/null 2>&1 || + fail "seeding an unsigned commit with commit-method: git failed" +unsigned="$(remote_tip "${branch}")" +grep -Fxq "${unsigned}" "${origin}/signed-commits" && fail "the git method's commit must count as unsigned" +switched="${tmp_dir}/switched" +new_checkout "${switched}" +run_action "${switched}" "${tmp_dir}/output-2b" >"${tmp_dir}/run-2b.log" 2>&1 || { + cat "${tmp_dir}/run-2b.log" >&2 + fail "replacing an unsigned commit failed" +} +tip="$(remote_tip "${branch}")" +[ "${tip}" != "${unsigned}" ] || fail "an identical but unsigned commit must be replaced" +grep -Fxq "${tip}" "${origin}/signed-commits" || fail "the replacement commit must be signed" +[ "$(git -C "${origin}" rev-parse "${tip}^")" = "$(remote_tip main)" ] || fail "the replacement must sit directly on main" +grep -Fq "not signed; replacing it" "${tmp_dir}/run-2b.log" || fail "no message about replacing the unsigned commit" +no_tmp_refs "switch" + +# 2c. A signed tip with the same files but an extra submodule (or symlink) is +# not identical and gets replaced. +index="${tmp_dir}/gitlink-index" +GIT_INDEX_FILE="${index}" git -C "${origin}" read-tree "${tip}" +GIT_INDEX_FILE="${index}" git -C "${origin}" update-index --add --cacheinfo "160000,$(remote_tip main),vendor/module" +gitlink_tree="$(GIT_INDEX_FILE="${index}" git -C "${origin}" write-tree)" +gitlink_tip="$(git -C "${origin}" commit-tree "${gitlink_tree}" -p "$(remote_tip main)" -m "add submodule")" +echo "${gitlink_tip}" >>"${origin}/signed-commits" +git -C "${origin}" update-ref "refs/heads/${branch}" "${gitlink_tip}" +gitlink="${tmp_dir}/gitlink" +new_checkout "${gitlink}" +run_action "${gitlink}" "${tmp_dir}/output-2c" >"${tmp_dir}/run-2c.log" 2>&1 || { + cat "${tmp_dir}/run-2c.log" >&2 + fail "replacing a tip with an extra submodule failed" +} +tip="$(remote_tip "${branch}")" +[ "${tip}" != "${gitlink_tip}" ] || fail "a tip with an extra submodule must not count as identical" +[ -z "$(git -C "${origin}" ls-tree "${tip}" vendor/module)" ] || fail "the stray submodule survived the update" +no_tmp_refs "gitlink" + +# 3. New content replaces the commit in one leased step; the branch (and an +# open PR on it) never points at main in between. +update="${tmp_dir}/update" +new_checkout "${update}" +echo "synced again" >"${update}/config.txt" +run_action "${update}" "${tmp_dir}/output-3" >/dev/null 2>&1 || fail "updating an existing branch failed" +new_tip="$(remote_tip "${branch}")" +[ "${new_tip}" != "${tip}" ] || fail "branch was not updated" +[ "$(git -C "${origin}" rev-parse "${new_tip}^")" = "$(remote_tip main)" ] || fail "updated branch must be one commit on main" +[ "$(git -C "${origin}" show "${new_tip}:config.txt")" = "synced again" ] || fail "updated branch has the wrong content" +git -C "${origin}" reflog show --format=%H "refs/heads/${branch}" | grep -Fxq "$(remote_tip main)" && + fail "the working branch was reset to main, which would close its PR" +no_tmp_refs "update" + +# 4. When main moves, the commit is rebuilt on the new main. +echo "other" >"${seed}/other.txt" +git -C "${seed}" add other.txt +git -C "${seed}" commit --quiet -m "main moved" +git -C "${seed}" push --quiet origin HEAD:refs/heads/main +moved="${tmp_dir}/moved" +new_checkout "${moved}" +run_action "${moved}" "${tmp_dir}/output-4" >/dev/null 2>&1 || fail "commit after main moved failed" +[ "$(git -C "${origin}" rev-parse "$(remote_tip "${branch}")^")" = "$(remote_tip main)" ] || + fail "branch was not rebuilt on the new main" + +# 5. A push to the working branch during the run is never overwritten. +race="${tmp_dir}/race" +new_checkout "${race}" +echo "raced" >"${race}/config.txt" +if run_action "${race}" "${tmp_dir}/output-5" FAKE_RACE_REF="refs/heads/${branch}" FAKE_RACE_OID="${tip}" \ + >"${tmp_dir}/run-5.log" 2>&1; then + fail "a concurrent push to the branch must fail the run" +fi +grep -Fq "changed on GitHub while this run was committing" "${tmp_dir}/run-5.log" || fail "no clear error for the race" +[ "$(remote_tip "${branch}")" = "${tip}" ] || fail "the concurrent push was overwritten" +no_tmp_refs "race" + +# 6. A brand-new branch after the old one was deleted (merged PR). +git -C "${origin}" update-ref -d "refs/heads/${branch}" +fresh="${tmp_dir}/fresh" +new_checkout "${fresh}" +run_action "${fresh}" "${tmp_dir}/output-6" >/dev/null 2>&1 || fail "re-creating the branch failed" +[ -n "$(remote_tip "${branch}")" ] || fail "branch was not re-created" + +# 7. Changes the API can't express fail before anything is written. +symlink="${tmp_dir}/symlink" +new_checkout "${symlink}" +ln -s config.txt "${symlink}/link" +before="$(remote_tip "${branch}")" +if run_action "${symlink}" "${tmp_dir}/output-7" >"${tmp_dir}/run-7.log" 2>&1; then + fail "a symlink must fail with commit-method: api" +fi +grep -Fq "which createCommitOnBranch can't commit. Use commit-method: git" "${tmp_dir}/run-7.log" || fail "symlink error does not point to commit-method: git" +[ "$(remote_tip "${branch}")" = "${before}" ] || fail "a failed run must not touch the branch" +no_tmp_refs "symlink" + +# 7b. A changed Git LFS pointer fails too: the API can't upload its object. +lfs="${tmp_dir}/lfs" +new_checkout "${lfs}" +printf 'version https://git-lfs.github.com/spec/v1\noid sha256:%064d\nsize 12\n' 0 >"${lfs}/model.bin" +if run_action "${lfs}" "${tmp_dir}/output-7b" >"${tmp_dir}/run-7b.log" 2>&1; then + fail "an LFS pointer must fail with commit-method: api" +fi +grep -Fq "model.bin is a Git LFS pointer" "${tmp_dir}/run-7b.log" || fail "no clear error for an LFS pointer" +[ "$(remote_tip "${branch}")" = "${before}" ] || fail "a failed run must not touch the branch" +no_tmp_refs "lfs" + +# 7c. A mode change on an existing file fails instead of being dropped. +chmod_dir="${tmp_dir}/chmod" +new_checkout "${chmod_dir}" +git -C "${chmod_dir}" update-index --chmod=-x tool.sh +chmod -x "${chmod_dir}/tool.sh" +if run_action "${chmod_dir}" "${tmp_dir}/output-7c" >"${tmp_dir}/run-7c.log" 2>&1; then + fail "a mode change must fail with commit-method: api" +fi +grep -Fq "tool.sh changes mode from 100755 to 100644" "${tmp_dir}/run-7c.log" || fail "no clear error for a mode change" +[ "$(remote_tip "${branch}")" = "${before}" ] || fail "a failed run must not touch the branch" + +# 8. A local commit that was never pushed can't be the parent. +local_commit="${tmp_dir}/local-commit" +new_checkout "${local_commit}" +git -C "${local_commit}" commit --quiet --allow-empty -m "local only" +if run_action "${local_commit}" "${tmp_dir}/output-8" >"${tmp_dir}/run-8.log" 2>&1; then + fail "a local-only base commit must fail with commit-method: api" +fi +grep -Fq "is not on GitHub" "${tmp_dir}/run-8.log" || fail "no clear error for a local-only base commit" + +# 9. Nothing to commit: no API calls, changed=false. +clean="${tmp_dir}/clean" +new_checkout "${clean}" +git -C "${clean}" reset --quiet --hard +git -C "${clean}" clean --quiet -fdx +before="$(wc -l <"${FAKE_LOG}")" +run_action "${clean}" "${tmp_dir}/output-9" >/dev/null 2>&1 || fail "no-change run failed" +grep -Fxq "changed=false" "${tmp_dir}/output-9" || fail "changed=false missing when there is nothing to commit" +[ "$(wc -l <"${FAKE_LOG}")" = "${before}" ] || fail "no-change run must not call the API" + +echo "✓ open-pr (api): new, identical, updated, rebased, raced, re-created, unsupported, local-only and no-change runs all handled." diff --git a/tests/open-pr-push.sh b/tests/open-pr-push.sh index 25a6ec49..20795b58 100755 --- a/tests/open-pr-push.sh +++ b/tests/open-pr-push.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash # -# Functional test for the push logic of actions/open-pr. +# Functional test for the push logic of actions/open-pr (commit-method: git). # -# Runs the action's "Stage and commit changes" script against a local bare +# Runs the action's commit script against a local bare # remote that mimics a GitHub checkout: a shallow clone with no # remote-tracking ref for the working branch. Before the fix, a bare # `git push --force-with-lease` then failed with "stale info" whenever the @@ -34,17 +34,10 @@ git config --global user.email "test@example.com" git config --global commit.gpgsign false git config --global protocol.file.allow always -script="${tmp_dir}/commit.sh" -awk ' - $0 == " - name: Stage and commit changes" { in_step = 1; next } - in_step && $0 ~ /^ run: [|]/ { in_run = 1; next } - in_run { - if ($0 ~ /^ - name:/ || $0 ~ /^ [a-z-]+:/) exit - sub(/^ /, "") - print - } -' "${action}" >"${script}" -[ -s "${script}" ] || fail "could not extract the commit script from ${action}" +script="${repo_root}/actions/open-pr/commit.sh" +# shellcheck disable=SC2016 # Literal text in action.yml. +grep -Fq 'run: bash "${GITHUB_ACTION_PATH}/commit.sh"' "${action}" || + fail "${action} no longer runs commit.sh; update this test" origin="${tmp_dir}/origin.git" git init --quiet --bare "${origin}" @@ -88,6 +81,7 @@ run_action() { BRANCH="${branch}" \ BASE="main" \ COMMIT_MESSAGE="" \ + COMMIT_METHOD="git" \ TITLE="chore: sync config files" \ PATHS="." \ SIGNOFF="false" \ @@ -184,4 +178,4 @@ run_action "${clean}" "${tmp_dir}/output-5" >/dev/null 2>&1 || fail "no-change r grep -Fxq "changed=false" "${tmp_dir}/output-5" || fail "changed=false missing when there is nothing to commit" [ "$(remote_tip "${branch}")" = "${before}" ] || fail "no-change run must not push" -echo "✓ open-pr: existing, identical, rebased, new, and no-change branches all handled." +echo "✓ open-pr (git): existing, identical, rebased, new, and no-change branches all handled."