-
Notifications
You must be signed in to change notification settings - Fork 9
149 lines (126 loc) · 4.82 KB
/
Copy pathupdate-api-spec.yml
File metadata and controls
149 lines (126 loc) · 4.82 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
name: Update API Specification
on:
schedule:
# Run weekdays at 02:00 UTC
- cron: '0 2 * * 1-5'
workflow_dispatch: # Allow manual triggering
concurrency:
group: update-api-spec
cancel-in-progress: false
jobs:
update-api-spec:
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: |
npm install -g swagger2openapi
npm install -g mintlify
- name: Configure git
run: |
git config user.name "API Spec Updater"
git config user.email "noreply@checkly.com"
- name: Ensure labels exist
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh label create "auto-generated" --color "0e8a16" --description "Automatically generated PR" --force
gh label create "api-docs" --color "1d76db" --description "API documentation changes" --force
- name: Fetch latest API specification
run: ./update-api-spec.sh
- name: Generate pages for any new endpoints
run: node .github/scripts/detect-new-endpoints.mjs
- name: Create or update API spec PR if anything changed
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -euo pipefail
# Stage everything that might have changed
git add api-reference docs.json
if git diff --cached --quiet; then
echo "✅ No changes — nothing to do"
exit 0
fi
# Summarise the diff for the commit/PR body
SPEC_CHANGED=0
if git diff --cached --name-only | grep -q '^api-reference/openapi\.json$'; then
SPEC_CHANGED=1
fi
NEW_PAGES=$(git diff --cached --name-only --diff-filter=A | grep -c '^api-reference/.*\.mdx$' || true)
DOCS_JSON_CHANGED=0
if git diff --cached --name-only | grep -q '^docs\.json$'; then
DOCS_JSON_CHANGED=1
fi
# Title reflects what actually changed
if [ "$SPEC_CHANGED" = "1" ] && [ "$NEW_PAGES" -gt 0 ]; then
TITLE="chore(api): update API spec and add $NEW_PAGES new endpoint page(s)"
elif [ "$SPEC_CHANGED" = "1" ]; then
TITLE="chore(api): update API specification"
elif [ "$NEW_PAGES" -gt 0 ]; then
TITLE="docs(api): add $NEW_PAGES new endpoint page(s)"
else
TITLE="chore(api): refresh API reference"
fi
BRANCH="auto/update-api-spec"
git checkout -B "$BRANCH"
git commit -m "$TITLE" \
-m "Automatically fetched from https://api.checklyhq.com/openapi.json" \
-m "Updated on $(date -u '+%Y-%m-%d %H:%M:%S UTC')"
git push --force-with-lease origin "$BRANCH"
# Build PR body
BODY_FILE=$(mktemp)
{
echo "Auto-generated from the API spec sync workflow."
echo ""
echo "- Spec changed: $([ "$SPEC_CHANGED" = "1" ] && echo yes || echo no)"
echo "- New endpoint pages: $NEW_PAGES"
echo "- \`docs.json\` updated: $([ "$DOCS_JSON_CHANGED" = "1" ] && echo yes || echo no)"
echo ""
echo "This PR reuses the \`$BRANCH\` branch so future runs update this PR instead of opening duplicates."
echo ""
echo "---"
echo "_Run: [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})_"
} > "$BODY_FILE"
PR_URL=$(gh pr list \
--base main \
--head "$BRANCH" \
--state open \
--json url \
--jq '.[0].url // ""')
if [ -n "$PR_URL" ]; then
gh pr edit "$PR_URL" \
--title "$TITLE" \
--body-file "$BODY_FILE" \
--add-label auto-generated \
--add-label api-docs
echo "Updated $PR_URL"
else
PR_URL=$(gh pr create \
--title "$TITLE" \
--body-file "$BODY_FILE" \
--base main \
--head "$BRANCH" \
--label auto-generated \
--label api-docs)
echo "Created $PR_URL"
fi
# Try to enable auto-merge, but do not create another PR if GitHub
# requires manual approval before pull_request workflows can run.
if ! gh pr merge "$PR_URL" --squash --auto --delete-branch; then
echo "::notice::Could not enable auto-merge for $PR_URL. The stable branch will be updated by the next run instead of opening another PR."
fi
- name: Ping Checkly
if: always()
run: curl -fsS --retry 3 https://ping.checklyhq.com/ad576f21-3f3e-4a6a-b8e4-67fd0f6ed92d