blob: 20c0a9667e5f5e3293ac2a55c5e6ef3896d654e5 (
plain)
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
|
#!/bin/bash
# Copyright 2012-2014 Holger Levsen <holger@layer-acht.org>
# released under the GPLv=2
#
# called by ~jenkins/.procmailrc
# to turn jenkins email notifications into irc announcements with kgb
# see http://kgb.alioth.debian.org/
#
LOGFILE=/var/log/jenkins/email.log
debug123() {
if $DEBUG ; then
echo "Debug: $1 $2 $3" >> $LOGFILE
fi
}
#
# parse email headers to check if they come from jenkins
#
DEBUG=false
HEADER=true
VALID_MAIL=false
MY_LINE=""
while read line ; do
if [ "$HEADER" = "true" ] ; then
# check if email header ends
if [[ $line =~ ^$ ]] ; then
HEADER=false
fi
# valid From: line?
if [[ $line =~ ^(From: jenkins@jenkins.debian.net) ]] ; then
VALID_MAIL=true
fi
# catch Subject (to send to IRC later)
if [[ $line =~ ^(Subject: .*) ]] ; then
SUBJECT=${line:9}
# the email-ext plugin sometimes sends multi line subjects..
NEXT=read line
if [ "${NEXT:0:1}" = " " ] ; then
SUBJECT="${SUBJECT}${NEXT}"
fi
fi
# determine the channel to send notifications to
# by parsing the To: line
if [[ $line =~ ^(To: .*) ]] ; then
echo $line >> $LOGFILE
CHANNEL=$(echo $line | cut -d "+" -f2| cut -d "@" -f1)
echo "CHANNEL = $CHANNEL" >> $LOGFILE
fi
# check if it's a valid jenkins job
if [[ $line =~ ^(X-Jenkins-Job: .*) ]] ; then
JENKINS_JOB=${line:15}
fi
fi
# catch first line of email body (to send to IRC later)
if [ "$HEADER" = "false" ] && [ -z "$MY_LINE" ] ; then
MY_LINE=$line
debug123 "#1" MY_LINE $MY_LINE
# if this is a multipart email it comes from the email extension plugin
if [ "${line:0:7}" = "------=" ] || [ "${line:0:9}" = "Content-T" ] ; then
debug123 "#2" line $line
MY_LINE=""
else
debug123 "#3" line $line
MY_LINE=$(echo $line | tr -d \< | tr -d \> | cut -d " " -f1-2)
debug123 "#4" MY_LINE $MY_LINE
fi
fi
done
# check that it's a valid job
if [ -z $JENKINS_JOB ] ; then
VALID_MAIL=false
fi
debug123 "#5" MY_LINE $MY_LINE
# only send notifications for valid emails
if [ "$VALID_MAIL" = "true" ] ; then
echo -e "----------\nvalid email\n-----------" >> $LOGFILE
date >> $LOGFILE
echo "Job: $JENKINS_JOB" >> $LOGFILE
echo "Subject: $SUBJECT" >> $LOGFILE
echo "My line: $MY_LINE" >> $LOGFILE
# only notify if there is a channel to notify
if [ ! -z $CHANNEL ] ; then
# log message
echo "#$CHANNEL: $SUBJECT. $MY_LINE" >> $LOGFILE
MESSAGE="$SUBJECT. $MY_LINE"
# notify kgb
kgb-client --conf /srv/jenkins/kgb/$CHANNEL.conf --relay-msg "$MESSAGE" && echo "kgb informed successfully." >> $LOGFILE
echo >> $LOGFILE
else
echo "But no irc channel detected." >> $LOGFILE
fi
else
echo -e "----------\nbad luck\n-----------" >> $LOGFILE
fi
|