-
Notifications
You must be signed in to change notification settings - Fork 40
296 lines (244 loc) · 9.48 KB
/
validate.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Reference for adding or expanding schemas: https://ajv.js.org/json-schema.html#json-data-type
name: Validate JSONs
on:
pull_request:
workflow_dispatch:
inputs:
all_files:
description: Validate all Components
type: boolean
default: true
jobs:
validate-each-component-type-definition:
runs-on: ubuntu-latest
strategy:
matrix:
componentType: [pin, i2c, servo, ds18x20, pwm, pixel, uart]
name: Validate ${{ matrix.componentType }} Component Definition Files
steps:
- uses: actions/checkout@v3
- name: Validate ${{ matrix.componentType }} Components
uses: lorennorman/validate-json-action@master
with:
schema: /components/${{ matrix.componentType }}/schema.json
jsons: components/${{ matrix.componentType }}/*/definition.json
gather-relevant-files:
name: Gather Relevant Files
runs-on: ubuntu-latest
outputs:
# All files that were Added, Copied, Modified, or Renamed
files: ${{ steps.list-changed-files.outputs.all_changed_files }} ${{ steps.list-all-files.outputs.all_files }}
steps:
- uses: actions/checkout@v3
- name: List All Changed Files
if: ${{ !inputs.all_files }}
id: list-changed-files
uses: tj-actions/changed-files@v37
- name: List All Component Files
if: ${{ inputs.all_files }}
id: list-all-files
run: echo "all_files=`find components -type f | paste -sd " "`" >> $GITHUB_OUTPUT
check-user-permissions:
name: Check Write Permission
runs-on: ubuntu-latest
outputs:
# Extract the permission for later jobs to use
has-write-permission: ${{ steps.set-permission.outputs.has-permission }}
steps:
- uses: octokit/[email protected]
id: fetch-permissions
with:
route: GET /repos/${{ github.repository }}/collaborators/${{ github.actor }}/permission
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- id: set-permission
if: fromJson(steps.fetch-permissions.outputs.data).permission == 'admin' || fromJson(steps.fetch-permissions.outputs.data).permission == 'write'
run: echo "has-permission=true" >> "$GITHUB_OUTPUT"
validate-expected-filenames:
name: Validate Filenames
runs-on: ubuntu-latest
needs:
- gather-relevant-files
- check-user-permissions
env:
FILES: ${{ needs.gather-relevant-files.outputs.files }}
CAN_WRITE_TO_REPO: ${{ needs.check-user-permissions.outputs.has-write-permission }}
steps:
- uses: actions/checkout@v3
- name: Validate Only Expected Filenames
run: |
EXIT_VALUE=0
# external contributors can modify some files
EXTERNAL_REGEX="^components\/(pin|i2c|servo|ds18x20|pwm|pixel|uart)\/.*\/(definition\.json|image\.(png|jpe?g|gif))$"
# folks with write access to the repo (Adafruit team) can change more sensitive files
INTERNAL_REGEX="^(\.github\/.*|components\/(sensors.json|(pin|i2c|servo|ds18x20|pwm|pixel|uart)\/(schema.json|.*\/(definition\.json|image\.(png|jpe?g|gif)))))$"
# apply the appropriate regex based on permissions of the user
if [[ $CAN_WRITE_TO_REPO ]]; then
component_definition_regex=$INTERNAL_REGEX
else
component_definition_regex=$EXTERNAL_REGEX
fi
for file in $FILES; do
if [[ $file =~ $component_definition_regex ]]; then
if [[ $file =~ [A-Z] ]]; then
echo "❌ $file (no uppercase characters allowed)"
EXIT_VALUE=1
else
echo "✅ $file"
fi
else
if [[ $file =~ $INTERNAL_REGEX ]]; then
echo "❌ $file (only Adafruit staff may modify this file)"
else
echo "❌ $file should not exist"
fi
EXIT_VALUE=1
fi
done
if [[ $EXIT_VALUE = 1 ]]; then
echo "Are there files listed that this PR didn't modify? Consider merging the "main" branch into this PR, it may have subsequently diverged."
fi
exit $EXIT_VALUE
validate-image-extension-mimetype-agreement:
name: Validate Extensions Match Mimetypes
runs-on: ubuntu-latest
needs:
- gather-relevant-files
- validate-expected-filenames
env:
FILES: ${{ needs.gather-relevant-files.outputs.files }}
steps:
- uses: actions/checkout@v3
- name: Validate Image File Extension<->Mimetype Agreement
run: |
EXIT_VALUE=0
for FILE in $FILES; do
if ! [[ $FILE =~ \.(svg|jpe?g|png)$ ]]; then
continue # non-image file
fi
# extract each file's mimetype and extension
MIME=`file -b --mime-type $FILE`
EXT="${FILE##*.}"
# ad-hoc check that extension matches mimetype
if [[ "image/$EXT" == $MIME || ($EXT == "jpg" && $MIME == "image/jpeg") || ($EXT == "svg" && ($MIME == "image/svg+xml" || $MIME == "text/xml")) ]]; then
# Match!
echo "✅ $FILE"
else
# Doesn't match? Give helpful report
# split the mimetype on '/'
IFS='/'
read -a SPLIT_MIME <<< "$MIME"
IFS=' '
# take the last item
MIME_EXT=${SPLIT_MIME[-1]}
if [[ "$MIME_EXT" == "xml" ]]; then
MIME_EXT="svg"
elif [[ "$MIME_EXT" == "jpeg" ]]; then
MIME_EXT="jpg"
fi
echo "❌ $FILE: expected .$MIME_EXT"
EXIT_VALUE=1
fi
done
if [[ $EXIT_VALUE = 1 ]]; then
echo "Fix these ☝️ issues by renaming each ❌ file to the indicated extension."
fi
exit $EXIT_VALUE
validate-image-dimensions:
name: Validate Image Dimensions
runs-on: ubuntu-latest
needs:
- gather-relevant-files
- validate-image-extension-mimetype-agreement
env:
FILES: ${{ needs.gather-relevant-files.outputs.files }}
steps:
- uses: actions/checkout@v3
- uses: mfinelli/setup-imagemagick@v2
- name: Validate Image Dimensions
run: |
EXIT_VALUE=0
MAX_WIDTH=400
MAX_HEIGHT=300
echo "Evaluating Files $FILES"
for FILE in $FILES; do
if ! [[ $FILE =~ \.(svg|jpe?g|png)$ ]]; then
echo "$FILE: not an image"
continue # non-image file
fi
# use imagemagick to pull the dimensions
WIDTH=`identify -ping -format "%w" ${FILE}[0]`
HEIGHT=`identify -ping -format "%h" ${FILE}[0]`
EXPECTED_HEIGHT=$(("$WIDTH"*3/4))
NOT_4_3_RATIO=false
if [[ $EXPECTED_HEIGHT -ne "$HEIGHT" ]]; then
EXIT_VALUE=1
NOT_4_3_RATIO=true
fi
BAD_WIDTH=false
if [[ "$WIDTH" -gt "$MAX_WIDTH" ]]; then
EXIT_VALUE=1
BAD_WIDTH=true
fi
BAD_HEIGHT=false
if [[ "$HEIGHT" -gt "$MAX_HEIGHT" ]]; then
EXIT_VALUE=1
BAD_HEIGHT=true
fi
if [[ $EXIT_VALUE = 1 ]]; then
echo "❌ $FILE (${WIDTH}x${HEIGHT})"
if [[ $NOT_4_3_RATIO = true ]]; then
echo " ▬ width x height must have a 4:3 ratio: resize height to ${EXPECTED_HEIGHT}"
fi
if [[ $BAD_WIDTH = true ]]; then
echo " ↔️ width must be ${MAX_WIDTH} pixels or less"
fi
if [[ $BAD_HEIGHT = true ]]; then
echo " ↕️ height must be ${MAX_HEIGHT} pixels or less"
fi
else
echo "✅ $FILE (${WIDTH}x${HEIGHT})"
fi
done
if [[ $EXIT_VALUE = 1 ]]; then
echo "Fix these ☝️ issues by resizing each ❌ image to be 4:3 dimension ratio and fit within ${MAX_WIDTH}x${MAX_HEIGHT}."
fi
exit $EXIT_VALUE
validate-image-file-sizes:
name: Validate Image File Sizes
runs-on: ubuntu-latest
needs:
- gather-relevant-files
- validate-image-dimensions
env:
FILES: ${{ needs.gather-relevant-files.outputs.files }}
steps:
- uses: actions/checkout@v3
- name: Validate Image File Sizes
run: |
EXIT_VALUE=0
MAX_FILESIZE=$((100*1024)) # 100kb
for FILE in $FILES; do
if ! [[ $FILE =~ \.(svg|jpe?g|png)$ ]]; then
continue # non-image file
fi
FILESIZE=$(stat -c%s "$FILE")
MAX=$MAX_FILESIZE
if [[ "$FILESIZE" -gt "$MAX" ]]; then
EXIT_VALUE=1
echo "❌ $FILE ($FILESIZE)"
else
echo "✅ $FILE ($FILESIZE)"
fi
done
if [[ $EXIT_VALUE = 1 ]]; then
echo "Fix these issues ☝️ by compressing each ❌ file to be smaller than 100KB. You can try:"
echo "- using an image compressor"
echo "- exporting at lower quality settings (png or jpg)"
echo "- exporting a different image format:"
echo " - photos are best saved as jpg"
echo " - screenshots and digital images are best saved as png"
echo " - svg might be efficient for images with few colors and simple shapes"
echo " - gif should not be used"
fi
exit $EXIT_VALUE