Repository navigation
195 lines (189 loc) · 14.4 KB
/
Copy pathbreaking_changes.yml
File metadata and controls
195 lines (189 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
name: OpenAPI
on:
pull_request:
types: [opened, edited, reopened, synchronize, labeled, unlabeled]
permissions:
contents: read
jobs:
# A regen PR must be a major, with a `!` in its title that squash-merges into the commit release-please reads, when its .spec-version moves to a protocol tag carrying approved breaking changes the base tag did not, moves back to an older tag, or names a generator other than the spec's own chat version. The sdk-not-breaking label passes added tickets once a human has judged them irrelevant to this SDK and named each one in the PR body.
breaking-changes:
name: Breaking changes check
runs-on: { group: github-public-amd64 }
timeout-minutes: 5
steps:
- env:
GH_TOKEN: ${{ github.token }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
TITLE: ${{ github.event.pull_request.title }}
BODY: ${{ github.event.pull_request.body }}
NOT_BREAKING: ${{ contains(github.event.pull_request.labels.*.name, 'sdk-not-breaking') }}
# 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.
read_version() {
if ! gh api "repos/$GITHUB_REPOSITORY/contents/.spec-version?ref=$1" -H 'Accept: application/vnd.github.raw' > "$RUNNER_TEMP/$2" 2> "$RUNNER_TEMP/err"; then
grep -q 'HTTP 404' "$RUNNER_TEMP/err" || { echo "::error::could not read .spec-version at $1: $(cat "$RUNNER_TEMP/err")"; exit 1; }
: > "$RUNNER_TEMP/$2"
fi
}
# 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
old=$(sed -n 's/^spec: //p' "$RUNNER_TEMP/base-version")
new=$(sed -n 's/^spec: //p' "$RUNNER_TEMP/head-version")
generator=$(sed -n 's/^generator: //p' "$RUNNER_TEMP/head-version")
tag_re='^openapi-v[0-9.]+$'
if ! [[ $new =~ $tag_re ]]; then echo "::warning::.spec-version names no protocol tag ($new), so it cannot be compared"; exit 0; fi
[[ $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.
# 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")
case $kind in
'!!null') ;;
'!!map') yq -r "$expr" "$RUNNER_TEMP/spec.yaml" | sort > "$RUNNER_TEMP/$2" ;;
*) echo "::error::the $1 breaking-change list is a $kind, not a map"; exit 1 ;;
esac
}
reasons=()
[ "$generator" = "chat ${new#openapi-}" ] || reasons+=("the generator (${generator:-not recorded}) is not chat ${new#openapi-}, so template changes it brings are missing from the $new list")
added=
if [ -z "$old" ]; then
echo "::warning::the base names no protocol tag, so the approved breaking changes $new brings cannot be listed"
elif [ "$(printf '%s\n' "$old" "$new" | sort -V | tail -1)" != "$new" ]; then
reasons+=("it moves the spec back from $old to $new, which removes whatever $old added")
elif [ "$old" != "$new" ]; then
list "$old" old
if [ "$kind" = '!!null' ]; then
echo "::warning::$old carries no breaking-change list, so the approved breaking changes $new brings cannot be listed"
else
list "$new" new
if [ "$kind" = '!!null' ]; then echo "::error::$new carries no breaking-change list although $old does, so the spec generation regressed"; exit 1; fi
added=$(comm -13 "$RUNNER_TEMP/old" "$RUNNER_TEMP/new")
fi
fi
if [ -n "$added" ]; then echo "$old to $new brings these approved breaking changes:"; echo "$added"; fi
for reason in "${reasons[@]}"; do echo "major needed: $reason"; done
if [ -z "$added" ] && [ ${#reasons[@]} -eq 0 ]; then echo "nothing here needs a major"; exit 0; fi
bang='^[a-z]+(\([^)]*\))?!:'
hold="If auto-merge is on, turn it off before adding '!', or the major merges as soon as this check passes."
if [[ $TITLE =~ $bang ]]; then
# A `!` speaks for no added ticket outside LINED until a human names it here.
unlined=
[ -z "$added" ] || unlined=$(comm -23 <(printf '%s\n' "$added") <(yq -r "$LINED | .[]" "$RUNNER_TEMP/spec.yaml" | sort))
missing=$(while read -r ticket; do [ -z "$ticket" ] || [[ $BODY == *"$ticket"* ]] || echo "$ticket"; done <<< "$unlined")
if [ -z "$missing" ]; then echo "the title marks a major"; exit 0; fi
echo "::error::The title marks a major, but no footer covers ${missing//$'\n'/, }, which has no Ruby lines. Name each in the PR body once you have judged it, with a BREAKING CHANGE footer if it breaks this SDK."
exit 1
fi
if [ ${#reasons[@]} -gt 0 ]; then
for reason in "${reasons[@]}"; do echo "::error::Add '!' to the PR title: $reason. $hold"; done
exit 1
fi
if [ "$NOT_BREAKING" = true ]; then
missing=$(while read -r ticket; do [[ $BODY == *"$ticket"* ]] || echo "$ticket"; done <<< "$added")
if [ -z "$missing" ]; then echo "labelled sdk-not-breaking, and the PR body names every ticket"; exit 0; fi
echo "::error::The sdk-not-breaking label covers only tickets named in the PR body. Name ${missing//$'\n'/, } there once you have judged them, or add '!' to the PR title. $hold"
exit 1
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 method, "file TAB Class#method TAB arguments" or "Class.method" for a singleton one, with signatures that span lines joined.
# Classes are kept on a stack by indentation, so a method after a nested class or class << self keeps its own class. private hides instance methods only.
defs() {
awk -v f="$1" '
BEGIN { cs[0] = "#" }
j != "" { x = $0; sub(/^ */, " ", x); j = j x; if (j !~ /\) *$/) next; $0 = j; j = "" }
(!hidden || /^ *def self\./) && /^ *def (self\.)?[a-z0-9_]+[?!]?\([^)]*, *$/ { j = $0; sub(/ *$/, "", j); next }
/^ *(class [A-Z]|module [A-Z]|class << self)/ && !/; *end *$/ { hv[d] = hidden; hidden = 0; d++; ci[d] = match($0, /[^ ]/); if ($2 == "<<") { cn[d] = cn[d - 1]; cs[d] = "." } else { cn[d] = $2; sub(/;.*/, "", cn[d]); cs[d] = "#" } }
/^ *end([ #]|$)/ && d && match($0, /[^ ]/) == ci[d] { d--; hidden = hv[d] }
/^ *(private|protected) *$/ { hidden = 1 }
/^ *public *$/ { hidden = 0 }
(!hidden || /^ *def self\./) && /^ *def (self\.)?[a-z0-9_]+[?!]?(\(.*\))? *$/ { s = $0; sub(/^ *def /, "", s); sub(/ *$/, "", s); p = cs[d]; if (sub(/^self\./, "", s)) p = "."; m = s; sub(/\(.*/, "", m); a = ""; if (s ~ /\(/) { a = s; sub(/^[^(]*\(/, "", a); sub(/\)$/, "", a) } print f "\t" cn[d] p 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' '
# 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 ~ /^\*\*/ }
# Splits on the commas between arguments, not those inside a default such as [1, 2] or {a: 1, b: 2}.
function args(s, out, n, i, c, d, cur) {
n = d = 0; cur = ""
for (i = 1; i <= length(s); i++) { c = substr(s, i, 1); if (index("([{", c)) d++; else if (index(")]}", c)) d--; if (c == "," && !d) { out[++n] = cur; cur = ""; if (substr(s, i + 1, 1) == " ") i++ } else cur = cur c }
if (s != "") out[++n] = cur
return n
}
function ext(o, n, a, b, k, j, i, x, y, opt, p, q, P, Q, ok, nk) {
k = args(o, a); j = args(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 p[2] " removed"; else if (!ext(old[k], new[k])) print 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, plus a BREAKING CHANGE footer if the body has a BEGIN_COMMIT_OVERRIDE block; 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
if: ${{ !cancelled() }}
env:
TITLE: ${{ github.event.pull_request.title }}
BODY: ${{ github.event.pull_request.body }}
run: |
set -euo pipefail
if [[ $BODY != *BEGIN_COMMIT_OVERRIDE* ]]; then exit 0; fi
block=${BODY#*BEGIN_COMMIT_OVERRIDE}
block=${block%%END_COMMIT_OVERRIDE*}
footer=false
if grep -Eq '^BREAKING[ -]CHANGE: ' <<< "$block"; then footer=true; fi
bang='^[a-z]+(\([^)]*\))?!:'
if [[ $TITLE =~ $bang ]] && [ $footer = false ]; then
echo "::error::The title marks a major, but the BEGIN_COMMIT_OVERRIDE block release-please reads has no BREAKING CHANGE footer. Before END_COMMIT_OVERRIDE, add a blank line and 'BREAKING CHANGE: <what breaks for customers>'."
exit 1
fi
if ! [[ $TITLE =~ $bang ]] && [ $footer = true ]; then
echo "::error::The BEGIN_COMMIT_OVERRIDE block has a BREAKING CHANGE footer, so release-please would cut a major. Add '!' to the PR title, or delete the BREAKING CHANGE lines if nothing there breaks this SDK."
exit 1
fi
echo "the title and the changelog agree"