@@ -26,142 +26,155 @@ jobs:
26
26
issues : write
27
27
contents : read
28
28
pull-requests : write
29
-
29
+ repository-projects : read
30
30
steps :
31
31
- name : Checkout repository
32
32
uses : actions/checkout@v2
33
33
with :
34
34
ref : ${{ github.event.pull_request.head.sha }}
35
35
fetch-depth : 0
36
36
37
-
38
37
- name : Validate Issue Reference
39
38
env :
40
- GITHUB_TOKEN : ${{ github.token }}
39
+ GH_TOKEN : ${{ github.token }}
41
40
PR_BODY : ${{ github.event.pull_request.body }}
42
- url : ${{ github.event.pull_request.url }}
43
41
PRNUM : ${{ github.event.pull_request.number }}
44
42
TITLE : ${{ github.event.pull_request.title }}
45
43
run : |
44
+
46
45
echo "base or target repo : ${{ github.event.pull_request.base.repo.full_name }}"
47
46
echo "head or source repo : ${{ github.event.pull_request.head.repo.full_name }}"
47
+
48
48
if [[ ${{ github.event.pull_request.head.repo.full_name }} == ${{ github.event.pull_request.base.repo.full_name }} ]]; then
49
49
export forked=false
50
50
else
51
51
export forked=true
52
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
53
+
54
+ set -x
55
+ # Skip validation for documentation or chore PRs
56
+ if [[ "$TITLE" =~ ^(doc:|docs:|chore:|misc:) ]]; then
57
+ echo "Skipping validation for docs/chore PR."
58
+ echo "PR NUMBER-: $PRNUM "
60
59
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
61
60
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
62
61
exit 0
63
62
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]+)"
63
+
64
+ # Define all issue matching patterns
65
+ patterns=(
66
+ " ((Fixes|Resolves) #[0-9]+)"
67
+ " ((Fixes|Resolves) https://github.com/devtron-labs/devtron/issues/[0-9]+)"
68
+ " ((Fixes|Resolves) devtron-labs/devtron#[0-9]+)"
69
+ " (Fixes|Resolves):?\\ s+\\ [#([0-9]+)\\ ]"
70
+ " ((Fixes|Resolves):? #devtron-labs/devops-sprint/issues/[0-9]+)"
71
+ " ((Fixes|Resolves):? #devtron-labs/sprint-tasks/issues/[0-9]+)"
72
+ " ((Fixes|Resolves) https://github.com/devtron-labs/devops-sprint/issues/[0-9]+)"
73
+ " ((Fixes|Resolves) https://github.com/devtron-labs/sprint-tasks/issues/[0-9]+)"
74
+ " ((Fixes|Resolves):? #devtron-labs/sprint-tasks#[0-9]+)"
75
+ )
76
+
77
+ # Extract issue number and repo from PR body
78
+ extract_issue_number() {
79
+ local pattern="$1" # Get the pattern as the first argument to the function
71
80
72
-
73
- ### For ex: Fixes devtron-labs/devtron#2123
74
- pattern3="((Fixes|Resolves):? devtron-labs/devtron#[0-9]+)"
81
+ # Check if PR_BODY matches the provided pattern using Bash's =~ regex operator
82
+ if [[ "$PR_BODY" =~ $pattern ]]; then
83
+ echo "matched for this pattern $pattern"
84
+
85
+ issue_num=$(echo "$PR_BODY" | grep -oE "$pattern" | grep -oE "[0-9]+")
86
+
87
+ # Extract the repository name (e.g., devtron-labs/devtron) from PR_BODY using grep
88
+ repo=$(echo "$PR_BODY" | grep -oE "devtron-labs/[a-zA-Z0-9_-]+")
89
+ echo "Extracted issue number : $issue_num from repo: $repo"
75
90
76
- ### For ex: Fixes [#4839](https://github.com/devtron-labs/devtron/issues/4839)
77
- pattern4="(Fixes|Resolves):?\s+\[#([0-9]+)\]"
91
+ return 0 # Return success
92
+ else
93
+ echo "No match for the pattern $pattern"
94
+ fi
95
+ return 1 # Return failure if no match
96
+ }
97
+
98
+ issue_num=""
99
+ repo="devtron-labs/devtron" # Default repo
100
+ for pattern in "${patterns[@]}"; do
101
+ echo "Now checking for $pattern"
102
+ extract_issue_number "$pattern" && break
103
+ done
104
+
105
+ if [[ -z "$issue_num" ]]; then
106
+ echo "No valid issue number found."
107
+ gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
108
+ gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
109
+ exit 1
110
+ fi
111
+
112
+ # Form the issue API URL dynamically
113
+ issue_api_url="https://api.github.com/repos/$repo/issues/$issue_num"
114
+ echo "API URL : $issue_api_url"
78
115
79
- # Get the pull request body
80
- PR_BODY=$(jq -r '.pull_request.body' $GITHUB_EVENT_PATH)
81
- echo "PR_BODY = $PR_BODY"
116
+ if [[ $repo == "devtron-labs/devtron" || $repo == "devtron-labs/devtron-services" || $repo == "devtron-labs/dashboard" ]]; then
117
+ echo "No extra arguments needed : public repository detected."
118
+ response_code=$(curl -s -o /dev/null -w "%{http_code}" \
119
+ " $issue_api_url" )
82
120
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
121
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
122
+ echo "Adding extra arguments for authentication : private repository detected."
123
+ response_code=$(curl -s -o /dev/null -w "%{http_code}" \
124
+ --header "authorization : Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
125
+ " $issue_api_url" )
113
126
fi
114
127
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")
128
+ echo "Response Code : $response_code"
130
129
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."
130
+ echo "Issue # $issue_num is valid and exists in $repo."
131
+
132
+ # Fetch the current state of the issue (open/closed) from the private repository.
133
+ if [[ $repo == "devtron-labs/devtron" || $repo == "devtron-labs/devtron-services" || $repo == "devtron-labs/dashboard" ]]; then
134
+ echo "No extra arguments needed : public repository detected."
135
+ issue_status=$(curl -s \
136
+ " $issue_api_url" | jq '.state'|tr -d \")
137
+ else
138
+ echo "Adding extra arguments for authentication : private repository detected."
139
+ issue_status=$(curl -s \
140
+ --header "authorization : Bearer ${{ secrets.GH_PR_VALIDATOR_TOKEN }}" \
141
+ " $issue_api_url" | jq '.state'|tr -d \")
142
+ fi
143
+ echo "Issue Status : $issue_status"
144
+
145
+ # Check if the issue is still open.
146
+ if [[ "$issue_status" == open ]]; then
147
+ echo "Issue # $issue_num is opened."
137
148
if [[ $forked == true ]]; then
138
149
echo "PR:Ready-to-Review, exiting gracefully"
139
150
exit 0
140
151
fi
152
+ # Remove the 'Issue-verification-failed' label (if present) and add 'Ready-to-Review'.
141
153
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
142
154
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
143
- exit 0
144
155
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
156
+ echo "Issue # $issue_num is closed. Please link an open issue to proceed."
157
+ if [[ $forked == true ]]; then
158
+ echo "PR:Ready-to-Review, exiting gracefully"
159
+ exit 0
160
+ fi
161
+ # Add a comment to the PR indicating the issue is not linked correctly.
162
+ gh pr comment $PRNUM --body "PR is linked to a closed issue. Please link an open issue to proceed."
163
+
164
+ # Add the 'Issue-verification-failed' label and remove 'Ready-to-Review'.
150
165
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
151
166
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
152
167
exit 1
153
168
fi
154
169
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
170
+ echo "Issue not found. Invalid URL or issue number."
171
+ # Add a comment to the PR indicating the issue is not linked correctly.
172
+ gh pr comment $PRNUM --body "PR is not linked to a valid issue. Please update the issue link."
173
+
174
+ # Apply 'Issue-verification-failed' label and remove 'Ready-to-Review' label.
175
+ gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
176
+ gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
177
+ exit 1
165
178
fi
166
179
- name : Check SQL file format and duplicates
167
180
shell : bash
@@ -268,6 +281,3 @@ jobs:
268
281
echo "All .up.sql migration file validations passed."
269
282
gh pr comment $pr_no --body "The migration files have successfully passed the criteria!!"
270
283
fi
271
-
272
-
273
-
0 commit comments