|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Check if parameter group struct modifications include version increments |
| 4 | +# This prevents settings corruption when struct layout changes without version bump |
| 5 | +# |
| 6 | +# Exit codes: |
| 7 | +# 0 - No issues found |
| 8 | +# 1 - Potential issues detected (will post comment) |
| 9 | +# 2 - Script error |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +# Output file for issues found |
| 14 | +ISSUES_FILE=$(mktemp) |
| 15 | +trap "rm -f $ISSUES_FILE" EXIT |
| 16 | + |
| 17 | +# Color output for local testing |
| 18 | +if [ -t 1 ]; then |
| 19 | + RED='\033[0;31m' |
| 20 | + GREEN='\033[0;32m' |
| 21 | + YELLOW='\033[1;33m' |
| 22 | + NC='\033[0m' # No Color |
| 23 | +else |
| 24 | + RED='' |
| 25 | + GREEN='' |
| 26 | + YELLOW='' |
| 27 | + NC='' |
| 28 | +fi |
| 29 | + |
| 30 | +echo "🔍 Checking for Parameter Group version updates..." |
| 31 | + |
| 32 | +# Get base and head commits |
| 33 | +BASE_REF=${GITHUB_BASE_REF:-} |
| 34 | +HEAD_REF=${GITHUB_HEAD_REF:-} |
| 35 | + |
| 36 | +if [ -z "$BASE_REF" ] || [ -z "$HEAD_REF" ]; then |
| 37 | + echo "⚠️ Warning: Not running in GitHub Actions PR context" |
| 38 | + echo "Using git diff against HEAD~1 for local testing" |
| 39 | + BASE_COMMIT="HEAD~1" |
| 40 | + HEAD_COMMIT="HEAD" |
| 41 | +else |
| 42 | + BASE_COMMIT="origin/$BASE_REF" |
| 43 | + HEAD_COMMIT="HEAD" |
| 44 | +fi |
| 45 | + |
| 46 | +# Get list of changed files |
| 47 | +CHANGED_FILES=$(git diff --name-only $BASE_COMMIT..$HEAD_COMMIT | grep -E '\.(c|h)$' || true) |
| 48 | + |
| 49 | +if [ -z "$CHANGED_FILES" ]; then |
| 50 | + echo "✅ No C/H files changed" |
| 51 | + exit 0 |
| 52 | +fi |
| 53 | + |
| 54 | +echo "📁 Changed files:" |
| 55 | +echo "$CHANGED_FILES" | sed 's/^/ /' |
| 56 | + |
| 57 | +# Function to extract PG info from a file |
| 58 | +check_file_for_pg_changes() { |
| 59 | + local file=$1 |
| 60 | + local diff_output=$(git diff $BASE_COMMIT..$HEAD_COMMIT -- "$file") |
| 61 | + |
| 62 | + # Check if file contains PG_REGISTER in current version |
| 63 | + if ! git show $HEAD_COMMIT:"$file" 2>/dev/null | grep -q "PG_REGISTER"; then |
| 64 | + return 0 |
| 65 | + fi |
| 66 | + |
| 67 | + echo " 🔎 Checking $file (contains PG_REGISTER)" |
| 68 | + |
| 69 | + # Extract all PG_REGISTER lines from the diff (both old and new) |
| 70 | + local pg_registers=$(echo "$diff_output" | grep -E "^[-+].*PG_REGISTER" || true) |
| 71 | + |
| 72 | + if [ -z "$pg_registers" ]; then |
| 73 | + # PG_REGISTER exists but wasn't changed |
| 74 | + # Still need to check if the struct changed |
| 75 | + pg_registers=$(git show $HEAD_COMMIT:"$file" | grep "PG_REGISTER" || true) |
| 76 | + fi |
| 77 | + |
| 78 | + # Process each PG registration |
| 79 | + while IFS= read -r pg_line; do |
| 80 | + [ -z "$pg_line" ] && continue |
| 81 | + |
| 82 | + # Extract struct name and version |
| 83 | + # Pattern: PG_REGISTER.*\((\w+),\s*(\w+),\s*PG_\w+,\s*(\d+)\) |
| 84 | + if [[ $pg_line =~ PG_REGISTER[^(]*\(([^,]+),([^,]+),([^,]+),([^)]+)\) ]]; then |
| 85 | + local struct_type="${BASH_REMATCH[1]}" |
| 86 | + local pg_name="${BASH_REMATCH[2]}" |
| 87 | + local pg_id="${BASH_REMATCH[3]}" |
| 88 | + local version="${BASH_REMATCH[4]}" |
| 89 | + |
| 90 | + # Clean up whitespace |
| 91 | + struct_type=$(echo "$struct_type" | xargs) |
| 92 | + version=$(echo "$version" | xargs) |
| 93 | + |
| 94 | + echo " 📋 Found: $struct_type (version $version)" |
| 95 | + |
| 96 | + # Check if this struct's typedef was modified in ANY changed file |
| 97 | + local struct_pattern="typedef struct ${struct_type%_t}_s" |
| 98 | + local struct_body_diff="" |
| 99 | + local struct_found_in="" |
| 100 | + |
| 101 | + # Search all changed files for this struct definition |
| 102 | + while IFS= read -r changed_file; do |
| 103 | + [ -z "$changed_file" ] && continue |
| 104 | + |
| 105 | + local file_diff=$(git diff $BASE_COMMIT..$HEAD_COMMIT -- "$changed_file") |
| 106 | + local struct_in_file=$(echo "$file_diff" | sed -n "/${struct_pattern}/,/\}.*${struct_type};/p") |
| 107 | + |
| 108 | + if [ -n "$struct_in_file" ]; then |
| 109 | + struct_body_diff="$struct_in_file" |
| 110 | + struct_found_in="$changed_file" |
| 111 | + echo " 🔍 Found struct definition in $changed_file" |
| 112 | + break |
| 113 | + fi |
| 114 | + done <<< "$CHANGED_FILES" |
| 115 | + |
| 116 | + local struct_changes=$(echo "$struct_body_diff" | grep -E "^[-+]" \ |
| 117 | + | grep -v -E "^[-+]\s*(typedef struct|}|//|\*)" \ |
| 118 | + | sed -E 's://.*$::' \ |
| 119 | + | sed -E 's:/\*.*\*/::' \ |
| 120 | + | tr -d '[:space:]') |
| 121 | + |
| 122 | + if [ -n "$struct_changes" ]; then |
| 123 | + echo " ⚠️ Struct definition modified in $struct_found_in" |
| 124 | + |
| 125 | + # Check if version was incremented in PG_REGISTER |
| 126 | + local old_version=$(echo "$diff_output" | grep "^-.*PG_REGISTER.*$struct_type" | grep -oP ',\s*\K\d+(?=\s*\))' || echo "") |
| 127 | + local new_version=$(echo "$diff_output" | grep "^+.*PG_REGISTER.*$struct_type" | grep -oP ',\s*\K\d+(?=\s*\))' || echo "") |
| 128 | + |
| 129 | + # Find line number of PG_REGISTER for error reporting |
| 130 | + local line_num=$(git show $HEAD_COMMIT:"$file" | grep -n "PG_REGISTER.*$struct_type" | cut -d: -f1 | head -1) |
| 131 | + |
| 132 | + if [ -n "$old_version" ] && [ -n "$new_version" ]; then |
| 133 | + # PG_REGISTER was modified - check if version increased |
| 134 | + if [ "$new_version" -le "$old_version" ]; then |
| 135 | + echo " ❌ Version NOT incremented ($old_version → $new_version)" |
| 136 | + cat >> $ISSUES_FILE << EOF |
| 137 | +### \`$struct_type\` ($file:$line_num) |
| 138 | +- **Struct modified:** Field changes detected in $struct_found_in |
| 139 | +- **Version status:** ❌ Not incremented (version $version) |
| 140 | +- **Recommendation:** Increment version from $old_version to $(($old_version + 1)) |
| 141 | +
|
| 142 | +EOF |
| 143 | + else |
| 144 | + echo " ✅ Version incremented ($old_version → $new_version)" |
| 145 | + fi |
| 146 | + elif [ -z "$old_version" ] && [ -z "$new_version" ]; then |
| 147 | + # PG_REGISTER wasn't modified but struct was - THIS IS THE BUG! |
| 148 | + echo " ❌ PG_REGISTER not modified, version still $version" |
| 149 | + cat >> $ISSUES_FILE << EOF |
| 150 | +### \`$struct_type\` ($file:$line_num) |
| 151 | +- **Struct modified:** Field changes detected in $struct_found_in |
| 152 | +- **Version status:** ❌ Not incremented (still version $version) |
| 153 | +- **Recommendation:** Increment version to $(($version + 1)) in $file |
| 154 | +
|
| 155 | +EOF |
| 156 | + else |
| 157 | + # One exists but not the other - unusual edge case |
| 158 | + echo " ⚠️ Unusual version change pattern detected" |
| 159 | + cat >> $ISSUES_FILE << EOF |
| 160 | +### \`$struct_type\` ($file:$line_num) |
| 161 | +- **Struct modified:** Field changes detected in $struct_found_in |
| 162 | +- **Version status:** ⚠️ Unusual change pattern (old: ${old_version:-none}, new: ${new_version:-none}) |
| 163 | +- **Current version:** $version |
| 164 | +- **Recommendation:** Manually verify version increment |
| 165 | +
|
| 166 | +EOF |
| 167 | + fi |
| 168 | + else |
| 169 | + echo " ✅ Struct unchanged" |
| 170 | + fi |
| 171 | + fi |
| 172 | + done <<< "$pg_registers" |
| 173 | +} |
| 174 | + |
| 175 | +# Build list of files to check (changed files + companions with PG_REGISTER) |
| 176 | +echo "🔍 Building file list including companions with PG_REGISTER..." |
| 177 | +FILES_TO_CHECK="" |
| 178 | +ALREADY_ADDED="" |
| 179 | + |
| 180 | +while IFS= read -r file; do |
| 181 | + [ -z "$file" ] && continue |
| 182 | + |
| 183 | + # Add this file to check list |
| 184 | + if ! echo "$ALREADY_ADDED" | grep -qw "$file"; then |
| 185 | + FILES_TO_CHECK="$FILES_TO_CHECK$file"$'\n' |
| 186 | + ALREADY_ADDED="$ALREADY_ADDED $file" |
| 187 | + fi |
| 188 | + |
| 189 | + # Determine companion file (.c <-> .h) |
| 190 | + local companion="" |
| 191 | + if [[ "$file" == *.c ]]; then |
| 192 | + companion="${file%.c}.h" |
| 193 | + elif [[ "$file" == *.h ]]; then |
| 194 | + companion="${file%.h}.c" |
| 195 | + fi |
| 196 | + |
| 197 | + # If companion exists and contains PG_REGISTER, add it to check list |
| 198 | + if [ -n "$companion" ]; then |
| 199 | + if git show $HEAD_COMMIT:"$companion" 2>/dev/null | grep -q "PG_REGISTER"; then |
| 200 | + if ! echo "$ALREADY_ADDED" | grep -qw "$companion"; then |
| 201 | + echo " 📎 Adding $companion (companion of $file with PG_REGISTER)" |
| 202 | + FILES_TO_CHECK="$FILES_TO_CHECK$companion"$'\n' |
| 203 | + ALREADY_ADDED="$ALREADY_ADDED $companion" |
| 204 | + fi |
| 205 | + fi |
| 206 | + fi |
| 207 | +done <<< "$CHANGED_FILES" |
| 208 | + |
| 209 | +# Check each file (including companions) |
| 210 | +while IFS= read -r file; do |
| 211 | + [ -z "$file" ] && continue |
| 212 | + check_file_for_pg_changes "$file" |
| 213 | +done <<< "$FILES_TO_CHECK" |
| 214 | + |
| 215 | +# Check if any issues were found |
| 216 | +if [ -s $ISSUES_FILE ]; then |
| 217 | + echo "" |
| 218 | + echo "${YELLOW}⚠️ Potential PG version issues detected${NC}" |
| 219 | + echo "Output saved to: $ISSUES_FILE" |
| 220 | + cat $ISSUES_FILE |
| 221 | + exit 1 |
| 222 | +else |
| 223 | + echo "" |
| 224 | + echo "${GREEN}✅ No PG version issues detected${NC}" |
| 225 | + exit 0 |
| 226 | +fi |
0 commit comments