Skip to content

Commit 829b0d7

Browse files
Merge pull request #6149 from devtron-labs/main-sync-develop-dec2
misc: Main sync develop dec2
2 parents 99b19c5 + 5c543f7 commit 829b0d7

File tree

61 files changed

+319
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+319
-182
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ scripts/sql @prakarsh-dt @vikramdevtron @nishant-d @vivek-devtron
1111
scripts/utilities @prakarsh-dt @nishant-d @pawan-mehta-dt @vivek-devtron
1212

1313
#Github Specific
14-
.github/ @prakarsh-dt @nishant-d @pawan-mehta-dt @vikramdevtron
14+
.github/ @prakarsh-dt @nishant-d @pawan-mehta-dt @vikramdevtron @tayalrishabh96

.github/pull_request_template.md

Lines changed: 2 additions & 2 deletions

.github/workflows/auto-label.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Devtron-auto-labeller
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
8+
jobs:
9+
manage-labels:
10+
if: ${{ !github.event.issue.pull_request }}
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout Repository
15+
uses: actions/checkout@v3
16+
17+
- name: Parse and manage labels
18+
env:
19+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
run: |
21+
set -e
22+
set -x # Enable debugging
23+
24+
# Extract comment on body and issue number
25+
COMMENT_BODY=$(jq -r '.comment.body' "$GITHUB_EVENT_PATH")
26+
ISSUE_NUMBER=$(jq -r '.issue.number // .pull_request.number' "$GITHUB_EVENT_PATH")
27+
28+
ORG_NAME="devtron-labs"
29+
30+
# checks if the person is authorized to add labels or not
31+
ORG_MEMBERSHIP_STATUS=$(gh api "orgs/$ORG_NAME/members/$COMMENT_AUTHOR" --silent --exit-status)
32+
33+
if [[ $? -ne 0 ]]; then
34+
gh issue comment "$ISSUE_NUMBER" --body "Hi @$COMMENT_AUTHOR, you must be a member of the organization '$ORG_NAME' to add or remove labels."
35+
36+
echo "User '$COMMENT_AUTHOR' is not a member of the organization '$ORG_NAME'. Exiting."
37+
exit 1
38+
fi
39+
40+
echo "User '$COMMENT_AUTHOR' is a verified member of the organization '$ORG_NAME'. Adding label"
41+
42+
43+
# Get the existing labels on the issue
44+
EXISTING_LABELS=$(gh issue view "$ISSUE_NUMBER" --json labels -q '.labels[].name')
45+
46+
# Add Label
47+
if [[ "$COMMENT_BODY" =~ ^/([^ ]+)$ ]]; then
48+
LABEL_NAME="${COMMENT_BODY:1}"
49+
50+
# check for already existing labels in reppo
51+
if gh label list --json name -q '.[].name' | grep -q "^$LABEL_NAME$"; then
52+
# Add the requested label, keeping existing ones intact
53+
gh issue edit "$ISSUE_NUMBER" --add-label "$LABEL_NAME"
54+
echo "Successfully added label '$LABEL_NAME' to issue #$ISSUE_NUMBER."
55+
else
56+
echo "The label '$LABEL_NAME' doesn't exist in the repository. You may need to create a label first."
57+
fi
58+
fi
59+
60+
# Remove Label Logic
61+
if [[ "$COMMENT_BODY" =~ ^/remove[[:space:]](.+)$ ]]; then
62+
LABEL_NAME_TO_REMOVE=$(echo "$COMMENT_BODY" | sed -n 's|/remove ||p')
63+
64+
# Remove the specified label
65+
if echo "$EXISTING_LABELS" | grep -q "^$LABEL_NAME_TO_REMOVE$"; then
66+
gh issue edit "$ISSUE_NUMBER" --remove-label "$LABEL_NAME_TO_REMOVE"
67+
echo "Successfully removed label '$LABEL_NAME_TO_REMOVE' from issue #$ISSUE_NUMBER."
68+
else
69+
echo "The label '$LABEL_NAME_TO_REMOVE' is not attached to issue #$ISSUE_NUMBER."
70+
fi
71+
fi

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

Lines changed: 105 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -26,142 +26,155 @@ jobs:
2626
issues: write
2727
contents: read
2828
pull-requests: write
29-
29+
repository-projects: read
3030
steps:
3131
- name: Checkout repository
3232
uses: actions/checkout@v2
3333
with:
3434
ref: ${{ github.event.pull_request.head.sha }}
3535
fetch-depth: 0
3636

37-
3837
- name: Validate Issue Reference
3938
env:
40-
GITHUB_TOKEN: ${{ github.token }}
39+
GH_TOKEN: ${{ github.token }}
4140
PR_BODY: ${{ github.event.pull_request.body }}
42-
url: ${{ github.event.pull_request.url }}
4341
PRNUM: ${{ github.event.pull_request.number }}
4442
TITLE: ${{ github.event.pull_request.title }}
4543
run: |
44+
4645
echo "base or target repo : ${{ github.event.pull_request.base.repo.full_name }}"
4746
echo "head or source repo : ${{ github.event.pull_request.head.repo.full_name }}"
47+
4848
if [[ ${{ github.event.pull_request.head.repo.full_name }} == ${{ github.event.pull_request.base.repo.full_name }} ]]; then
4949
export forked=false
5050
else
5151
export forked=true
5252
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 "
6059
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
6160
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
6261
exit 0
6362
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
7180

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"
7590

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"
78115

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")
82120

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"
104121
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")
113126
fi
114127

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"
130129
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."
137148
if [[ $forked == true ]]; then
138149
echo "PR:Ready-to-Review, exiting gracefully"
139150
exit 0
140151
fi
152+
# Remove the 'Issue-verification-failed' label (if present) and add 'Ready-to-Review'.
141153
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
142154
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
143-
exit 0
144155
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'.
150165
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
151166
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
152167
exit 1
153168
fi
154169
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
165178
fi
166179
- name: Check SQL file format and duplicates
167180
shell: bash
@@ -268,6 +281,3 @@ jobs:
268281
echo "All .up.sql migration file validations passed."
269282
gh pr comment $pr_no --body "The migration files have successfully passed the criteria!!"
270283
fi
271-
272-
273-
1.03 KB
1.14 KB
1.28 KB
1.28 KB
946 Bytes
1.27 KB

0 commit comments

Comments
 (0)