Skip to content

Commit d7b5b85

Browse files
committed
Add GitHub Actions workflow for automatic OpenAPI spec updates
1 parent 43025b8 commit d7b5b85

File tree

3 files changed

+143
-4034
lines changed

3 files changed

+143
-4034
lines changed

.github/workflows/auto-update.yaml

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
name: Auto Update from OpenAPI Spec
2+
3+
on:
4+
schedule:
5+
- cron: "0 4 * * *" # 04:00 UTC daily
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
concurrency:
13+
group: auto-update-openapi
14+
cancel-in-progress: false
15+
16+
jobs:
17+
check-generate-release:
18+
runs-on: ubuntu-latest
19+
env:
20+
SPEC_URL: https://test.ngu.no/api/nadag/innmelding/openapi/v1
21+
SPEC_PATH: openapi_specification/nadag-innmelding.yaml
22+
PACKAGE_PYPROJECT: nadag-innmelding-python-client/pyproject.toml
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Set up Python
30+
uses: actions/setup-python@v5
31+
with:
32+
python-version: '3.11'
33+
34+
- name: Install generator
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install openapi-python-client jq
38+
39+
- name: Download latest spec
40+
id: download
41+
run: |
42+
set -e
43+
echo "Downloading spec from $SPEC_URL" >&2
44+
curl -sSL "$SPEC_URL" -o new_spec.yaml
45+
if [ ! -s new_spec.yaml ]; then
46+
echo "Spec download failed or empty" >&2
47+
exit 1
48+
fi
49+
# Extract version (first occurrence of a line that starts (optionally indented) with version: )
50+
NEW_VERSION=$(grep -E '^[[:space:]]*version:' new_spec.yaml | head -n1 | sed -E 's/^[[:space:]]*version:[[:space:]]*"?([^" ]+)"?.*/\1/')
51+
if [ -z "$NEW_VERSION" ]; then
52+
echo "Could not extract version from downloaded spec" >&2
53+
exit 1
54+
fi
55+
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
56+
# Get current spec version (if file exists). If not present, force update.
57+
if [ -f "$SPEC_PATH" ]; then
58+
CURRENT_VERSION=$(grep -E '^[[:space:]]*version:' "$SPEC_PATH" | head -n1 | sed -E 's/^[[:space:]]*version:[[:space:]]*"?([^" ]+)"?.*/\1/')
59+
else
60+
CURRENT_VERSION="(none)"
61+
fi
62+
echo "Current version: $CURRENT_VERSION" >&2
63+
echo "Downloaded version: $NEW_VERSION" >&2
64+
if [ "$NEW_VERSION" != "$CURRENT_VERSION" ]; then
65+
echo "changed=true" >> $GITHUB_OUTPUT
66+
else
67+
echo "changed=false" >> $GITHUB_OUTPUT
68+
fi
69+
70+
- name: Regenerate client & bump version
71+
id: regenerate
72+
if: steps.download.outputs.changed == 'true'
73+
run: |
74+
set -e
75+
echo "Spec version changed. Regenerating client..." >&2
76+
mv new_spec.yaml "$SPEC_PATH"
77+
# Run generator (using overwrite & custom templates/config)
78+
openapi-python-client generate \
79+
--path "$SPEC_PATH" \
80+
--overwrite \
81+
--custom-template-path templates \
82+
--config config.yaml
83+
NEW_VERSION='${{ steps.download.outputs.new_version }}'
84+
# Bump package version in pyproject to the spec version
85+
if [ -f "$PACKAGE_PYPROJECT" ]; then
86+
# Replace only the first occurrence of version = "..."
87+
sed -i "0,/^version = \".*\"/s//version = \"${NEW_VERSION}\"/" "$PACKAGE_PYPROJECT"
88+
fi
89+
echo "updated_version=$NEW_VERSION" >> $GITHUB_OUTPUT
90+
91+
- name: Create pull request
92+
if: steps.download.outputs.changed == 'true'
93+
uses: peter-evans/create-pull-request@v6
94+
with:
95+
commit-message: chore: update OpenAPI spec to version ${{ steps.regenerate.outputs.updated_version }}
96+
branch: chore/openapi-spec-v${{ steps.regenerate.outputs.updated_version }}
97+
title: chore: update OpenAPI spec to version ${{ steps.regenerate.outputs.updated_version }}
98+
body: |
99+
Automated update for OpenAPI spec version `${{ steps.regenerate.outputs.updated_version }}`.
100+
101+
Changes:
102+
- Updated spec file from upstream
103+
- Regenerated client code
104+
- Bumped package version in pyproject.toml
105+
106+
After merging this PR, create a GitHub Release (tag `v${{ steps.regenerate.outputs.updated_version }}`) to trigger the publish workflow.
107+
labels: automated, openapi, chore
108+
signoff: false
109+
draft: false
110+
delete-branch: false
111+
112+
- name: No change summary
113+
if: steps.download.outputs.changed != 'true'
114+
run: echo "No spec version change detected; nothing to do."

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,39 @@ pip install nadag-innmelding-python-client
2121

2222
```
2323

24+
## Automation: keeping the client in sync with upstream OpenAPI
25+
26+
A scheduled / on-demand GitHub Actions workflow (`auto-update.yaml`) keeps the client current:
27+
28+
1. Downloads the upstream OpenAPI specification from:
29+
`https://test.ngu.no/api/nadag/innmelding/openapi/v1` (stored at `openapi_specification/nadag-innmelding.yaml`).
30+
2. Extracts the `info.version` value.
31+
3. If the version differs from the committed spec, it regenerates the client using `openapi-python-client` with `templates/` + `config.yaml`.
32+
4. Updates the package version in `nadag-innmelding-python-client/pyproject.toml` to match the spec version.
33+
5. Creates a pull request (branch name: `chore/openapi-spec-v<spec-version>`) instead of committing directly.
34+
35+
After the pull request is merged:
36+
- Manually create a git tag `v<spec-version>` (or create a GitHub Release with that tag).
37+
- The existing `release.yaml` workflow (triggered on release creation) publishes the new version to PyPI using the `PYPI_TOKEN` secret.
38+
39+
Rationale for PR-based flow:
40+
- Enables code review of generated changes.
41+
- Lets you amend / adjust the version or templates before publishing.
42+
43+
### Manual update (optional)
44+
To replicate the regeneration locally:
45+
46+
```bash
47+
pip install openapi-python-client
48+
python scripts/update_from_spec.py
49+
```
50+
51+
This will download the latest spec, regenerate the client, and bump the package version. You can then open a PR or tag manually.
52+
2453
## Developer installation
2554

2655
Clone the repository.
2756

2857
## Contributing
2958

3059
Please open an issue before submitting a pull request. We welcome contributions and suggestions!
31-
32-

0 commit comments

Comments
 (0)