blob: e0703756a0555fa1621d7f28f84902f5b464b70b [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
32GERRIT_BRANCH=${GERRIT_BRANCH:-opencord/master}
33
34# Create list of changed files compared to branch
35changed_files=$(git diff --name-only "${GERRIT_BRANCH}")
36
37# Create list of untracked by git files
38untracked_files=$(git ls-files -o --exclude-standard)
39
40# Print lists of files that are changed/untracked
41if [ -z "$changed_files" ] && [ -z "$untracked_files" ]
42then
43 echo "# chart_version_check.sh - No changes, Success! #"
44 exit 0
45else
46 if [ -n "$changed_files" ]
47 then
48 echo "Changed files compared with $GERRIT_BRANCH:"
49 # Search and replace per SC2001 doesn't recognize ^ (beginning of line)
50 # shellcheck disable=SC2001
51 echo "${changed_files}" | sed 's/^/ /'
52 fi
53 if [ -n "$untracked_files" ]
54 then
55 echo "Untracked files:"
56 # shellcheck disable=SC2001
57 echo "${untracked_files}" | sed 's/^/ /'
58 fi
59fi
60
61# combine lists
62if [ -n "$changed_files" ]
63then
64 if [ -n "$untracked_files" ]
65 then
66 changed_files+=$'\n'
67 changed_files+="${untracked_files}"
68 fi
69else
70 changed_files="${untracked_files}"
71fi
72
73# For all the charts, fail on changes within a chart without a version change
74# loop on result of 'find -name Chart.yaml'
75while IFS= read -r -d '' chart
76do
Zack Williamsa6cb4be2019-01-02 16:03:20 -070077 chartdir=$(dirname "${chart#${WORKSPACE}/}")
Zack Williams48542de2018-12-19 17:26:41 -070078 chart_changed_files=""
79 version_updated=0
80
81 # create a list of files that were changed in the chart
82 for file in $changed_files; do
83 if [[ $file =~ $chartdir ]]
84 then
85 chart_changed_files+=$'\n'
86 chart_changed_files+=" ${file}"
87 fi
88 done
89
90 # See if chart version changed using 'git diff', and is SemVer
91 chart_yaml_diff=$(git diff -p "$GERRIT_BRANCH" "${chartdir}/Chart.yaml")
92
93 if [ -n "$chart_yaml_diff" ]
94 then
95 echo "Changes to Chart.yaml in '$chartdir'"
Zack Williamsa3586092019-01-02 17:11:56 -070096 old_version_string=$(echo "$chart_yaml_diff" | awk '/^\-version:/ { print $2 }')
97 new_version_string=$(echo "$chart_yaml_diff" | awk '/^\+version:/ { print $2 }')
Zack Williams48542de2018-12-19 17:26:41 -070098
99 if [ -n "$new_version_string" ]
100 then
101 version_updated=1
102 echo " Old version string:${old_version_string//-version:/}"
103 echo " New version string:${new_version_string//+version:/}"
104 fi
105 fi
106
107 # if there are any changed files
108 if [ -n "$chart_changed_files" ]
109 then
110 # and version updated, print changed files
111 if [ $version_updated -eq 1 ]
112 then
113 echo " Files changed:${chart_changed_files}"
114 else
115 # otherwise fail this chart
116 echo "Changes to chart but no version update in '$chartdir':${chart_changed_files}"
117 fail_version=1
118 failed_charts+=$'\n'
119 failed_charts+=" $chartdir"
120 fi
121 fi
122
123done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
124
125if [[ $fail_version != 0 ]]; then
126 echo "# chart_version_check.sh Failure! #"
127 echo "Charts that need to be fixed:$failed_charts"
128 exit 1
129fi
130
131echo "# chart_version_check.sh Success! - all charts have updated versions #"
132
133exit 0
134