-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathci_check_commit.sh
More file actions
85 lines (76 loc) · 2.45 KB
/
ci_check_commit.sh
File metadata and controls
85 lines (76 loc) · 2.45 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
#!/bin/bash
set -e
scan_code_script="cobra/cobra.py -f json -o /tmp/report.json -t "
ignore_files=(sh crt key json toml)
commit_limit=6
skip_check_words="sync code"
LOG_ERROR() {
content=${1}
echo -e "\033[31m${content}\033[0m"
}
LOG_INFO() {
content=${1}
echo -e "\033[32m${content}\033[0m"
}
should_ignore() {
local file=${1}
for ignore in ${ignore_files[*]}; do
if echo ${file} | grep ${ignore} &>/dev/null; then
echo "ignore ${file} ${ignore}"
return 0
fi
done
return 1
}
scan_code() {
# Redirect output to stderr.
exec 1>&2
for file in $(git diff-index --name-status HEAD^ | awk '{print $2}'); do
if should_ignore ${file}; then continue; fi
if [ ! -f ${file} ]; then continue; fi
LOG_INFO "check file ${file}"
python ${scan_code_script} $file
trigger_rules=$(jq -r '.' /tmp/report.json | grep 'trigger_rules' | awk '{print $2}' | sed 's/,//g')
echo "trigger_rules is ${trigger_rules}"
rm /tmp/report.json
if [ ${trigger_rules} -ne 0 ]; then
echo "######### ERROR: Scan code failed, please adjust them before commit"
exit 1
fi
done
}
install_cobra() {
git clone https://github.com/WhaleShark-Team/cobra.git
pip install -r cobra/requirements.txt
cp cobra/config.template cobra/config
}
check_commit_message()
{
if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
local skip=$(curl -s https://api.github.com/repos/FISCO-BCOS/java-sdk/pulls/${TRAVIS_PULL_REQUEST} | grep "title\"" | grep "${skip_check_words}")
if [ ! -z "${skip}" ]; then
LOG_INFO "sync code PR, skip PR limit check!"
exit 0
else
LOG_INFO "PR-${TRAVIS_PULL_REQUEST}, checking PR limit..."
fi
fi
local commits=$(git rev-list --count HEAD^..HEAD)
if [ ${commit_limit} -lt ${commits} ]; then
LOG_ERROR "${commits} commits, limit is ${commit_limit}"
exit 1
fi
local unique_commit=$(git log --format=%s HEAD^..HEAD | sort -u | wc -l)
if [ ${unique_commit} -ne ${commits} ]; then
LOG_ERROR "${commits} != ${unique_commit}, please make commit message unique!"
exit 1
fi
local merges=$(git log --format=%s HEAD^..HEAD | grep -i merge | wc -l)
if [ ${merges} -gt 2 ]; then
LOG_ERROR "PR contain merge : ${merges}, Please rebase!"
exit 1
fi
}
check_commit_message
install_cobra
scan_code