|
| 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 | +# keep in sync with hack/verify-toc.sh |
| 22 | +TOOL_VERSION=v1.1.0 |
| 23 | + |
| 24 | +# cd to the root path |
| 25 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" |
| 26 | +cd "${ROOT}" |
| 27 | + |
| 28 | +# create a temporary directory |
| 29 | +TMP_DIR=$(mktemp -d) |
| 30 | +# cleanup |
| 31 | +exitHandler() ( |
| 32 | + echo "Cleaning up..." |
| 33 | + rm -rf "${TMP_DIR}" |
| 34 | +) |
| 35 | +trap exitHandler EXIT |
| 36 | +# Perform go install in a temp dir as we are not tracking this version in a go |
| 37 | +# module. |
| 38 | +# If we do the go install in the repo, it will create/update go.mod and go.sum. |
| 39 | +cd "${TMP_DIR}" |
| 40 | +GO111MODULE=on GOBIN="${TMP_DIR}" go install "sigs.k8s.io/mdtoc@${TOOL_VERSION}" |
| 41 | +export PATH="${TMP_DIR}:${PATH}" |
| 42 | +cd "${ROOT}" |
| 43 | + |
| 44 | +# Identify KEP files changed by the PR: |
| 45 | +# default from prow env if unset from args |
| 46 | +# https://docs.prow.k8s.io/docs/jobs/#job-environment-variables |
| 47 | +# TODO: handle batch PR testing |
| 48 | +# TODO: hardcoded for ease of testing |
| 49 | +base=0bb74a98e252defc36f5d8f9d497cdc581a949b6 |
| 50 | +target=HEAD |
| 51 | +if [[ -z "${target:-}" && -n "${PULL_PULL_SHA:-}" ]]; then |
| 52 | + target="${PULL_PULL_SHA}" |
| 53 | +fi |
| 54 | +# target must be a something that git can resolve to a commit. |
| 55 | +# "git rev-parse --verify" checks that and prints a detailed |
| 56 | +# error. |
| 57 | +if [[ -n "${target}" ]]; then |
| 58 | + target="$(git rev-parse --verify "${target}")" |
| 59 | +fi |
| 60 | +if [[ -z "${base}" && -n "${PULL_BASE_SHA:-}" && -n "${PULL_PULL_SHA:-}" ]]; then |
| 61 | + if ! base="$(git merge-base "${PULL_BASE_SHA}" "${PULL_PULL_SHA}")"; then |
| 62 | + echo >&2 "Failed to detect base revision correctly with prow environment variables." |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | +elif [[ -z "${base}" ]]; then |
| 66 | + if ! base="$(git merge-base origin/master "${target:-HEAD}")"; then |
| 67 | + echo >&2 "Could not determine default base revision. -r must be used explicitly." |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | +fi |
| 71 | +base="$(git rev-parse --verify "${base}")" |
| 72 | + |
| 73 | +echo "base: $base target: $target" |
| 74 | + |
| 75 | +# get TOC for template |
| 76 | +template_toc=$(mdtoc 'keps/NNNN-kep-template/README.md') |
| 77 | + |
| 78 | +result=0 |
| 79 | +# get KEP README files changed in the diff |
| 80 | +kep_readmes=() |
| 81 | +while IFS= read -r changed_file |
| 82 | +do |
| 83 | + if [[ "$changed_file" == "keps"*"README.md" ]]; then |
| 84 | + kep_readmes+=("$changed_file") |
| 85 | + fi |
| 86 | +done < <(git diff-tree --no-commit-id --name-only -r "${base}".."${target}") |
| 87 | + |
| 88 | +for kep_readme in "${kep_readmes[@]}"; do |
| 89 | + kep_toc=$(mdtoc "${kep_readme}") |
| 90 | + echo >&2 "Diffing table of contents for $kep_readme:" |
| 91 | + # diff only removals versus the template |
| 92 | + # we don't care about _additional_ headings in the KEP |
| 93 | + # TODO: ignore certain allowed removals "(Optional)" |
| 94 | + git diff <(echo "${template_toc}" ) <(echo "${kep_toc}" ) | grep -E '^-' | grep -v '(Optional)' || result=-1 |
| 95 | +done |
| 96 | + |
| 97 | + |
| 98 | +echo >&2 "Checked: ${kep_readmes[@]}" |
| 99 | +echo >&2 "Result: ${result}" |
| 100 | +exit "${result}" |
| 101 | + |
0 commit comments