Skip to content

Commit bf0f5b9

Browse files
author
Verónica López
authored
Merge pull request #21 from authzed/feat/import-spec
feat:add OpenApi spec automatic update workflow
2 parents fa436fe + bfcd9d2 commit bf0f5b9

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: Update OpenAPI Spec
2+
3+
on:
4+
# Run once daily at 00:00 UTC
5+
schedule:
6+
- cron: '0 0 * * *'
7+
8+
# Run when PRs are opened or synchronized
9+
pull_request:
10+
types: [opened, synchronize]
11+
paths:
12+
- 'openapi-spec.yaml'
13+
- '.github/workflows/update-openapi-spec.yml'
14+
15+
# Allow manual triggering
16+
workflow_dispatch:
17+
18+
# Add a repository_dispatch event for external triggers if needed
19+
repository_dispatch:
20+
types: [openapi_spec_updated]
21+
22+
jobs:
23+
update-openapi-spec:
24+
name: Update OpenAPI Spec
25+
# Don't run this job on PR events if the PR is from the update-openapi-spec branch (to avoid loops)
26+
if: github.event_name != 'pull_request' || github.head_ref != 'update-openapi-spec'
27+
runs-on: ubuntu-latest
28+
permissions:
29+
contents: write
30+
pull-requests: write
31+
32+
steps:
33+
- name: Checkout code
34+
uses: actions/checkout@v3
35+
with:
36+
# For PRs, check out the head of the PR
37+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || '' }}
38+
39+
- name: Set up Go
40+
uses: actions/setup-go@v4
41+
with:
42+
go-version: '1.24.3'
43+
check-latest: true
44+
45+
- name: Install mage
46+
run: go install github.com/magefile/mage@latest
47+
48+
- name: Update OpenAPI spec
49+
id: update-spec
50+
run: |
51+
mage openapi:update
52+
# Check if there are changes
53+
if [[ -n "$(git status --porcelain openapi-spec.yaml)" ]]; then
54+
echo "changes=true" >> $GITHUB_OUTPUT
55+
else
56+
echo "changes=false" >> $GITHUB_OUTPUT
57+
fi
58+
59+
# When not in a PR, create a new PR with changes
60+
- name: Create Pull Request
61+
if: steps.update-spec.outputs.changes == 'true' && github.event_name != 'pull_request'
62+
uses: peter-evans/create-pull-request@v5
63+
with:
64+
commit-message: 'chore: update OpenAPI spec'
65+
title: 'chore: update OpenAPI spec'
66+
body: |
67+
This PR updates the OpenAPI spec to the latest version.
68+
69+
This is an automated PR created by the GitHub Actions workflow.
70+
branch: update-openapi-spec
71+
base: main
72+
labels: dependencies,automated
73+
74+
# When in a PR context, update the PR with the latest spec
75+
- name: Commit changes to PR
76+
if: steps.update-spec.outputs.changes == 'true' && github.event_name == 'pull_request'
77+
run: |
78+
git config --global user.name 'GitHub Actions'
79+
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
80+
git add openapi-spec.yaml
81+
git commit -m "chore: update OpenAPI spec to latest version"
82+
git push
83+
84+
# Also add a comment to the PR about the update
85+
- name: Comment on PR
86+
if: steps.update-spec.outputs.changes == 'true' && github.event_name == 'pull_request'
87+
uses: actions/github-script@v6
88+
with:
89+
script: |
90+
github.rest.issues.createComment({
91+
issue_number: context.issue.number,
92+
owner: context.repo.owner,
93+
repo: context.repo.repo,
94+
body: '✅ The OpenAPI spec in this PR has been automatically updated to the latest version.'
95+
})

magefiles/openapi.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"os"
8+
9+
"github.com/magefile/mage/mg"
10+
"github.com/magefile/mage/sh"
11+
)
12+
13+
type OpenAPI mg.Namespace
14+
15+
// Update fetches the latest OpenAPI specification from the API
16+
func (OpenAPI) Update() error {
17+
apiURL := "https://api.admin.stage.aws.authzed.net/openapi-spec"
18+
outputFile := "openapi-spec.yaml"
19+
20+
fmt.Printf("Fetching latest OpenAPI spec from %s...\n", apiURL)
21+
22+
client := &http.Client{}
23+
24+
req, err := http.NewRequest("GET", apiURL, nil)
25+
if err != nil {
26+
return fmt.Errorf("failed to create request: %w", err)
27+
}
28+
29+
// Set headers (User-Agent, Accept, etc.)
30+
req.Header.Set("User-Agent", "AuthZed-Terraform-Provider-Builder")
31+
req.Header.Set("Accept", "application/yaml, text/yaml, application/json")
32+
33+
resp, err := client.Do(req)
34+
if err != nil {
35+
return fmt.Errorf("failed to fetch OpenAPI spec: %w", err)
36+
}
37+
defer resp.Body.Close()
38+
39+
// Check response status
40+
if resp.StatusCode != http.StatusOK {
41+
return fmt.Errorf("failed to fetch OpenAPI spec, status code: %d", resp.StatusCode)
42+
}
43+
44+
file, err := os.Create(outputFile)
45+
if err != nil {
46+
return fmt.Errorf("failed to create output file: %w", err)
47+
}
48+
defer file.Close()
49+
50+
_, err = io.Copy(file, resp.Body)
51+
if err != nil {
52+
return fmt.Errorf("failed to write OpenAPI spec to file: %w", err)
53+
}
54+
55+
fmt.Printf("OpenAPI spec successfully updated to %s\n", outputFile)
56+
57+
// Check for changes if git is available
58+
if _, err := sh.Exec(nil, os.Stdout, os.Stderr, "git", "diff", "--quiet", outputFile); err != nil {
59+
fmt.Println("Changes detected in the OpenAPI spec.")
60+
} else {
61+
fmt.Println("No changes detected in the OpenAPI spec.")
62+
}
63+
64+
return nil
65+
}

openapi-spec.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ info:
55
description: Cloud API for AuthZed Dedicated
66
termsOfService: https://authzed.com/terms-conditions
77
title: AuthZed Cloud API
8-
version: 25r1
8+
version: ""
99
externalDocs:
1010
description: Find out more at the official Authzed Docs site
1111
url: https://authzed.com/docs
12+
servers:
13+
- url: https://api.admin.stage.aws.authzed.net
1214
tags:
1315
- description: A permissions system defines a managed SpiceDB instance
1416
externalDocs:
@@ -2367,4 +2369,4 @@ components:
23672369
bearerFormat: Bearer token
23682370
description: Token used for authentication.
23692371
scheme: bearer
2370-
type: http
2372+
type: http

0 commit comments

Comments
 (0)