Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions .github/bin/test/pr-description/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
# Test runner for PR description validation

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VALIDATOR="$SCRIPT_DIR/../../validate-pr-description.py"
TEST_DIR="$SCRIPT_DIR"

echo "Running PR description validation tests..."
echo

# Track test results
PASSED=0
FAILED=0

# Test helper function
run_test() {
local test_name="$1"
local test_file="$2"
local expected_exit_code="$3"

echo -n "Testing $test_name... "

if "$VALIDATOR" "$test_file" > /dev/null 2>&1; then
actual_exit_code=0
else
actual_exit_code=$?
fi

if [ "$actual_exit_code" -eq "$expected_exit_code" ]; then
echo "✓ PASS"
PASSED=$((PASSED + 1))
else
echo "✗ FAIL (expected exit code $expected_exit_code, got $actual_exit_code)"
FAILED=$((FAILED + 1))
fi
}

# Run tests
run_test "empty description" "$TEST_DIR/test_empty.txt" 1
run_test "valid description" "$TEST_DIR/test_valid.txt" 0
run_test "sourcery bot only" "$TEST_DIR/test_sourcery.txt" 1
run_test "no template, valid" "$TEST_DIR/test_no_template_valid.txt" 0
run_test "no template, short" "$TEST_DIR/test_no_template_short.txt" 1

echo
echo "================================"
echo "Test Results: $PASSED passed, $FAILED failed"
echo "================================"

if [ "$FAILED" -gt 0 ]; then
exit 1
fi
26 changes: 26 additions & 0 deletions .github/bin/test/pr-description/test_empty.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!-- Thank you for submitting a pull request! Find more information
at https://trino.io/development/process.html,
at https://github.com/trinodb/trino/blob/master/.github/DEVELOPMENT.md
and contact us on #core-dev in Slack. -->
<!-- Provide an overview for maintainers and reviewers. -->
## Description



<!-- Provide details that help an engineer who is unfamiliar with this part of the code. -->
## Additional context and related issues



<!-- Mark the appropriate option with an (x). Propose a release note if you can.
More info at https://trino.io/development/process#release-note -->
## Release notes

( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text:

```markdown
## Section
* Fix some things. ({issue}`issuenumber`)
```
5 changes: 5 additions & 0 deletions .github/bin/test/pr-description/test_no_template_short.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix bug

## Summary by Sourcery

Long verbose AI-generated content that we should ignore.
9 changes: 9 additions & 0 deletions .github/bin/test/pr-description/test_no_template_valid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This PR fixes the authentication bug by properly validating JWT tokens before processing requests. The issue was caused by missing null checks in the token validation logic.

## Summary by Sourcery

Some verbose AI-generated text here that we want to ignore.

Changes:
- Added null checks
- Updated tests
14 changes: 14 additions & 0 deletions .github/bin/test/pr-description/test_sourcery.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Description



## Summary by Sourcery

This pull request includes various improvements to the codebase including refactoring some methods, updating documentation, and optimizing performance in several key areas. The changes are backward compatible and include comprehensive test coverage.

Key changes:
- Refactored method X
- Updated documentation for Y
- Optimized Z

## Additional context and related issues
17 changes: 17 additions & 0 deletions .github/bin/test/pr-description/test_valid.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Thank you for submitting a pull request! -->
## Description

This PR adds support for JSON parsing with better error handling. It includes validation for malformed JSON and provides helpful error messages to users.

## Additional context and related issues

Fixes #12345

## Release notes

(x) Release notes are required, with the following suggested text:

```markdown
## General
* Add improved JSON parsing with better error messages. ({issue}`12345`)
```
135 changes: 135 additions & 0 deletions .github/bin/validate-pr-description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env python3
"""
Validate that PR descriptions contain meaningful content.

This script checks:
1. PR description is not empty
2. Description section (if template is used) has content
3. Ignores Sourcery AI bot summaries
4. Requires minimum meaningful content
"""

import re
import sys


def remove_html_comments(text):
"""Remove HTML comments from text."""
return re.sub(r'<!--.*?-->', '', text, flags=re.DOTALL)


def remove_sourcery_section(text):
"""Remove 'Summary by Sourcery' section."""
# Match the Sourcery heading and everything until the next heading or end
pattern = r'##\s+Summary by Sourcery.*?(?=##|$)'
return re.sub(pattern, '', text, flags=re.DOTALL | re.IGNORECASE)


def extract_description_section(text):
"""
Extract content from the Description section of the template.
Returns None if template is not used.
"""
# Look for "## Description" heading
match = re.search(r'##\s+Description\s*\n(.*?)(?=##|$)', text, flags=re.DOTALL)
if match:
return match.group(1)
return None


def get_meaningful_content(text):
"""
Extract meaningful content by removing comments, sourcery, and whitespace.
"""
# Remove HTML comments
text = remove_html_comments(text)

# Remove Sourcery section
text = remove_sourcery_section(text)

# Remove markdown code blocks that are empty or just placeholders
text = re.sub(r'```[a-z]*\s*```', '', text)

# Remove excessive whitespace
text = re.sub(r'\n\s*\n', '\n', text)

return text.strip()


def validate_pr_description(description):
"""
Validate PR description.
Returns (is_valid, error_message).
"""
if not description or not description.strip():
return False, "PR description is empty. Please provide a description of your changes."

# Check if template is used
description_section = extract_description_section(description)

if description_section is not None:
# Template is used, validate the Description section
meaningful_content = get_meaningful_content(description_section)

if not meaningful_content:
return False, (
"PR description's 'Description' section is empty. "
"Please provide a meaningful description of your changes."
)

# Require at least 20 characters of meaningful content
if len(meaningful_content) < 20:
return False, (
f"PR description's 'Description' section is too short ({len(meaningful_content)} chars). "
"Please provide a more detailed description (at least 20 characters)."
)
else:
# Template not used, validate the entire description
meaningful_content = get_meaningful_content(description)

if not meaningful_content:
return False, (
"PR description is empty or contains only template comments. "
"Please provide a meaningful description of your changes."
)

# Require at least 50 characters when template is not used
if len(meaningful_content) < 50:
return False, (
f"PR description is too short ({len(meaningful_content)} chars). "
"Please provide a more detailed description (at least 50 characters)."
)

return True, "PR description is valid."


def main():
if len(sys.argv) < 2:
print("Usage: validate-pr-description.py <pr_description_file>", file=sys.stderr)
sys.exit(1)

description_file = sys.argv[1]

try:
with open(description_file, 'r', encoding='utf-8') as f:
description = f.read()
except FileNotFoundError:
print(f"Error: File '{description_file}' not found.", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error reading file: {e}", file=sys.stderr)
sys.exit(1)

is_valid, message = validate_pr_description(description)

if is_valid:
print("✓", message)
sys.exit(0)
else:
print("✗", message, file=sys.stderr)
print("\nPlease update your PR description to include meaningful information about your changes.", file=sys.stderr)
sys.exit(1)


if __name__ == "__main__":
main()
35 changes: 35 additions & 0 deletions .github/workflows/pr-description.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: "PR Description Check"

on:
pull_request:
types: [opened, edited, synchronize, reopened]

permissions:
contents: read

jobs:
validate-description:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Run validation script tests
run: |
.github/bin/test/pr-description/run-tests.sh
- name: Get PR description
id: pr-description
env:
PR_BODY: ${{ github.event.pull_request.body }}
run: |
echo "$PR_BODY" > pr_description.txt
- name: Validate PR description
run: |
python3 .github/bin/validate-pr-description.py pr_description.txt
Loading