-
Notifications
You must be signed in to change notification settings - Fork 1
166 lines (146 loc) · 5.99 KB
/
validate-openapi.yml
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
name: Validate OpenAPI Spec and Generated Client Code
on:
pull_request:
branches: [main]
types: [opened, reopened, edited, synchronize]
jobs:
validate-openapi:
name: Validate OpenAPI Spec
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
env:
# Check whether server changes, openapi.yaml changes, or client openapi folder changes
PATHS_TO_CHECK: |
server/application-server/src/main/java/
server/application-server/openapi.yaml
client/src/app/core/modules/openapi/
outputs:
CHANGE_DETECTED: ${{ steps.check_changes.outputs.CHANGE_DETECTED }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Fetch all branches
run: |
git fetch origin ${{ github.base_ref }}:refs/remotes/origin/${{ github.base_ref }}
git fetch origin ${{ github.head_ref }}:refs/remotes/origin/${{ github.head_ref }}
- name: Check for changes in specified paths
id: check_changes
run: |
CHANGED_PATHS=$(git diff --name-only origin/${{ github.base_ref }} origin/${{ github.head_ref }} | grep -E "^($(echo "$PATHS_TO_CHECK" | tr '\n' '|'))")
if [[ -z "$CHANGED_PATHS" ]]; then
echo "CHANGE_DETECTED=true" >> "$GITHUB_OUTPUT"
echo "No OpenAPI changes detected."
else
echo "CHANGE_DETECTED=false" >> "$GITHUB_OUTPUT"
echo "OpenAPI changes detected in the following paths:"
echo "$CHANGED_PATHS"
fi
- name: Set up Java
if: steps.check_changes.outputs.CHANGE_DETECTED == 'true'
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
- name: Run Gradle to generate OpenAPI docs
if: steps.check_changes.outputs.CHANGE_DETECTED == 'true'
working-directory: ./server/application-server
run: ./gradlew generateOpenApiDocs
- name: Check for OpenAPI spec differences
if: steps.check_changes.outputs.CHANGE_DETECTED == 'true'
id: check_openapi_spec
run: |
if git diff --exit-code ./server/application-server/openapi.yml; then
echo "OpenAPI spec is up-to-date."
else
echo "OpenAPI specs in openapi.yml differ from the generated version."
exit 1
fi
- name: Post comment about OpenAPI validation failure
if: failure() && steps.check_changes.outputs.CHANGE_DETECTED == 'true'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: openapi-validation-yml
message: |
🚨 **OpenAPI Validation Failed** 🚨
The OpenAPI specs in `openapi.yml` differ from the generated version.
Please update the OpenAPI specs by running:
```bash
cd ./server/application-server
./gradlew generateOpenApiDocs
```
Commit and push the updated file.
- name: Remove sticky comment on OpenAPI validation success
if: success() || steps.check_changes.outputs.CHANGE_DETECTED == 'false'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: openapi-validation-yml
delete: true
validate-client-code:
name: Validate Client Code
runs-on: ubuntu-latest
needs: validate-openapi
permissions:
contents: read
pull-requests: write
env:
CHANGE_DETECTED: ${{needs.validate-openapi.outputs.CHANGE_DETECTED}}
steps:
- name: Checkout code
if: env.CHANGE_DETECTED == 'true'
uses: actions/checkout@v4
- name: Set up Java
if: env.CHANGE_DETECTED == 'true'
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
- name: Set up Node.js
if: env.CHANGE_DETECTED == 'true'
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install OpenAPI Generator CLI
if: env.CHANGE_DETECTED == 'true'
run: npm install -g @openapitools/openapi-generator-cli
- name: Generate client code
if: env.CHANGE_DETECTED == 'true'
run: |
npx openapi-generator-cli generate \
-i ./server/application-server/openapi.yaml \
-g typescript-angular \
-o ./client/src/app/core/modules/openapi \
--additional-properties fileNaming=kebab-case,withInterfaces=true --generate-alias-as-model
- name: Check for client code differences
if: env.CHANGE_DETECTED == 'true'
id: check_client_code
run: |
if git diff --exit-code ./client/src/app/core/modules/openapi; then
echo "Client code is up-to-date."
else
echo "Client code in /client/src/app/core/modules/openapi is not up-to-date."
exit 1
- name: Post comment about client code validation failure
if: failure() && env.CHANGE_DETECTED == 'true'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: client-code-validation
message: |
🚨 **Client Code Validation Failed** 🚨
The client code in `/client/src/app/core/modules/openapi` is not up-to-date.
Please regenerate the client code by running:
```bash
npm install -g @openapitools/openapi-generator-cli
# In root directory
npx openapi-generator-cli generate -i ./server/application-server/openapi.yaml -g typescript-angular -o ./client/src/app/core/modules/openapi --additional-properties fileNaming=kebab-case,withInterfaces=true --generate-alias-as-model
```
Commit and push the updated files.
- name: Remove sticky comment on client code validation success
if: success() || env.CHANGE_DETECTED == 'false'
uses: marocchino/sticky-pull-request-comment@v2
with:
header: client-code-validation
delete: true