Skip to content

Commit 7f1a6ff

Browse files
ssitar583anand-ky
andauthored
RDKEMW-3485: Add validate_pr_desc.yml (#1920)
Co-authored-by: Anand Kandasamy <[email protected]>
1 parent 3c28ee0 commit 7f1a6ff

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
name: PR Description Validation
2+
3+
on:
4+
pull_request:
5+
branches: [develop]
6+
types: [opened, edited, synchronize]
7+
8+
jobs:
9+
validate-pr-description:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v4
14+
15+
- name: Validate PR Description
16+
env:
17+
PR_TITLE: ${{ github.event.pull_request.title }}
18+
PR_BODY: ${{ github.event.pull_request.body }}
19+
run: |
20+
# Define valid ticket IDs
21+
VALID_TICKET_IDS=("RDKEMW" "RDKEVD" "IMMUI" "RDK")
22+
23+
# Function to validate ticket format and ID
24+
validate_ticket() {
25+
local text="$1"
26+
local field_name="$2"
27+
28+
echo "Validating $field_name: $text"
29+
30+
# Check if text matches the pattern <TICKETID>-<ticketno.> : <desc>
31+
if [[ ! "$text" =~ ^[A-Z0-9]+-[0-9]+[[:space:]]*:[[:space:]]*.+ ]]; then
32+
echo "ERROR: $field_name format is invalid."
33+
echo "Expected format: <TICKETID>-<ticketno.> : <description>"
34+
echo "Example: RDKEMW-123 : Fix playbook issue"
35+
echo ""
36+
echo "Valid ticket IDs are:"
37+
printf "%s\n" "${VALID_TICKET_IDS[@]}"
38+
return 1
39+
fi
40+
41+
# Extract ticket ID from the text
42+
local ticket_prefix=$(echo "$text" | sed -n 's/^\([A-Z0-9]\+\)-[0-9]\+[[:space:]]*:.*$/\1/p')
43+
44+
if [ -z "$ticket_prefix" ]; then
45+
echo "ERROR: Could not extract ticket ID from $field_name."
46+
echo "Expected format: <TICKETID>-<ticketno.> : <description>"
47+
echo ""
48+
echo "Valid ticket IDs are:"
49+
printf "%s\n" "${VALID_TICKET_IDS[@]}"
50+
return 1
51+
fi
52+
53+
# Check if extracted ticket ID is in the valid list
54+
local valid=false
55+
for valid_id in "${VALID_TICKET_IDS[@]}"; do
56+
if [ "$ticket_prefix" = "$valid_id" ]; then
57+
valid=true
58+
break
59+
fi
60+
done
61+
62+
if [ "$valid" = false ]; then
63+
echo "ERROR: Invalid ticket ID '$ticket_prefix' in $field_name"
64+
echo ""
65+
echo "Valid ticket IDs are:"
66+
printf "%s\n" "${VALID_TICKET_IDS[@]}"
67+
echo ""
68+
echo "Your $field_name should start with one of the above ticket IDs followed by a number."
69+
echo "Example: RDKEMW-123 : Fix playbook issue"
70+
return 1
71+
fi
72+
73+
echo "$field_name validation passed! Ticket ID: $ticket_prefix"
74+
return 0
75+
}
76+
77+
# Track validation results
78+
TITLE_VALID=true
79+
DESCRIPTION_VALID=true
80+
81+
# Validate PR Title
82+
echo "=== Validating PR Title ==="
83+
if ! validate_ticket "$PR_TITLE" "PR title"; then
84+
TITLE_VALID=false
85+
fi
86+
87+
echo ""
88+
echo "=== Validating PR Description ==="
89+
# Validate PR Description
90+
if [ -n "$PR_BODY" ]; then
91+
if ! validate_ticket "$PR_BODY" "PR description"; then
92+
DESCRIPTION_VALID=false
93+
fi
94+
else
95+
echo "ERROR: PR description is empty."
96+
echo "Both PR title and description must contain valid ticket IDs."
97+
DESCRIPTION_VALID=false
98+
fi
99+
100+
echo ""
101+
echo "=== Validation Summary ==="
102+
echo "PR Title: $([ "$TITLE_VALID" = true ] && echo "PASSED" || echo " FAILED")"
103+
echo "PR Description: $([ "$DESCRIPTION_VALID" = true ] && echo "PASSED" || echo "FAILED")"
104+
105+
# Exit with error if either validation failed
106+
if [ "$TITLE_VALID" = false ] || [ "$DESCRIPTION_VALID" = false ]; then
107+
echo ""
108+
echo "VALIDATION FAILED: Both PR title and description must contain valid ticket IDs from the approved list: ${VALID_TICKET_IDS[@]}"
109+
exit 1
110+
fi
111+
112+
echo ""
113+
echo "ALL VALIDATIONS PASSED! Both PR title and description contain valid ticket IDs."

0 commit comments

Comments
 (0)