|
2 | 2 | set -euo pipefail |
3 | 3 | IFS=$'\n\t' |
4 | 4 |
|
| 5 | +# Mode: "validate" (default, exits non-zero on human edit) or "detect" (outputs to GITHUB_OUTPUT) |
| 6 | +MODE="${MODE:-validate}" |
5 | 7 | ALLOWED_BOTS="${ALLOWED_BOTS:-github-actions[bot],dependabot[bot]}" |
6 | 8 |
|
7 | 9 | # Determine the comparison range |
|
24 | 26 | COMPARE_RANGE="HEAD~1..HEAD" |
25 | 27 | fi |
26 | 28 |
|
27 | | -# If requirements.txt changed in comparison range, ensure latest change's commit |
28 | | -# was authored by an allowed bot, or the latest commit message exactly matches |
29 | | -# the canonical bot commit message, or fallback to any bot-authored commit. |
30 | | -if git diff --name-only $COMPARE_RANGE | grep -q "^requirements.txt$"; then |
31 | | - latest_sha=$(git log -1 --pretty=format:'%H' $COMPARE_RANGE -- requirements.txt || true) |
| 29 | +# Check if requirements.txt changed |
| 30 | +if ! git diff --name-only $COMPARE_RANGE | grep -q "^requirements.txt$"; then |
| 31 | + echo "'requirements.txt' unchanged" |
| 32 | + if [ "$MODE" = "detect" ]; then |
| 33 | + echo "is_human_edit=false" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 34 | + fi |
| 35 | + exit 0 |
| 36 | +fi |
32 | 37 |
|
33 | | - if [ -z "$latest_sha" ]; then |
34 | | - echo "::error::No commits found touching requirements.txt in range $COMPARE_RANGE" |
35 | | - exit 1 |
| 38 | +# Check if requirements.txt differs from base (net change across all commits in range) |
| 39 | +if [ -n "$is_pr" ] && [ -n "$has_base_ref" ] && [ -n "$origin_base_ref_exists" ]; then |
| 40 | + BASE_REF_PARSED="origin/${GITHUB_BASE_REF}" |
| 41 | + if git diff --quiet "$BASE_REF_PARSED" HEAD -- requirements.txt; then |
| 42 | + echo "requirements.txt touched but matches base branch (likely reverted): OK" |
| 43 | + if [ "$MODE" = "detect" ]; then |
| 44 | + echo "is_human_edit=false" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 45 | + fi |
| 46 | + exit 0 |
36 | 47 | fi |
| 48 | +fi |
37 | 49 |
|
38 | | - latest_author=$(git show -s --format='%an' "$latest_sha") |
39 | | - latest_committer=$(git show -s --format='%cn' "$latest_sha") |
40 | | - latest_message=$(git show -s --format='%B' "$latest_sha") |
| 50 | +# Get latest commit that touched requirements.txt |
| 51 | +latest_sha=$(git log -1 --pretty=format:'%H' $COMPARE_RANGE -- requirements.txt || true) |
41 | 52 |
|
42 | | - echo "Latest commit touching requirements.txt: $latest_sha" |
43 | | - echo " author: $latest_author" |
44 | | - echo " committer: $latest_committer" |
45 | | - echo " message: $(echo "$latest_message" | head -n1)" |
| 53 | +if [ -z "$latest_sha" ]; then |
| 54 | + echo "::error::No commits found touching requirements.txt in range $COMPARE_RANGE" |
| 55 | + if [ "$MODE" = "detect" ]; then |
| 56 | + echo "is_human_edit=false" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 57 | + fi |
| 58 | + exit 1 |
| 59 | +fi |
46 | 60 |
|
47 | | - # Build a grep-friendly regex from comma-separated allowed bots |
48 | | - allowed_regex=$(echo "$ALLOWED_BOTS" | sed 's/,/\\|/g') |
| 61 | +latest_author=$(git show -s --format='%an' "$latest_sha") |
| 62 | +latest_committer=$(git show -s --format='%cn' "$latest_sha") |
| 63 | +latest_message=$(git show -s --format='%B' "$latest_sha") |
| 64 | +latest_subject=$(echo "$latest_message" | head -n1 | sed -e 's/[[:space:]]*$//') |
49 | 65 |
|
50 | | - # 1) author or committer is allowed bot |
51 | | - if echo "$latest_author" | grep -qE "^($allowed_regex)$" || echo "$latest_committer" | grep -qE "^($allowed_regex)$"; then |
52 | | - echo "Latest change by allowed bot: OK" |
53 | | - exit 0 |
54 | | - fi |
| 66 | +# Build a grep-friendly regex from comma-separated allowed bots |
| 67 | +allowed_regex=$(echo "$ALLOWED_BOTS" | sed 's/,/\\|/g') |
55 | 68 |
|
56 | | - # 2) latest commit message exactly equals canonical message |
57 | | - if [ -n "${COMMIT_MSG_FILE:-}" ] && [ -f "$COMMIT_MSG_FILE" ]; then |
58 | | - canonical_msg=$(sed -n '1p' "$COMMIT_MSG_FILE" | tr -d '\r') |
59 | | - # Compare exact first-line equality (trim trailing newline/space) |
60 | | - latest_first_line=$(echo "$latest_message" | head -n1 | sed -e 's/[[:space:]]*$//') |
61 | | - if [ "$latest_first_line" = "$canonical_msg" ]; then |
62 | | - echo "Latest commit message exactly matches canonical bot message: OK" |
63 | | - exit 0 |
64 | | - fi |
| 69 | +# Check 1: author or committer is allowed bot |
| 70 | +if echo "$latest_author" | grep -qE "^($allowed_regex)$" || echo "$latest_committer" | grep -qE "^($allowed_regex)$"; then |
| 71 | + echo "Latest change by allowed bot: OK" |
| 72 | + if [ "$MODE" = "detect" ]; then |
| 73 | + echo "is_human_edit=false" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
65 | 74 | fi |
| 75 | + exit 0 |
| 76 | +fi |
66 | 77 |
|
67 | | - # 3) fallback: any commit touching the file in range has allowed bot author or committer |
68 | | - if git log $COMPARE_RANGE --pretty=format:'%an|%cn' -- requirements.txt | grep -qE "($allowed_regex)"; then |
69 | | - echo "Found a bot-authored/committed change touching requirements.txt in the range: OK" |
| 78 | +# Check 2: commit message exactly matches canonical message |
| 79 | +if [ -n "${COMMIT_MSG_FILE:-}" ] && [ -f "$COMMIT_MSG_FILE" ]; then |
| 80 | + canonical_msg=$(sed -n '1p' "$COMMIT_MSG_FILE" | tr -d '\r') |
| 81 | + if [ "$latest_subject" = "$canonical_msg" ]; then |
| 82 | + echo "Latest commit message exactly matches canonical bot message: OK" |
| 83 | + if [ "$MODE" = "detect" ]; then |
| 84 | + echo "is_human_edit=false" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 85 | + fi |
70 | 86 | exit 0 |
71 | 87 | fi |
| 88 | +fi |
72 | 89 |
|
| 90 | +# Human edit detected |
| 91 | +if [ "$MODE" = "detect" ]; then |
| 92 | + echo "is_human_edit=true" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 93 | + echo "offender_author=$latest_author" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 94 | + echo "offender_subject=$latest_subject" >> "${GITHUB_OUTPUT:-/dev/stdout}" |
| 95 | + echo "Human edit detected" |
| 96 | + exit 0 |
| 97 | +else |
73 | 98 | echo "::error::You may NOT edit 'requirements.txt'" |
74 | 99 | echo "::warning::Undo your changes to requirements.txt, so robot can maintain it." |
75 | 100 | echo "::notice::To pin dependencies, use 'poetry add <package-name>'." |
76 | 101 | echo "Latest commit: $latest_sha" |
77 | 102 | echo "Latest author: $latest_author" |
78 | 103 | echo "Latest committer: $latest_committer" |
79 | | - echo "Latest message: $(echo "$latest_message" | head -n1)" |
| 104 | + echo "Latest message: \"$latest_subject\"" |
80 | 105 | exit 1 |
81 | 106 | fi |
82 | | - |
83 | | -echo "'requirements.txt' unchanged (or latest change by allowed bot/marker)" |
|
0 commit comments