Skip to content

Commit 9178790

Browse files
Badal Kumar PrustyBadal Kumar Prusty
authored andcommitted
Refactored after review
1 parent 5fe3c95 commit 9178790

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: |
3333
set -x
3434
# Skip validation for documentation or chore PRs
35-
if [[ "$TITLE" =~ ^(doc:|docs:|chore:) ]]; then
35+
if [[ "$TITLE" =~ ^(doc:|docs:|chore:|misc:) ]]; then
3636
echo "Skipping validation for docs/chore PR."
3737
echo "PR NUMBER-: $PRNUM "
3838
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
@@ -54,17 +54,26 @@ jobs:
5454

5555
# Extract issue number and repo from PR body
5656
extract_issue_number() {
57-
local pattern="$1"
57+
local pattern="$1" # Get the pattern as the first argument to the function
58+
59+
# Check if PR_BODY matches the provided pattern using Bash's =~ regex operator
5860
if [[ "$PR_BODY" =~ $pattern ]]; then
5961
echo "matched for this pattern $pattern"
60-
issue_num=$(echo "${BASH_REMATCH[0]}" | grep -oE "[0-9]+" | tr -d '\r\n')
62+
63+
# # If there is a match, Bash populates the BASH_REMATCH array with the results. stores the entire matched portion of the string
64+
# issue_num=$(echo "${BASH_REMATCH[0]}" | grep -oE "[0-9]+" | tr -d '\r\n')
65+
66+
issue_num=$(echo "$PR_BODY" | grep -oE "$pattern" | grep -oE "[0-9]+")
67+
68+
# Extract the repository name (e.g., devtron-labs/devtron) from PR_BODY using grep
6169
repo=$(echo "$PR_BODY" | grep -oE "devtron-labs/[a-zA-Z0-9_-]+")
6270
echo "Extracted issue number: $issue_num from repo: $repo"
63-
return 0
71+
72+
return 0 # Return success
6473
else
6574
echo "No match for the pattern $pattern"
6675
fi
67-
return 1
76+
return 1 # Return failure if no match
6877
}
6978

7079
issue_num=""
@@ -92,11 +101,33 @@ jobs:
92101

93102
if [[ "$response_code" -eq 200 ]]; then
94103
echo "Issue #$issue_num is valid and exists in $repo."
95-
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
96-
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
104+
105+
# Fetch the current state of the issue (open/closed) from the private repository.
106+
issue_status=$(curl -s \
107+
--header "authorization: Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
108+
"$issue_api_url" | jq '.state')
109+
# Check if the issue is still open.
110+
if [[ "$issue_status" -eq "open" ]]; then
111+
echo "Issue #$issue_num is opened."
112+
# Remove the 'Issue-verification-failed' label (if present) and add 'Ready-to-Review'.
113+
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
114+
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
115+
else
116+
echo "Issue #$issue_num is closed. Please link an open issue to proceed."
117+
# Add a comment to the PR indicating the issue is not linked correctly.
118+
gh pr comment $PRNUM --body "PR is linked to a closed issue. Please link an open issue to proceed."
119+
120+
# Add the 'Issue-verification-failed' label and remove 'Ready-to-Review'.
121+
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
122+
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
123+
exit 1
124+
fi
97125
else
98126
echo "Issue not found. Invalid URL or issue number."
99-
gh pr comment $PRNUM --body "PR is not linked to a valid issue. Please update."
127+
# Add a comment to the PR indicating the issue is not linked correctly.
128+
gh pr comment $PRNUM --body "PR is not linked to a valid issue. Please update the issue link."
129+
130+
# Apply 'Issue-verification-failed' label and remove 'Ready-to-Review' label.
100131
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
101132
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
102133
exit 1

0 commit comments

Comments
 (0)