@@ -2,23 +2,33 @@ name: sync-openapi
22
33on :
44 workflow_dispatch :
5+ repository_dispatch :
6+ types :
7+ - openapi-updated
58 schedule :
6- - cron : ' 0 2 * * *'
9+ - cron : " */15 * * * *"
10+
11+ permissions :
12+ contents : write
13+
14+ concurrency :
15+ group : sync-openapi
16+ cancel-in-progress : false
717
818jobs :
919 sync :
1020 if : github.repository == 'justoneapi/justoneapi-python'
1121 runs-on : ubuntu-latest
12- permissions :
13- contents : write
14- pull-requests : write
1522
1623 steps :
1724 - uses : actions/checkout@v4
25+ with :
26+ ref : main
27+ fetch-depth : 0
1828
1929 - uses : actions/setup-python@v5
2030 with :
21- python-version : ' 3.11'
31+ python-version : " 3.11"
2232
2333 - name : Install dependencies
2434 run : python -m pip install -e '.[dev]'
@@ -44,11 +54,72 @@ jobs:
4454 - name : Build sync summary
4555 run : python scripts/diff_openapi.py "$RUNNER_TEMP/public-api.previous.json" openapi/public-api.json --output "$RUNNER_TEMP/openapi-sync-summary.md"
4656
47- - name : Create pull request
48- uses : peter-evans/create-pull-request@v7
49- with :
50- branch : codex/openapi-sync
51- delete-branch : true
52- title : ' chore: sync OpenAPI spec and generated SDK'
53- commit-message : ' chore: sync OpenAPI spec and generated SDK'
54- body-path : ${{ runner.temp }}/openapi-sync-summary.md
57+ - name : Detect SDK changes
58+ id : changes
59+ run : |
60+ if git diff --quiet -- openapi/public-api.json openapi/public-api.normalized.json justoneapi/generated; then
61+ echo "changed=false" >> "$GITHUB_OUTPUT"
62+ echo "OpenAPI spec and generated SDK are already up to date."
63+ else
64+ echo "changed=true" >> "$GITHUB_OUTPUT"
65+ fi
66+
67+ - name : Bump package version
68+ if : steps.changes.outputs.changed == 'true'
69+ id : version
70+ run : |
71+ version="$(python scripts/bump_version.py)"
72+ echo "version=${version}" >> "$GITHUB_OUTPUT"
73+ echo "tag=v${version}" >> "$GITHUB_OUTPUT"
74+ echo "Bumped package version to ${version}"
75+
76+ - name : Commit generated SDK update
77+ if : steps.changes.outputs.changed == 'true'
78+ env :
79+ VERSION : ${{ steps.version.outputs.version }}
80+ run : |
81+ git config user.name "github-actions[bot]"
82+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
83+ git add openapi/public-api.json openapi/public-api.normalized.json justoneapi/generated justoneapi/_version.py
84+ {
85+ echo "chore: sync OpenAPI spec and generated SDK v${VERSION}"
86+ echo
87+ cat "$RUNNER_TEMP/openapi-sync-summary.md"
88+ } > "$RUNNER_TEMP/commit-message.txt"
89+ git commit -F "$RUNNER_TEMP/commit-message.txt"
90+
91+ - name : Push main and release tag
92+ if : steps.changes.outputs.changed == 'true'
93+ env :
94+ TAG : ${{ steps.version.outputs.tag }}
95+ run : |
96+ if git ls-remote --exit-code --tags origin "refs/tags/${TAG}" >/dev/null 2>&1; then
97+ echo "Tag ${TAG} already exists on origin." >&2
98+ exit 1
99+ fi
100+ for attempt in 1 2 3; do
101+ git fetch --tags origin main
102+ git rebase origin/main
103+ git tag -f -a "${TAG}" -m "Release ${TAG}"
104+ if git push --atomic origin HEAD:main "refs/tags/${TAG}"; then
105+ exit 0
106+ fi
107+ git tag -d "${TAG}" || true
108+ sleep 10
109+ done
110+ echo "Failed to push main and ${TAG} after 3 attempts." >&2
111+ exit 1
112+
113+ - name : Trigger release workflow
114+ if : steps.changes.outputs.changed == 'true'
115+ env :
116+ GITHUB_TOKEN : ${{ github.token }}
117+ TAG : ${{ steps.version.outputs.tag }}
118+ run : |
119+ curl --fail-with-body --silent --show-error \
120+ --request POST \
121+ --header "Accept: application/vnd.github+json" \
122+ --header "Authorization: Bearer ${GITHUB_TOKEN}" \
123+ --header "X-GitHub-Api-Version: 2022-11-28" \
124+ "https://api.github.com/repos/${GITHUB_REPOSITORY}/dispatches" \
125+ --data "{\"event_type\":\"openapi-sdk-release\",\"client_payload\":{\"tag\":\"${TAG}\"}}"
0 commit comments