|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright Istio Authors |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# check-release-channel.sh |
| 16 | +# |
| 17 | +# Ensures that newly added proto fields include a |
| 18 | +# +cue-gen:<APIName>:releaseChannel:extended annotation in their comment block. |
| 19 | +# |
| 20 | +# Per GUIDELINES.md, new fields added to stable v1 APIs must go through the |
| 21 | +# "extended" release channel before being promoted to stable. This is indicated |
| 22 | +# by a comment annotation above the field definition. Existing stable fields |
| 23 | +# intentionally lack this annotation, so we use a diff-based approach: only |
| 24 | +# fields added relative to the base branch are checked. |
| 25 | +# |
| 26 | +# How it works: |
| 27 | +# 1. Find proto files changed vs the base branch (excluding common-protos/). |
| 28 | +# 2. Parse the git diff to extract added lines with their line numbers. |
| 29 | +# 3. Identify lines that are proto field definitions (type name = N;), |
| 30 | +# skipping enum values, reserved statements, and options. |
| 31 | +# 4. For each new field, walk upward through the contiguous comment block |
| 32 | +# immediately above it, looking for the releaseChannel annotation. |
| 33 | +# 5. Report any fields missing the annotation and exit non-zero. |
| 34 | +# |
| 35 | +# Usage: |
| 36 | +# ./scripts/check-release-channel.sh <base-branch> |
| 37 | +# ./scripts/check-release-channel.sh master |
| 38 | +# |
| 39 | +# See https://github.com/istio/api/issues/3147 |
| 40 | + |
| 41 | +set -euo pipefail |
| 42 | + |
| 43 | +branch="${1:-master}" |
| 44 | +errors=0 |
| 45 | + |
| 46 | +# Step 1: Find changed/added proto files, excluding third-party common-protos. |
| 47 | +changed_protos=$(git diff "${branch}" --name-only --diff-filter=AM -- '*.proto' | grep -v common-protos || true) |
| 48 | +if [[ -z "${changed_protos}" ]]; then |
| 49 | + exit 0 |
| 50 | +fi |
| 51 | + |
| 52 | +# check_comment_block: starting from the line above a field definition, |
| 53 | +# walk upward through comment lines (// ...) and blank lines. If we find |
| 54 | +# a +cue-gen:*:releaseChannel: annotation, return 0 (success). If we hit |
| 55 | +# a non-comment, non-blank line (previous field, message boundary, etc.), |
| 56 | +# stop and return 1 (not found). This prevents matching annotations that |
| 57 | +# belong to a different field. |
| 58 | +check_comment_block() { |
| 59 | + local file="$1" lineno="$2" |
| 60 | + local i=$((lineno - 1)) |
| 61 | + while [[ ${i} -ge 1 ]]; do |
| 62 | + local line |
| 63 | + line=$(sed -n "${i}p" "${file}") |
| 64 | + # Comment line — check for the annotation |
| 65 | + if echo "${line}" | grep -qP '^\s*//'; then |
| 66 | + if echo "${line}" | grep -q '+cue-gen:.*:releaseChannel:'; then |
| 67 | + return 0 |
| 68 | + fi |
| 69 | + i=$((i - 1)) |
| 70 | + continue |
| 71 | + fi |
| 72 | + # Blank line — skip (comments may have a gap before the field) |
| 73 | + if echo "${line}" | grep -qP '^\s*$'; then |
| 74 | + i=$((i - 1)) |
| 75 | + continue |
| 76 | + fi |
| 77 | + # Any other line (field, message, etc.) — stop searching |
| 78 | + break |
| 79 | + done |
| 80 | + return 1 |
| 81 | +} |
| 82 | + |
| 83 | +for file in ${changed_protos}; do |
| 84 | + # Step 2: Parse "git diff -U0" to get added lines with their new-file line numbers. |
| 85 | + # -U0 means no context lines, so we only see actual additions. |
| 86 | + # The awk script reads @@ hunk headers to get the starting line number, |
| 87 | + # then counts up for each "+" line (skipping the "+++ b/file" header). |
| 88 | + added_lines=$(git diff "${branch}" -U0 -- "${file}" | awk ' |
| 89 | + /^@@/ { match($0, /\+([0-9]+)/, a); nr = a[1]; next } |
| 90 | + /^\+/ && !/^\+\+\+/ { print nr ":" substr($0, 2); nr++ } |
| 91 | + ') |
| 92 | + [[ -z "${added_lines}" ]] && continue |
| 93 | + |
| 94 | + while IFS= read -r entry; do |
| 95 | + [[ -z "${entry}" ]] && continue |
| 96 | + lineno="${entry%%:*}" |
| 97 | + content="${entry#*:}" |
| 98 | + |
| 99 | + # Step 3: Match proto field definitions. |
| 100 | + # Pattern: [repeated|optional] type name = N; or map<K,V> name = N; |
| 101 | + # This matches: |
| 102 | + # string foo = 1; |
| 103 | + # repeated string foo = 2; |
| 104 | + # google.protobuf.Duration timeout = 3; |
| 105 | + # map<string, string> labels = 4; |
| 106 | + # But NOT enum values (single word before =): SOME_VALUE = 0; |
| 107 | + echo "${content}" | grep -qP '^\s*((repeated|optional)\s+)?((\w[\w.]*\s+\w+)|(map<.*>\s+\w+))\s*=\s*\d+\s*[;\[]' || continue |
| 108 | + # Skip reserved/option/comment-only lines that might match the pattern |
| 109 | + echo "${content}" | grep -qP '^\s*(reserved|option|//)\s' && continue |
| 110 | + |
| 111 | + # Step 4: Walk upward through the comment block above this field. |
| 112 | + if ! check_comment_block "${file}" "${lineno}"; then |
| 113 | + # Step 5: Report the error. |
| 114 | + echo "ERROR: ${file}:${lineno}: new field missing releaseChannel annotation" |
| 115 | + echo " ${content}" |
| 116 | + errors=$((errors + 1)) |
| 117 | + fi |
| 118 | + done <<< "${added_lines}" |
| 119 | +done |
| 120 | + |
| 121 | +if [[ ${errors} -gt 0 ]]; then |
| 122 | + echo "" |
| 123 | + echo "Found ${errors} new field(s) without releaseChannel annotation." |
| 124 | + echo "Add: // +cue-gen:<APIName>:releaseChannel:extended" |
| 125 | + echo "See GUIDELINES.md for details." |
| 126 | + exit 1 |
| 127 | +fi |
0 commit comments