Skip to content

Instantly share code, notes, and snippets.

@bookwar
Created May 22, 2017 12:51
Show Gist options
  • Save bookwar/cd3613ef18d286aa9c46bdc9dd82e40b to your computer and use it in GitHub Desktop.
Save bookwar/cd3613ef18d286aa9c46bdc9dd82e40b to your computer and use it in GitHub Desktop.
compare_jjb.xml outputA/ outputB/
#!/bin/bash
# Recursively compare XML outputs of two JJB test runs
set -e
A_DIR="$1"
B_DIR="$2"
# Cleanup trailing slashes if exist
A_DIR=${A_DIR%/}
B_DIR=${B_DIR%/}
REPORT_DIR="diff"
LOGFILE="${REPORT_DIR}/diff.log"
HTML="report.html"
TXT="report.txt"
CHANGED=0
ADDED=0
REMOVED=0
mkdir -p "${REPORT_DIR}"
DIFF="$(diff -q -r -u -N "${A_DIR}" "${B_DIR}" &>"${LOGFILE}"; echo ${?})"
# are there any changes?
if [[ ${DIFF} -eq 0 ]]; then
echo "No changes found, clean exit"
exit 0
fi
if [[ ${DIFF} -eq 2 ]]; then
echo "ERROR: Can not calculate diff"
exit 1
fi
# DIFF=1 thus there is a difference
# Loop through all changed jobs and compare them with a blocklist
for A_FILE in $(awk '/Files/ {print $2}' "${LOGFILE}"); do
JOB_NAME="$(basename ${A_FILE})"
# We support several Jenkins instances in one repo, therefore A_DIR might include subfolders. We use JOB_PREFIX
# variable to store path to the job file, relative to the A_DIR.
A_PATH="$(dirname $A_FILE)"
JOB_PREFIX="${A_PATH#${A_DIR}}"
B_FILE="${B_DIR}${JOB_PREFIX}/${JOB_NAME}"
REPORT_PATH="${REPORT_DIR}/${JOB_PREFIX}"
mkdir -p "${REPORT_PATH}/diff"
if [ ! -f "${A_FILE}" ]; then
# Job exists in B only
ADDED+=1
echo "Added <a href=${BUILD_URL}artifact/output/${B_DIR}${JOB_PREFIX}/${JOB_NAME}/*view*/>${JOB_PREFIX}/${JOB_NAME}</a><br>" \
>> "${REPORT_PATH}/added.${HTML}"
echo "Added ${JOB_PREFIX}/${JOB_NAME}" >> "${REPORT_PATH}/added.${TXT}"
elif [ ! -f "${B_FILE}" ]; then
# Job exists in A only
REMOVED+=1
echo "Removed <a href=${BUILD_URL}artifact/output/${A_DIR}${JOB_PREFIX}/${JOB_NAME}/*view*/>${JOB_PREFIX}/${JOB_NAME}</a><br>" \
>> "${REPORT_PATH}/removed.${HTML}"
echo "Removed ${JOB_PREFIX}/${JOB_NAME}" >> "${REPORT_PATH}/removed.${TXT}"
else
CHANGED+=1
diff -U 50 "${A_FILE}" "${B_FILE}" >> "${REPORT_PATH}/diff/${JOB_NAME}" || true
echo "Changed <a href=${BUILD_URL}artifact/output/${REPORT_PATH}/diff/${JOB_NAME}/*view*/>${JOB_PREFIX}/${JOB_NAME}</a><br>" \
>> "${REPORT_PATH}/changed.${HTML}"
echo "Changed ${JOB_PREFIX}/${JOB_NAME}" >> "${REPORT_PATH}/changed.${TXT}"
fi
done
find "$REPORT_DIR" -name "*.${HTML}" -exec cat {} \; > "${REPORT_DIR}/${HTML}"
find "$REPORT_DIR" -name "*.${TXT}" -exec cat {} \; > "${REPORT_DIR}/${TXT}"
cat "${REPORT_DIR}/${TXT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment