-
Notifications
You must be signed in to change notification settings - Fork 0
/
openapi-lint.sh
executable file
·143 lines (109 loc) · 3.62 KB
/
openapi-lint.sh
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
#!/bin/bash
debug=false
if [[ $1 == "--debug" ]]; then
debug=true
fi
# Check that the IBM OpenAPI Linter is installed
if ! command -v lint-openapi >/dev/null; then
gum style --foreground 196 "Error: IBM OpenAPI Linter is not installed. Please install the linter by following the instructions at https://github.com/IBM/openapi-validator"
exit 1
fi
# Find all routes.yaml files in the ./pkg/codegen/apis directory
routesFiles=$(find ./pkg/codegen/apis -name "routes.yaml")
touch ./pr-report.md
echo "## OpenAPI Linting Report" >./pr-report.md
totalErrors=0
totalWarnings=0
totalInfos=0
totalHints=0
# Lint each routes.yaml file
for file in $routesFiles; do
rm -rf ./lint-output.json
gum spin --spinner dot --title "Linting $file" -- lint-openapi -c ./openapi-lint-config.yaml -s "$file" >./lint-output.json
# Make ./pkg/codegen/apis/api/routes.yaml -> api/routes.yaml
prettyName=$(echo $file | sed 's/\.\/pkg\/codegen\/apis\///' | sed 's/\/routes.yaml//')
gum style --foreground 10 "Linting $prettyName"
# Print the lint output (Only used when the --debug flag is passed)
if [[ $debug == true ]]; then
cat ./lint-output.json
fi
# Put the header on the PR report
cat <<EOF >>./pr-report.md
### Linting $prettyName
\`\`\`
EOF
# Get the total number of errors, warnings, infos, and hints
errors=$(cat ./lint-output.json | jq .error.summary.total)
warnings=$(cat ./lint-output.json | jq .warning.summary.total)
infos=$(cat ./lint-output.json | jq .info.summary.total)
hints=$(cat ./lint-output.json | jq .hint.summary.total)
# Put the lint output in the PR report
cat <<EOF >>./pr-report.md
$errors errors, $warnings warnings, $infos infos, $hints hints
EOF
# Put the footer on the PR report
cat <<EOF >>./pr-report.md
\`\`\`
EOF
if [[ $errors -gt 0 ]]; then
cat <<EOF >>./pr-report.md
#### Error Messages
\`\`\`
EOF
cat ./lint-output.json | jq -r '.error.summary.entries[].generalizedMessage' >>./pr-report.md
cat <<EOF >>./pr-report.md
\`\`\`
EOF
fi
if [[ $warnings -gt 0 ]]; then
cat <<EOF >>./pr-report.md
#### Warning Messages
\`\`\`
EOF
cat ./lint-output.json | jq -r '.warning.summary.entries[].generalizedMessage' >>./pr-report.md
cat <<EOF >>./pr-report.md
\`\`\`
EOF
fi
if [[ $infos -gt 0 ]]; then
cat <<EOF >>./pr-report.md
#### Info Messages
\`\`\`
EOF
cat ./lint-output.json | jq -r '.info.summary.entries[].generalizedMessage' >>./pr-report.md
cat <<EOF >>./pr-report.md
\`\`\`
EOF
fi
if [[ $hints -gt 0 ]]; then
cat <<EOF >>./pr-report.md
#### Hint Messages
\`\`\`
EOF
cat ./lint-output.json | jq -r '.hint.summary.entries[].generalizedMessage' >>./pr-report.md
cat <<EOF >>./pr-report.md
\`\`\`
EOF
fi
# Add the errors, warnings, infos, and hints to the total
totalErrors=$((totalErrors + errors))
totalWarnings=$((totalWarnings + warnings))
totalInfos=$((totalInfos + infos))
totalHints=$((totalHints + hints))
done
# CLean up when the script exits if the --debug flag is passed
if [[ $debug == true ]]; then
gum style --foreground 214 "Cleaning up"
rm -rf lint-output.json
rm -rf pr-report.md
fi
if [[ $totalErrors -gt 0 ]]; then
gum style --foreground 196 "FAIL: Linting failed with $totalErrors errors, $totalWarnings warnings, $totalInfos infos, and $totalHints hints"
exit 1
elif [[ $totalWarnings -gt 0 ]]; then
gum style --foreground 214 "PASS: Linting passed with $totalErrors errors, $totalWarnings warnings, $totalInfos infos, and $totalHints hints"
exit 1
else
gum style --foreground 10 "PASS: Linting passed with $totalErrors errors, $totalWarnings warnings, $totalInfos infos, and $totalHints hints"
exit 0
fi