539f029be044567beca7585c27d2e62ebc181d76
[releng/builder.git] / jjb / autorelease / include-raw-autorelease-notify-build-failure.sh
1 #!/bin/bash
2 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11
12 NEWLINE=$'\n'
13 RELEASE_EMAIL="release@lists.opendaylight.org"
14 ARCHIVES_DIR="$JENKINS_HOSTNAME/$JOB_NAME/$BUILD_NUMBER"
15 CONSOLE_LOG="/tmp/autorelease-build.log"
16 STREAM=${JOB_NAME#*-*e-}
17 ERROR_LOG="$WORKSPACE/error.log.gz"
18
19 # get console logs
20 wget -O "$CONSOLE_LOG" "${BUILD_URL}consoleText"
21
22 # TODO: This section is still required since some of the projects use
23 # description. Remove this section when the reactor info is more consistant.
24 # extract failing project from reactor information
25 REACTOR_INFO=$(awk '/Reactor Summary:/ { flag=1 }
26           flag {
27              if ( sub(/^\[(INFO)\]/,"") && sub(/FAILURE \[.*/,"") ) {
28                  gsub(/[[:space:]]*::[[:space:]]*/,"::")
29                  gsub(/^[[:space:]]+|[[:space:]]+$|[.]/,"")
30                  print
31              }
32           }
33           /Final Memory:/ { flag=0 }' $CONSOLE_LOG)
34
35 # check for project format
36 if [[ ${REACTOR_INFO} =~ .*::*.*::*. ]]; then
37     # extract project and artifactId from full format
38     ODL=$(echo "${REACTOR_INFO}" | awk -F'::' '{ gsub(/^[ \t]+|[ \t]+$/, "", $1); print $1 }')
39     PROJECT_=$(echo "${REACTOR_INFO}" | awk -F'::' '{ gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2 }')
40     NAME=$(echo "${REACTOR_INFO}" | awk -F'::' '{ gsub(/^[ \t]+|[ \t]+$/, "", $3); print $3 }')
41 else
42     # set project from partial format
43     ODL=""
44     PROJECT_=""
45     NAME=$(echo "${REACTOR_INFO}" | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }')
46 fi
47
48 # determine ARTIFACT_ID for project mailing list
49 ARTIFACT_ID=$(awk -F: '/\[ERROR\].*mvn <goals> -rf :/ { print $2}' $CONSOLE_LOG)
50
51 # determine project mailing list using xpaths
52 # if project.groupId:
53 #     project.groupId is set and is not inherited
54 # else if project.parent.groupId:
55 #     project.groupId is not set but IS inherited from project.parent.groupId
56 # else
57 #     exclude project mailing list
58 if [ ! -z "$ARTIFACT_ID" ]; then
59     grouplist=()
60     while IFS="" read -r p; do
61         GROUP=$(xmlstarlet sel\
62               -N "x=http://maven.apache.org/POM/4.0.0"\
63               -t -m "/x:project[x:artifactId='$ARTIFACT_ID']"\
64               --if "/x:project/x:groupId"\
65               -v "/x:project/x:groupId"\
66               --elif "/x:project/x:parent/x:groupId"\
67               -v "/x:project/x:parent/x:groupId"\
68               --else -o ""\
69               "$p" 2>/dev/null)
70         if [ ! -z "${GROUP}" ]; then
71             grouplist+=($(echo "${GROUP}" | awk -F'.' '{ print $3 }'))
72         fi
73     done < <(find . -name "pom.xml")
74
75     if [ "${#grouplist[@]}" -eq 1 ]; then
76         PROJECT="${grouplist[0]}"
77     elif [ "${#grouplist[@]}" -gt 1 ]; then
78         GROUPLIST="NOTE: The artifactId: $ARTIFACT_ID matches multiple groups: ${grouplist[*]}"
79     else
80         echo "Failed to determine project.groupId using xpaths"
81     fi
82 else
83     echo "Failed to determine ARTIFACT_ID"
84     exit 1
85 fi
86
87 # Construct email subject & body
88 PROJECT_STRING=${PROJECT:+" from $PROJECT"}
89 SUBJECT="[release] Autorelease $STREAM failed to build $ARTIFACT_ID$PROJECT_STRING"
90 # shellcheck disable=SC2034
91 ATTACHMENT_INCLUDE="Attached is a snippet of the error message related to the
92 failure that we were able to automatically parse as well as console logs."
93 # shellcheck disable=SC2034
94 ATTACHMENT_EXCLUDE="Unable to attach error message snippet related to the failure
95 since this exceeds the mail server attachment size limit. Please
96 refer $ERROR_LOG in archives directory."
97 ATTACHMENT=ATTACHMENT_INCLUDE  # default behaviour
98 BODY="Attention ${PROJECT:-"OpenDaylight"}-devs,
99
100 Autorelease $STREAM failed to build $ARTIFACT_ID$PROJECT_STRING in build
101 $BUILD_NUMBER. \${!ATTACHMENT} ${PROJECT:+${NEWLINE}${GROUPLIST}}
102
103 Console Logs:
104 https://logs.opendaylight.org/$SILO/$ARCHIVES_DIR
105
106 Jenkins Build:
107 $BUILD_URL
108
109 Please review and provide an ETA on when a fix will be available.
110
111 Thanks,
112 ODL releng/autorelease team
113 "
114
115 # check if remote staging is complete successfully
116 BUILD_STATUS=$(awk '/\[INFO\] Remote staging finished/{flag=1;next}/Total time:/{flag=0}flag' $CONSOLE_LOG \
117                    | grep '\] BUILD' | awk '{print $3}')
118
119 if ([ ! -z "${NAME}" ] || [ ! -z "${ARTIFACT_ID}" ]) && [[ "${BUILD_STATUS}" != "SUCCESS" ]]; then
120     # project search pattern should handle both scenarios
121     # 1. Full format:    ODL :: $PROJECT :: $ARTIFACT_ID
122     # 2. Partial format: Building $ARTIFACT_ID
123     sed -e "/\[INFO\] Building \(${NAME} \|${ARTIFACT_ID} \|${ODL} :: ${PROJECT_} :: ${NAME} \)/,/Reactor Summary:/!d;//d" \
124           $CONSOLE_LOG | gzip > "$ERROR_LOG"
125
126     if [ -n "${PROJECT}" ]; then
127         RELEASE_EMAIL="${RELEASE_EMAIL}, ${PROJECT}-dev@lists.opendaylight.org"
128     fi
129
130     file_size=$(du -k "$ERROR_LOG" | cut -f1)
131
132     # Only send emails in production (releng), not testing (sandbox)
133     if [ "${SILO}" == "releng" ]; then
134         if [[ "$file_size" -gt 100 ]]; then
135             # shellcheck disable=SC2034
136             ATTACHMENT=ATTACHMENT_EXCLUDE
137             eval echo \""${BODY}"\" | mail \
138                 -r "Jenkins <jenkins-dontreply@opendaylight.org>" \
139                 -s "${SUBJECT}" "${RELEASE_EMAIL}"
140         else
141             eval echo \""${BODY}"\" | mail -a "$ERROR_LOG" \
142                 -r "Jenkins <jenkins-dontreply@opendaylight.org>" \
143                 -s "${SUBJECT}" "${RELEASE_EMAIL}"
144         fi
145     elif [ "${SILO}" == "sandbox" ]; then
146         echo "Running in sandbox, not actually sending notification emails"
147         echo "Subject: ${SUBJECT}"
148         eval echo \""Body: ${BODY}"\"
149     else
150         echo "Not sure how to notify in \"${SILO}\" Jenkins silo"
151     fi
152 fi
153
154 rm $CONSOLE_LOG