diff --git a/.github/workflows/breaking_changes.yml b/.github/workflows/breaking_changes.yml index 662e4cd..faf9afc 100644 --- a/.github/workflows/breaking_changes.yml +++ b/.github/workflows/breaking_changes.yml @@ -21,8 +21,8 @@ jobs: TITLE: ${{ github.event.pull_request.title }} BODY: ${{ github.event.pull_request.body }} NOT_BREAKING: ${{ contains(github.event.pull_request.labels.*.name, 'sdk-not-breaking') }} - # wire applies to every SDK. go tickets carry no Ruby lines, so each one waits for a human to judge it here rather than pass unseen. - LISTS: (.wire // []) + (.go // []) + # The tickets the regen writes footers for: wire applies to every SDK, an sdk ticket only with a getstream-ruby line. + LINED: .info as $i | ($i."x-stream-breaking-changes".wire // []) + [$i."x-stream-breaking-change-lines" // {} | to_entries | .[] | select(.value.sdk // [] | any_c(test("^getstream-ruby "))) | .key] run: | set -euo pipefail # A fork's head commit resolves through this repository too. A 404 leaves the file empty: the commit predates .spec-version. @@ -32,7 +32,9 @@ jobs: : > "$RUNNER_TEMP/$2" fi } - read_version "$BASE_SHA" base-version + # The merge base, not the base tip: a branch cut before a regen merged still carries the older .spec-version, which is no downgrade. + base=$(gh api "repos/$GITHUB_REPOSITORY/compare/$BASE_SHA...$HEAD_SHA" --jq .merge_base_commit.sha) + read_version "$base" base-version read_version "$HEAD_SHA" head-version if [ ! -s "$RUNNER_TEMP/head-version" ]; then echo "the PR has no .spec-version"; exit 0; fi if cmp -s "$RUNNER_TEMP/base-version" "$RUNNER_TEMP/head-version"; then echo ".spec-version unchanged"; exit 0; fi @@ -44,7 +46,8 @@ jobs: [[ $old =~ $tag_re ]] || old= # Sets kind to the YAML type of the tag's list. Only a missing list is tolerated; any other shape or a yq error fails the step. - expr=".info.\"x-stream-breaking-changes\" | $LISTS | unique | .[]" + # go tickets carry no Ruby lines, so each one waits for a human to judge it here rather than pass unseen. + expr="($LINED) + (.info.\"x-stream-breaking-changes\".go // []) | unique | .[]" list() { curl -fsSL "https://raw.githubusercontent.com/GetStream/protocol/$1/openapi/v2/serverside-api.yaml" -o "$RUNNER_TEMP/spec.yaml" || { echo "::error::could not download the $1 spec"; exit 1; } kind=$(yq '.info."x-stream-breaking-changes" | tag' "$RUNNER_TEMP/spec.yaml") @@ -99,6 +102,56 @@ jobs: fi echo "::error::$new brings approved breaking changes (${added//$'\n'/, }). Add '!' to the PR title to release a major, or label the PR sdk-not-breaking and name each ticket in the PR body if none of them breaks this SDK. $hold" exit 1 + # A hand-written change never moves .spec-version, and a regen can break callers in ways chat's check allows, so the public methods and classes themselves are compared too, between the base tip and the merge commit. + - uses: actions/checkout@v4 + if: ${{ !cancelled() }} + with: + fetch-depth: 2 + persist-credentials: false + - name: Public API stays compatible + if: ${{ !cancelled() }} + env: + TITLE: ${{ github.event.pull_request.title }} + NOT_BREAKING: ${{ contains(github.event.pull_request.labels.*.name, 'sdk-not-breaking') }} + run: | + set -euo pipefail + # Ruby callers pass arguments by position and name classes directly. Each removed public method or class, and each public method whose arguments moved, lost a default or gained a required one, breaks them. A default added to an existing argument is compatible, unless an optional one comes before it, which rebinds positional calls. Model methods are not compared, since callers build models by keyword. + # One line per public instance method, "file TAB name TAB arguments". + defs() { awk -v f="$1" '/^ *(class|module) / { hidden = 0 } /^ *(private|protected) *$/ { hidden = 1 } /^ *public *$/ { hidden = 0 } !hidden && /^ *def [a-z0-9_]+[?!]?(\(.*\))? *$/ { s = $0; sub(/^ *def /, "", s); sub(/ *$/, "", s); m = s; sub(/\(.*/, "", m); a = ""; if (s ~ /\(/) { a = s; sub(/^[^(]*\(/, "", a); sub(/\)$/, "", a) } print f "\t" m "\t" a }'; } + surface() { for f in $(git ls-tree -r --name-only "$1" -- lib | grep '\.rb$' | grep -v '^lib/getstream_ruby/generated/models/'); do git show "$1:$f" | defs "$f"; done | sort -u; } + classes() { { git grep -h -E '^ *class [A-Z]' "$1" -- lib || true; } | sed -nE 's/^ *class ([A-Z][A-Za-z0-9_]*).*/\1/p' | sort -u; } + # The checkout is the merge commit, whose first parent is the base tip, so the difference is this PR's alone. + surface HEAD^1 > "$RUNNER_TEMP/old-defs" + surface HEAD > "$RUNNER_TEMP/new-defs" + { + awk -F'\t' ' + function name(f, m, p, k, i, s) { sub(/.*\//, "", f); sub(/\.rb$/, "", f); k = split(f, p, "_"); s = ""; for (i = 1; i <= k; i++) s = s toupper(substr(p[i], 1, 1)) substr(p[i], 2); return s "#" m } + # Keyword arguments bind by name: each old one stays, an optional one stays optional, and a new one is optional. Positional ones keep their place. + function kw(x) { return x ~ /^[A-Za-z_][A-Za-z0-9_]*:/ || x ~ /^\*\*/ } + function ext(o, n, a, b, k, j, i, x, y, opt, p, q, P, Q, ok, nk) { + k = o == "" ? 0 : split(o, a, ", "); j = n == "" ? 0 : split(n, b, ", ") + for (i = 1; i <= k; i++) if (kw(a[i])) { x = a[i]; sub(/:.*/, "", x); ok[x] = a[i] } else p[++P] = a[i] + for (i = 1; i <= j; i++) if (kw(b[i])) { x = b[i]; sub(/:.*/, "", x); nk[x] = b[i] } else q[++Q] = b[i] + for (x in ok) { if (!(x in nk)) return 0; if (ok[x] ~ /: ./ && nk[x] !~ /: ./) return 0 } + for (x in nk) if (!(x in ok) && nk[x] ~ /:$/) return 0 + if (Q < P) return 0 + for (i = 1; i <= P; i++) { x = p[i]; sub(/ = .*/, "", x); y = q[i]; sub(/ = .*/, "", y); if (x != y || (p[i] ~ / = / && q[i] !~ / = /) || (opt && p[i] !~ / = / && q[i] ~ / = /)) return 0; if (p[i] ~ / = /) opt = 1 } + for (i = P + 1; i <= Q; i++) if (q[i] !~ / = / && q[i] !~ /^[*&]/) return 0 + return 1 + } + FILENAME == ARGV[1] { old[$1 FS $2] = $3; next } + { new[$1 FS $2] = $3 } + END { for (k in old) { split(k, p, FS); if (!(k in new)) print name(p[1], p[2]) " removed"; else if (!ext(old[k], new[k])) print name(p[1], p[2]) " takes (" new[k] ") instead of (" old[k] ")" } }' "$RUNNER_TEMP/old-defs" "$RUNNER_TEMP/new-defs" + comm -23 <(classes HEAD^1) <(classes HEAD) | sed 's/$/ removed/' + } | sort > "$RUNNER_TEMP/incompatible" + if [ ! -s "$RUNNER_TEMP/incompatible" ]; then echo "the public API stays compatible"; exit 0; fi + echo "incompatible public API changes:" + cat "$RUNNER_TEMP/incompatible" + bang='^[a-z]+(\([^)]*\))?!:' + if [[ $TITLE =~ $bang ]]; then echo "the title marks a major"; exit 0; fi + if [ "$NOT_BREAKING" = true ]; then echo "labelled sdk-not-breaking"; exit 0; fi + echo "::error::This PR breaks the public API (listed above). Prefer a compatible change. To release a major, add '!' to the PR title; if no customer can hit these, label the PR sdk-not-breaking. If auto-merge is on, turn it off before adding '!', or the major merges as soon as this check passes." + exit 1 # release-please reads a BEGIN_COMMIT_OVERRIDE block in place of the title, so the block releases a major only through a BREAKING CHANGE footer, whatever the title says. - name: Title and changelog agree on a major env: diff --git a/.github/workflows/regen.yml b/.github/workflows/regen.yml index c9963c0..d4c8e04 100644 --- a/.github/workflows/regen.yml +++ b/.github/workflows/regen.yml @@ -205,8 +205,8 @@ jobs: # Keeps the fields of schemas and operations present in both specs; a new or removed one stands for its own fields. kept() { while read -r f; do if grep -Eqx "(type|op) ${f%%.*}" "$RUNNER_TEMP/old" && grep -Eqx "(type|op) ${f%%.*}" "$RUNNER_TEMP/new"; then echo "$f"; fi; done; } group() { awk -F. -v word="$1" '{ f[$1] = (f[$1] ? f[$1] ", " : "") $2 } END { for (t in f) print f[t] " " word " " t }' | sort; } - # The approved wire breaking changes the new spec brings, as chat allowlisted them, one "ticket TAB line" each. With lines on both sides it diffs the lines, since chat may append one under a header an earlier tag published; against a current spec from before the lines, it takes the lines of each new ticket. A new go ticket has no Ruby lines, so the Breaking changes check leaves it to a human, as it does a new spec without lines. - pairs() { yq -r '.info."x-stream-breaking-change-lines" // {} | to_entries | .[] | .key as $t | (.value.wire // [])[] | $t + "\t" + .' "$RUNNER_TEMP/$1.yaml"; } + # The approved breaking changes the new spec brings, as chat allowlisted them, one "ticket TAB line" each: its wire lines and its getstream-ruby sdk lines. With lines on both sides it diffs the lines, since chat may append one under a header an earlier tag published; against a current spec from before the lines, it takes the lines of each new ticket. A new go ticket has no Ruby lines, so the Breaking changes check leaves it to a human, as it does a new spec without lines. + pairs() { yq -r '.info."x-stream-breaking-change-lines" // {} | to_entries | .[] | .key as $t | (.value.wire // []) + (.value.sdk // [] | map(select(test("^getstream-ruby ")))) | .[] | $t + "\t" + .' "$RUNNER_TEMP/$1.yaml"; } tickets() { yq -r '.info."x-stream-breaking-changes" | (.wire // []) + (.go // []) | .[]' "$RUNNER_TEMP/$1.yaml"; } : > "$RUNNER_TEMP/breaking" if yq -e '.info."x-stream-breaking-change-lines"' "$RUNNER_TEMP/$current.yaml" > /dev/null 2>&1; then @@ -214,39 +214,19 @@ jobs: elif yq -e '.info."x-stream-breaking-changes"' "$RUNNER_TEMP/$current.yaml" > /dev/null 2>&1; then pairs "$TAG" | awk -F'\t' 'FILENAME != "-" { old[$0]; next } !($1 in old)' <(tickets "$current") - > "$RUNNER_TEMP/breaking" fi - # One entry per schema or endpoint: "Schema: change; change". The list stops well below GitHub's 65,536-character PR body. release-please keeps one BREAKING CHANGE footer per entry, and only its last line. + # One entry per schema, endpoint or Ruby class: "Schema: change; change", with an sdk line's repo and file left out. An sdk line goes in backticks, so markdown keeps it as written, and is left out when it removes a method or type the inventory already removes. The list stops well below GitHub's 65,536-character PR body. release-please keeps one BREAKING CHANGE footer per entry, and only its last line. breaking() { cut -f2 "$RUNNER_TEMP/breaking" | sort -u \ | awk ' + FILENAME != "-" { gone[$0]; next } + sub(/^getstream-ruby /, "") { sub(/^[^ ]+: /, ""); t = $0; sub(/^type /, "", t); sub(/[.( ].*/, "", t); m = $0; sub(/[( ].*/, "", m); sub(/.*\./, "", m); if (/ removed$/ && ((/^type / ? "type " t : "op " m) in gone)) next; p = (t in g) ? g[t] "; " : ""; g[t] = p "`" $0 "`"; next } match($0, /^(GET|POST|PUT|PATCH|DELETE) [^ ]+ /) { t = substr($0, 1, RLENGTH - 1); p = (t in g) ? g[t] "; " : t ": "; g[t] = p substr($0, RLENGTH + 1); next } match($0, /'\''[A-Z][A-Za-z0-9_]*\//) { t = substr($0, RSTART + 1, RLENGTH - 2); p = (t in g) ? g[t] "; " : ""; g[t] = p $0; next } { g[$0] = $0 } - END { for (t in g) print t "\t" g[t] }' \ + END { for (t in g) print t "\t" g[t] }' <(comm -23 "$RUNNER_TEMP/old" "$RUNNER_TEMP/new") - \ | sort | awk -F'\t' '{ e = sprintf("feat: change %s\n\nBREAKING CHANGE: %s\n", $1, $2); if (size + length(e) > 40000) { more++; next } size += length(e); printf "%s", e } END { if (more) printf "feat: change %d more APIs\n\nBREAKING CHANGE: %d more API changes are left out to fit the PR body\n", more, more }' } - # Ruby callers pass arguments by position and name model classes directly, and a generator change alone moves neither in the spec. So the generated code before and after is compared too: each removed method or class, and each method whose arguments moved, lost a default or gained a required one, gets a footer. A default added to an existing argument is compatible, unless an optional one comes before it, which rebinds positional calls. Removals the spec diff already lists are left out. - surface() { - defs() { sed -nE 's#^(HEAD:)?lib/getstream_ruby/generated/([a-z_]+)\.rb: *def ([a-z0-9_]+[?!]?)(\((.*)\))?$#\2\t\3\t\5#p' | sort -u; } - classes() { sed -nE 's#^(HEAD:)?[^:]*: *class ([A-Z][A-Za-z0-9_]*).*#\2#p' | sort -u; } - { git grep -E '^ *def ' HEAD -- ':(glob)lib/getstream_ruby/generated/*.rb' || true; } | defs > "$RUNNER_TEMP/old-defs" - { grep -H -E '^ *def ' lib/getstream_ruby/generated/*.rb 2> /dev/null || true; } | defs > "$RUNNER_TEMP/new-defs" - { git grep -E '^ *class ' HEAD -- ':(glob)lib/getstream_ruby/generated/models/*.rb' || true; } | classes > "$RUNNER_TEMP/old-classes" - { grep -H -E '^ *class ' lib/getstream_ruby/generated/models/*.rb 2> /dev/null || true; } | classes > "$RUNNER_TEMP/new-classes" - { removed op; removed type; } > "$RUNNER_TEMP/spec-removed" - { - awk -F'\t' ' - function name(f, m, p, k, i, s) { k = split(f, p, "_"); s = ""; for (i = 1; i <= k; i++) s = s toupper(substr(p[i], 1, 1)) substr(p[i], 2); return s "#" m } - function ext(o, n, a, b, k, j, i, x, y, opt) { k = o == "" ? 0 : split(o, a, ", "); j = n == "" ? 0 : split(n, b, ", "); if (j < k) return 0; for (i = 1; i <= k; i++) { x = a[i]; sub(/ = .*/, "", x); y = b[i]; sub(/ = .*/, "", y); if (x != y || (a[i] ~ / = / && b[i] !~ / = /) || (opt && a[i] !~ / = / && b[i] ~ / = /)) return 0; if (a[i] ~ / = /) opt = 1 } for (i = k + 1; i <= j; i++) if (b[i] !~ / = /) return 0; return 1 } - FILENAME == ARGV[1] { old[$1 FS $2] = $3; next } - { new[$1 FS $2] = $3 } - END { for (k in old) { split(k, p, FS); if (!(k in new)) print "gone" FS p[2] FS name(p[1], p[2]); else if (!ext(old[k], new[k])) print "moved" FS p[2] FS name(p[1], p[2]) " takes (" new[k] ") instead of (" old[k] ")" } }' "$RUNNER_TEMP/old-defs" "$RUNNER_TEMP/new-defs" - comm -23 "$RUNNER_TEMP/old-classes" "$RUNNER_TEMP/new-classes" | awk '{ print "gone" "\t" $0 "\t" $0 }' - } | sort | while IFS=$'\t' read -r kind key text; do - if [ "$kind" = gone ]; then grep -qxF "$key" "$RUNNER_TEMP/spec-removed" || printf 'feat: remove %s\n\nBREAKING CHANGE: removes %s\n' "$text" "$text" - else printf 'feat: change %s\n\nBREAKING CHANGE: %s\n' "${text%% takes *}" "$text"; fi - done - } # generate.sh keeps the class of a schema that left the spec, so a removed type the gem still defines gets no footer. { added op | sed 's/^/feat: add /' @@ -254,7 +234,6 @@ jobs: added field | kept | group to | sed 's/^/feat: add /' { removed op; removed type | { grep -vxFf <(sed -nE 's/^ *class ([A-Z][A-Za-z0-9_]*).*/\1/p' lib/getstream_ruby/generated/models/*.rb) || true; }; removed field | kept | group from; } | while read -r r; do printf 'feat: remove %s\n\nBREAKING CHANGE: removes %s\n' "$r" "$r"; done breaking - surface } | awk 'NR > 1 && /^feat: / { print "" } { print }' > "$RUNNER_TEMP/changelog" type=chore diff --git a/README.md b/README.md index 260e3fe..d2fe255 100644 --- a/README.md +++ b/README.md @@ -415,7 +415,7 @@ The client regenerates itself on every chat production release: 2. protocol notifies this repo, and the **Regenerate** workflow opens one PR from `regen/auto`, titled `feat: regenerate client from openapi-vX`. Its `BEGIN_COMMIT_OVERRIDE` block lists the changelog lines. 3. Once the required checks pass and the **Regen integration** run passes on the PR's head commit, the PR merges itself, and release-please adds those lines to the Release PR. Merging the Release PR ships, as described below. -A regen PR waits for a human when its changelog carries a `BREAKING CHANGE:` footer, when the `Breaking changes check` finds an approved breaking change in the new spec, or when a required check fails. For each approved breaking change with wire lines, the regen writes `BREAKING CHANGE:` footers from the lines chat allowlisted and titles the PR `feat!:`, so the check passes and the PR only waits for a human to merge it. A ticket chat approved for getstream-go only has no Ruby lines, so the check fails until a human names it in the PR body, also on a `feat!:` PR. Because Ruby callers pass arguments by position and name model classes directly, the regen also compares the generated code before and after, and writes a footer for each removed method or class and each method whose arguments moved, even when the spec did not change them. Only `getstream-sdk-bot` can push to `regen/**`. +A regen PR waits for a human when its changelog carries a `BREAKING CHANGE:` footer, when the `Breaking changes check` finds an approved breaking change in the new spec, or when a required check fails. For each approved breaking change with wire lines or `getstream-ruby` sdk lines, the regen writes `BREAKING CHANGE:` footers from the lines chat allowlisted and titles the PR `feat!:`, so the check passes and the PR only waits for a human to merge it. A ticket chat approved for getstream-go only has no Ruby lines, so the check fails until a human names it in the PR body, also on a `feat!:` PR. Because Ruby callers pass arguments by position and name classes directly, the `Breaking changes check` also compares the public methods and classes of every PR, hand-written ones included, against its base, and fails on each removed method or class and each method whose arguments moved, unless the title has `!` or the PR is labelled `sdk-not-breaking`. Only `getstream-sdk-bot` can push to `regen/**`. ### Manual cases