-
Notifications
You must be signed in to change notification settings - Fork 30
/
post-commit
executable file
·144 lines (128 loc) · 4.93 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
#!/bin/bash
# modified from http://brunohq.com/journal/speed-project-git-hook-for-asana/
#
# Added Bitbucket and GitHub links to Asana commits
# modified by Richard Sumilang <[email protected]>
# modified by Andrew Bennett <[email protected]>
# -----------------------
# necessary configuration:
# git config --global user.asana-token "MY_ASANA_PERSONAL_ACCESS_TOKEN" (http://app.asana.com/-/account_api)
# git config --global user.display-branch-name-in-comment "true/false" # (This is config is optional, defaults to false)
# -----------------------
access_token=$(git config user.asana-token)
display_branch_name=$(git config user.display-branch-name-in-comment)
# defaults
if [ $access_token == "" ]; then
echo "Please Set Your Asana Token git config --global user.asana-token \"MY_ASANA_PERSONAL_ACCESS_TOKEN\" (http://app.asana.com/-/account_api)" >&2
exit 1
fi
if [ "$display_branch_name" == "" ]; then
display_branch_name=false;
fi
# Find repository host
for repository in $(git remote -v | awk '{ print $2 }'); do
scheme="$(echo $repository | grep :// | sed -e's,^\(.*://\).*,\1,g')"
url="$(echo ${repository/$scheme/})"
user="$(echo $url | grep @ | cut -d@ -f1)"
if [[ $scheme == "" ]]; then
host="$(echo ${url/$user@/} | cut -d: -f1)"
path="$(echo $url | grep : | cut -d: -f2-)"
else
host="$(echo ${url/$user@/} | cut -d/ -f1)"
path="$(echo $url | grep / | cut -d/ -f2-)"
fi
project="$(echo ${path%.*})"
# Bitbucket or GitHub
if [[ $host == "bitbucket.org" ]]; then
commit_url="https://bitbucket.org/$project/commits/"
break;
elif [[ $host == "github.com" ]]; then
commit_url="https://github.com/$project/commit/"
break;
else
commit_url=""
fi
done
# hold the closed ticket numbers
declare -a closed
# hold the ticket numbers that are not closed, just touched
declare -a referenced
# track whether we're currently closing tickets or just referencing them
closes='NO'
# regex pattern to recognize a story number
taskid_pattern='#([0-9]*)'
# regex pattern to recognize a "closing this ticket" word
closes_pattern='([Ff]ix|[Cc]lose|[Cc]losing)'
# regex pattern to recognize an "and" word (eg "fixes #1, #2, and #3")
and_pattern='([Aa]nd|&)'
# get the checkin comment for parsing
comment=$(git log --pretty=format:"%B" -n1 | tr '\n' ' ')
branch_message=""
if [ "$display_branch_name" == "true" ]; then
branch=$(git rev-parse --abbrev-ref --quiet HEAD)
branch_message=" in branch $branch"
fi
if [[ $commit_url == "" ]]; then
print_comment=$(git log -n1 --pretty=format:"<body>Committed %h$branch_message on $repository:%n<code>%B</code></body>")
else
print_comment=$(git log -n1 --pretty=format:"<body>Committed$branch_message: $commit_url%H%n<code>%B</code></body>")
fi
# break the commit comment down into words
IFS=' ' read -a words <<< "$comment"
found_task_id=false
for element in "${words[@]}"
do
# if we have a task id, save it to the appropriate array
if [[ $element =~ $taskid_pattern ]]; then
if [ "${closes}" == "YES" ]; then
closed=("${closed[@]}" "${BASH_REMATCH[1]}")
fi
referenced=("${referenced[@]}" "${BASH_REMATCH[1]}")
found_task_id=true
# or else if we have a "closes" word, set the tracking bool accordingly
elif [[ $element =~ $closes_pattern ]]; then
closes='YES'
# and if we don't, set us back to referencing
# (if we're an "and", don't change any state)
elif [[ ! $element =~ $and_pattern ]]; then
closes='NO'
fi
done
# if task id not found in comment, then look for it in the branch name
if [ "$found_task_id" = false ]; then
# get task id from branch name. Then send a comment request if task_id exists.
task_id=$(git branch | grep '*' | sed 's/^..//' | grep -o '#.*' | sed 's/^.//')
if [ "$task_id" ]; then
curl \
-H "Authorization: Bearer ${access_token}" \
-X POST \
--data-urlencode "html_text=${print_comment}" \
"https://app.asana.com/api/1.0/tasks/${task_id}/stories" \
> /dev/null 2>&1
fi
fi
# touch the stories we've referenced
for element in "${referenced[@]}"; do
http_code=$(curl \
-sw '%{http_code}' \
-H "Authorization: Bearer ${access_token}" \
-X POST \
--data-urlencode "html_text=${print_comment}" \
"https://app.asana.com/api/1.0/tasks/${element}/stories" \
-o /dev/null 2>&1)
if [ "$http_code" = "201" ]; then
echo "Successfully added comment for task $element"
else
echo "Error for task $element. HTTP return code:$http_code"
fi
done
# close the tasks we've fixed
for element in "${closed[@]}"
do
curl \
-H "Authorization: Bearer ${access_token}" \
-X PUT \
--data-urlencode "completed=true" \
"https://app.asana.com/api/1.0/tasks/${element}" \
> /dev/null 2>&1
done