forked from apache/mesos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-review.sh
executable file
·121 lines (94 loc) · 2.68 KB
/
apply-review.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
#!/usr/bin/env bash
# Provides a tool to "apply" a review from Review Board or a
# pull request from Github.
# Use 'atexit' for cleanup.
. $(dirname ${0})/atexit.sh
# Use colors for errors.
. $(dirname ${0})/colors.sh
JSONURL=$(dirname ${0})/jsonurl.py
GITHUB_URL="https://github.com/apache/mesos/pull"
REVIEWBOARD_URL="https://reviews.apache.org/r"
function usage {
cat <<EOF
Apache Mesos apply patch tool.
Usage: $0 [-h] [-n] [-r | -g] <ID Number>
-h Print this help message and exit
-n Don't amend the commit message
-r Apply a patch from Review Board (default)
-g Apply a patch from Github
EOF
}
AMEND=true
REVIEW_LOCATION='reviewboard'
while getopts ":nhrg" opt; do
case $opt in
n)
AMEND=false
;;
r)
REVIEW_LOCATION='reviewboard'
;;
g)
REVIEW_LOCATION='github'
;;
h)
usage
exit 0
;;
*)
echo "Unknown option: -$OPTARG"
usage
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if test ${#} -ne 1; then
usage
exit 1
fi
REVIEW=${1}
if [[ "${REVIEW_LOCATION}" == "github" ]]; then
DIFF_URL="${GITHUB_URL}/${REVIEW}.patch"
else
DIFF_URL="${REVIEWBOARD_URL}/${REVIEW}/diff/raw/"
fi
atexit "rm -f ${REVIEW}.patch"
wget --no-check-certificate --no-verbose -O ${REVIEW}.patch ${DIFF_URL} || \
{ echo "${RED}Failed to download patch${NORMAL}"; exit 1; }
git apply --index ${REVIEW}.patch || \
{ echo "${RED}Failed to apply patch${NORMAL}"; exit 1; }
if [[ "${REVIEW_LOCATION}" == "reviewboard" ]]; then
API_URL="https://reviews.apache.org/api/review-requests/${REVIEW}/"
SUMMARY=$(${JSONURL} ${API_URL} review_request summary)
DESCRIPTION=$(${JSONURL} ${API_URL} review_request description)
USERNAME=$(${JSONURL} ${API_URL} review_request links submitter title)
USER_URL="https://reviews.apache.org/api/users/${USERNAME}/"
AUTHOR_NAME=$(${JSONURL} ${USER_URL} user fullname)
AUTHOR_EMAIL=$(${JSONURL} ${USER_URL} user email)
AUTHOR="${AUTHOR_NAME} <${AUTHOR_EMAIL}>"
REVIEW_URL="${REVIEWBOARD_URL}/${REVIEW}"
elif [[ "${REVIEW_LOCATION}" == "github" ]]; then
API_URL="https://api.github.com/repos/apache/mesos/pulls/${REVIEW}"
SUMMARY=$(${JSONURL} ${API_URL} title)
DESCRIPTION=$(${JSONURL} ${API_URL} body)
AUTHOR=$(head -2 ${REVIEW}.patch | grep "From: " | cut -d ' ' -f3-)
REVIEW_URL="${GITHUB_URL}/${REVIEW}"
REVIEW_DETAILS=$(cat <<__EOF__
This closes: #${REVIEW}
__EOF__
)
fi
MESSAGE=$(cat <<__EOF__
${SUMMARY}
${DESCRIPTION}
${REVIEW_DETAILS}
Review: ${REVIEW_URL}
__EOF__
)
echo "Successfully applied: ${MESSAGE}"
git commit --author="${AUTHOR}" -am "${MESSAGE}" || \
{ echo "${RED}Failed to commit patch${NORMAL}"; exit 1; }
if $AMEND; then
git commit --amend
fi