-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc_checker.sh
executable file
·328 lines (228 loc) · 8.2 KB
/
doc_checker.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
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
#!/bin/bash -
# vim: ft=sh
#title :doc_checker.sh
#description :Script to check the Documentation of Git Repository
#author :Alejandro Villegas Lopez (alex.ansi.c@gmail.com).
#===============================================================================
#=============================
# Global Variables
#===============================================================================
TRUE=1
FALSE=0
#=============================
# MSG Functions
#===============================================================================
# Error Message
function log_err () {
echo -e "\033[37m[\033[31m ERR \033[37m]\033[0m $@"
}
# Warning Message
function log_warn () {
echo -e "\033[37m[\033[33m WAR \033[37m]\033[0m $@"
}
# OK Message
function log_ok () {
echo -e "\033[37m[\033[32m OK \033[37m]\033[0m $@"
}
# Info Message
function log_info () {
echo -e "\033[37m[\033[34m INF \033[37m]\033[0m $@"
}
#=============================
# AUX Functions
#===============================================================================
function check_if_file_exists () {
[[ -f $1 ]] && { return $TRUE; } || { log_err "$1 file not found in $(pwd)"; return $FALSE; }
}
#=============================
# Git Checking Module
#===============================================================================
# Check if is a Git Repository
function check_git_is_a_git_repo () {
[[ -d .git ]] && { log_ok "Is a Git Repository"; return $TRUE; } || { log_err "Is not a Git Repository"; return $FALSE; }
}
# Check Git
function check_git () {
log_info "Checking Git"
check_git_is_a_git_repo
[[ $? -ne $TRUE ]] && { return $FALSE; }
log_ok "Git repo is correct"
return $TRUE
}
#=============================
# Version File Checking Module
#===============================================================================
# Check if VERSION file exists
function check_version_file_exists () {
check_if_file_exists "VERSION"
}
# Check VERSION format
function check_version_format () {
egrep -q "^[0-9]+\.[0-9]+(\.[0-9]+)?(-beta)?(-LTS)?$" VERSION
[[ $? -eq 0 ]] && { return $TRUE; } || { log_err "Version format invalid"; return $FALSE; }
}
# Check if current version is pushed in Git
function check_version_git_tags () {
grep -q $(cat VERSION) <<< "$(git tag)"
[[ $? -eq 0 ]] && { return $TRUE; } || { log_warn "Current version ( $(cat VERSION) ) are not in the git remote server"; return $FALSE; }
}
# Check VERSION file
function check_version () {
log_info "Checking Version file"
local rc=$TRUE
check_version_file_exists
[[ $? -ne $TRUE ]] && { return $FALSE; }
check_version_format
[[ $? -ne $TRUE ]] && { rc=$FALSE; }
check_version_git_tags
[[ $? -ne $TRUE ]] && { rc=$FALSE; }
[[ $rc == $FALSE ]] && { log_err "VERSION file check failed"; } || { log_ok "VERSION file is correct"; }
return $TRUE
}
#=============================
# Notice File Checking Module
#===============================================================================
# Check if NOTICE file exists
function check_notice_file_exists () {
check_if_file_exists "NOTICE.md"
}
# Check NOTICE.md file Copyright year
function check_notice_copyright_year () {
local current_year=$(date +%Y)
local notice_year=$(grep Copyright NOTICE.md | awk '{ print $2 }')
[[ $current_year == $notice_year ]] && { log_ok "Copyright Year is updated"; return $TRUE; } || { log_err "Notice file Copyright Year is outdated"; return $FALSE; }
}
# Check NOTICE.md file
function check_notice () {
log_info "Checking NOTICE.md file"
check_notice_file_exists
[[ $? -ne $TRUE ]] && { return $FALSE; }
check_notice_copyright_year
[[ $? -ne $TRUE ]] && { return $FALSE; }
log_ok "NOTICE.md file is correct"
return $TRUE
}
#=============================
# Changelog File Checking Module
#===============================================================================
# ^[0-9]+\.[0-9]+(\.[0-9]+)?(-beta)?(-LTS)? \((([0-2][0-9])|(3[0-1]))\/(([1-9])|(0[1-9])|(1[0-2]))\/([0-9]{4})\)$
function check_changelog_file_exists () {
check_if_file_exists "CHANGELOG.md"
}
function check_changelog_if_version_is_documented () {
local chl_versions="$(egrep '^v*[0-9]+\.[0-9]+(\.[0-9]+)?(-beta)?(-LTS)? \((([0-2][0-9])|(3[0-1]))\/(([1-9])|(0[1-9])|(1[0-2]))\/([0-9]{4})\)$' CHANGELOG.md | awk 'FS=" " { print $1 }' | sort)"
local git_versions="$(git tag | sort)"
local retval=$TRUE
# Versions documented and not uploaded
vdnu="$(comm -2 -3 <(echo "${chl_versions[@]}") <(echo "${git_versions[@]}"))"
# Versions not documented and uploaded
vndu="$(comm -1 -3 <(echo "${chl_versions[@]}") <(echo "${git_versions[@]}"))"
# Check if there are versions documented but not uploaded
for v in ${vdnu[@]}; do
log_err "Version $v is documented in changelog but is not present on Git Repository"
retval=$FALSE
done
# Check if there are versions uploaded but not documented
for v in ${vndu[@]}; do
log_err "Version $v is not documented in changelog but is present on Git Repository"
retval=$FALSE
done
return $retval
}
function check_changelog () {
log_info "Checking Changelog file"
check_changelog_file_exists
[[ $? -ne $TRUE ]] && { return $FALSE; }
check_changelog_if_version_is_documented
[[ $? -ne $TRUE ]] && { log_err "CHANGELOG have erros"; return $FALSE; }
log_ok "Changelog file is correct"
return $TRUE
}
#=============================
# License File Checking Module
#===============================================================================
# Check if LICENSE file exists
function check_license_file_exists () {
check_if_file_exists "LICENSE"
}
# Check if LICENSE file is Apache 2.0
function check_license_apache () {
local license_name=$(head -1 LICENSE | sed -e 's/^[[:space:]]*//g')
local license_version=$(sed '2q;d' LICENSE | sed -e 's/^[[:space:]]*//g' | awk '{ print $2 }' | sed 's/.$//')
[[ "$license_name" == "Apache License" ]] && { log_ok "License is Apache Type"; return $TRUE; } || { log_err "License is not Apache"; return $FALSE; }
[[ $license_version == "2.0" ]] && { log_ok "Apache License is at version 2.0"; return $TRUE; } || { log_err "Apache License is at version 2.0"; return $FALSE; }
}
# Check if LICENSE file is signed
function check_license_signed () {
local fill=$(egrep "Copyright [0-9]{4} .*$" LICENSE)
local retval_grep=$?
local year=$(echo "$fill" | awk '{ print $2 }')
[[ $retval_grep -eq 0 ]] && { log_ok "License signed"; } || { log_err "License is not signed"; return $FALSE; }
[[ "$year" != $(date +%Y) ]] && { log_err "Year of the License Copyright is outdated"; return $FALSE; } || { return $TRUE; }
}
# Check LICENSE file
function check_license () {
log_info "Checking LICENSE file"
local rc=$TRUE
check_license_file_exists
[[ $? -ne $TRUE ]] && { return $FALSE; }
check_license_apache
[[ $? -ne $TRUE ]] && { rc=$FALSE; }
check_license_signed
[[ $? -ne $TRUE ]] && { rc=$FALSE; }
[[ $rc == $FALSE ]] && { log_err "LICENSE file check failed"; } || { log_ok "LICENSE file is correct"; }
return $rc
}
#=============================
# Main Functions
#===============================================================================
function help_msg () {
printf "Repo Checker Help message:
Version: 0.1-beta
./autocheck.sh -p <REPO_PATH> Check the repo in the path specified
./autocheck.sh -h Display this message
\n"
}
function get_cl_args () {
while getopts "p:h" arg; do
case $arg in
p)
CHECK_PATHS="$OPTARG"
;;
h)
help_msg
;;
*)
help_msg
;;
esac
done
}
function run () {
cd $CHECK_PATHS
local total_checks=5
local retval=0
# Check if is a Git repository
check_git
retval=$(($retval + $?))
# Check version
check_version
retval=$(($retval + $?))
# Check Notice
check_notice
retval=$(($retval + $?))
# Check Changelog
check_changelog
retval=$(($retval + $?))
# Check License
check_license
retval=$(($retval + $?))
cd - &>/dev/null
[[ $retval -eq $total_checks ]] && { log_ok "Finished without errors! :D"; } || { log_err "Docs not completed. Total errors: $(($total_checks - $retval))"; }
}
function main () {
[[ $# -eq 0 ]] && { help_msg; return; }
get_cl_args $@
run
}
main $@