|
| 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." |
0 commit comments