|
| 1 | +# Copyright 2025 "Google LLC" |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +name: Check PR Description |
| 16 | +on: |
| 17 | + pull_request: |
| 18 | + types: |
| 19 | + - opened |
| 20 | + - edited |
| 21 | + - synchronize |
| 22 | +jobs: |
| 23 | + check-description: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + steps: |
| 26 | + - name: Checkout repository |
| 27 | + uses: actions/checkout@v4 |
| 28 | + - name: Check PR description |
| 29 | + env: |
| 30 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 31 | + run: | |
| 32 | + TEMPLATE_PATH=".github/pull_request_template.md" |
| 33 | +
|
| 34 | + if [ -f "$TEMPLATE_PATH" ]; then |
| 35 | + TEMPLATE_BODY=$(cat "$TEMPLATE_PATH") |
| 36 | + else |
| 37 | + TEMPLATE_BODY="" |
| 38 | + fi |
| 39 | +
|
| 40 | + # Function to normalize text: removes empty lines, trims leading/trailing whitespace, and removes carriage returns |
| 41 | + normalize_text() { |
| 42 | + echo -n "$1" | tr -d '\r' | awk 'NF' | awk '{$1=$1};1' |
| 43 | + } |
| 44 | +
|
| 45 | + # 1. Check if the PR body is empty or contains only whitespace |
| 46 | + if [ -z "$(echo "$PR_BODY" | tr -d '[:space:]')" ]; then |
| 47 | + echo "Error: PR description is empty or contains only whitespace." |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +
|
| 51 | + # 2. Check if the PR body is identical to the template |
| 52 | + NORMALIZED_PR_BODY=$(normalize_text "$PR_BODY") |
| 53 | + NORMALIZED_TEMPLATE_BODY=$(normalize_text "$TEMPLATE_BODY") |
| 54 | +
|
| 55 | + if [[ "$NORMALIZED_PR_BODY" == "$NORMALIZED_TEMPLATE_BODY" ]]; then |
| 56 | + echo "Error: PR description only contains the template text. Please add a description of your changes." |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +
|
| 60 | + # 3. Check if at least 10 characters have been added to the description |
| 61 | + # Use `diff` to isolate the added lines between the template and the PR body. |
| 62 | + ADDED_TEXT=$(diff --changed-group-format="%>" --unchanged-group-format="" <(echo "$NORMALIZED_TEMPLATE_BODY") <(echo "$NORMALIZED_PR_BODY") || true) |
| 63 | + ADDED_TEXT_LEN=${#ADDED_TEXT} |
| 64 | +
|
| 65 | + if [[ "$ADDED_TEXT_LEN" -lt 10 ]]; then |
| 66 | + echo "Error: PR description must contain at least 10 added characters." |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | +
|
| 70 | + echo "PR description check passed." |
0 commit comments