diff --git a/.github/workflows/test-breaking.yaml b/.github/workflows/test-breaking.yaml index 7044199..a243768 100644 --- a/.github/workflows/test-breaking.yaml +++ b/.github/workflows/test-breaking.yaml @@ -593,9 +593,11 @@ jobs: name: Test breaking Pro path posts the review comment and sets the gate # With an oasdiff-token the breaking action is the Pro action: it uploads # an authenticated review of the breaking changes, posts the review - # comment and sets the oasdiff commit status. Stubs oasdiff (one change in - # the json render; --open prints a /review/ep URL and a pending status) - # and curl (keeps the comment body). + # comment and sets the oasdiff commit status. The comment shows the + # decision recorded for each breaking change, and none for an info change. + # Stubs oasdiff (three changes in the json render; --open prints a + # /review/ep URL and a pending status) and curl (serves the decisions, + # keeps the comment body). steps: - uses: actions/checkout@v7 - name: Stub oasdiff + curl (Pro success), run entrypoint @@ -605,7 +607,7 @@ jobs: cat > /tmp/stub/oasdiff <<'STUB' #!/bin/sh - for a in "$@"; do [ "$a" = "json" ] && { printf '[{"id":"api-removed-without-deprecation","text":"API removed","level":3,"operation":"GET","path":"/pets","section":"paths"}]'; exit 0; }; done + for a in "$@"; do [ "$a" = "json" ] && { printf '[{"id":"api-removed-without-deprecation","text":"API removed","level":3,"operation":"GET","path":"/pets","section":"paths","fingerprint":"aaaaaaaaaaaa"},{"id":"request-property-removed","text":"removed the request property","level":2,"operation":"POST","path":"/pets","section":"paths","fingerprint":"bbbbbbbbbbbb"},{"id":"endpoint-added","text":"endpoint added","level":1,"operation":"PUT","path":"/pets","section":"paths","fingerprint":"cccccccccccc"}]'; exit 0; }; done for a in "$@"; do if [ "$a" = "--open" ]; then echo "$1" > /tmp/run/open-command @@ -620,11 +622,20 @@ jobs: cat > /tmp/stub/curl <<'STUB' #!/bin/sh + prev="" for a in "$@"; do case "$a" in *"%{http_code}"*) http_code=1 ;; esac case "$a" in *"/issues/"*) comment=1 ;; esac case "$a" in *"/statuses/"*) status=1 ;; esac + case "$a" in *"/encrypted-pr-review/test-token-uuid/changes") decisions=1 ;; esac + [ "$prev" = "-o" ] && out_file="$a" + prev="$a" done + if [ -n "${decisions:-}" ]; then + printf '{"changes":[{"fingerprint":"aaaaaaaaaaaa","status":"approved"},{"fingerprint":"bbbbbbbbbbbb","status":"pending"},{"fingerprint":"cccccccccccc","status":"pending"}]}' > "$out_file" + echo "200" + exit 0 + fi if [ -n "${http_code:-}" ]; then if [ -n "${comment:-}" ]; then cat > /tmp/run/pro-comment.json elif [ -n "${status:-}" ]; then cat > /tmp/run/status.json @@ -657,10 +668,12 @@ jobs: pro_body=$(jq -r '.body' < /tmp/run/pro-comment.json) echo "--- Pro comment body ---"; echo "$pro_body" for expected in \ - '1 breaking change in this pull request' \ + '3 breaking changes in this pull request' \ 'Review required' \ - '| severity | change | endpoint |' \ - 'api-removed-without-deprecation' \ + '| severity | change | endpoint | status |' \ + 'GET /pets | ✅ approved |' \ + 'POST /pets | ⏳ pending |' \ + 'PUT /pets | |' \ 'Review and approve the changes' do case "$pro_body" in diff --git a/README.md b/README.md index 029ec1c..b3a9d3e 100644 --- a/README.md +++ b/README.md @@ -269,6 +269,8 @@ oasdiff Pro adds a sign-off step to the review, so a breaking change can't merge On every pull request, oasdiff posts the encrypted side-by-side review and gives each change **Approve / Reject** buttons. A commit status check named `oasdiff` blocks the merge until every breaking change is approved. Approvals are tied to the change fingerprint and carry forward across commits, with a record of who approved what and when. +The comment lists the changes, and each breaking change shows its status: approved, rejected or pending. The action writes the comment, so the statuses are brought up to date on every run of the job: after a push, or when you re-run the job. + [Start a free trial](https://www.oasdiff.com/start-trial) (no credit card) to get your token, then add it as an `OASDIFF_TOKEN` repository secret. ```yaml diff --git a/breaking/entrypoint.sh b/breaking/entrypoint.sh index f92c6db..20fe031 100755 --- a/breaking/entrypoint.sh +++ b/breaking/entrypoint.sh @@ -88,13 +88,30 @@ from_fork () { # the rest collapse, up to $comment_rows_listed, since a changelog can run to # thousands of changes and a GitHub comment is limited to 65,536 characters. # $1: the json render of the comparison (Run 3). +# $2: optional, the decisions recorded on the review (read_review_decisions). +# With it, each breaking change carries its status: approved, rejected or +# pending. Other changes need no approval and have none. comment_rows_shown=5 comment_rows_listed=50 review_comment_changes () { changes_file="$1" + decisions_file="${2:-}" [ -s "$changes_file" ] || return 0 - - jq -r --argjson shown "$comment_rows_shown" --argjson listed "$comment_rows_listed" ' + if [ -s "$decisions_file" ]; then + with_status=true + else + with_status=false + decisions_file=/dev/null + fi + + jq -r --argjson shown "$comment_rows_shown" --argjson listed "$comment_rows_listed" \ + --argjson with_status "$with_status" --slurpfile decisions "$decisions_file" ' + (($decisions[0].changes // []) | map({(.fingerprint): .status}) | add // {}) as $status + | def status_cell: + if .level < 2 then "" + elif $status[.fingerprint // ""] == "approved" then "✅ approved" + elif $status[.fingerprint // ""] == "rejected" then "❌ rejected" + else "⏳ pending" end; def severity: if . == 3 then "error" elif . == 2 then "warning" else "info" end; def endpoint: if (.operation // "") == "" then (.section // "") else .operation + " " + .path end; @@ -109,8 +126,11 @@ review_comment_changes () { | gsub("_"; "_") | gsub("://"; "://") | gsub("www\\."; "www.") | gsub("[\r\n]+"; " "); def row: "| " + (.level | severity) + " | " + ((.text // "") | inert) - + " [`" + .id + "`](https://www.oasdiff.com/checks/" + .id + ") | " + (endpoint | inert) + " |"; - def header: "| severity | change | endpoint |", "| --- | --- | --- |"; + + " [`" + .id + "`](https://www.oasdiff.com/checks/" + .id + ") | " + (endpoint | inert) + " |" + + (if $with_status then " " + status_cell + " |" else "" end); + def header: + if $with_status then "| severity | change | endpoint | status |", "| --- | --- | --- | --- |" + else "| severity | change | endpoint |", "| --- | --- | --- |" end; sort_by([-.level, (.path // ""), (.operation // ""), .id]) as $all | ($all | length) as $total | header, @@ -303,10 +323,29 @@ pro_review () { pro_gate="$gate" echo "### 📋 [Review and approve the API changes](${review_url})" >> "$GITHUB_STEP_SUMMARY" + read_review_decisions "$review_url" pro_comment "$review_url" "$gate" + rm -f "$review_decisions" set_commit_status "$owner" "$repo" "$head_sha" "$gate" "$review_url" } +# read_review_decisions reads the decision recorded for each change of the +# review into $review_decisions, for the comment to show. A reviewer approves +# or rejects on the review page; the decisions are kept per change, so every +# run of the job shows the ones made so far. +# $1: the review URL. +read_review_decisions () { + review_decisions=$(mktemp) + review_token=$(printf '%s' "$1" | sed -n 's|.*/review/ep/\([^#?/]*\).*|\1|p') + api_url="${OASDIFF_API_URL:-https://api.oasdiff.com}" + code=$(curl -s -m 10 -o "$review_decisions" -w "%{http_code}" \ + "${api_url%/}/tenants/${oasdiff_token}/encrypted-pr-review/${review_token}/changes" 2>/dev/null "$review_decisions" + fi +} + # pro_comment posts or updates the Pro review comment (marker oasdiff-pro-review). pro_comment () { review_url="$1" @@ -339,7 +378,7 @@ pro_comment () { ${status_line} -$(review_comment_changes "${changes_json:-}") +$(review_comment_changes "${changes_json:-}" "${review_decisions:-}") [Review and approve the changes](${review_url})