Comment Pending JJB Changes on Patchset
[releng.git] / jjb / releng / generate-job-list.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14
15 # Job Number Formatter
16 function JOBS {
17     local NUMS=$1
18     if [ $NUMS == 1 ]; then
19         echo -n "Job"
20     else
21         echo -n "Jobs"
22     fi
23 }
24
25 # We expect job_output to exist prior to this being run and contain the
26 # output from jenkins-jobs test
27
28 echo "> Generating list of previous JJB jobs..."
29 git checkout -q -b previous-commit HEAD^
30 jenkins-jobs -l ERROR test -r jjb -o job_output_prev
31 git checkout -q - && git branch -q -d previous-commit
32
33 echo "> Finding job changes ..."
34 diff -r -q job_output job_output_prev &> job_diff.txt || true
35
36 # Only in (job_output) = NEW
37 # Only in (job_output_prev) = DELETED
38 # Files ... differ = MODIFIED
39
40 declare -a JOBS_ADDED=($(grep 'job_output:' job_diff.txt | cut -d':' -f2- | sed 's/^[ \t]*//;s/[ \t]*$//'))
41 declare -a JOBS_MODIFIED=($(grep 'differ$' job_diff.txt | sed "s/Files job_output\/\(.*\) and.*/\1/g"))
42 declare -a JOBS_REMOVED=($(grep 'job_output_prev:' job_diff.txt | cut -d ':' -f2- | sed 's/^[ \t]*//;s/[ \t]*$//'))
43
44 NUM_JOBS_ADDED=${#JOBS_ADDED[@]}
45 NUM_JOBS_MODIFIED=${#JOBS_MODIFIED[@]}
46 NUM_JOBS_REMOVED=${#JOBS_REMOVED[@]}
47
48 echo "> Writing gerrit comment ..."
49 if [ $NUM_JOBS_ADDED -gt 0 ]; then
50     JOB_STRING="$(JOBS $NUM_JOBS_ADDED)"
51     { printf "Added %s %s:\n\n" "${NUM_JOBS_ADDED}" "$JOB_STRING";
52     printf '* %s\n' "${JOBS_ADDED[@]}";
53     printf "\n"; } >> gerrit_comment.txt
54 fi
55
56 if [ $NUM_JOBS_MODIFIED -gt 0 ]; then
57     JOB_STRING="$(JOBS $NUM_JOBS_MODIFIED)"
58     { printf "Modified %s %s:\n\n" "${NUM_JOBS_MODIFIED}" "$JOB_STRING";
59     printf '* %s\n' "${JOBS_MODIFIED[@]}";
60     printf "\n"; } >> gerrit_comment.txt
61 fi
62
63 if [ $NUM_JOBS_REMOVED -gt 0 ]; then
64     JOB_STRING="$(JOBS $NUM_JOBS_REMOVED)"
65     { printf "Removed %s %s:\n\n" "${NUM_JOBS_REMOVED}" "$JOB_STRING";
66     printf '* %s\n' "${JOBS_REMOVED[@]}";
67     printf "\n"; } >> gerrit_comment.txt
68 fi