Repository navigation
314 lines (292 loc) · 12.2 KB
/
Copy pathsync-openapi.yml
File metadata and controls
314 lines (292 loc) · 12.2 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
name: Sync OpenAPI client
on:
repository_dispatch:
types: [attune-openapi-updated]
workflow_dispatch:
inputs:
source_ref:
description: Published Attune release tag
required: true
type: string
source_sha:
description: Attune release commit
required: true
type: string
spec_sha256:
description: Expected SHA-256 digest
required: true
type: string
permissions:
contents: read
concurrency:
group: sync-openapi-client
cancel-in-progress: false
jobs:
sync:
runs-on: ubuntu-24.04
permissions:
contents: write
pull-requests: write
outputs:
base-sha: ${{ steps.base.outputs.sha }}
head-sha: ${{ steps.pull-request.outputs.pull-request-head-sha }}
pr-number: ${{ steps.pull-request.outputs.pull-request-number }}
prerelease: ${{ steps.source.outputs.prerelease }}
resume-ref: ${{ steps.state.outputs.ref }}
resume: ${{ steps.state.outputs.resume }}
source-ref: ${{ env.SOURCE_REF }}
spec-sha256: ${{ env.SPEC_SHA256 }}
version: ${{ steps.version.outputs.value || steps.state.outputs.version }}
env:
SOURCE_REF: ${{ github.event.client_payload.source_ref || inputs.source_ref }}
SOURCE_SHA: ${{ github.event.client_payload.source_sha || inputs.source_sha }}
SPEC_SHA256: ${{ github.event.client_payload.spec_sha256 || inputs.spec_sha256 }}
steps:
- name: Validate source identity
id: source
shell: bash
run: |
set -euo pipefail
[[ "$SOURCE_REF" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([+-][0-9A-Za-z.-]+)?$ ]]
[[ "$SOURCE_SHA" =~ ^[0-9a-f]{40}$ ]]
[[ "$SPEC_SHA256" =~ ^[0-9a-f]{64}$ ]]
if [[ "$SOURCE_REF" == *-* ]]; then
echo "prerelease=true" >> "$GITHUB_OUTPUT"
else
echo "prerelease=false" >> "$GITHUB_OUTPUT"
fi
- name: Checkout
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
fetch-depth: 0
ref: ${{ github.event.repository.default_branch }}
- name: Record base revision
id: base
run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
- name: Check synchronization state
id: state
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
shell: bash
run: |
set -euo pipefail
applied=false
if [ -f .attune-openapi-source.json ]; then
previous_sha="$(jq -r .source_sha .attune-openapi-source.json)"
if [ "$previous_sha" != "$SOURCE_SHA" ]; then
comparison="$(gh api "repos/attune-system/attune/compare/${previous_sha}...${SOURCE_SHA}" --jq .status)"
if [ "$comparison" != ahead ] && [ "$comparison" != identical ]; then
echo "Refusing OpenAPI rollback from $previous_sha to $SOURCE_SHA" >&2
exit 1
fi
fi
if [ "$(jq -r .source_ref .attune-openapi-source.json)" = "$SOURCE_REF" ] && \
[ "$(jq -r .source_sha .attune-openapi-source.json)" = "$SOURCE_SHA" ] && \
[ "$(jq -r .spec_sha256 .attune-openapi-source.json)" = "$SPEC_SHA256" ]; then
applied=true
fi
fi
resume=false
if [ "$applied" = true ]; then
version="$(jq -r .version .attune-openapi-source.json)"
ref="$(git log -n 1 --format=%H -- .attune-openapi-source.json)"
test -n "$ref"
echo "ref=$ref" >> "$GITHUB_OUTPUT"
echo "version=$version" >> "$GITHUB_OUTPUT"
if ! gh release view "v$version" >/dev/null 2>&1; then
resume=true
fi
fi
echo "applied=$applied" >> "$GITHUB_OUTPUT"
echo "resume=$resume" >> "$GITHUB_OUTPUT"
- name: Setup Python
if: steps.state.outputs.applied != 'true'
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.12"
cache: pip
- name: Download OpenAPI specification
if: steps.state.outputs.applied != 'true'
shell: bash
run: |
set -euo pipefail
tag_sha="$(git ls-remote https://github.com/attune-system/attune.git "refs/tags/$SOURCE_REF^{}" | cut -f1)"
if [ -z "$tag_sha" ]; then
tag_sha="$(git ls-remote https://github.com/attune-system/attune.git "refs/tags/$SOURCE_REF" | cut -f1)"
fi
test "$tag_sha" = "$SOURCE_SHA"
curl --fail --silent --show-error --location \
"https://github.com/attune-system/attune/releases/download/${SOURCE_REF}/attune-openapi.json" \
--output "$RUNNER_TEMP/openapi.json"
printf '%s %s\n' "$SPEC_SHA256" "$RUNNER_TEMP/openapi.json" | sha256sum --check
- name: Regenerate and version client
if: steps.state.outputs.applied != 'true'
id: version
env:
ATTUNE_OPENAPI_PATH: ${{ runner.temp }}/openapi.json
run: |
set -euo pipefail
current="$(python -c 'from pathlib import Path; import re; print(re.search(r"__version__ = \"([^\"]+)\"", Path("src/attune/_version.py").read_text()).group(1))')"
current_core="$(python -c 'import re, sys; print(re.match(r"\d+\.\d+\.\d+", sys.argv[1]).group(0))' "$current")"
IFS=. read -r major minor patch <<< "$current_core"
next="$major.$minor.$((patch + 1))"
if [[ "$SOURCE_REF" == *-* ]]; then
next="$next.dev$((16#${SOURCE_SHA:0:12}))"
fi
CURRENT_VERSION="$current" NEXT_VERSION="$next" python - <<'PY'
import os
import re
from pathlib import Path
current = os.environ["CURRENT_VERSION"]
next_version = os.environ["NEXT_VERSION"]
version_file = Path("src/attune/_version.py")
version_file.write_text(version_file.read_text().replace(current, next_version))
readme = Path("README.md")
text = readme.read_text()
text = re.sub(
r"SDK \S+ is generated from the Attune OpenAPI \S+ contract\.",
f"SDK {next_version} is generated from the Attune OpenAPI {os.environ['SOURCE_REF'].removeprefix('v')} contract.",
text,
count=1,
)
readme.write_text(text)
PY
python -m pip install -e ".[dev]"
./scripts/generate-client.sh "$ATTUNE_OPENAPI_PATH"
pytest
jq -n \
--arg source_ref "$SOURCE_REF" \
--arg source_sha "$SOURCE_SHA" \
--arg spec_sha256 "$SPEC_SHA256" \
--arg version "$next" \
'{source_ref: $source_ref, source_sha: $source_sha, spec_sha256: $spec_sha256, version: $version}' \
> .attune-openapi-source.json
echo "value=$next" >> "$GITHUB_OUTPUT"
- name: Create SDK update pull request
if: steps.state.outputs.applied != 'true'
id: pull-request
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8
with:
branch: automation/openapi-sync
delete-branch: true
commit-message: Release SDK ${{ steps.version.outputs.value }} from Attune ${{ env.SOURCE_REF }}
title: Release SDK ${{ steps.version.outputs.value }} from Attune ${{ env.SOURCE_REF }}
body: |
Generated from attune-system/attune release ${{ env.SOURCE_REF }} at ${{ env.SOURCE_SHA }}.
OpenAPI SHA-256: `${{ env.SPEC_SHA256 }}`
verify:
needs: sync
if: needs.sync.outputs.pr-number != ''
runs-on: ubuntu-24.04
permissions:
contents: read
steps:
- name: Checkout tested pull request revision
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
ref: ${{ needs.sync.outputs.head-sha }}
- name: Setup Python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.12"
cache: pip
- name: Reproduce and verify generated client
env:
SOURCE_REF: ${{ needs.sync.outputs.source-ref }}
SPEC_SHA256: ${{ needs.sync.outputs.spec-sha256 }}
shell: bash
run: |
set -euo pipefail
curl --fail --silent --show-error --location \
"https://github.com/attune-system/attune/releases/download/${SOURCE_REF}/attune-openapi.json" \
--output "$RUNNER_TEMP/openapi.json"
printf '%s %s\n' "$SPEC_SHA256" "$RUNNER_TEMP/openapi.json" | sha256sum --check
python -m pip install -e ".[dev]"
./scripts/generate-client.sh "$RUNNER_TEMP/openapi.json"
git diff --exit-code
pytest
merge:
needs: [sync, verify]
if: needs.verify.result == 'success'
runs-on: ubuntu-24.04
permissions:
contents: write
pull-requests: write
outputs:
sha: ${{ steps.merge.outputs.sha }}
steps:
- name: Merge the exact tested revision
id: merge
env:
BASE_SHA: ${{ needs.sync.outputs.base-sha }}
EXPECTED_HEAD_SHA: ${{ needs.sync.outputs.head-sha }}
GH_REPO: ${{ github.repository }}
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ needs.sync.outputs.pr-number }}
shell: bash
run: |
set -euo pipefail
default_branch="$(gh api "repos/$GITHUB_REPOSITORY" --jq .default_branch)"
current_base="$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$default_branch" --jq .object.sha)"
test "$current_base" = "$BASE_SHA"
test "$(gh pr view "$PR_NUMBER" --json headRefOid --jq .headRefOid)" = "$EXPECTED_HEAD_SHA"
gh pr merge "$PR_NUMBER" --squash --delete-branch --match-head-commit "$EXPECTED_HEAD_SHA"
merge_sha="$(gh pr view "$PR_NUMBER" --json mergeCommit --jq .mergeCommit.oid)"
test -n "$merge_sha"
echo "sha=$merge_sha" >> "$GITHUB_OUTPUT"
verify-release:
needs: [sync, merge]
if: always() && needs.sync.result == 'success' && (needs.merge.outputs.sha != '' || needs.sync.outputs.resume == 'true')
runs-on: ubuntu-24.04
permissions:
contents: read
steps:
- name: Checkout release revision
uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6
with:
ref: ${{ needs.merge.outputs.sha || needs.sync.outputs.resume-ref }}
- name: Setup Python
uses: actions/setup-python@ece7cb06caefa5fff74198d8649806c4678c61a1 # v6
with:
python-version: "3.12"
cache: pip
- name: Verify release revision
env:
SOURCE_REF: ${{ needs.sync.outputs.source-ref }}
SPEC_SHA256: ${{ needs.sync.outputs.spec-sha256 }}
shell: bash
run: |
set -euo pipefail
curl --fail --silent --show-error --location \
"https://github.com/attune-system/attune/releases/download/${SOURCE_REF}/attune-openapi.json" \
--output "$RUNNER_TEMP/openapi.json"
printf '%s %s\n' "$SPEC_SHA256" "$RUNNER_TEMP/openapi.json" | sha256sum --check
python -m pip install -e ".[dev]"
./scripts/generate-client.sh "$RUNNER_TEMP/openapi.json"
git diff --exit-code
pytest
dispatch-publish:
needs: [sync, merge, verify-release]
if: always() && needs.verify-release.result == 'success'
permissions:
actions: write
runs-on: ubuntu-24.04
steps:
- name: Dispatch trusted publishing workflow
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
GH_TOKEN: ${{ github.token }}
RELEASE_REF: ${{ needs.merge.outputs.sha || needs.sync.outputs.resume-ref }}
VERSION: ${{ needs.sync.outputs.version }}
shell: bash
run: |
set -euo pipefail
jq -n \
--arg ref "$DEFAULT_BRANCH" \
--arg release_ref "$RELEASE_REF" \
--arg version "$VERSION" \
'{ref: $ref, inputs: {release_ref: $release_ref, version: $version}}' \
| gh api --method POST \
"repos/$GITHUB_REPOSITORY/actions/workflows/publish.yml/dispatches" \
--input -