forked from triplea-game/triplea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_checkstyle_thresholds
executable file
·99 lines (89 loc) · 3.11 KB
/
update_checkstyle_thresholds
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
#!/bin/bash
#
# This script updates the Checkstyle thresholds based on the most recent
# Checkstyle run (therefore, this script must be run after the Gradle "check"
# task has run). If the thresholds have changed, the updated thresholds are
# pushed back to the remote repository.
#
# NOTE: There is no danger in INCREASING the thresholds because the build will
# have already failed at this point if the violation counts have increased.
#
readonly FALSE=1
readonly GRADLE_PROPERTIES=gradle.properties
readonly TRUE=0
main() {
should_skip_build && exit 0
local -r old_hash=$(get_gradle_properties_hash)
update_local_thresholds
has_local_thresholds_changed "$old_hash" && update_remote_thresholds "$old_hash"
}
should_skip_build() {
echo "Checking if Checkstyle thresholds should be updated for this build..."
local skip=$FALSE
if [[ "$TRAVIS_PULL_REQUEST" != "false" ]]; then
echo " ...skipping because pull requests are not permitted."
skip=$TRUE
fi
if [[ "$TRAVIS_BRANCH" != "master" ]]; then
echo " ...skipping because this branch is not permitted."
skip=$TRUE
fi
if [[ "$TRAVIS_REPO_SLUG" != "triplea-game/triplea" ]]; then
echo " ...skipping because this repo is not permitted."
skip=$TRUE
fi
return $skip
}
get_gradle_properties_hash() {
git hash-object "$GRADLE_PROPERTIES"
}
update_local_thresholds() {
echo "Updating Checkstyle thresholds..."
update_threshold main.xml Main
update_threshold test.xml Test
update_threshold integTest.xml IntegTest
}
update_threshold() {
local -r report_file=$1
local -r violation_property_name=$2
local -r checkstyle_warning_pattern='<error [^>]*severity="warning"'
local -r checkstyle_reports_dir=build/reports/checkstyle
local -r violation_count=$(grep -c "$checkstyle_warning_pattern" $checkstyle_reports_dir/$report_file)
local -r violation_property="checkstyle${violation_property_name}MaxWarnings"
sed -i -r "s/$violation_property=[[:digit:]]+/$violation_property=$violation_count/" $GRADLE_PROPERTIES
}
has_local_thresholds_changed() {
echo "Checking for changes to '$GRADLE_PROPERTIES'..."
local -r old_hash=$1
local -r new_hash=$(get_gradle_properties_hash)
if [[ "$old_hash" != "$new_hash" ]]; then
echo " ...changes detected."
return $TRUE
else
echo " ...no changes detected."
return $FALSE
fi
}
update_remote_thresholds() {
echo "Pushing '$GRADLE_PROPERTIES' changes to '$TRAVIS_REPO_SLUG:$TRAVIS_BRANCH'..."
local -r old_hash=$1
local -r update_request="{ \
\"message\": \"Bot: Update Checkstyle thresholds after build $TRAVIS_BUILD_NUMBER\", \
\"committer\": { \
\"name\": \"tripleabuilderbot\", \
\"email\": \"[email protected]\" \
}, \
\"branch\": \"$TRAVIS_BRANCH\", \
\"content\": \"$(base64 -w 0 $GRADLE_PROPERTIES)\", \
\"sha\": \"$old_hash\" \
}"
curl \
--silent \
--show-error \
-X PUT \
--header "Accept: application/vnd.github.v3+json" \
--data "$update_request" \
--user ":$GITHUB_PERSONAL_ACCESS_TOKEN_FOR_TRAVIS" \
"https://api.github.com/repos/$TRAVIS_REPO_SLUG/contents/$GRADLE_PROPERTIES"
}
main