-
Notifications
You must be signed in to change notification settings - Fork 76
211 lines (183 loc) Β· 7.58 KB
/
Copy pathsync-openapi-spec.yml
File metadata and controls
211 lines (183 loc) Β· 7.58 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
---
# Sync generated OpenAPI spec to the central spec repository.
# Called by CI workflows after spec generation via workflow_call.
name: Sync OpenAPI Spec
on:
workflow_call:
secrets:
OPENAPI_SPEC_SYNC_TOKEN:
required: true
OPENAPI_SPEC_REPO:
required: true
OPENAPI_SPEC_SYNC_GPG_PRIVATE_KEY:
required: true
OPENAPI_SPEC_PR_REVIEW_TEAM:
required: true
jobs:
sync:
name: Sync OpenAPI spec to central repo
runs-on: ubuntu-latest
steps:
- name: Map EDA branch to spec repo branch
id: branch_map
run: |
SOURCE_BRANCH="${{ github.ref_name }}"
case "$SOURCE_BRANCH" in
main)
SPEC_BRANCH="devel"
;;
*)
# Default: use the same branch name
SPEC_BRANCH="$SOURCE_BRANCH"
;;
esac
echo "spec_branch=$SPEC_BRANCH" >> $GITHUB_OUTPUT
echo "π EDA branch: $SOURCE_BRANCH β Spec repo branch: $SPEC_BRANCH"
- name: Download spec artifact
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: openapi-spec
- name: Verify spec file exists
run: |
if [ ! -f "openapi.json" ]; then
echo "β Spec artifact not found"
ls -la .
exit 1
fi
echo "β
Found openapi.json ($(wc -c < openapi.json) bytes)"
- name: Checkout spec repo
id: checkout_spec_repo
continue-on-error: true
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ secrets.OPENAPI_SPEC_REPO }}
ref: ${{ steps.branch_map.outputs.spec_branch }}
path: spec-repo
token: ${{ secrets.OPENAPI_SPEC_SYNC_TOKEN }}
- name: Fail if branch doesn't exist
if: steps.checkout_spec_repo.outcome == 'failure'
run: |
echo "##[error]β Branch '${{ steps.branch_map.outputs.spec_branch }}' does not exist in the central spec repository."
echo "##[error]EDA branch: ${{ github.ref_name }} β Spec repo branch: ${{ steps.branch_map.outputs.spec_branch }}"
echo "##[error]This branch must be created in the spec repo before specs can be synced."
exit 1
- name: Copy spec to repo
run: |
cp "./openapi.json" "spec-repo/eda.json"
echo "β
Copied openapi.json β spec-repo/eda.json"
- name: Check for changes
id: check
working-directory: spec-repo
run: |
STATUS=$(git status --porcelain eda.json)
if [ -z "$STATUS" ]; then
echo "β
No differences found β specs are identical"
echo "has_diff=false" >> $GITHUB_OUTPUT
else
echo "π Changes detected: $STATUS"
echo "has_diff=true" >> $GITHUB_OUTPUT
if echo "$STATUS" | grep -q "^??"; then
echo "is_new_file=true" >> $GITHUB_OUTPUT
else
echo "is_new_file=false" >> $GITHUB_OUTPUT
fi
fi
- name: Create or update PR in spec repo
if: steps.check.outputs.has_diff == 'true'
working-directory: spec-repo
env:
GH_TOKEN: ${{ secrets.OPENAPI_SPEC_SYNC_TOKEN }}
GPG_PRIVATE_KEY: ${{ secrets.OPENAPI_SPEC_SYNC_GPG_PRIVATE_KEY }}
SPEC_REPO: ${{ secrets.OPENAPI_SPEC_REPO }}
SPEC_PR_REVIEW_TEAM: ${{ secrets.OPENAPI_SPEC_PR_REVIEW_TEAM }}
SPEC_BRANCH: ${{ steps.branch_map.outputs.spec_branch }}
SOURCE_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
run: |
# Import GPG key and configure git for signed commits
echo "$GPG_PRIVATE_KEY" | gpg --batch --import 2>/dev/null
GPG_KEY_ID=$(gpg --list-secret-keys --keyid-format long 2>/dev/null | grep sec | head -1 | awk '{print $2}' | cut -d'/' -f2)
if [ -z "$GPG_KEY_ID" ]; then
echo "β Failed to import GPG key or extract key ID"
exit 1
fi
git config user.name "aap-api-bot"
git config user.email "aap-api-bot@redhat.com"
git config commit.gpgsign true
git config user.signingkey "$GPG_KEY_ID"
# Configure git to use the token for push
git remote set-url origin "https://x-access-token:${GH_TOKEN}@github.com/${SPEC_REPO}.git"
# Use a stable branch name so we can reuse it across runs
BRANCH_NAME="auto/update-eda-${SPEC_BRANCH}"
# Check if there's already an open PR from this branch
EXISTING_PR=$(gh pr list \
--repo "$SPEC_REPO" \
--head "$BRANCH_NAME" \
--base "$SPEC_BRANCH" \
--state open \
--json number \
--jq '.[0].number // empty')
if [ -n "$EXISTING_PR" ]; then
echo "π Found existing PR #${EXISTING_PR} - will update it"
else
echo "π No existing PR found - will create a new one"
# Clean up stale remote branch from a previously merged PR
git push origin --delete "$BRANCH_NAME" 2>/dev/null || true
fi
git checkout -b "$BRANCH_NAME"
# Add and commit changes
git add "eda.json"
if [ "${{ steps.check.outputs.is_new_file }}" == "true" ]; then
COMMIT_MSG="Add EDA OpenAPI spec for ${SPEC_BRANCH}"
else
COMMIT_MSG="Update EDA OpenAPI spec for ${SPEC_BRANCH}"
fi
git commit -m "$COMMIT_MSG
Synced from ${{ github.repository }}@${{ github.sha }}
Source branch: ${{ github.ref_name }}
Spec repo branch: ${SPEC_BRANCH}
Co-Authored-By: github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
# Push branch
git push --force origin "$BRANCH_NAME"
# Create PR (commit message passed via env var to avoid script injection)
PR_TITLE="[${SPEC_BRANCH}] Update EDA spec from merged commit"
PR_BODY=$(cat <<EOF
## Summary
Automated OpenAPI spec sync from component repository merge.
**Source:** ${{ github.repository }}@${{ github.sha }}
**Source Branch:** \`${{ github.ref_name }}\`
**Spec Repo Branch:** \`${SPEC_BRANCH}\`
**Component:** \`EDA\`
**Spec File:** \`eda.json\`
## Changes
$(if [ "${{ steps.check.outputs.is_new_file }}" == "true" ]; then echo "- π New spec file created"; else echo "- π Spec file updated with latest changes"; fi)
## Source Commit
\`\`\`
${SOURCE_COMMIT_MESSAGE}
\`\`\`
---
π€ This PR was automatically generated by the OpenAPI spec sync workflow.
EOF
)
if [ -n "$EXISTING_PR" ]; then
gh pr edit "$EXISTING_PR" \
--repo "$SPEC_REPO" \
--body "$PR_BODY"
echo "β
Updated existing PR #${EXISTING_PR}"
else
gh pr create \
--repo "$SPEC_REPO" \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base "$SPEC_BRANCH" \
--head "$BRANCH_NAME" \
--reviewer "$SPEC_PR_REVIEW_TEAM"
echo "β
Created PR in spec repo"
fi
- name: Report results
if: always()
run: |
if [ "${{ steps.check.outputs.has_diff }}" == "true" ]; then
echo "π Spec sync completed β PR created in spec repo"
else
echo "β
Spec sync completed β no changes needed"
fi