|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2021 The Kubernetes Authors. |
| 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 | +changelog="$(git rev-parse --show-toplevel)/CHANGELOG.md" |
| 18 | + |
| 19 | +function util::changelog::has_release { |
| 20 | + local release=$1 |
| 21 | + return $(grep -q "^# $release$" $changelog) |
| 22 | +} |
| 23 | + |
| 24 | +# find_release_start returns the number of the first line of the given release |
| 25 | +function util::changelog::find_release_start { |
| 26 | + local release=$1 |
| 27 | + echo $(grep -n "^# $release$" $changelog | head -1 | cut -d: -f1) |
| 28 | +} |
| 29 | + |
| 30 | +# find_release_end returns the number of the last line of the given release |
| 31 | +function util::changelog::find_release_end { |
| 32 | + local release=$1 |
| 33 | + |
| 34 | + local release_start=$(util::changelog::find_release_start $release) |
| 35 | + local next_release_index=0 |
| 36 | + local releases=($(grep -n "^# " $changelog | cut -d: -f1)) |
| 37 | + for i in "${!releases[@]}"; do |
| 38 | + if [[ "${releases[$i]}" = "$release_start" ]]; then |
| 39 | + next_release_index=$((i+1)) |
| 40 | + break |
| 41 | + fi |
| 42 | + done |
| 43 | + # return the line before the next release |
| 44 | + echo $((${releases[${next_release_index}]}-1)) |
| 45 | +} |
| 46 | + |
| 47 | +# has_section returns if the given section exists between start and end |
| 48 | +function util::changelog::has_section_in_range { |
| 49 | + local section="$1" |
| 50 | + local start=$2 |
| 51 | + local end=$3 |
| 52 | + |
| 53 | + local lines=($(grep -n "$section" "$changelog" | cut -d: -f1)) |
| 54 | + for i in "${!lines[@]}"; do |
| 55 | + if [[ ${lines[$i]} -ge $start && ${lines[$i]} -le $end ]]; then |
| 56 | + return 0 |
| 57 | + fi |
| 58 | + done |
| 59 | + return 1 |
| 60 | +} |
| 61 | + |
| 62 | +# find_section returns the number of the first line of the given section |
| 63 | +function util::changelog::find_section_in_range { |
| 64 | + local section="$1" |
| 65 | + local start=$2 |
| 66 | + local end=$3 |
| 67 | + |
| 68 | + local line="0" |
| 69 | + local lines=($(grep -n "$section" "$changelog" | cut -d: -f1)) |
| 70 | + for i in "${!lines[@]}"; do |
| 71 | + if [[ ${lines[$i]} -ge $start && ${lines[$i]} -le $end ]]; then |
| 72 | + line=${lines[$i]} |
| 73 | + break |
| 74 | + fi |
| 75 | + done |
| 76 | + echo $line |
| 77 | +} |
| 78 | + |
| 79 | +# write_changelog writes release_notes to section in target_release |
| 80 | +function util::changelog::write_changelog { |
| 81 | + local target_release="$1" |
| 82 | + local section="$2" |
| 83 | + local release_notes="$3" |
| 84 | + |
| 85 | + # find the place in the changelog that we want to edit |
| 86 | + local line_to_edit="1" |
| 87 | + if util::changelog::has_release $target_release; then |
| 88 | + # the target release exists |
| 89 | + release_first_line=$(util::changelog::find_release_start $target_release) |
| 90 | + release_last_line=$(util::changelog::find_release_end $target_release) |
| 91 | + if util::changelog::has_section_in_range "$section" "$release_first_line" "$release_last_line"; then |
| 92 | + # prepend to existing section |
| 93 | + line_to_edit=$(($(util::changelog::find_section_in_range "$section" "$release_first_line" "$release_last_line")+1)) |
| 94 | + else |
| 95 | + # add a new section; plus 4 so that the section is placed below "Kubernetes API Version" |
| 96 | + line_to_edit=$(($(util::changelog::find_release_start $target_release)+4)) |
| 97 | + release_notes="$section\n$release_notes\n" |
| 98 | + fi |
| 99 | + else |
| 100 | + # add a new release |
| 101 | + release_notes="# $target_release\n\nKubernetes API Version: To Be Updated\n\n$section\n$release_notes\n" |
| 102 | + fi |
| 103 | + |
| 104 | + echo "Writing the following release notes to CHANGELOG line $line_to_edit:" |
| 105 | + echo -e $release_notes |
| 106 | + |
| 107 | + # update changelog |
| 108 | + sed -i "${line_to_edit}i${release_notes}" $changelog |
| 109 | +} |
0 commit comments