|
| 1 | +name: Enforce Commit Email and Signature Policy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + pull_request: |
| 6 | + types: [opened, synchronize, reopened] |
| 7 | + |
| 8 | +jobs: |
| 9 | + check-commits: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + fetch-depth: 0 |
| 17 | + |
| 18 | + - name: Validate commits |
| 19 | + run: | |
| 20 | + set -e |
| 21 | +
|
| 22 | + echo "Event: ${{ github.event_name }}" |
| 23 | +
|
| 24 | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 25 | + BASE_SHA="${{ github.event.pull_request.base.sha }}" |
| 26 | + HEAD_SHA="${{ github.event.pull_request.head.sha }}" |
| 27 | + elif [[ "${{ github.event_name }}" == "push" ]]; then |
| 28 | + BASE_SHA="${{ github.event.before }}" |
| 29 | + HEAD_SHA="${{ github.event.after }}" |
| 30 | + else |
| 31 | + echo "Unsupported event: ${{ github.event_name }}" |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | +
|
| 35 | + echo "Checking commits between $BASE_SHA and $HEAD_SHA..." |
| 36 | +
|
| 37 | + BLOCKED=false |
| 38 | +
|
| 39 | + git log ${BASE_SHA}..${HEAD_SHA} --pretty=format:"%H|%ae|%G?|%GS" | while IFS="|" read -r hash email sig_status signer; do |
| 40 | + echo "➡ Commit: $hash" |
| 41 | + echo " Author Email: $email" |
| 42 | + echo " Signature: $sig_status by $signer" |
| 43 | +
|
| 44 | + # Check approved emails |
| 45 | + if [[ "$email" == *@hitachivantara.com ]] || [[ "$email" == *@hitachids.com ]] || [[ "$email" =~ @users\.noreply\.github\.com$ ]] || [[ "$email" == "noreply@github.com" ]]; then |
| 46 | + echo "Allowed email domain" |
| 47 | + else |
| 48 | + echo "Blocked email domain: $email" |
| 49 | + BLOCKED=true |
| 50 | + fi |
| 51 | +
|
| 52 | + # Signature check |
| 53 | + if [[ "$sig_status" != "G" && "$sig_status" != "U" ]]; then |
| 54 | + echo "Commit not signed or not verified" |
| 55 | + BLOCKED=true |
| 56 | + else |
| 57 | + echo "Commit is signed and verified" |
| 58 | + fi |
| 59 | + done |
| 60 | +
|
| 61 | + if [ "$BLOCKED" = true ]; then |
| 62 | + echo "One or more commits have unauthorized emails or are not properly signed." |
| 63 | + exit 1 |
| 64 | + else |
| 65 | + echo "All commits pass policy checks." |
| 66 | + fi |
0 commit comments