forked from noctalia-dev/legacy-v4-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
380 lines (341 loc) · 15.5 KB
/
Copy pathcode-quality.yml
File metadata and controls
380 lines (341 loc) · 15.5 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
name: Automatic Code Quality Review
on:
pull_request_target:
types:
- opened
- synchronize
- reopened
paths:
- "**.qml"
permissions:
contents: read
pull-requests: write
jobs:
code-quality:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.sha }}
- name: Automatic Code Quality
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
BASE_REF: ${{ github.base_ref }}
HEAD_REF: ${{ github.head_ref }}
run: |
git fetch origin "$BASE_REF"
diff_changes=$(git diff -U10000 origin/$BASE_REF...HEAD |
awk '/^diff --git.*\.qml /{found=1} /^diff --git/{if(!/\.qml /) found=0} found{print}' |
grep -vE "^(index|\+\+\+|-)") || echo "No qml file changed"
if [ -z "$diff_changes" ]; then
echo "No diff changes found!"
echo "The base ref checking: $BASE_REF"
echo "The head ref checking: $HEAD_REF"
exit 0
fi
comment_body=$(printf -- '### Automatic Code Quality Review')
issues=0
line_index=0
l_priority="[(L)]($PR_NUMBER 'Low priority, it is okay for this to remain unfixed before merging')"
h_priority="[(H)]($PR_NUMBER 'High priority, this needs to be fixed for the PR to be merged')"
function print_line() {
check_file_title
local priority=$1
local index=$2
local comment=$3
local code_line=$4
comment_body+=$(printf -- '\n- %s Line %d: %s\n```qml\n%s\n```' "$priority" "$index" "$comment" "$code_line")
}
function print_missing_property() {
check_file_title
local priority=$1
local property=$2
local code_line=$3
comment_body+=$(printf -- '\n- %s Missing required property `%s`. For example:\n```qml\n%s\n```' "$priority" "$property" "$code_line")
}
title_added=false
file_title=""
function check_file_title() {
if [ "$title_added" = false ]; then
title_added=true
comment_body+="$(printf -- '\n---\n### File: %s' "$file_title")"
fi
}
# Required Properties in Components
inBarWidget=false
inDesktopWidget=false
inDesktopWidgetSettings=false
inControlCenterWidget=false
inLauncherProvider=false
inMain=false
inPanel=false
inSettings=false
hasPluginApi=false
hasScreen=false
hasWidgetId=false
hasSection=false
hasSectionWidgetIndex=false
hasSectionWidgetsCount=false
hasWidgetSettings=false
hasLauncher=false
hasName=false
hasGeometryPlaceholder=false
hasAllowAttach=false
function reset_required_properties() {
inBarWidget=false
inDesktopWidget=false
inDesktopWidgetSettings=false
inControlCenterWidget=false
inLauncherProvider=false
inMain=false
inPanel=false
inSettings=false
hasPluginApi=false
hasScreen=false
hasWidgetId=false
hasSection=false
hasSectionWidgetIndex=false
hasSectionWidgetsCount=false
hasWidgetSettings=false
hasLauncher=false
hasName=false
hasGeometryPlaceholder=false
hasAllowAttach=false
}
function print_required_properties() {
if [ "$inBarWidget" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
if [ "$hasScreen" = false ]; then
print_missing_property "$h_priority" 'screen' 'property ShellScreen screen'
issues=$((issues + 1))
fi
if [ "$hasWidgetId" = false ]; then
print_missing_property "$h_priority" 'widgetId' 'property string widgetId: ""'
issues=$((issues + 1))
fi
if [ "$hasSection" = false ]; then
print_missing_property "$h_priority" 'section' 'property string section: ""'
issues=$((issues + 1))
fi
if [ "$hasSectionWidgetIndex" = false ]; then
print_missing_property "$h_priority" 'sectionWidgetIndex' 'property int sectionWidgetIndex: -1'
issues=$((issues + 1))
fi
if [ "$hasSectionWidgetsCount" = false ]; then
print_missing_property "$h_priority" 'sectionWidgetsCount' 'property int sectionWidgetsCount: 0'
issues=$((issues + 1))
fi
fi
if [ "$inDesktopWidget" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
fi
if [ "$inDesktopWidgetSettings" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
if [ "$hasWidgetSettings" = false ]; then
print_missing_property "$h_priority" 'widgetSettings' 'property var widgetSettings: null'
issues=$((issues + 1))
fi
fi
if [ "$inControlCenterWidget" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
if [ "$hasScreen" = false ]; then
print_missing_property "$h_priority" 'screen' 'property ShellScreen screen'
issues=$((issues + 1))
fi
fi
if [ "$inLauncherProvider" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
if [ "$hasLauncher" = false ]; then
print_missing_property "$h_priority" 'launcher' 'property var launcher: ""'
issues=$((issues + 1))
fi
if [ "$hasName" = false ]; then
print_missing_property "$h_priority" 'name' 'property string name: "Example"'
issues=$((issues + 1))
fi
fi
if [ "$inMain" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
fi
if [ "$inPanel" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
if [ "$hasGeometryPlaceholder" = false ]; then
print_missing_property "$h_priority" 'geometryPlaceholder' 'readonly property var geometryPlaceholder: panelContainer'
issues=$((issues + 1))
fi
if [ "$hasAllowAttach" = false ]; then
print_missing_property "$h_priority" 'allowAttach' 'readonly property bool allowAttach: true'
issues=$((issues + 1))
fi
fi
if [ "$inSettings" = true ]; then
if [ "$hasPluginApi" = false ]; then
print_missing_property "$h_priority" 'pluginApi' 'property var pluginApi: null'
issues=$((issues + 1))
fi
fi
}
while IFS= read -r line; do
if echo "$line" | grep -E "^diff --git" >/dev/null; then
print_required_properties
reset_required_properties
title_added=false
file_title=$(echo "$line" | sed 's/diff --git a\/\(.*\.qml\) .*/\1/g')
echo "Checking file: $file_title"
if [[ "$file_title" == */BarWidget.qml ]]; then
inBarWidget=true
elif [[ "$file_title" == */DesktopWidget.qml ]]; then
inDesktopWidget=true
elif [[ "$file_title" == */DesktopWidgetSettings.qml ]]; then
inDesktopWidgetSettings=true
elif [[ "$file_title" == */ControlCenterWidget.qml ]]; then
inControlCenterWidget=true
elif [[ "$file_title" == */LauncherProvider.qml ]]; then
inLauncherProvider=true
elif [[ "$file_title" == */Main.qml ]]; then
inMain=true
elif [[ "$file_title" == */Panel.qml ]]; then
inPanel=true
elif [[ "$file_title" == */Settings.qml ]]; then
inSettings=true
fi
elif echo "$line" | grep -E "^@@" >/dev/null; then
line_index=$(echo "$line" | grep -oE "\+[0-9]+" | grep -oE "[0-9]+")
echo "Current line index reset: $line_index"
else
if echo "$line" | grep -E "^\+" >/dev/null; then
# Only new lines should be auto checked for this type of code quality
if echo "$line" | grep -E "(border(\.width)?|spacing|pointSize|radius|margin(s)?): [1-9]+[0-9]*" >/dev/null; then
print_line "$h_priority" "$line_index" 'Do not use hardcoded values, always prefer to use the `Style` singleton instead' "$line"
issues=$((issues + 1))
fi
if echo "$line" | grep -E "console\.log" >/dev/null; then
print_line "$h_priority" "$line_index" 'Do not use `console.log`, always prefer to use the `Logger` singleton instead' "$line"
issues=$((issues + 1))
fi
if echo "$line" | grep -E "^(Text|Button|Checkbox|Switch) *\{" >/dev/null; then
print_line "$h_priority" "$line_index" 'Always prefer to use noctalia-widgets instead of qml widgets to keep it consistent with the shell. For example instead of `Text`, use `NText`' "$line"
issues=$((issues + 1))
fi
if echo "$line" | grep -E "applyUiScale: *false" >/dev/null; then
if [[ "$file_title" != *BarWidget.qml ]]; then
print_line "$l_priority" "$line_index" 'The `applyUiScale: false` would make it so that the component does not support ui scaling. Always check if this is the correct behaviour you want when changing the ui scale!' "$line"
issues=$((issues + 1))
fi
fi
if echo "$line" | grep -E "pluginApi\??\.tr\((.)*\) *(\?\?|\|\|)" >/dev/null; then
print_line "$l_priority" "$line_index" 'When it comes to translations there is no need for fallback values. From: `pluginApi?.tr("example") || "value"`. To: `pluginApi?.tr("example")`' "$line"
issues=$((issues + 1))
fi
if echo "$line" | grep -E "(text|label|description): *(\"|').+(\"|')" >/dev/null; then
print_line "$h_priority" "$line_index" 'Use translations instead of hardcoded text. Instead of: `"Example Label"`. To: `pluginApi?.tr("panel.example-label")`' "$line"
issues=$((issues + 1))
fi
fi
# Check for required properties
if [ "$inBarWidget" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
if echo "$line" | grep -E 'property +ShellScreen +screen' >/dev/null; then
hasScreen=true
fi
if echo "$line" | grep -E 'property +string +widgetId: +""' >/dev/null; then
hasWidgetId=true
fi
if echo "$line" | grep -E 'property +string +section: +""' >/dev/null; then
hasSection=true
fi
if echo "$line" | grep -E 'property +int +sectionWidgetIndex: +-1' >/dev/null; then
hasSectionWidgetIndex=true
fi
if echo "$line" | grep -E 'property +int +sectionWidgetsCount: +0' >/dev/null; then
hasSectionWidgetsCount=true
fi
fi
if [ "$inDesktopWidget" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
fi
if [ "$inDesktopWidgetSettings" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
if echo "$line" | grep -E 'property +var +widgetSettings: +null' >/dev/null; then
hasWidgetSettings=true
fi
fi
if [ "$inControlCenterWidget" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
if echo "$line" | grep -E 'property +ShellScreen +screen' >/dev/null; then
hasScreen=true
fi
fi
if [ "$inLauncherProvider" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
if echo "$line" | grep -E 'property +var +launcher: +null' >/dev/null; then
hasLauncher=true
fi
if echo "$line" | grep -E 'property +string +name: +".*"' >/dev/null; then
hasName=true
fi
fi
if [ "$inMain" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
fi
if [ "$inPanel" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
if echo "$line" | grep -E 'readonly +property +var +geometryPlaceholder:' >/dev/null; then
hasGeometryPlaceholder=true
fi
if echo "$line" | grep -E 'readonly +property +bool +allowAttach:' >/dev/null; then
hasAllowAttach=true
fi
fi
if [ "$inSettings" = true ]; then
if echo "$line" | grep -E 'property +var +pluginApi: +null' >/dev/null; then
hasPluginApi=true
fi
fi
line_index=$((line_index + 1))
fi
done <<<"$diff_changes"
# Print any required properties not printed on a new file found.
print_required_properties
if [[ "$issues" -ne 0 ]]; then
gh pr comment "$PR_NUMBER" --body "$comment_body"
fi