-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-commit
executable file
·66 lines (59 loc) · 1.45 KB
/
pre-commit
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
#Will run from the root directory
#Only works with gradle, not hard to adjust though
#cat ./pre-commit >> .git/hooks/pre-commit
#Add the following plugin to your build.gradle:
#plugins {
# id "jacoco"
#}
#Add the following config to the same build.gradle:
#jacocoTestReport {
# reports {
# csv.required = true
# }
#}
java_module_to_test=":backend"
path_to_jacoco_report="backend/build/reports/jacoco/test/jacocoTestReport.csv"
badge_name="Java_Test_Coverage"
./gradlew $java_module_to_test:test $java_module_to_test:jacocoTestReport -q
missed=0
covered=0
while read -r p; do
missed=$((missed + $(echo "$p" | cut -d "," -f 4)))
covered=$((covered + $(echo "$p" | cut -d "," -f 5)))
done <<< "$(cat $path_to_jacoco_report | tail -n +2)"
total=$((covered + missed))
percent=$((covered * 100 / total))
colour="brightgreen"
if ((percent < 50))
then
colour="red"
elif ((percent < 60))
then
colour="orange"
elif ((percent < 70))
then
colour="yellow"
elif ((percent < 80))
then
colour="yellowgreen"
elif ((percent < 90))
then
colour="green"
elif ((percent < 100))
then
colour="brightgreen"
else
colour="blueviolet"
fi
badge=\\\!\[\]\(https:\\/\\/img.shields.io\\/badge\\/$badge_name-"$percent"%25-"$colour"\)
if (grep -q $badge_name "./README.md")
then
badgeLineNum=$(grep -n $badge_name "./README.md" | cut -d : -f 1)
sed -i "" "$badgeLineNum s/.*/$badge/" ./README.md
else
sed -i "" '1i\
'"$badge"'\
''\
' ./README.md
fi
git add ./README.md