forked from Spaceman-Labs/asana-post-commit
-
Notifications
You must be signed in to change notification settings - Fork 6
/
post-commit
executable file
·106 lines (91 loc) · 3.28 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
#!/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]>
# -----------------------
# necessary configuration:
# git config --global user.asana-key "MY_ASANA_API_KEY" (http://app.asana.com/-/account_api)
#
# not necessary at the moment, but may be useful in future revisions of the script:
# git config --local user.asana-workspace "WORKSPACE_ID" (found in the URL)
# git config --local user.asana-project "PROJECT_ID" (found in the URL)
# -----------------------
apikey=$(git config user.asana-key)
# defaults
if [ $apikey == '' ] ; then exit 0; fi
# Find repo host
for gitRemote in `git remote -v`; do
repo=`echo $gitRemote | egrep -o 'https?://[^ ]+'`
domain=`echo $repo | awk -F \/ '{l=split($3,a,"."); print (a[l-1]=="com"?a[l-2] OFS:X) a[l-1] OFS a[l]}' OFS="."`
# Remove username if part of url
if [[ $domain == *"@"* ]]
then
domain=`echo $domain | awk -F"@" '{print $2}'`
fi
# Bitbucket or GitHub paths
if [ $domain == 'bitbucket.org' ]
then
repoPath=${repo/*bitbucket.org\//}
repoPath=${repoPath%.*}
commitUrl="https://bitbucket.org/$repoPath/commits/"
break;
elif [ $domain == 'github.com' ]
then
repoPath=${repo/*github.com\//}
repoPath=${repoPath%.*}
commitUrl="https://github.com/$repoPath/commit/"
break;
else
repoPath=$repo
commitUrl=''
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:"%s %b" -n1)
print_comment=$(git log --pretty=format:"Committed $commitUrl%h with message:
\"%s\"
to repository $repoPath" -n1)
# break the commit comment down into words
IFS=' ' read -a words <<< "$comment"
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]}")
# 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
# touch the stories we've referenced
for element in "${referenced[@]}"
do
curl -u ${apikey}: https://app.asana.com/api/1.0/tasks/${element}/stories \
-d "text=${print_comment}" > /dev/null 2>&1
done
# close the tasks we've fixed
for element in "${closed[@]}"
do
curl --request PUT -u ${apikey}: https://app.asana.com/api/1.0/tasks/${element} \
-d "completed=true" > /dev/null 2>&1
done