|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. The ASF licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +# Usage: ./scripts/verify-signed-commits.sh [--base-ref <ref>] [--head-ref <ref>] [--strict] [--help] |
| 21 | +set -e |
| 22 | + |
| 23 | +BASE_REF="origin/develop" |
| 24 | +HEAD_REF="HEAD" |
| 25 | +STRICT_MODE=false |
| 26 | + |
| 27 | +show_help() { |
| 28 | + cat << 'EOF' |
| 29 | +Usage: ./scripts/verify-signed-commits.sh [OPTIONS] |
| 30 | +
|
| 31 | +Options: |
| 32 | + --base-ref <ref> Base reference (default: origin/develop) |
| 33 | + --head-ref <ref> Head reference (default: HEAD) |
| 34 | + --strict Exit with error if unsigned commits found |
| 35 | + --help Show this help |
| 36 | +EOF |
| 37 | +} |
| 38 | + |
| 39 | +while [[ $# -gt 0 ]]; do |
| 40 | + case $1 in |
| 41 | + --base-ref) BASE_REF="$2"; shift 2 ;; |
| 42 | + --head-ref) HEAD_REF="$2"; shift 2 ;; |
| 43 | + --strict) STRICT_MODE=true; shift ;; |
| 44 | + --help) show_help; exit 0 ;; |
| 45 | + *) echo "Unknown option: $1"; show_help; exit 1 ;; |
| 46 | + esac |
| 47 | +done |
| 48 | + |
| 49 | +MERGE_BASE=$(git merge-base "$BASE_REF" "$HEAD_REF" 2>/dev/null || echo "") |
| 50 | +if [ -z "$MERGE_BASE" ]; then |
| 51 | + COMMIT_RANGE="$HEAD_REF~10..$HEAD_REF" |
| 52 | +else |
| 53 | + COMMIT_RANGE="$MERGE_BASE..$HEAD_REF" |
| 54 | +fi |
| 55 | + |
| 56 | +echo "Verifying commit signatures in range: $COMMIT_RANGE" |
| 57 | + |
| 58 | +COMMITS=$(git log --format="%H%x1f%G?%x1f%an%x1f%s" "$COMMIT_RANGE" 2>/dev/null || echo "") |
| 59 | +if [ -z "$COMMITS" ]; then |
| 60 | + echo "No commits to verify." |
| 61 | + exit 0 |
| 62 | +fi |
| 63 | + |
| 64 | +UNSIGNED_COUNT=0 |
| 65 | + |
| 66 | +while IFS=$'\x1f' read -r HASH SIG_STATUS AUTHOR SUBJECT; do |
| 67 | + [ -z "$HASH" ] && continue |
| 68 | + SHORT_HASH="${HASH:0:7}" |
| 69 | + |
| 70 | + case "$SIG_STATUS" in |
| 71 | + N) |
| 72 | + UNSIGNED_COUNT=$((UNSIGNED_COUNT + 1)) |
| 73 | + if [ -n "$GITHUB_ACTIONS" ]; then |
| 74 | + echo "::error title=Unsigned Commit::Commit $SHORT_HASH by $AUTHOR is not signed." |
| 75 | + else |
| 76 | + echo "❌ Unsigned: $SHORT_HASH - $SUBJECT ($AUTHOR)" |
| 77 | + fi |
| 78 | + ;; |
| 79 | + *) |
| 80 | + echo "✅ Signed: $SHORT_HASH - $SUBJECT" |
| 81 | + ;; |
| 82 | + esac |
| 83 | +done <<< "$COMMITS" |
| 84 | + |
| 85 | +echo "" |
| 86 | +echo "Summary: $UNSIGNED_COUNT unsigned commit(s) found." |
| 87 | + |
| 88 | +if [ "$STRICT_MODE" = true ] && [ "$UNSIGNED_COUNT" -gt 0 ]; then |
| 89 | + if [ -n "$GITHUB_ACTIONS" ]; then |
| 90 | + echo "::error::$UNSIGNED_COUNT unsigned commit(s). See CONTRIBUTING.md#signing-your-commits" |
| 91 | + else |
| 92 | + echo "❌ $UNSIGNED_COUNT unsigned commit(s). See CONTRIBUTING.md#signing-your-commits" |
| 93 | + fi |
| 94 | + exit 1 |
| 95 | +fi |
| 96 | + |
| 97 | +exit 0 |
0 commit comments