1name: Validate commit message 2 3on: 4 pull_request: 5 branches: [ master, google ] 6 7concurrency: 8 group: ${{ github.workflow }}-${{ github.ref }} 9 cancel-in-progress: true 10 11permissions: 12 contents: read 13 14jobs: 15 validate_commit_message: 16 runs-on: ubuntu-22.04 17 18 steps: 19 - uses: actions/checkout@v4 20 with: 21 ref: ${{ github.event.pull_request.head.sha }} 22 23 - name: Validate commit title 24 run: | 25 # Check that the commit title isn't excessively long. 26 commit_title="$(git log -1 --pretty=format:'%s')" 27 if [ "${#commit_title}" -gt 120 ]; then 28 echo "Error: the title of the commit is too long (max: 120 characters)" 29 exit 1 30 fi 31 32 lowercase_title="$(echo $commit_title | awk '{print tolower($0)}')" 33 # Check that the commit title isn't 'internal' (ignore case) 34 if [ "$lowercase_title" = "internal" ]; then 35 echo "Error: '$commit_title' is not a valid commit title" 36 exit 1 37 fi 38 39 - name: Validate commit body 40 run: | 41 # Check that the commit has a body 42 commit_body="$(git log -1 --pretty=format:'%b' | grep -v 'PiperOrigin-RevId')" 43 if [ -z "$commit_body" ]; then 44 echo "Error: the commit should have a descriptive body" 45 exit 1 46 fi 47 48 while read -r line; do 49 if [ "${#line}" -gt 120 ] && [[ ! "$line" =~ (https?://|www\.) ]]; then 50 echo "Error: the following line of the commit body is too long (max: 120 characters):" 51 echo "> $line" 52 exit 1 53 fi 54 done <<< $commit_body 55