From 852fbe955d3edd487b81d2c52caae978709fc218 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Mon, 20 Apr 2026 13:12:58 +0100 Subject: [PATCH] ci: Add workflow to check PR description has been filled (#6965) --- .github/scripts/check-pr-description.py | 85 ++++++++++++++++++++++ .github/workflows/check-pr-description.yml | 30 ++++++++ 2 files changed, 115 insertions(+) create mode 100644 .github/scripts/check-pr-description.py create mode 100644 .github/workflows/check-pr-description.yml diff --git a/.github/scripts/check-pr-description.py b/.github/scripts/check-pr-description.py new file mode 100644 index 0000000000..36abb0f5a4 --- /dev/null +++ b/.github/scripts/check-pr-description.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 + +""" +Checks that a pull request description has been customized from the +pull_request_template.md. Exits with code 1 if the description is empty +or identical to the template (ignoring HTML comments and whitespace). + +Usage: + python check-pr-description.py --template-file TEMPLATE --pr-body-file BODY +""" + +import argparse +import re +import sys +from pathlib import Path + + +def normalize(text: str) -> str: + """Strip HTML comments, trim lines, and remove blank lines.""" + # Remove HTML comments (possibly multi-line) + text = re.sub(r"", "", text, flags=re.DOTALL) + # Strip each line and drop empties + lines = [line.strip() for line in text.splitlines()] + lines = [line for line in lines if line] + return "\n".join(lines) + + +def main() -> int: + parser = argparse.ArgumentParser( + description="Check that a PR description differs from the template." + ) + parser.add_argument( + "--template-file", + type=Path, + required=True, + help="Path to the pull request template file.", + ) + parser.add_argument( + "--pr-body-file", + type=Path, + required=True, + help="Path to a file containing the PR body text.", + ) + args = parser.parse_args() + + template_path: Path = args.template_file + pr_body_path: Path = args.pr_body_file + + if not template_path.is_file(): + print(f"::error::Template file {template_path} not found") + return 1 + + if not pr_body_path.is_file(): + print(f"::error::PR body file {pr_body_path} not found") + return 1 + + template = template_path.read_text(encoding="utf-8") + pr_body = pr_body_path.read_text(encoding="utf-8") + + # Check if the PR body is empty or whitespace-only + if not pr_body.strip(): + print( + "::error::PR description is empty. " + "Please fill in the pull request template." + ) + return 1 + + norm_template = normalize(template) + norm_pr_body = normalize(pr_body) + + if norm_pr_body == norm_template: + print( + "::error::PR description (ignoring HTML comments) is identical" + " to the template. Please fill in the details of your change." + f"\n\nVisible template content:\n---\n{norm_template}\n---" + f"\n\nVisible PR description content:\n---\n{norm_pr_body}\n---" + ) + return 1 + + print("PR description has been customized from the template.") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/workflows/check-pr-description.yml b/.github/workflows/check-pr-description.yml new file mode 100644 index 0000000000..f6eee50291 --- /dev/null +++ b/.github/workflows/check-pr-description.yml @@ -0,0 +1,30 @@ +name: Check PR description + +on: + merge_group: + types: + - checks_requested + pull_request: + types: [opened, edited, reopened, synchronize, ready_for_review] + branches: [develop] + +jobs: + check_description: + if: ${{ github.event.pull_request.draft != true }} + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - name: Write PR body to file + env: + PR_BODY: ${{ github.event.pull_request.body }} + if: ${{ github.event_name == 'pull_request' }} + run: printenv PR_BODY > pr_body.md + + - name: Check PR description differs from template + if: ${{ github.event_name == 'pull_request' }} + run: > + python .github/scripts/check-pr-description.py + --template-file .github/pull_request_template.md + --pr-body-file pr_body.md