-
Notifications
You must be signed in to change notification settings - Fork 54
230 lines (193 loc) · 8.48 KB
/
ci-openapi.yml
File metadata and controls
230 lines (193 loc) · 8.48 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
name: Update Database ERD Diagrams and OpenAPI Docs
on:
workflow_dispatch:
schedule:
# Runs daily at 9 AM EST
- cron: '0 14 * * *'
defaults:
run:
working-directory: ./api
jobs:
update-openapi-docs:
runs-on: ubuntu-22.04
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch full history so we can create branches properly
# Generate artifacts on the triggering ref BEFORE switching branches
- name: Build Docker images
run: make build
- name: Start database
run: make start-db
- name: Create ERD diagram
run: make create-erds
- name: Update OpenAPI spec
run: make openapi-spec
- name: Stash generated artifacts
working-directory: .
run: |
mkdir -p /tmp/sgg-artifacts
cp -v api/openapi.generated.yml /tmp/sgg-artifacts/ || true
# Copy ERD PNGs if they exist
if ls documentation/api/database/erds/*.png >/dev/null 2>&1; then
cp -v documentation/api/database/erds/*.png /tmp/sgg-artifacts/
fi
# Now create/switch to the bot branch based on main for a clean PR
- name: Setup branch
working-directory: .
run: |
git config user.name "nava-platform-bot"
git config user.email "platform-admins@navapbc.com"
BRANCH="nava-platform-bot/update-erd-and-openapi-docs"
echo "Branch name: $BRANCH"
git fetch origin main
git fetch origin "$BRANCH" || true
# Check if branch exists remotely and use it, otherwise create from main
if git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then
echo "Remote branch exists, checking it out"
# Reset any local changes that might conflict with checkout
# (Artifacts are safe in /tmp/sgg-artifacts and will be restored after checkout)
git reset --hard HEAD || true
git clean -fd || true
# Force checkout/create the branch (creates if doesn't exist locally, resets if it does)
git checkout -B "$BRANCH" "origin/$BRANCH"
else
echo "Remote branch does not exist, creating from main"
git reset --hard HEAD || true
git clean -fd || true
git checkout -b "$BRANCH" origin/main
fi
- name: Rebase branch onto latest main
working-directory: .
run: |
BRANCH="nava-platform-bot/update-erd-and-openapi-docs"
git checkout "$BRANCH" || exit 1
git fetch origin main
if ! git rebase origin/main; then
git checkout --theirs . 2>/dev/null || true
git add . || true
# Restore our generated files
if [ -f /tmp/sgg-artifacts/openapi.generated.yml ]; then
cp -v /tmp/sgg-artifacts/openapi.generated.yml api/openapi.generated.yml
git add api/openapi.generated.yml || true
fi
if [ -d /tmp/sgg-artifacts ]; then
for png in /tmp/sgg-artifacts/*.png; do
if [ -f "$png" ]; then
cp -v "$png" "documentation/api/database/erds/$(basename "$png")"
git add "documentation/api/database/erds/$(basename "$png")" || true
fi
done
fi
git rebase --continue
fi
- name: Restore artifacts
working-directory: .
run: |
# Restore generated files from temp stash
if [ -f /tmp/sgg-artifacts/openapi.generated.yml ]; then
cp -v /tmp/sgg-artifacts/openapi.generated.yml api/
fi
if ls /tmp/sgg-artifacts/*.png >/dev/null 2>&1; then
mkdir -p documentation/api/database/erds
cp -v /tmp/sgg-artifacts/*.png documentation/api/database/erds/
fi
- name: Commit & push changes
id: commit
working-directory: .
run: |
BRANCH="nava-platform-bot/update-erd-and-openapi-docs"
# Show file status before adding
echo "Files before git add:"
ls -lh api/openapi.generated.yml || echo "openapi.generated.yml not found"
ls -lh documentation/api/database/erds/*.png || echo "ERD files not found"
# Add files from both api/ and documentation/ directories
git add api/openapi.generated.yml documentation/api/database/erds/*.png
echo "Git status after adding files:"
git status
echo "Checking for staged changes:"
if git diff --cached --quiet; then
echo "No changes detected in staged files"
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Changes detected! Showing diff:"
git diff --cached --stat
git commit -m "Update ERD Diagrams and OpenAPI Specs"
git push --force --set-upstream origin "$BRANCH"
echo "changed=true" >> "$GITHUB_OUTPUT"
echo "Changes committed and pushed successfully"
- name: Debug - Check outputs
working-directory: .
run: |
echo "Changed value: ${{ steps.commit.outputs.changed }}"
echo "Condition will be: ${{ steps.commit.outputs.changed == 'true' }}"
- name: Create Pull Request (only if none exists)
if: steps.commit.outputs.changed == 'true'
working-directory: .
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
API="https://api.github.com/repos/${{ github.repository }}/pulls"
BRANCH="nava-platform-bot/update-erd-and-openapi-docs"
echo "Checking for existing PR..."
echo "API endpoint: $API?head=${{ github.repository_owner }}:$BRANCH&base=main"
# Check if a PR already exists
PR_RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"$API?head=${{ github.repository_owner }}:$BRANCH&base=main")
echo "PR check response:"
echo "$PR_RESPONSE" | jq '.'
PR_NUM=$(echo "$PR_RESPONSE" | jq '.[0].number // empty')
if [ -n "$PR_NUM" ]; then
echo "PR #$PR_NUM already exists — skipping creation."
exit 0
fi
echo "No existing PR found, creating new PR..."
# Create a new PR if none exists
CREATE_RESPONSE=$(curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"$API" \
-d "$(jq -n \
--arg title "[Unticketed] Automated Update to Database ERD Diagrams and OpenAPI Docs" \
--arg head "$BRANCH" \
--arg base "main" \
--arg body "Automated update of ERD diagrams and OpenAPI specs." \
'{title: $title, head: $head, base: $base, body: $body}')")
echo "PR creation response:"
echo "$CREATE_RESPONSE" | jq '.'
PR_URL=$(echo "$CREATE_RESPONSE" | jq -r '.html_url // empty')
if [ -n "$PR_URL" ]; then
echo "Successfully created PR: $PR_URL"
else
echo "Failed to create PR"
exit 1
fi
# Add reviewers to the newly created PR
NEW_PR_NUM=$(echo "$CREATE_RESPONSE" | jq -r '.number // empty')
if [ -n "$NEW_PR_NUM" ]; then
echo "Adding reviewers to PR #$NEW_PR_NUM..."
REVIEWER_RESPONSE=$(curl -s -X POST -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$NEW_PR_NUM/requested_reviewers" \
-d '{
"reviewers": [
"chouinar",
"mdragon",
"joshtonava",
"jakobpederson",
"chay-REIsys",
"hao10282025-sudo",
"kkrug",
"dghazvini"
]
}')
echo "Reviewer assignment response:"
echo "$REVIEWER_RESPONSE" | jq '.requested_reviewers[].login // "No reviewers in response"'
if echo "$REVIEWER_RESPONSE" | jq -e '.url' > /dev/null 2>&1; then
echo "Successfully requested reviewers"
else
echo "Warning: Could not add reviewers, but PR was created successfully"
fi
fi