blob: 265a817358ec595b826dc270e0b1fa9fbdaece07 [file] [log] [blame]
Zack Williams48542de2018-12-19 17:26:41 -07001#!/usr/bin/env bash
2
3# Copyright 2018-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# chart_version_check.sh
18# checks that changes to a chart include a change to the chart version
19
20set -u
21
22echo "# chart_version_check.sh, using git: $(git --version) #"
23
24# Collect success/failure, and list/types of failures
25fail_version=0
26failed_charts=""
27
28# when not running under Jenkins, use current dir as workspace
29WORKSPACE=${WORKSPACE:-.}
30
31# branch to compare against, defaults to master
Zack Williams6a1c64a2019-01-23 12:08:33 -070032COMPARISON_BRANCH="${COMPARISON_BRANCH:-opencord/master}"
33echo "# Comparing with branch: $COMPARISON_BRANCH"
Zack Williams48542de2018-12-19 17:26:41 -070034
35# Create list of changed files compared to branch
Zack Williams6a1c64a2019-01-23 12:08:33 -070036changed_files=$(git diff --name-only "${COMPARISON_BRANCH}")
Zack Williams48542de2018-12-19 17:26:41 -070037
38# Create list of untracked by git files
39untracked_files=$(git ls-files -o --exclude-standard)
40
41# Print lists of files that are changed/untracked
42if [ -z "$changed_files" ] && [ -z "$untracked_files" ]
43then
44 echo "# chart_version_check.sh - No changes, Success! #"
45 exit 0
46else
47 if [ -n "$changed_files" ]
48 then
Zack Williams6a1c64a2019-01-23 12:08:33 -070049 echo "Changed files compared with $COMPARISON_BRANCH:"
Zack Williams48542de2018-12-19 17:26:41 -070050 # Search and replace per SC2001 doesn't recognize ^ (beginning of line)
51 # shellcheck disable=SC2001
52 echo "${changed_files}" | sed 's/^/ /'
53 fi
54 if [ -n "$untracked_files" ]
55 then
56 echo "Untracked files:"
57 # shellcheck disable=SC2001
58 echo "${untracked_files}" | sed 's/^/ /'
59 fi
60fi
61
62# combine lists
63if [ -n "$changed_files" ]
64then
65 if [ -n "$untracked_files" ]
66 then
67 changed_files+=$'\n'
68 changed_files+="${untracked_files}"
69 fi
70else
71 changed_files="${untracked_files}"
72fi
73
74# For all the charts, fail on changes within a chart without a version change
75# loop on result of 'find -name Chart.yaml'
76while IFS= read -r -d '' chart
77do
Zack Williamsa6cb4be2019-01-02 16:03:20 -070078 chartdir=$(dirname "${chart#${WORKSPACE}/}")
Zack Williams48542de2018-12-19 17:26:41 -070079 chart_changed_files=""
80 version_updated=0
81
82 # create a list of files that were changed in the chart
83 for file in $changed_files; do
Zack Williams69cd0422019-02-11 10:42:37 -070084 if [[ $file =~ ^$chartdir/ ]]
Zack Williams48542de2018-12-19 17:26:41 -070085 then
86 chart_changed_files+=$'\n'
87 chart_changed_files+=" ${file}"
88 fi
89 done
90
91 # See if chart version changed using 'git diff', and is SemVer
Zack Williams6a1c64a2019-01-23 12:08:33 -070092 chart_yaml_diff=$(git diff -p "$COMPARISON_BRANCH" -- "${chartdir}/Chart.yaml")
Zack Williams48542de2018-12-19 17:26:41 -070093
94 if [ -n "$chart_yaml_diff" ]
95 then
96 echo "Changes to Chart.yaml in '$chartdir'"
Zack Williamsa3586092019-01-02 17:11:56 -070097 old_version_string=$(echo "$chart_yaml_diff" | awk '/^\-version:/ { print $2 }')
98 new_version_string=$(echo "$chart_yaml_diff" | awk '/^\+version:/ { print $2 }')
Zack Williams48542de2018-12-19 17:26:41 -070099
100 if [ -n "$new_version_string" ]
101 then
102 version_updated=1
103 echo " Old version string:${old_version_string//-version:/}"
104 echo " New version string:${new_version_string//+version:/}"
105 fi
106 fi
107
108 # if there are any changed files
109 if [ -n "$chart_changed_files" ]
110 then
111 # and version updated, print changed files
112 if [ $version_updated -eq 1 ]
113 then
114 echo " Files changed:${chart_changed_files}"
115 else
116 # otherwise fail this chart
117 echo "Changes to chart but no version update in '$chartdir':${chart_changed_files}"
118 fail_version=1
119 failed_charts+=$'\n'
120 failed_charts+=" $chartdir"
121 fi
122 fi
123
124done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
125
126if [[ $fail_version != 0 ]]; then
127 echo "# chart_version_check.sh Failure! #"
128 echo "Charts that need to be fixed:$failed_charts"
129 exit 1
130fi
131
132echo "# chart_version_check.sh Success! - all charts have updated versions #"
133
134exit 0
135