Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 1 | #!/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 | |
| 20 | set -u |
| 21 | |
| 22 | echo "# chart_version_check.sh, using git: $(git --version) #" |
| 23 | |
| 24 | # Collect success/failure, and list/types of failures |
| 25 | fail_version=0 |
| 26 | failed_charts="" |
| 27 | |
| 28 | # when not running under Jenkins, use current dir as workspace |
| 29 | WORKSPACE=${WORKSPACE:-.} |
| 30 | |
| 31 | # branch to compare against, defaults to master |
Zack Williams | 6a1c64a | 2019-01-23 12:08:33 -0700 | [diff] [blame] | 32 | COMPARISON_BRANCH="${COMPARISON_BRANCH:-opencord/master}" |
| 33 | echo "# Comparing with branch: $COMPARISON_BRANCH" |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 34 | |
| 35 | # Create list of changed files compared to branch |
Zack Williams | 6a1c64a | 2019-01-23 12:08:33 -0700 | [diff] [blame] | 36 | changed_files=$(git diff --name-only "${COMPARISON_BRANCH}") |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 37 | |
| 38 | # Create list of untracked by git files |
| 39 | untracked_files=$(git ls-files -o --exclude-standard) |
| 40 | |
| 41 | # Print lists of files that are changed/untracked |
| 42 | if [ -z "$changed_files" ] && [ -z "$untracked_files" ] |
| 43 | then |
| 44 | echo "# chart_version_check.sh - No changes, Success! #" |
| 45 | exit 0 |
| 46 | else |
| 47 | if [ -n "$changed_files" ] |
| 48 | then |
Zack Williams | 6a1c64a | 2019-01-23 12:08:33 -0700 | [diff] [blame] | 49 | echo "Changed files compared with $COMPARISON_BRANCH:" |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 50 | # 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 |
| 60 | fi |
| 61 | |
| 62 | # combine lists |
| 63 | if [ -n "$changed_files" ] |
| 64 | then |
| 65 | if [ -n "$untracked_files" ] |
| 66 | then |
| 67 | changed_files+=$'\n' |
| 68 | changed_files+="${untracked_files}" |
| 69 | fi |
| 70 | else |
| 71 | changed_files="${untracked_files}" |
| 72 | fi |
| 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' |
| 76 | while IFS= read -r -d '' chart |
| 77 | do |
Zack Williams | a6cb4be | 2019-01-02 16:03:20 -0700 | [diff] [blame] | 78 | chartdir=$(dirname "${chart#${WORKSPACE}/}") |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 79 | 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 Williams | 69cd042 | 2019-02-11 10:42:37 -0700 | [diff] [blame] | 84 | if [[ $file =~ ^$chartdir/ ]] |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 85 | 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 Williams | 6a1c64a | 2019-01-23 12:08:33 -0700 | [diff] [blame] | 92 | chart_yaml_diff=$(git diff -p "$COMPARISON_BRANCH" -- "${chartdir}/Chart.yaml") |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 93 | |
| 94 | if [ -n "$chart_yaml_diff" ] |
| 95 | then |
| 96 | echo "Changes to Chart.yaml in '$chartdir'" |
Zack Williams | a358609 | 2019-01-02 17:11:56 -0700 | [diff] [blame] | 97 | old_version_string=$(echo "$chart_yaml_diff" | awk '/^\-version:/ { print $2 }') |
| 98 | new_version_string=$(echo "$chart_yaml_diff" | awk '/^\+version:/ { print $2 }') |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 99 | |
| 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 | |
| 124 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
| 125 | |
| 126 | if [[ $fail_version != 0 ]]; then |
| 127 | echo "# chart_version_check.sh Failure! #" |
| 128 | echo "Charts that need to be fixed:$failed_charts" |
| 129 | exit 1 |
| 130 | fi |
| 131 | |
| 132 | echo "# chart_version_check.sh Success! - all charts have updated versions #" |
| 133 | |
| 134 | exit 0 |
| 135 | |