Merge "jjb/releng: fix compass-container $DOCKERFILE"
[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 pipefail
13
14 # Job Number Formatter
15 function JOBS {
16     local NUMS=$1
17     if [ $NUMS == 1 ]; then
18         echo -n "Job"
19     else
20         echo -n "Jobs"
21     fi
22 }
23
24 # Activiate the virtualenv so we have access to 'jenkins-jobs'
25 source /opt/virtualenv/jenkins-job-builder/bin/activate
26
27 echo "> Generating list of current JJB jobs..."
28 jenkins-jobs -l ERROR test -r jjb -o job_output
29
30 echo "> Generating list of previous JJB jobs..."
31 git checkout -b previous-commit HEAD^
32 jenkins-jobs -l ERROR test -r jjb -o job_output_prev
33 git checkout - && git branch -d previous-commit
34
35 echo "> Finding job changes ..."
36 diff -r -q job_output job_output_prev &> job_diff.txt || true
37
38 # Only in (job_output) = NEW
39 # Only in (job_output_prev) = DELETED
40 # Files ... differ = MODIFIED
41
42 declare -a JOBS_ADDED=($(grep 'job_output:' job_diff.txt | cut -d':' -f2- | sed 's/^[ \t]*//;s/[ \t]*$//'))
43 declare -a JOBS_MODIFIED=($(grep 'differ$' job_diff.txt | sed "s/Files job_output\/\(.*\) and.*/\1/g"))
44 declare -a JOBS_REMOVED=($(grep 'job_output_prev:' job_diff.txt | cut -d ':' -f2- | sed 's/^[ \t]*//;s/[ \t]*$//'))
45
46 NUM_JOBS_ADDED=${#JOBS_ADDED[@]}
47 NUM_JOBS_MODIFIED=${#JOBS_MODIFIED[@]}
48 NUM_JOBS_REMOVED=${#JOBS_REMOVED[@]}
49
50 echo "> Writing gerrit comment ..."
51 if [ $NUM_JOBS_ADDED -gt 0 ]; then
52     JOB_STRING="$(JOBS $NUM_JOBS_ADDED)"
53     { printf "Added %s %s:\n\n" "${NUM_JOBS_ADDED}" "$JOB_STRING";
54     printf '* %s\n' "${JOBS_ADDED[@]}";
55     printf "\n"; } >> gerrit_comment.txt
56 fi
57
58 if [ $NUM_JOBS_MODIFIED -gt 0 ]; then
59     JOB_STRING="$(JOBS $NUM_JOBS_MODIFIED)"
60     { printf "Modified %s %s:\n\n" "${NUM_JOBS_MODIFIED}" "$JOB_STRING";
61     printf '* %s\n' "${JOBS_MODIFIED[@]}";
62     printf "\n"; } >> gerrit_comment.txt
63 fi
64
65 if [ $NUM_JOBS_REMOVED -gt 0 ]; then
66     JOB_STRING="$(JOBS $NUM_JOBS_REMOVED)"
67     { printf "Removed %s %s:\n\n" "${NUM_JOBS_REMOVED}" "$JOB_STRING";
68     printf '* %s\n' "${JOBS_REMOVED[@]}";
69     printf "\n"; } >> gerrit_comment.txt
70 fi