diff --git a/.github/workflows/pr-text-checker.yaml b/.github/workflows/pr-text-checker.yaml new file mode 100644 index 00000000..ea24c9f6 --- /dev/null +++ b/.github/workflows/pr-text-checker.yaml @@ -0,0 +1,93 @@ +# Copyright 2026 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Check the subject and description of each PR for basic best practices and +# fail with an error if a PR doesn't meet the conditions. + +name: Pull request text checker +run-name: >- + Check title and description of PR + #${{github.event.inputs.pr-number || github.event.pull_request.number}} + by ${{github.actor}} + +on: + pull_request: + types: + - opened + - reopened + - synchronize + branches: + - main + + workflow_dispatch: + inputs: + pr-number: + description: 'The PR number of the PR to check:' + type: string + required: true + debug: + description: 'Run with debugging options' + type: boolean + default: true + +permissions: read-all + +jobs: + check-pr-text: + name: "Title & description check" + if: github.repository_owner == 'quantumlib' + runs-on: ubuntu-slim + timeout-minutes: 5 + steps: + - name: Enable shell debugging if requested + if: ${{inputs.debug || runner.debug}} + run: echo "SHELLOPTS=xtrace" >> "${GITHUB_ENV}" + + # Note that this workflow does not need to check out the git repo because + # it does not need to read any files in the repo. + - name: Check title and description + run: | + pr_title=$(jq -r ".pull_request.title" "${GITHUB_EVENT_PATH}") + pr_body=$(jq -r ".pull_request.body" "${GITHUB_EVENT_PATH}") + + declare -a problems=() + if [[ -z "${pr_title}" ]]; then + problems+=("PR title cannot be empty.") + else + read -ra title_words <<< "${pr_title}" + if [[ ${#title_words[@]} -lt 2 ]]; then + problems+=("PR title must contain at least two words.") + fi + fi + + if [[ -z "${pr_body}" || ${#pr_body} -lt 10 ]]; then + problems+=("PR description must be at least 10 characters long.") + fi + + if [[ ${#problems[@]} -gt 0 ]]; then + # shellcheck disable=SC2034 + { + echo "Please fix the following problem(s) with this PR:" + printf -- ":x: %s\n" "${problems[@]}" + } >> "${GITHUB_STEP_SUMMARY}" + fi + + # Point people to the list of problems. Done as a separate step to make + # this text easier for people to find in the workflow results. + - name: Report outcome + run: | + if [[ "${result}" == "error" ]]; then + echo '::error::Failed checks. Please check the workflow summary.' + exit 1 + fi