15
15
16
16
jobs :
17
17
validate-PR-issue :
18
- uses : devtron-labs/utilities/.github/workflows/pr-validator.yaml@feat/central-pr-validator
19
- secrets : inherit
20
- with :
21
- validate_sql : false # Enable/disable SQL validation
18
+ runs-on : ubuntu-latest
19
+ permissions :
20
+ issues : write
21
+ contents : read
22
+ pull-requests : write
23
+ repository-projects : read
24
+ steps :
25
+ - name : Checkout repository
26
+ uses : actions/checkout@v2
27
+
28
+ - name : Validate Issue Reference
29
+ env :
30
+ GH_TOKEN : ${{ github.token }}
31
+ PR_BODY : ${{ github.event.pull_request.body }}
32
+ PRNUM : ${{ github.event.pull_request.number }}
33
+ TITLE : ${{ github.event.pull_request.title }}
34
+ run : |
35
+ set -x
36
+ # Skip validation for documentation or chore PRs
37
+ if [[ "$TITLE" =~ ^(doc:|docs:|chore:|misc:|release:|sync:) ]]; then
38
+ echo "Skipping validation for docs/chore PR."
39
+ echo "PR NUMBER-: $PRNUM "
40
+ gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
41
+ gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
42
+ exit 0
43
+ fi
44
+
45
+ # Define all issue matching patterns
46
+ patterns=(
47
+ " ((Fixes|Resolves) #[0-9]+)"
48
+ " ((Fixes|Resolves) https://github.com/devtron-labs/devtron/issues/[0-9]+)"
49
+ " ((Fixes|Resolves) devtron-labs/devtron#[0-9]+)"
50
+ " (Fixes|Resolves):?\\ s+\\ [#([0-9]+)\\ ]"
51
+ " ((Fixes|Resolves):? #devtron-labs/devops-sprint/issues/[0-9]+)"
52
+ " ((Fixes|Resolves):? #devtron-labs/sprint-tasks/issues/[0-9]+)"
53
+ " ((Fixes|Resolves) https://github.com/devtron-labs/devops-sprint/issues/[0-9]+)"
54
+ " ((Fixes|Resolves) https://github.com/devtron-labs/sprint-tasks/issues/[0-9]+)"
55
+ )
56
+
57
+ # Extract issue number and repo from PR body
58
+ extract_issue_number() {
59
+ local pattern="$1" # Get the pattern as the first argument to the function
60
+
61
+ # Check if PR_BODY matches the provided pattern using Bash's =~ regex operator
62
+ if [[ "$PR_BODY" =~ $pattern ]]; then
63
+ echo "matched for this pattern $pattern"
64
+
65
+ issue_num=$(echo "$PR_BODY" | grep -oE "$pattern" | grep -oE "[0-9]+")
66
+
67
+ # Extract the repository name (e.g., devtron-labs/devtron) from PR_BODY using grep
68
+ repo=$(echo "$PR_BODY" | grep -oE "devtron-labs/[a-zA-Z0-9_-]+")
69
+ echo "Extracted issue number : $issue_num from repo: $repo"
70
+
71
+ return 0 # Return success
72
+ else
73
+ echo "No match for the pattern $pattern"
74
+ fi
75
+ return 1 # Return failure if no match
76
+ }
77
+
78
+ issue_num=""
79
+ repo="devtron-labs/devtron" # Default repo
80
+ for pattern in "${patterns[@]}"; do
81
+ echo "Now checking for $pattern"
82
+ extract_issue_number "$pattern" && break
83
+ done
84
+
85
+ if [[ -z "$issue_num" ]]; then
86
+ echo "No valid issue number found."
87
+ gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
88
+ gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
89
+ exit 1
90
+ fi
91
+
92
+ # Form the issue API URL dynamically
93
+ issue_api_url="https://api.github.com/repos/$repo/issues/$issue_num"
94
+ echo "API URL : $issue_api_url"
95
+
96
+ # Check if the issue exists in the private repo
97
+ response_code=$(curl -s -o /dev/null -w "%{http_code}" \
98
+ --header "authorization : Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
99
+ " $issue_api_url" )
100
+
101
+ if [[ "$response_code" -eq 200 ]]; then
102
+ echo "Issue # $issue_num is valid and exists in $repo."
103
+
104
+ # Fetch the current state of the issue (open/closed) from the private repository.
105
+ issue_status=$(curl -s \
106
+ --header "authorization : Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
107
+ " $issue_api_url" | jq '.state'|tr -d \")
108
+ # Check if the issue is still open.
109
+ # if [[ "$issue_status" == open ]]; then
110
+ # echo "Issue #$issue_num is opened."
111
+ if [[ $forked == true ]]; then
112
+ echo "PR:Ready-to-Review, exiting gracefully"
113
+ exit 0
114
+ fi
115
+ # Remove the 'Issue-verification-failed' label (if present) and add 'Ready-to-Review'.
116
+ gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
117
+ gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
118
+ echo "PR:Ready-to-Review, exiting gracefully"
119
+ exit 0
120
+ # else
121
+ # echo "Issue #$issue_num is closed. Please link an open issue to proceed."
122
+ # if [[ $forked == true ]]; then
123
+ # echo "PR:Ready-to-Review, exiting gracefully"
124
+ # exit 0
125
+ # fi
126
+ # Add a comment to the PR indicating the issue is not linked correctly.
127
+ # gh pr comment $PRNUM --body "PR is linked to a closed issue. Please link an open issue to proceed."
128
+
129
+ # Add the 'Issue-verification-failed' label and remove 'Ready-to-Review'.
130
+ # gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
131
+ # gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
132
+ # exit 1
133
+ # fi
134
+ else
135
+ echo "Issue not found. Invalid URL or issue number."
136
+ # Add a comment to the PR indicating the issue is not linked correctly.
137
+ gh pr comment $PRNUM --body "PR is not linked to a valid issue. Please update the issue link."
138
+
139
+ # Apply 'Issue-verification-failed' label and remove 'Ready-to-Review' label.
140
+ gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
141
+ gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
142
+ exit 1
143
+ fi
0 commit comments