|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 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 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +# cd to the root path |
| 22 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" |
| 23 | +cd "${ROOT}" |
| 24 | + |
| 25 | +MDTOC=(go tool -modfile="${ROOT}/hack/tools/mdtoc/go.mod" mdtoc) |
| 26 | + |
| 27 | +# Identify KEP files changed by the PR: |
| 28 | +# default from prow env if unset from args |
| 29 | +# https://docs.prow.k8s.io/docs/jobs/#job-environment-variables |
| 30 | +# TODO: handle batch PR testing |
| 31 | +target=HEAD |
| 32 | +base="${BASE_COMMIT:-}" |
| 33 | +if [[ -z "${target:-}" && -n "${PULL_PULL_SHA:-}" ]]; then |
| 34 | + target="${PULL_PULL_SHA}" |
| 35 | +fi |
| 36 | +# target must be a something that git can resolve to a commit. |
| 37 | +# "git rev-parse --verify" checks that and prints a detailed |
| 38 | +# error. |
| 39 | +if [[ -n "${target}" ]]; then |
| 40 | + target="$(git rev-parse --verify "${target}")" |
| 41 | +fi |
| 42 | +if [[ -z "${base}" && -n "${PULL_BASE_SHA:-}" && -n "${PULL_PULL_SHA:-}" ]]; then |
| 43 | + if ! base="$(git merge-base "${PULL_BASE_SHA}" "${PULL_PULL_SHA}")"; then |
| 44 | + echo >&2 "Failed to detect base revision correctly with prow environment variables." |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | +elif [[ -z "${base}" ]]; then |
| 48 | + # origin is the default remote, but we encourage our contributors |
| 49 | + # to have both origin (their fork) and upstream, if upstream is present |
| 50 | + # then prefer upstream |
| 51 | + # if they have called it something else, there's no good way to be sure ... |
| 52 | + remote='origin' |
| 53 | + if git remote | grep -q 'upstream'; then |
| 54 | + remote='upstream' |
| 55 | + fi |
| 56 | + default_branch="$(git rev-parse --abbrev-ref "${remote}"/HEAD | cut -d/ -f2)" |
| 57 | + if ! base="$(git merge-base "${remote}/${default_branch}" "${target:-HEAD}")"; then |
| 58 | + echo >&2 "Could not determine default base revision. -r must be used explicitly." |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | +fi |
| 62 | +base="$(git rev-parse --verify "${base}")" |
| 63 | + |
| 64 | +echo "base: $base target: $target" |
| 65 | + |
| 66 | +readonly template_readme='keps/NNNN-kep-template/README.md' |
| 67 | + |
| 68 | +# get TOC for template |
| 69 | +readonly mdtoc_options=( |
| 70 | + # make sure to include all headings for this purpose even if we |
| 71 | + # wouldn't surface them in the checked-in toc in update-toc.sh |
| 72 | + '--max-depth' '100' |
| 73 | +) |
| 74 | +template_toc=$("${MDTOC[@]}" "${mdtoc_options[@]}" "${template_readme}") |
| 75 | + |
| 76 | +result=0 |
| 77 | +# get KEP README files changed in the diff |
| 78 | +kep_readmes=() |
| 79 | +while IFS= read -r changed_file |
| 80 | +do |
| 81 | + # make sure to ignore the template kep itself, we don't want to self-diff |
| 82 | + if [[ "${changed_file}" == "keps"*"README.md" ]] && [[ "${changed_file}" != "${template_readme}" ]]; then |
| 83 | + kep_readmes+=("${changed_file}") |
| 84 | + fi |
| 85 | +done < <(git diff-tree --no-commit-id --name-only -r "${base}".."${target}") |
| 86 | + |
| 87 | +for kep_readme in "${kep_readmes[@]}"; do |
| 88 | + kep_toc=$("${MDTOC[@]}" "${mdtoc_options[@]}" "${kep_readme}") |
| 89 | + echo >&2 "Diffing table of contents for $kep_readme:" |
| 90 | + # diff only removals versus the template |
| 91 | + # we don't care about _additional_ headings in the KEP |
| 92 | + # we also don't care if (Optional) headings are missing |
| 93 | + git diff <(echo "${template_toc}" ) <(echo "${kep_toc}" ) \ |
| 94 | + | grep -E '^-' \ |
| 95 | + | grep -v '(Optional)' \ |
| 96 | + || result=-1 |
| 97 | +done |
| 98 | + |
| 99 | + |
| 100 | +echo >&2 "Checked: ${kep_readmes[@]}" |
| 101 | +echo >&2 "Result: ${result}" |
| 102 | +exit "${result}" |
| 103 | + |
0 commit comments