-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-hook-post-receive
executable file
·120 lines (99 loc) · 2.95 KB
/
git-hook-post-receive
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
#!/bin/bash
# http://blog.popstas.ru/blog/2016/03/02/git-gitlab-planfix-integration/
GIT_PLANFIX_USERS_FILE="/usr/local/etc/git_planfix_users.txt"
GITLAB_BRANCH_NAME="gitlab"
GITLAB_PROTOCOL="https"
PLANFIX_ANALYTIC_NAME="Поминутная работа программиста"
get_task_url() {
msg="$1"
echo "$msg" | grep -oE "https:\/\/.*\.planfix\.ru\/task\/[0-9]+" | head -n1
}
get_planfix_email() {
msg="$1"
task_url="$(get_task_url "$msg")"
if [ -n "$task_url" ]; then
domain=$(echo "$task_url" | tail -c +9 | cut -d'/' -f1)
task_id=$(echo "$task_url" | grep -oE "[0-9]+$")
task_email="task+${task_id}@${domain}"
#domain=$(echo "$task_url" | head -c8 | cut-grep -oE "
fi
echo $task_email
}
get_gitlab_url() {
is_gitlab="$(git remote show | grep -c "$GITLAB_BRANCH_NAME" )"
if [ "$is_gitlab" = 1 ]; then
gitlab_url="$(git config remote.${GITLAB_BRANCH_NAME}.url \
| sed "s/^ssh/$GITLAB_PROTOCOL/g" \
| sed 's/git@//g' \
| sed 's/:[0-9][0-9]*//g' \
| sed 's/\.git$//g' )"
echo "$gitlab_url"
fi
}
send_planfix_email() {
from="$1"
to="$2"
body="$3"
echo "$body" | mail -s "@commit @nonotify" -r "$from" \
"$to"
}
get_planfix_name_by_email() {
email="$1"
if [ -e "$GIT_PLANFIX_USERS_FILE" ]; then
# grep email with tab, get text after tab
grep "$email"$'\t' $GIT_PLANFIX_USERS_FILE | cut -d$'\t' -f2
fi
}
commit_actions() {
sha="$1"
msg=$(git log --format=%B -n 1 "$sha")
from=$(git log --format=%ae -n 1 "$sha")
to=$(get_planfix_email "$msg")
gitlab_url=$(get_gitlab_url)
gitlab_commit_url="${gitlab_url}/commit/${sha}"
author_name=$(get_planfix_name_by_email "$from")
# add footer delimeter
visible_msg="$gitlab_commit_url"
delimeter_regex="\n\-\+\-\+\-\n" # -+-+-
task_url="$(get_task_url "$msg")"
regex="${task_url}"
if [ "$(echo "$msg" | grep -c "time:")" -gt 0 ]; then
regex="time:"
fi
msg="$(echo "$msg" | sed "s|${regex}|${visible_msg}${delimeter_regex}${regex}|g")"
if [ -n "$to" ]; then
body="$msg
commit $(echo "$sha" | head -c 7)"
if [ -n "$PLANFIX_ANALYTIC_NAME" ] && [ -n "$author_name" ]; then
body="$body
Вид работы: $PLANFIX_ANALYTIC_NAME
Email: $from
Дата: $(date '+%Y-%m-%d')
Автор: $author_name"
fi
send_planfix_email "$from" "$to" "$body"
fi
}
main() {
# clean push onto dirty repo
which gitclean > /dev/null && gitclean || true
# walk through received commits
# http://stackoverflow.com/questions/8263186/git-post-receive-hook-that-grabs-commit-messages-and-posts-back-to-url
while read oval nval ref ; do
if expr "$ref" : "^refs/heads/"; then
if expr "$oval" : '0*$' >/dev/null
then
revspec=$nval
else
revspec=$oval..$nval
fi
other_branches=$(git for-each-ref --format='%(refname)' refs/heads/ | grep -F -v $ref)
for revision in `git rev-parse --not $other_branches | git rev-list --stdin $revspec`; do
for sha in $(git log --format=%H $revision~1..$revision); do
commit_actions "$sha"
done
done
fi
done
}
main "$@"