Skip to content
Merged
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
41 changes: 41 additions & 0 deletions .github/scripts/validate_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os

def validate_branch_and_pr_title(branch, pr_title):
"""
Validates if the branch name and PR title follow the 'TADocs-' pattern.
Args:
branch (str): The name of the branch to validate.
pr_title (str): The title of the pull request (PR) to validate.
Returns:
bool: True if both the branch and the pull request follow the 'TADocs-' pattern, False otherwise.
Prints:
str: A message indicating whether the branch and PR title are valid or specifying the validation error.
"""

# Verify if the branch starts with 'TADocs-'
if not branch.startswith("TADocs-"):
print(f"The branch '{branch}' does not start with the pattern 'TADocs-'.")
return False
# Verify if the branch contains with 'TADocs-'
if "TADocs-" not in pr_title:
print(f"The PR title '{pr_title}' does not contain the pattern 'TADocs-'.")
return False

print(f"Validation successful: The branch '{branch}' and the PR title '{pr_title}' follow the 'TADocs-' pattern.")

return True

if __name__ == "__main__":
PR_BRANCH = os.getenv("GITHUB_HEAD_REF")
PR_TITLE = os.getenv("PR_TITLE")

if not PR_BRANCH:
raise ValueError("The environment variable GITHUB_HEAD_REF is not defined.")
if not PR_TITLE:
raise ValueError("The environment variable PR_TITLE is not defined.")

print(f"Captured PR branch: {PR_BRANCH}")
print(f"Captured PR title: {PR_TITLE}")

if not validate_branch_and_pr_title(PR_BRANCH, PR_TITLE):
exit(1)
51 changes: 51 additions & 0 deletions .github/workflows/card-validation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Validate PR with Ticket
on:
pull_request:
types: [opened, reopened, synchronize, edited]
jobs:
validate-pr:
runs-on: ubuntu-latest

permissions:
pull-requests: write
issues: write
contents: read

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Validate PR ticket link
id: validate_pr
env:
GITHUB_HEAD_REF: ${{ github.head_ref }} # Branch name
PR_TITLE: ${{ github.event.pull_request.title }} # PR Title
run: |
if ! python scripts/validate_pr.py; then
echo "validation_failed=true" >> $GITHUB_OUTPUT
fi

- name: Notify user and fail job if validation fails
if: steps.validate_pr.outputs.validation_failed == 'true'
uses: actions/github-script@v6
with:
script: |
// Add a comment to the PR
const { data: comment } = await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: "**Validation Failed!** \n\n" +
"The branch name and PR title must follow the `TADocs-` pattern.\n" +
"- Ensure the branch name starts with `TADocs-`.\n" +
"- Ensure the PR title contains `TADocs-`.\n\n" +
"Please adjust and try again. Thank you!"
});
console.log(`Comment added: ${comment.html_url}`);
// Fail the job manually
throw new Error("Validation failed. Check the comment on the PR for more details.");
41 changes: 41 additions & 0 deletions scripts/validate_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os

def validate_branch_and_pr_title(branch, pr_title):
"""
Validates if the branch name and PR title follow the 'TADocs-' pattern.
Args:
branch (str): The name of the branch to validate.
pr_title (str): The title of the pull request (PR) to validate.
Returns:
bool: True if both the branch and the pull request follow the 'TADocs-' pattern, False otherwise.
Prints:
str: A message indicating whether the branch and PR title are valid or specifying the validation error.
"""

# Verify if the branch starts with 'TADocs-'
if not branch.startswith("TADocs-"):
print(f"The branch '{branch}' does not start with the pattern 'TADocs-'.")
return False
# Verify if the branch contains with 'TADocs-'
if "TADocs-" not in pr_title:
print(f"The PR title '{pr_title}' does not contain the pattern 'TADocs-'.")
return False

print(f"Validation successful: The branch '{branch}' and the PR title '{pr_title}' follow the 'TADocs-' pattern.")

return True

if __name__ == "__main__":
PR_BRANCH = os.getenv("GITHUB_HEAD_REF")
PR_TITLE = os.getenv("PR_TITLE")

if not PR_BRANCH:
raise ValueError("The environment variable GITHUB_HEAD_REF is not defined.")
if not PR_TITLE:
raise ValueError("The environment variable PR_TITLE is not defined.")

print(f"Captured PR branch: {PR_BRANCH}")
print(f"Captured PR title: {PR_TITLE}")

if not validate_branch_and_pr_title(PR_BRANCH, PR_TITLE):
exit(1)
Loading