-
Notifications
You must be signed in to change notification settings - Fork 3
/
pr-response.sh
executable file
·146 lines (128 loc) · 5.61 KB
/
pr-response.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
#!/bin/bash
function execute_build() {
COMMIT=${1}
REPO=${2}
USERNAME_JENKINS=${3}
PASSWORD_JENKINS=${4}
JOB_NAME=${5}
BUILD_NUMBER=${6}
USERNAME_BITBUCKET=${7}
PASSWORD_BITBUCKET=${8}
BUCKET_API_V1_URL=${9}
OWNER=${10}
PULL_REQUEST=${11}
BUILD_URL=${12}
JENKINS_URL=${13}
echo "build will be executed..."
git checkout "$COMMIT"
echo "executing ci.sh..."
sleep 5
cd $REPO
./ci.sh
if [ $? -eq 0 ]; then
JENKINS_RESULT="SUCCESS"
else
JENKINS_RESULT="FAILED"
fi
collect_coverage_percentage
language_comment
echo -e "bitbucket comment registered"
}
function collect_state() {
PRSTATE=`curl --silent -u ${USERNAME_BITBUCKET}:${PASSWORD_BITBUCKET} https://${BUCKET_API_V2_URL}/${OWNER}/${REPO}/pullrequests?state=open | python -mjson.tool | jq '.values[] | select(.state == "OPEN") | .state' | sed 's/"//g'`
}
function collect_comment() {
COMMENT=`curl --silent -u ${USERNAME_BITBUCKET}:${PASSWORD_BITBUCKET} https://${BUCKET_API_V2_URL}/${OWNER}/${REPO}/pullrequests/${PULL_REQUEST}/comments | python -mjson.tool | jq '.values[] | .content | .raw' | sed 's/"//g' | egrep -m 1 -e 'SUCCESS|FAILED'`
}
function content_comment() {
CONTENT_COMMENT=`curl --silent -u ${USERNAME_BITBUCKET}:${PASSWORD_BITBUCKET} https://${BUCKET_API_V2_URL}/${OWNER}/${REPO}/pullrequests/${PULL_REQUEST}/comments | python -mjson.tool | jq '.values[] | .content | .raw' | sed 's/"//g'`
}
function post_comment() {
curl --silent -u ${USERNAME_BITBUCKET}:${PASSWORD_BITBUCKET} https://${BUCKET_API_V1_URL}/${OWNER}/${REPO}/pullrequests/${PULL_REQUEST}/comments --data "content=${1}"
}
function check_null_percentage() {
if [ "${PERCENTAGE}" == " %" ]; then
PERCENTAGE=''
fi
}
function collect_coverage_percentage() {
case ${LANGUAGE} in
python)
collect_python_coverage_percentage
;;
ruby)
collect_ruby_coverage_percentage
;;
*)
collect_unknown_language_coverage_percentage
;;
esac
}
function collect_python_coverage_percentage() {
TEMP=`curl --silent http://${USERNAME_JENKINS}:${PASSWORD_JENKINS}@${JENKINS_URL}/job/${JOB_NAME}/ws/${REPO}/coverage.xml | grep -m 1 line-rate | awk '{ print $3 }' | sed 's/.*line-rate="\(.*\)".*/\1/'`
PERCENTAGE=" `echo "scale=2; $TEMP*100" | bc | cut -c1-5`%"
check_null_percentage
}
function collect_ruby_coverage_percentage() {
PERCENTAGE=" `curl --silent http://${USERNAME_JENKINS}:${PASSWORD_JENKINS}@${JENKINS_URL}/job/${JOB_NAME}/ws/${REPO}/coverage/.last_run.json | python -mjson.tool | grep -m 2 covered_percent | awk '{ print $2 }'`%"
check_null_percentage
}
function collect_unknown_language_coverage_percentage() {
PERCENTAGE="Warning: Unknown Language. Could not get Code Coverage."
}
function language_comment() {
case ${LANGUAGE} in
python)
language_python_comment
;;
ruby)
language_ruby_comment
;;
*)
comment_unknown_language
;;
esac
}
function language_python_comment() {
if [ "$JENKINS_RESULT" == 'SUCCESS' ]; then
post_comment "SUCCESS on commit: ${COMMIT}. [Console Output](${BUILD_URL}console). [Code Coverage](http://${JENKINS_URL}/job/${JOB_NAME}/ws/${REPO}/htmlcov/index.html)${PERCENTAGE}. [Violations](http://${JENKINS_URL}/job/${JOB_NAME}/violations/)"
else
post_comment "FAILED on commit: ${COMMIT}. [Console Output](${BUILD_URL}console). [Code Coverage](http://${JENKINS_URL}/job/${JOB_NAME}/ws/${REPO}/htmlcov/index.html)${PERCENTAGE}. [Violations](http://${JENKINS_URL}/job/${JOB_NAME}/violations/)"
fi
}
function language_python_comment() {
if [ "$JENKINS_RESULT" == 'SUCCESS' ]; then
post_comment "SUCCESS on commit: ${COMMIT}. [Console Output](${BUILD_URL}console). [Code Coverage](http://${JENKINS_URL}/job/${JOB_NAME}/ws/${REPO}/coverage/index.html)${PERCENTAGE}. [Violations](http://${JENKINS_URL}/job/${JOB_NAME}/violations/)"
else
post_comment "FAILED on commit: ${COMMIT}. [Console Output](${BUILD_URL}console). [Code Coverage](http://${JENKINS_URL}/job/${JOB_NAME}/ws/${REPO}/coverage/index.html)${PERCENTAGE}. [Violations](http://${JENKINS_URL}/job/${JOB_NAME}/violations/)"
fi
}
function comment_unknown_language() {
echo "Unknown Language"
}
if [ ! -d "${REPO}" ]; then
git clone git@bitbucket.org:${OWNER}/${REPO}.git
fi
cd ${REPO}
git fetch origin
collect_state
if [[ "${PRSTATE}" =~ ^OPEN.*$ ]]; then
curl --silent -u ${USERNAME_BITBUCKET}:${PASSWORD_BITBUCKET} https://${BUCKET_API_V2_URL}/${OWNER}/${REPO}/pullrequests?state=open | python -mjson.tool | jq '.values[] | select(.state == "OPEN") | .id' | while read PULL_REQUEST; do
collect_comment
curl --silent -u ${USERNAME_BITBUCKET}:${PASSWORD_BITBUCKET} https://${BUCKET_API_V2_URL}/${OWNER}/${REPO}/pullrequests/${PULL_REQUEST} | python -mjson.tool | jq '.source[] | .hash' | grep -v null | sed 's/"//g' | while read COMMIT; do
if [[ ! -z "${COMMENT}" ]] && [[ ! "${COMMENT}" =~ "SUCCESS|FAILED" ]]; then
content_comment
if [[ "${CONTENT_COMMENT}" == *"${COMMIT}"* ]]; then
echo "Jenkins already evaluated this Pull Request"
else
execute_build "${COMMIT}" "${REPO}" "${USERNAME_JENKINS}" "${PASSWORD_JENKINS}" "${JOB_NAME}" "${BUILD_NUMBER}" "${USERNAME_BITBUCKET}" "${PASSWORD_BITBUCKET}" "${BUCKET_API_V1_URL}" "${OWNER}" "${PULL_REQUEST}" "${BUILD_URL}"
fi
else
execute_build "${COMMIT}" "${REPO}" "${USERNAME_JENKINS}" "${PASSWORD_JENKINS}" "${JOB_NAME}" "${BUILD_NUMBER}" "${USERNAME_BITBUCKET}" "${PASSWORD_BITBUCKET}" "${BUCKET_API_V1_URL}" "${OWNER}" "${PULL_REQUEST}" "${BUILD_URL}"
fi
done
done
else
echo "There is no open Pull Request"
exit 0
fi