Skip to content
Open
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
113 changes: 113 additions & 0 deletions .github/workflows/validate_pr_desc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
name: PR Description Validation

on:
pull_request:
branches: [develop]
types: [opened, edited, synchronize]

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

- name: Validate PR Description
env:
PR_TITLE: ${{ github.event.pull_request.title }}
PR_BODY: ${{ github.event.pull_request.body }}
run: |
# Define valid ticket IDs
VALID_TICKET_IDS=("RDKEMW" "RDKEVD" "IMMUI" "RDK")

# Function to validate ticket format and ID
validate_ticket() {
local text="$1"
local field_name="$2"

echo "Validating $field_name: $text"

# Check if text matches the pattern <TICKETID>-<ticketno.> : <desc>
if [[ ! "$text" =~ ^[A-Z0-9]+-[0-9]+[[:space:]]*:[[:space:]]*.+ ]]; then
echo "ERROR: $field_name format is invalid."
echo "Expected format: <TICKETID>-<ticketno.> : <description>"
echo "Example: RDKEMW-123 : Fix playbook issue"
echo ""
echo "Valid ticket IDs are:"
printf "%s\n" "${VALID_TICKET_IDS[@]}"
return 1
fi

# Extract ticket ID from the text
local ticket_prefix=$(echo "$text" | sed -n 's/^\([A-Z0-9]\+\)-[0-9]\+[[:space:]]*:.*$/\1/p')

if [ -z "$ticket_prefix" ]; then
echo "ERROR: Could not extract ticket ID from $field_name."
echo "Expected format: <TICKETID>-<ticketno.> : <description>"
echo ""
echo "Valid ticket IDs are:"
printf "%s\n" "${VALID_TICKET_IDS[@]}"
return 1
fi

# Check if extracted ticket ID is in the valid list
local valid=false
for valid_id in "${VALID_TICKET_IDS[@]}"; do
if [ "$ticket_prefix" = "$valid_id" ]; then
valid=true
break
fi
done

if [ "$valid" = false ]; then
echo "ERROR: Invalid ticket ID '$ticket_prefix' in $field_name"
echo ""
echo "Valid ticket IDs are:"
printf "%s\n" "${VALID_TICKET_IDS[@]}"
echo ""
echo "Your $field_name should start with one of the above ticket IDs followed by a number."
echo "Example: RDKEMW-123 : Fix playbook issue"
return 1
fi

echo "$field_name validation passed! Ticket ID: $ticket_prefix"
return 0
}

# Track validation results
TITLE_VALID=true
DESCRIPTION_VALID=true

# Validate PR Title
echo "=== Validating PR Title ==="
if ! validate_ticket "$PR_TITLE" "PR title"; then
TITLE_VALID=false
fi

echo ""
echo "=== Validating PR Description ==="
# Validate PR Description
if [ -n "$PR_BODY" ]; then
if ! validate_ticket "$PR_BODY" "PR description"; then
DESCRIPTION_VALID=false
fi
else
echo "ERROR: PR description is empty."
echo "Both PR title and description must contain valid ticket IDs."
DESCRIPTION_VALID=false
fi

echo ""
echo "=== Validation Summary ==="
echo "PR Title: $([ "$TITLE_VALID" = true ] && echo "PASSED" || echo " FAILED")"
echo "PR Description: $([ "$DESCRIPTION_VALID" = true ] && echo "PASSED" || echo "FAILED")"

# Exit with error if either validation failed
if [ "$TITLE_VALID" = false ] || [ "$DESCRIPTION_VALID" = false ]; then
echo ""
echo "VALIDATION FAILED: Both PR title and description must contain valid ticket IDs from the approved list: ${VALID_TICKET_IDS[@]}"
exit 1
fi

echo ""
echo "ALL VALIDATIONS PASSED! Both PR title and description contain valid ticket IDs."