-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpre-push
116 lines (91 loc) · 4.24 KB
/
pre-push
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
#!/bin/sh
# This git hook script is to create a code review entry on Crucible. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local sha1> <remote ref> <remote sha1>
#
crucible_url="#HOST_PORT#"
project_key="#PROJECT_KEY#"
username="#USERNAME#"
password_base64="#PASSWORD_IN_BASE64#"
reviewers="#COMMA_SEPERATED_USERNAMES#"
remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
# Find local branch name and git user
local_branch=`echo ${local_ref:11}`
git_author=`git config user.name`
# Decode the password
password=`echo $password_base64 | base64 --decode`
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits
# Find the parent branch
vbc_col=$(( $(git show-branch | grep '^[^\[]*\*' | head -1 | cut -d* -f1 | wc -c) - 1 ))
swimming_lane_start_row=$(( $(git show-branch | grep -n "^[\-]*$" | cut -d: -f1) + 1 ))
parent_branch=`git show-branch | tail -n +$swimming_lane_start_row | grep -v "^[^\[]*\[$local_branch" | grep "^.\{$vbc_col\}[^ ]" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'`
range="$parent_branch..$local_branch"
# Get the diff from git
commit_diff=`git diff -U1000 "$range"`
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
# Create a temp branch to extract changes of the user
temp_branch="temp_$remote_sha"
temp_branch+="_$RANDOM"
git_operation=`git checkout -b "$temp_branch" "$remote_sha"`
git_operation=`git cherry-pick $(git log --format=%H --author="$git_author" "$range")`
# Get the diff from git
commit_diff=`git diff -U1000 "$remote_sha" "$temp_branch"`
# Clean up temp branch
git_operation=`git checkout "$local_branch"`
git_operation=`git branch -D "$temp_branch"`
fi
if [ ! -z "${commit_diff// }" ]
then
commit_diff='<![CDATA['"$commit_diff"']]>'
# Get commit messages for the user
last_commit_message=`git log --author="$git_author" --format=%f -n 1 "$range"`
all_commit_messages=`git log --author="$git_author" --format=%f "$range"`
# Call Crucible for create a new review
review_data='<?xml version="1.0" encoding="UTF-8" standalone="yes"?><createReview><reviewData><allowReviewersToJoin>true</allowReviewersToJoin><author><userName>'"$username"'</userName></author><creator><userName>'"$username"'</userName></creator><description>'"$all_commit_messages"'</description><moderator><userName>'"$username"'</userName></moderator><name>'"$last_commit_message"'</name><projectKey>'"$project_key"'</projectKey><state>Review</state><summary>'"$last_commit_message"'</summary><type>REVIEW</type></reviewData><patch>'"$commit_diff"'</patch></createReview>'
create_review_response=`curl -i --user "$username:$password" -H "Content-Type: application/xml" -X POST --data "$review_data" "$crucible_url/rest-service/reviews-v1"`
# Extract location of the review from response
location_header=`echo "$create_review_response" | grep Location:`
review_url=`echo ${location_header:9}`
if [ ! -z "${reviewers// }" ]
then
# Add reviewers to the review
add_reviewers_response=`curl --user "$username:$password" -H "Content-Type: application/json" -X POST "$review_url/reviewers" --data "$reviewers"`
# Send reminders to reviewers
recipient_data=""
IFS=',' read -ra splits <<< "$reviewers"
for i in "${splits[@]}"; do
recipient_data+="recipients=$i&"
done
recipient_data="${recipient_data: : -1}"
remind_reviewers_response=`curl --user "$username:$password" -X POST "$review_url/remind" --data "$recipient_data"`
fi
echo >&2 "Successfully created a new Code Review for the changes. Code Review URL = $review_url"
fi
fi
done
exit 0