forked from rsmmr/git-notifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run-for-repository
executable file
·119 lines (98 loc) · 2.78 KB
/
run-for-repository
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
#!/bin/bash
# die on any error
set -e
config_read_file() {
(grep -E "^${2}=" -m 1 "${1}" 2>/dev/null || echo "VAR=__UNDEFINED__") | head -n 1 | cut -d '=' -f 2-;
}
config_get() {
val="$(config_read_file .git-notifier "${1}")";
if [ "${val}" = "__UNDEFINED__" ]; then
val="";
fi
printf -- "%s" "${val}";
}
REPOSITORY="$1";
CALL_DIRECTORY=$(pwd)
GITNOTIFIER_BASE=$(dirname $0)
BRANCH="$(config_get branch)";
if [ -z "${BRANCH}" ]; then
BRANCH="master"
fi
if [ -z "${REPOSITORY}" ]; then
echo Run with
echo " run-for-repository /path/to/repository"
exit
fi
cd $REPOSITORY
REPONAME=$(basename $(pwd))
if [ ! -e .git ]; then
echo Not a git repository
exit 1
fi
echo "Preparing to run for repository ${REPONAME}"
git fetch
git clean -dff .
git checkout ${BRANCH}
git reset --hard "$(git rev-parse --abbrev-ref --symbolic-full-name @{u})"
echo
git log -1
echo
if [ ! -f .git-notifier ]; then
echo "No .git-notifier file found in ${REPOSITORY}";
exit
fi
# myvar="$(config_get myvar)"; # how to just read a value without echoing
RECIPIENT_USER="$(config_get recipient)";
RECIPIENT_HOST="$(config_get recipient_domain)";
if [ -z "${RECIPIENT_USER}" ] || [ -z "${RECIPIENT_HOST}" ]; then
echo No recipient defined
exit 1
fi
SENDER_NAME=$(config_get sender)
if [ -z "${SENDER_NAME}" ]; then
SENDER_NAME=Jenkins
fi
REPLYTO_USER="$(config_get replyto)";
REPLYTO_HOST="$(config_get replyto_domain)";
if [ -z "${REPLYTO_USER}" ] || [ -z "${REPLYTO_HOST}" ]; then
REPLYTO_USER="${RECIPIENT_USER}"
REPLYTO_HOST="${RECIPIENT_HOST}"
fi
LINK="$(config_get link)";
if [ -z "${LINK}" ]; then
LINK_PARAMETER=""
else
LINK_PARAMETER=--repouri="${LINK}"
fi
COMMITLINK="$(config_get commit_link)";
if [ -z "${COMMITLINK}" ]; then
COMMITLINK_PARAMETER=""
else
COMMITLINK_PARAMETER=--link="${COMMITLINK}"
fi
COMMITSELECT=""
if [ ! -z "$OLDCOMMITID" ]; then
echo
echo Checking for previous commit ID ${OLDCOMMITID}
set +e
git rev-parse --quiet --verify ${OLDCOMMITID}^{commit} && {
COMMITSELECT="--manual ${OLDCOMMITID}..$(git rev-parse HEAD)"
echo Commit found! Selecting $COMMITSELECT
}
set -e
fi
touch "${CALL_DIRECTORY}/log-${REPONAME}"
# terminate leftover 'tail' processes on exit
trap 'kill $(jobs -p)' SIGINT SIGTERM EXIT
tail -f -n 0 "${CALL_DIRECTORY}/log-${REPONAME}" &
echo "Notifying for repository ${REPONAME}"
${GITNOTIFIER_BASE}/git-notifier \
--mailinglist "${RECIPIENT_USER}@${RECIPIENT_HOST}" \
--log "${CALL_DIRECTORY}/log-${REPONAME}" \
--sender="${SENDER_NAME} <[email protected]>" \
--replyto="${REPLYTO_USER}@${REPLYTO_HOST}" \
${LINK_PARAMETER} \
${COMMITLINK_PARAMETER} \
${COMMITSELECT}
${GITNOTIFIER_BASE}/git-notifier --updateonly \
--log "${CALL_DIRECTORY}/log-${REPONAME}"