Skip to content

Commit

Permalink
chore: add validation for PR description and checklist
Browse files Browse the repository at this point in the history
  • Loading branch information
oCHRISo committed Oct 9, 2024
1 parent b3be362 commit 8c046cd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/scripts/validate_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
import sys
import re
import requests

# GitHub environment variables
pr_number = os.getenv("PR_NUMBER")
repo_name = os.getenv("GITHUB_REPOSITORY")
token = os.getenv("GITHUB_TOKEN")

# API request to get PR body
url = f"https://api.github.com/repos/{repo_name}/pulls/{pr_number}"
headers = {"Authorization": f"token {token}"}
response = requests.get(url, headers=headers)

if response.status_code != 200:
print("Error fetching PR details")
sys.exit(1)

pr_body = response.json().get("body", "")

# Check for 'Proposed changes' section
proposed_changes_match = re.search(r"### Proposed changes\s+(.+)", pr_body, re.DOTALL)
if proposed_changes_match:
proposed_changes_text = proposed_changes_match.group(1).strip()
word_count = len(proposed_changes_text.split())

if word_count <= 10:
print(f"Error: 'Proposed changes' section should have more than 10 words. Found {word_count} words.")
sys.exit(1)
else:
print("Error: 'Proposed changes' section is missing.")
sys.exit(1)

# Check if the first two checklist items are selected
if not re.search(r"- \[x\] I have read the \[`CONTRIBUTING`\]", pr_body):
print("Error: The first checklist item is not checked.")
sys.exit(1)

if not re.search(r"- \[x\] I have run `make install-tools`", pr_body):
print("Error: The second checklist item is not checked.")
sys.exit(1)

print("PR description is valid.")
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ jobs:
run: make install-tools
- name: run lint
run: make lint
pr-description-checklist-check:
name: Validate PR Description and Checklist
runs-on: ubuntu-24.04
steps:
- name: Check out code
uses: actions/[email protected]

- name: Validate PR description and checklist
run: |
python3 .github/scripts/validate_pr.py
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
unit-test:
name: Unit Tests
runs-on: ubuntu-22.04
Expand Down

0 comments on commit 8c046cd

Please sign in to comment.