Skip to content

Commit 8866cbe

Browse files
fix: Update pr-issue-validator.yaml (#6133)
* Update pr-issue-validator.yaml * Update pr-issue-validator.yaml * Update pull_request_template.md PR template modifications to fix PR validator * Update pull_request_template.md revert last commit to take the changes through new PR * Update pr-issue-validator.yaml --------- Co-authored-by: Prakarsh <71125043+prakarsh-dt@users.noreply.github.com>
1 parent 88bd89e commit 8866cbe

File tree

1 file changed

+83
-109
lines changed

1 file changed

+83
-109
lines changed

.github/workflows/pr-issue-validator.yaml

Lines changed: 83 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -26,142 +26,119 @@ jobs:
2626
issues: write
2727
contents: read
2828
pull-requests: write
29-
3029
steps:
3130
- name: Checkout repository
3231
uses: actions/checkout@v2
3332
with:
3433
ref: ${{ github.event.pull_request.head.sha }}
3534
fetch-depth: 0
3635

37-
3836
- name: Validate Issue Reference
3937
env:
40-
GITHUB_TOKEN: ${{ github.token }}
38+
GH_TOKEN: ${{ github.token }}
4139
PR_BODY: ${{ github.event.pull_request.body }}
42-
url: ${{ github.event.pull_request.url }}
4340
PRNUM: ${{ github.event.pull_request.number }}
4441
TITLE: ${{ github.event.pull_request.title }}
4542
run: |
46-
echo "base or target repo : ${{ github.event.pull_request.base.repo.full_name }}"
47-
echo "head or source repo : ${{ github.event.pull_request.head.repo.full_name }}"
48-
if [[ ${{ github.event.pull_request.head.repo.full_name }} == ${{ github.event.pull_request.base.repo.full_name }} ]]; then
49-
export forked=false
50-
else
51-
export forked=true
52-
fi
53-
echo "forked: $forked"
54-
if [[ "$TITLE" == *"doc:"* || "$TITLE" == *"docs:"* || "$TITLE" == *"misc:"* || "$TITLE" == *"release:"* || "$TITLE" == *"Release:"* ]]; then
55-
echo "Skipping validation as this is a PR for documentation or misc."
56-
if [[ $forked == true ]]; then
57-
echo "PR:Ready-to-Review, exiting gracefully"
58-
exit 0
59-
fi
43+
set -x
44+
# Skip validation for documentation or chore PRs
45+
if [[ "$TITLE" =~ ^(doc:|docs:|chore:|misc:) ]]; then
46+
echo "Skipping validation for docs/chore PR."
47+
echo "PR NUMBER-: $PRNUM "
6048
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
6149
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
6250
exit 0
6351
fi
64-
65-
### For ex: Fixes #2123
66-
### For ex: Fixes: #2123
67-
pattern1="((Fixes|Resolves):? #[0-9]+)"
68-
69-
### For ex: Resolves https://github.com/devtron-labs/devtron/issues/2123
70-
pattern2="((Fixes|Resolves):? https://github.com/devtron-labs/devtron/issues/[0-9]+)"
52+
53+
# Define all issue matching patterns
54+
patterns=(
55+
"((Fixes|Resolves) #[0-9]+)"
56+
"((Fixes|Resolves) https://github.com/devtron-labs/devtron/issues/[0-9]+)"
57+
"((Fixes|Resolves) devtron-labs/devtron#[0-9]+)"
58+
"(Fixes|Resolves):?\\s+\\[#([0-9]+)\\]"
59+
"((Fixes|Resolves):? #devtron-labs/devops-sprint/issues/[0-9]+)"
60+
"((Fixes|Resolves):? #devtron-labs/sprint-tasks/issues/[0-9]+)"
61+
"((Fixes|Resolves) https://github.com/devtron-labs/devops-sprint/issues/[0-9]+)"
62+
"((Fixes|Resolves) https://github.com/devtron-labs/sprint-tasks/issues/[0-9]+)"
63+
"((Fixes|Resolves):? #devtron-labs/sprint-tasks#[0-9]+)"
64+
)
65+
66+
# Extract issue number and repo from PR body
67+
extract_issue_number() {
68+
local pattern="$1" # Get the pattern as the first argument to the function
7169

72-
73-
### For ex: Fixes devtron-labs/devtron#2123
74-
pattern3="((Fixes|Resolves):? devtron-labs/devtron#[0-9]+)"
70+
# Check if PR_BODY matches the provided pattern using Bash's =~ regex operator
71+
if [[ "$PR_BODY" =~ $pattern ]]; then
72+
echo "matched for this pattern $pattern"
73+
74+
issue_num=$(echo "$PR_BODY" | grep -oE "$pattern" | grep -oE "[0-9]+")
7575

76-
### For ex: Fixes [#4839](https://github.com/devtron-labs/devtron/issues/4839)
77-
pattern4="(Fixes|Resolves):?\s+\[#([0-9]+)\]"
78-
79-
# Get the pull request body
80-
PR_BODY=$(jq -r '.pull_request.body' $GITHUB_EVENT_PATH)
81-
echo "PR_BODY = $PR_BODY"
76+
# Extract the repository name (e.g., devtron-labs/devtron) from PR_BODY using grep
77+
repo=$(echo "$PR_BODY" | grep -oE "devtron-labs/[a-zA-Z0-9_-]+")
78+
echo "Extracted issue number: $issue_num from repo: $repo"
8279

83-
### Checks if PR_BODY matches pattern1 or pattern2 or pattern3 or none
84-
### grep -i (case insensitive) -E (enables extended regular expression in grep) -q (this option suppresses normal output)
85-
if echo "$PR_BODY" | grep -iEq "$pattern1"; then
86-
### Here we are taking only the numerical value ie. issue number
87-
### head -n1 only prints the 1st line.
88-
### grep -o -E "[0-9]+ basically outputs only the number between [0-9]+
89-
echo "$PR_BODY" | grep -iE "$pattern1" | head -n1 | grep -o -E "[0-9]+" | tr -d '\r\n' > issue_num
90-
issue_num=$(cat issue_num)
91-
echo "issue_num is : $issue_num"
92-
elif echo "$PR_BODY" | grep -iEq "$pattern2"; then
93-
echo "$PR_BODY" | grep -iE "$pattern2" | head -n1 | awk -F '/' '{print $NF}' | tr -d '\r\n' > issue_num
94-
issue_num=$(cat issue_num)
95-
echo "issue_num is : $issue_num"
96-
elif echo "$PR_BODY" | grep -iEq "$pattern3"; then
97-
echo "$PR_BODY" | grep -iE "$pattern3" | head -n1 | awk -F '#' '{print $NF}' | tr -d '\r\n' > issue_num
98-
issue_num=$(cat issue_num)
99-
echo "issue_num is : $issue_num"
100-
elif echo "$PR_BODY" | grep -iEq "$pattern4"; then
101-
echo "$PR_BODY" | grep -oP "$pattern4" | head -n1 | grep -oP '#\K[0-9]+' | tr -d '\r\n' > issue_num
102-
issue_num=$(cat issue_num)
103-
echo "issue_num is : $issue_num"
104-
else
105-
echo "No Issue number detected hence failing the PR Validation check."
106-
if [[ $forked == true ]]; then
107-
echo "PR:Issue-verification-failed, exiting forcefully!"
108-
exit 1
109-
fi
110-
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
111-
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
112-
exit 1
80+
return 0 # Return success
81+
else
82+
echo "No match for the pattern $pattern"
83+
fi
84+
return 1 # Return failure if no match
85+
}
86+
87+
issue_num=""
88+
repo="devtron-labs/devtron" # Default repo
89+
for pattern in "${patterns[@]}"; do
90+
echo "Now checking for $pattern"
91+
extract_issue_number "$pattern" && break
92+
done
93+
94+
if [[ -z "$issue_num" ]]; then
95+
echo "No valid issue number found."
96+
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
97+
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
98+
exit 1
11399
fi
114-
115-
### Here we are setting the Internal Field Separator to "/"
116-
### read -r -> reads input from variable $url
117-
### -a url_parts -> tells read command to store input into an array named url_parts[]
118-
IFS="/" read -r -a url_parts <<< "$url"
119-
120-
# Remove the last two elements (repos and the issue number)
121-
unset url_parts[-1]
122-
unset url_parts[-1]
123-
# Reattach the URL pieces
124-
url=$(IFS=/; echo "${url_parts[*]}")
125-
126-
# Add the issue number to the URL
127-
url="${url}/issues/${issue_num}"
128-
echo "$url"
129-
response_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
100+
101+
# Form the issue API URL dynamically
102+
issue_api_url="https://api.github.com/repos/$repo/issues/$issue_num"
103+
echo "API URL: $issue_api_url"
104+
105+
# Check if the issue exists in the private repo
106+
response_code=$(curl -s -o /dev/null -w "%{http_code}" \
107+
--header "authorization: Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
108+
"$issue_api_url")
109+
130110
if [[ "$response_code" -eq 200 ]]; then
131-
# Check if issue is open or closed
132-
text=$(curl -s "$url")
133-
echo "checking status of the issue"
134-
if [[ $(echo "$text" | jq -r '.state') == "open" ]]; then
135-
echo "Issue #$issue_num is open"
136-
echo "Issue reference found in the pull request body."
137-
if [[ $forked == true ]]; then
138-
echo "PR:Ready-to-Review, exiting gracefully"
139-
exit 0
140-
fi
111+
echo "Issue #$issue_num is valid and exists in $repo."
112+
113+
# Fetch the current state of the issue (open/closed) from the private repository.
114+
issue_status=$(curl -s \
115+
--header "authorization: Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
116+
"$issue_api_url" | jq '.state'|tr -d \")
117+
# Check if the issue is still open.
118+
if [[ "$issue_status" == open ]]; then
119+
echo "Issue #$issue_num is opened."
120+
# Remove the 'Issue-verification-failed' label (if present) and add 'Ready-to-Review'.
141121
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
142122
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
143-
exit 0
144123
else
145-
echo "Issue #$issue_num is not open"
146-
if [[ $forked == true ]]; then
147-
echo "PR:Issue-verification-failed, exiting forcefully!"
148-
exit 1
149-
fi
124+
echo "Issue #$issue_num is closed. Please link an open issue to proceed."
125+
# Add a comment to the PR indicating the issue is not linked correctly.
126+
gh pr comment $PRNUM --body "PR is linked to a closed issue. Please link an open issue to proceed."
127+
128+
# Add the 'Issue-verification-failed' label and remove 'Ready-to-Review'.
150129
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
151130
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
152131
exit 1
153132
fi
154133
else
155-
echo "Invalid Response Code obtained - error code: $response_code"
156-
echo "No valid issue reference found in the pull request body."
157-
gh pr comment $PRNUM --body "PR is not linked to any issue, please make the corresponding changes in the body."
158-
if [[ $forked == true ]]; then
159-
echo "PR:Issue-verification-failed, exiting forcefully!"
160-
exit 1
161-
fi
162-
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
163-
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
164-
exit 1
134+
echo "Issue not found. Invalid URL or issue number."
135+
# Add a comment to the PR indicating the issue is not linked correctly.
136+
gh pr comment $PRNUM --body "PR is not linked to a valid issue. Please update the issue link."
137+
138+
# Apply 'Issue-verification-failed' label and remove 'Ready-to-Review' label.
139+
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
140+
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
141+
exit 1
165142
fi
166143
- name: Check SQL file format and duplicates
167144
shell: bash
@@ -268,6 +245,3 @@ jobs:
268245
echo "All .up.sql migration file validations passed."
269246
gh pr comment $pr_no --body "The migration files have successfully passed the criteria!!"
270247
fi
271-
272-
273-

0 commit comments

Comments
 (0)