-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpost-commit
executable file
·173 lines (149 loc) · 5.82 KB
/
post-commit
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/sh
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
# Ask for JIRA subdomain
parse_jira_subdomain() {
echo "Please enter your JIRA subdomain"
echo "Example: I would type 'thrillistmediagroup' because my JIRA url is: https://thrillistmediagroup.atlassian.net"
read jira_subdomain;
jira_subdomain=$(printf "$jira_subdomain")
old_umask=`umask`
umask 0077
echo "$jira_subdomain" > "$jira_subdomain_file"
umask $old_umask
}
# Ask for JIRA credentials/url and store the base64 encoded version of what the user enters
parse_jira_credentials() {
echo "Please enter your jira username (not your email): ";
read username;
echo "Please enter your jira password (nothing will show as you type): ";
stty -echo
read password;
stty echo
jira_credentials=$(printf "$username":"$password" | base64)
old_umask=`umask`
umask 0077
echo "$jira_credentials" > "$jira_credentials_file"
umask $old_umask
}
# Parse the git branch
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
# Parse the parameter for a JIRA ticket
parse_jira_ticket() {
echo $1 | grep -e '[A-Za-z]\+-[0-9]\+' -o
}
# function for checking the existence of jira credentials and jira subdomain
check_jira_data() {
# If the user does not have jira credentials stored, we'll need to ask them to enter credentials
if [ -e "$jira_credentials_file" ]; then
jira_credentials=$(cat "$jira_credentials_file")
else
parse_jira_credentials
fi
# If the user does not have jira subdomain stored, we'll need to ask them to enter the subdomain
if [ -e "$jira_subdomain_file" ]; then
jira_subdomain=$(cat "$jira_subdomain_file")
else
parse_jira_subdomain
fi
}
# Sum the total amount of time logged today by the user so we can display it later
get_total_time() {
# Decode the base64 encoded username:password
usernamepassword=$(echo "$1" | openssl enc -d -base64)
# Keep only the username (needed to for running some queries in JIRA)
username=${usernamepassword%:*}
# Export these variables so that they can be used in the ruby file
export JIRA_CREDENTIALS=$1
export JIRA_SUBDOMAIN=$2
export USERNAME=$username
# Use a ruby script to query JIRA for all the time that this user has already logged today
total_time=$(ruby `git rev-parse --git-dir`/hooks/timesheet-githook/sum_time.rb)
if [ "$total_time" = "unauthorized" ] || [ "$total_time" = "bad gateway" ]; then
exit
else
echo "$total_time"
fi
}
log_time() {
echo "[post-commit hook] Total Time Logged Today: $total_time"
read -p "[post-commit hook] How long did that take to do? " time
if [ "$time" = "" ]; then
exit
else
response=$(eval curl "-D- --write-out %{http_code} --silent --output /dev/null -X POST -d '{\"timeSpent\":\"$time\",\"comment\":\"$commit_message\"}' -H 'Authorization: Basic $1' -H 'Content-Type: application/json' https://$jira_subdomain.atlassian.net/rest/api/2/issue/\"$2\"/worklog")
if [[ $response =~ "201 Created" ]]; then
echo "[post-commit hook] $time successfully logged to $ticket!"
elif [[ $response =~ "401 Unauthorized" ]]; then
read -p "[post-commit hook] $time was not logged successfully to $ticket. Looks like your JIRA credentials are not proper. Would you like to update them? (y/n) " option
if [[ $option == "y" ]] || [ $option == "Y" ]; then
parse_jira_credentials
else
exit
fi
elif [[ $response =~ "400 Bad Request" ]]; then
read -p "[post-commit hook] $time was not logged successfully to $ticket. Check the format of your time. Try again? (y/n) " option
if [ $option == "y" ] || [ $option == "Y" ]; then
log_time $1 $2
else
exit
fi
elif [[ $response =~ "502 Bad Gateway" ]]; then
read -p "[post-commit hook] $time was not logged successfully to $ticket. Looks like your URL is not proper. Would you like to change it? (y/n) " option
if [ $option == "y" ] || [ $option == "Y" ]; then
parse_jira_subdomain
else
exit
fi
else
read -p "[post-commit hook] $time was not logged successfully to $ticket for an unknown reason. Try again? (y/n) " option
if [ $option == "y" ] || [ $option == "Y" ]; then
log_time $1 $2
else
exit
fi
fi
exit
fi
}
# Read JIRA credentials if they exist
jira_credentials_file="`git rev-parse --git-dir`/hooks/timesheet-githook/jira_credentials.txt"
# Read JIRA url if it exists
jira_subdomain_file="`git rev-parse --git-dir`/hooks/timesheet-githook/jira_subdomain.txt"
# Get the branch name
git_branch=`parse_git_branch`
# skip hook if this commit is a merge
# http://stackoverflow.com/a/3824122
num_git_parents=$(git cat-file -p HEAD | grep -E "parent [a-zA-Z0-9]{40,}" -c)
if [[ $num_git_parents -gt 1 ]]
then
exit
fi
# Find out if this is a smart commit (e.g. git commit -m "JQWE-123 #time 1h #comment fixed it" will log 1 hour already)
commit_message=$(git log -1 HEAD --pretty=%B)
if [[ "$commit_message" == *"#time"* ]]; then
is_smart_commit=true
else
is_smart_commit=false
fi
# Check for a ticket in the branch name
ticket_in_branch=`parse_jira_ticket $git_branch`
# If the user explicitly mentioned a ticket in their commit message, that ticket trumps the git branch name ticket
ticket_in_commit_message=`parse_jira_ticket $commit_message`
if [ "$ticket_in_commit_message" != "" ]; then
ticket=$ticket_in_commit_message
else
ticket=$ticket_in_branch
fi
# check that the jira credentials file and url exists
check_jira_data
# The total amount of time logged by the user today
total_time=`get_total_time $jira_credentials $jira_subdomain`
# If the branch has a ticket number and the commit is not a smart commit, prompt the user for time logging
if [ "$ticket" != "" ] && [ "$is_smart_commit" = false ]; then
log_time $jira_credentials $ticket $total_time
else
exit
fi