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