From c32373adaa3929a9204ea4efb9fb86b3738870f6 Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Fri, 27 Nov 2026 22:26:01 +0200 Subject: [PATCH 1/2] Fail the step when the job's token can't post the review comment When the job's token lacked pull-requests: write, the review comment failed with only a warning on a passing job, so the comment silently never appeared. The step now fails with an error naming the permission to add, or the opt-out (github-token: ''). A pull request from a fork, where the token is read-only whatever the job grants, still only gets a plain line, and any other refusal (such as a rate limit) still warns. The anonymous delivery event now carries GitHub's HTTP status when the comment fails. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test-breaking.yaml | 55 +++++++++++++++++++++------- README.md | 2 +- breaking/entrypoint.sh | 36 ++++++++++++++---- 3 files changed, 71 insertions(+), 22 deletions(-) diff --git a/.github/workflows/test-breaking.yaml b/.github/workflows/test-breaking.yaml index f2e2883..f6d0271 100644 --- a/.github/workflows/test-breaking.yaml +++ b/.github/workflows/test-breaking.yaml @@ -489,11 +489,12 @@ jobs: oasdiff_breaking_comment_failure_names_the_fix: runs-on: ubuntu-latest name: Test a blocked PR comment says what to change - # A comment that cannot post is reported two different ways, because only - # one of them is actionable: a job without pull-requests:write gets a - # warning naming the permission, while a pull request from a fork gets a + # A comment that cannot post is reported three ways, because only one of + # them is the job's configuration: a job without pull-requests:write fails + # with an error naming the permission, a pull request from a fork gets a # plain line, since GitHub makes the token read-only there whatever the - # job does. Both cases are also reported separately in the delivery mode. + # job does, and any other refusal (here a rate limit) only warns. Each case + # is also reported separately in the delivery event. steps: - uses: actions/checkout@v7 - name: Stub oasdiff + a rejecting curl, run with and without a fork event @@ -519,12 +520,13 @@ jobs: STUB chmod +x /tmp/stub/oasdiff - # Rejects the comment with 403, the shape GitHub returns both for a - # missing permission and for a fork's read-only token. + # Rejects the comment with 403 and the message in /tmp/run/message: + # GitHub's message for a missing permission (also what a fork's + # read-only token gets), or a rate-limit message. cat > /tmp/stub/curl <<'STUB' #!/bin/sh for a in "$@"; do - case "$prev" in -X) method=$a ;; esac + case "$prev" in -X) method=$a ;; -o) out=$a ;; esac prev=$a done case " $* " in @@ -532,6 +534,7 @@ jobs: esac if [ "${method:-GET}" = GET ]; then echo '[]'; exit 0; fi cat > /dev/null + printf '{"message":"%s"}' "$(cat /tmp/run/message)" > "${out:-/dev/null}" printf '403' STUB chmod +x /tmp/stub/curl @@ -551,21 +554,45 @@ jobs: 'INPUT_GITHUB-TOKEN=fake-token' bash ./breaking/entrypoint.sh } - echo "=== same-repo pull request: expect a warning naming the permission" + echo "=== same-repo pull request, missing permission: expect the step to fail naming the permission" + echo 'Resource not accessible by integration' > /tmp/run/message printf '{"pull_request":{"head":{"repo":{"full_name":"acme/api"}}}}' > /tmp/run/event.json - out=$(run_entrypoint 2>&1) + rc=0; out=$(run_entrypoint 2>&1) || rc=$? echo "$out" | grep -v '^flags:\|^running oasdiff' + if [ "$rc" -ne 1 ]; then + echo "FAIL: expected the step to fail with exit 1, got $rc" >&2 + exit 1 + fi case "$out" in - *"::warning::oasdiff: couldn't post the review link"*"pull-requests: write"*) ;; - *) echo "FAIL: no warning naming the permission" >&2; exit 1 ;; + *"::error title=oasdiff couldn't post the PR comment::"*"pull-requests: write"*"github-token"*) ;; + *) echo "FAIL: no error naming the permission and the opt-out" >&2; exit 1 ;; esac - mode=$(jq -r '.props.mode' < /tmp/run/telemetry.json) - if [ "$mode" != "comment_failed" ]; then - echo "FAIL: expected delivery mode 'comment_failed', got '$mode'" >&2 + props=$(jq -c '.props | {mode, reason}' < /tmp/run/telemetry.json) + if [ "$props" != '{"mode":"comment_failed","reason":"http_403_permission"}' ]; then + echo "FAIL: unexpected delivery event: $props" >&2 + exit 1 + fi + + echo "=== same-repo pull request, rate limited: expect a warning and a passing step" + echo 'You have exceeded a secondary rate limit' > /tmp/run/message + rc=0; out=$(run_entrypoint 2>&1) || rc=$? + echo "$out" | grep -v '^flags:\|^running oasdiff' + if [ "$rc" -ne 0 ]; then + echo "FAIL: a rate-limited comment must not fail the step, got exit $rc" >&2 + exit 1 + fi + case "$out" in + *"::warning::oasdiff: couldn't post the review link as a PR comment (HTTP 403)"*) ;; + *) echo "FAIL: no warning for the rate-limited comment" >&2; exit 1 ;; + esac + props=$(jq -c '.props | {mode, reason}' < /tmp/run/telemetry.json) + if [ "$props" != '{"mode":"comment_failed","reason":"http_403"}' ]; then + echo "FAIL: unexpected delivery event: $props" >&2 exit 1 fi echo "=== fork pull request: expect a plain line and no warning" + echo 'Resource not accessible by integration' > /tmp/run/message printf '{"pull_request":{"head":{"repo":{"full_name":"contrib/api-fork"}}}}' > /tmp/run/event.json out=$(run_entrypoint 2>&1) echo "$out" | grep -v '^flags:\|^running oasdiff' diff --git a/README.md b/README.md index 902e153..8f62e64 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ jobs: | `revision` | required | The revised (new) OpenAPI spec, in the same forms | | `fail-on` | `''` | Fail the job when a change is at or above this severity: `ERR` or `WARN`, or `MISSING-VERSION-BUMP` to fail only when breaking changes come without a major version increase in `info.version`. Empty never fails. This is policy rather than a fact about the run: once you have a `.oasdiff.yaml`, set it there | | `oasdiff-token` | `''` | Your oasdiff Pro token. See [Pro: Approve and gate changes](#pro-approve-and-gate-changes) | -| `github-token` | `${{ github.token }}` | Token used to post the comment. The default is the job's own token, so you only need to grant `permissions: pull-requests: write`. Set it to `''` to keep the review link in the job summary only | +| `github-token` | `${{ github.token }}` | Token used to post the comment. The default is the job's own token, so you only need to grant `permissions: pull-requests: write`. Without that permission the step fails with an error saying so. Set it to `''` to keep the review link in the job summary only | | `review` | `true` | Upload the encrypted comparison and link to the side-by-side review. Set `false` so that no spec leaves CI; annotations and the job result are unaffected | | `config` | `''` | Path to an oasdiff config file, when `.oasdiff.yaml` is not at the repository root | | `output-to-file` | `''` | Also write the report to this file | diff --git a/breaking/entrypoint.sh b/breaking/entrypoint.sh index 7c7ae52..b019b4d 100755 --- a/breaking/entrypoint.sh +++ b/breaking/entrypoint.sh @@ -157,13 +157,17 @@ review_comment_changes () { # or failed to post. This tells the maintainers how often users end up on the # summary fallback, which is what drives fixes to the defaults. It carries no # repo, organization, or user identity; action_ref is the action's own -# version tag. Best-effort with a 5 second cap, a failure never affects the -# run, and it is skipped when OASDIFF_INTERNAL=1 (the marker the CLI also -# uses for internal test runs). +# version tag, and reason is GitHub's HTTP status when the comment failed. +# Best-effort with a 5 second cap, a failure never affects the run, and it is +# skipped when OASDIFF_INTERNAL=1 (the marker the CLI also uses for internal +# test runs). report_review_delivery () { delivery_mode="$1" + delivery_reason="${2:-}" [ "${OASDIFF_INTERNAL:-}" = "1" ] && return 0 - printf '{"event":"review_delivery","props":{"mode":"%s","platform":"github-action","action_ref":"%s"}}' "$delivery_mode" "${GITHUB_ACTION_REF:-unknown}" | curl -s -m 5 -o /dev/null -X POST -H "Content-Type: application/json" "https://www.oasdiff.com/api/telemetry" --data-binary @- 2>/dev/null || true + jq -nc --arg mode "$delivery_mode" --arg ref "${GITHUB_ACTION_REF:-unknown}" --arg reason "$delivery_reason" \ + '{event: "review_delivery", props: ({mode: $mode, platform: "github-action", action_ref: $ref} + (if $reason == "" then {} else {reason: $reason} end))}' \ + | curl -s -m 5 -o /dev/null -X POST -H "Content-Type: application/json" "https://www.oasdiff.com/api/telemetry" --data-binary @- 2>/dev/null || true } post_review_comment () { @@ -226,10 +230,17 @@ $(review_comment_changes "${changes_json:-}" "$review_url") method="POST" fi - code=$(printf '%s' "$payload" | curl -s -o /dev/null -w "%{http_code}" -X "$method" \ + response=$(mktemp) + code=$(printf '%s' "$payload" | curl -s -o "$response" -w "%{http_code}" -X "$method" \ -H "Authorization: token $github_token" \ -H "Accept: application/vnd.github+json" \ "$endpoint" --data-binary @- 2>/dev/null) || code="000" + # GitHub answers a token without pull-requests: write with this message. A + # 403 for a secondary rate limit says something else, and is not the + # job's configuration. + missing_permission="" + grep -q "Resource not accessible by integration" "$response" 2>/dev/null && missing_permission=1 + rm -f "$response" if [ "$code" -ge 200 ] && [ "$code" -lt 300 ]; then [ -n "$review_url" ] && report_review_delivery "comment" @@ -240,8 +251,16 @@ $(review_comment_changes "${changes_json:-}" "$review_url") [ -n "$review_url" ] && report_review_delivery "comment_fork" echo "oasdiff: this pull request comes from a fork, where GitHub makes the token read-only, so the review link is in the job summary instead of a comment." else - [ -n "$review_url" ] && report_review_delivery "comment_failed" - echo "::warning::oasdiff: couldn't post the review link as a PR comment (HTTP ${code}). Add 'permissions: pull-requests: write' to the job that runs this action. The link is in the job summary meanwhile." + [ -n "$review_url" ] && report_review_delivery "comment_failed" "http_${code}${missing_permission:+_permission}" + if [ -n "$missing_permission" ] && [ -n "$review_url" ]; then + # A warning on a passing job is never read, so the step fails (in + # finish) until the job grants the permission or opts out of the + # comment. + review_comment_blocked=1 + echo "::error title=oasdiff couldn't post the PR comment::GitHub refused the comment (HTTP 403) because the job's token can't write to pull requests. Add 'permissions: pull-requests: write' to the job that runs this action, or set 'github-token: \"\"' on the action to keep the review link in the job summary only." + else + echo "::warning::oasdiff: couldn't post the review link as a PR comment (HTTP ${code}). The link is in the job summary." + fi fi } @@ -452,6 +471,9 @@ finish () { code=0 fi fi + if [ -n "${review_comment_blocked:-}" ]; then + code=1 + fi exit "$code" } From 3d660e8c8250adb786958d35c402c2d681632bd7 Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Fri, 27 Nov 2026 22:27:27 +0200 Subject: [PATCH 2/2] Keep the output tests off the PR comment These steps check the action's output, not the comment, and the test workflows don't grant pull-requests: write, so a comment attempt would now fail them. Set github-token: '' on each, the opt-out the new error names. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/test-breaking.yaml | 12 ++++++++++++ .github/workflows/test-changelog.yaml | 3 +++ 2 files changed, 15 insertions(+) diff --git a/.github/workflows/test-breaking.yaml b/.github/workflows/test-breaking.yaml index f6d0271..5bade94 100644 --- a/.github/workflows/test-breaking.yaml +++ b/.github/workflows/test-breaking.yaml @@ -23,6 +23,7 @@ jobs: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' output-to-file: 'breaking.txt' + github-token: '' - name: Test breaking changes action output env: ACTUAL_OUTPUT: ${{ steps.test_breaking_changes.outputs.breaking }} @@ -59,6 +60,7 @@ jobs: revision: 'specs/revision-breaking-warn.yaml' output-to-file: 'breaking.txt' fail-on: 'ERR' + github-token: '' - name: Test breaking changes action output env: ACTUAL_OUTPUT: ${{ steps.test_breaking_changes.outputs.breaking }} @@ -93,6 +95,7 @@ jobs: with: base: 'specs/petstore-base.yaml' revision: 'specs/petstore-revision.yaml' + github-token: '' - name: Test breaking changes action output env: ACTUAL_OUTPUT: ${{ steps.test_breaking_changes_matching_delimiter_not_found.outputs.breaking }} @@ -117,6 +120,7 @@ jobs: base: 'specs/glob/base/*.yaml' revision: 'specs/glob/revision/*.yaml' composed: true + github-token: '' - name: Test breaking action output env: ACTUAL_OUTPUT: ${{ steps.test_breaking_composed.outputs.breaking }} @@ -146,6 +150,7 @@ jobs: revision: specs/base-deprecation.yaml deprecation-days-beta: 14 deprecation-days-stable: 21 + github-token: '' oasdiff_breaking_flatten_allof: runs-on: ubuntu-latest name: Test breaking action with flatten-allof option @@ -161,6 +166,7 @@ jobs: base: 'specs/base-allof.yaml' revision: 'specs/revision-allof.yaml' flatten-allof: true + github-token: '' - name: Test breaking action output env: ACTUAL_OUTPUT: ${{ steps.test_breaking_flatten_allof.outputs.breaking }} @@ -183,6 +189,7 @@ jobs: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' err-ignore: 'specs/err-ignore.txt' + github-token: '' - name: Test breaking changes are suppressed env: ACTUAL_OUTPUT: ${{ steps.test_breaking_err_ignore.outputs.breaking }} @@ -218,6 +225,7 @@ jobs: with: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' + github-token: '' - name: Assert action failed (.oasdiff.yaml fail-on triggered) and report rendered env: ACTUAL_OUTPUT: ${{ steps.test_yaml_fail_on.outputs.breaking }} @@ -252,6 +260,7 @@ jobs: with: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' + github-token: '' - name: Assert breaking changes were suppressed by .oasdiff.yaml env: ACTUAL_OUTPUT: ${{ steps.test_yaml_err_ignore.outputs.breaking }} @@ -283,6 +292,7 @@ jobs: with: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' + github-token: '' - name: Assert the finding was downgraded by .oasdiff.yaml env: ACTUAL_OUTPUT: ${{ steps.test_yaml_severity_levels.outputs.breaking }} @@ -312,6 +322,7 @@ jobs: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' include-checks: 'not-a-real-check' + github-token: '' - name: Assert the run was unaffected env: ACTUAL_OUTPUT: ${{ steps.test_include_checks.outputs.breaking }} @@ -340,6 +351,7 @@ jobs: base: 'specs/base.yaml' revision: 'specs/revision-breaking.yaml' exclude-elements: 'endpoints' + github-token: '' - name: Assert the run was unaffected env: ACTUAL_OUTPUT: ${{ steps.test_exclude_elements.outputs.breaking }} diff --git a/.github/workflows/test-changelog.yaml b/.github/workflows/test-changelog.yaml index 8de5a46..fd4040b 100644 --- a/.github/workflows/test-changelog.yaml +++ b/.github/workflows/test-changelog.yaml @@ -23,6 +23,7 @@ jobs: base: https://raw.githubusercontent.com/oasdiff/oasdiff/main/data/openapi-test1.yaml revision: https://raw.githubusercontent.com/oasdiff/oasdiff/main/data/openapi-test3.yaml output-to-file: "changelog.txt" + github-token: '' - name: Test changelog action output run: | output=$(echo "${{steps.test_changelog.outputs.changelog}}" | head -n 1) @@ -54,6 +55,7 @@ jobs: base: 'specs/glob/base/*.yaml' revision: 'specs/glob/revision/*.yaml' composed: true + github-token: '' - name: Test changelog action output env: ACTUAL_OUTPUT: ${{ steps.test_changelog_composed.outputs.changelog }} @@ -80,6 +82,7 @@ jobs: with: base: https://raw.githubusercontent.com/oasdiff/oasdiff/main/data/openapi-test1.yaml revision: https://raw.githubusercontent.com/oasdiff/oasdiff/main/data/openapi-test3.yaml + github-token: '' - name: Assert level filter from .oasdiff.yaml suppressed warning/info findings run: | # Without the YAML, the same specs produce "26 changes: 4 error, 4 warning, 18 info".