|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | +IFS=$'\n\t' |
| 4 | + |
| 5 | +# Mode: "validate" (default, exits non-zero on human edit) or "detect" (outputs to GITHUB_OUTPUT) |
| 6 | +MODE="${MODE:-validate}" |
| 7 | +ALLOWED_BOTS="${ALLOWED_BOTS:-github-actions[bot],dependabot[bot]}" |
| 8 | + |
| 9 | +# Determine the comparison range |
| 10 | +is_pr="" |
| 11 | +if [ "${GITHUB_EVENT_NAME:-}" = "pull_request" ]; then |
| 12 | + is_pr=1 |
| 13 | +fi |
| 14 | +has_base_ref="" |
| 15 | +if [ -n "${GITHUB_BASE_REF:-}" ]; then |
| 16 | + has_base_ref=1 |
| 17 | +fi |
| 18 | +origin_base_ref_exists="" |
| 19 | +if [ -n "${GITHUB_BASE_REF:-}" ] && git rev-parse --verify "origin/${GITHUB_BASE_REF}" >/dev/null 2>&1; then |
| 20 | + origin_base_ref_exists=1 |
| 21 | +fi |
| 22 | +if [ -n "$is_pr" ] && [ -n "$has_base_ref" ] && [ -n "$origin_base_ref_exists" ]; then |
| 23 | + BASE_REF="$(git rev-parse "origin/${GITHUB_BASE_REF}")" |
| 24 | + COMPARE_RANGE="$BASE_REF...HEAD" |
| 25 | +else |
| 26 | + COMPARE_RANGE="HEAD~1..HEAD" |
| 27 | +fi |
| 28 | + |
| 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 |
| 37 | + |
| 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 |
| 47 | + fi |
| 48 | +fi |
| 49 | + |
| 50 | +# Get latest commit that touched requirements.txt |
| 51 | +latest_sha=$(git log -1 --pretty=format:'%H' $COMPARE_RANGE -- requirements.txt || true) |
| 52 | + |
| 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 |
| 60 | + |
| 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:]]*$//') |
| 65 | + |
| 66 | +# Build a grep-friendly regex from comma-separated allowed bots |
| 67 | +allowed_regex=$(echo "$ALLOWED_BOTS" | sed 's/,/\\|/g') |
| 68 | + |
| 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}" |
| 74 | + fi |
| 75 | + exit 0 |
| 76 | +fi |
| 77 | + |
| 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 |
| 86 | + exit 0 |
| 87 | + fi |
| 88 | +fi |
| 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 |
| 98 | + echo "::error::You may NOT edit 'requirements.txt'" |
| 99 | + echo "::warning::Undo your changes to requirements.txt, so robot can maintain it." |
| 100 | + echo "::notice::To pin dependencies, use 'poetry add <package-name>'." |
| 101 | + echo "Latest commit: $latest_sha" |
| 102 | + echo "Latest author: $latest_author" |
| 103 | + echo "Latest committer: $latest_committer" |
| 104 | + echo "Latest message: \"$latest_subject\"" |
| 105 | + exit 1 |
| 106 | +fi |
0 commit comments