Skip to content

Commit b5dbc29

Browse files
Merge pull request #1250 from devtron-labs/tayalrishabh96-patch-1
chore: create pr-issue-validator
2 parents b1eb28f + c7621db commit b5dbc29

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
name: Validate Pull Request
2+
3+
on:
4+
pull_request:
5+
types:
6+
- opened
7+
- synchronize
8+
- edited
9+
- reopened
10+
branches:
11+
- 'main'
12+
- 'release-**'
13+
# paths-ignore:
14+
# - 'docs/**'
15+
# - '.github/'
16+
# - 'CHANGELOG/'
17+
# - 'charts/'
18+
# - 'manifests/'
19+
# - 'sample-docker-templates/'
20+
21+
jobs:
22+
validate-PR-issue:
23+
runs-on: ubuntu-latest
24+
permissions:
25+
issues: write
26+
contents: read
27+
pull-requests: write
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@v2
31+
32+
- name: Validate Issue Reference
33+
env:
34+
GITHUB_TOKEN: ${{ github.token }}
35+
PR_BODY: ${{ github.event.pull_request.body }}
36+
url: ${{ github.event.pull_request.url }}
37+
PRNUM: ${{ github.event.pull_request.number }}
38+
TITLE: ${{ github.event.pull_request.title }}
39+
run: |
40+
set -x
41+
if [[ "$TITLE" == *"doc:"* || "$TITLE" == *"docs:"* || "$TITLE" == *"chore:"* ]]; then
42+
echo "Skipping validation as this is a PR for documentation or chore."
43+
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
44+
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
45+
exit 0
46+
fi
47+
48+
### For ex: Fixes #2123
49+
pattern1="((Fixes|Resolves) #[0-9]+)"
50+
51+
### For ex: Resolves https://github.com/devtron-labs/devtron/issues/2123
52+
pattern2="((Fixes|Resolves) https://github.com/devtron-labs/devtron/issues/[0-9]+)"
53+
54+
### For ex: Fixes devtron-labs/devtron#2123
55+
pattern3="((Fixes|Resolves) devtron-labs/devtron#[0-9]+)"
56+
57+
# Get the pull request body
58+
PR_BODY=$(jq -r '.pull_request.body' $GITHUB_EVENT_PATH)
59+
echo "PR_BODY = $PR_BODY"
60+
61+
### Checks if PR_BODY matches pattern1 or pattern2 or pattern3 or none
62+
### grep -i (case insensitive) -E (enables extended regular expression in grep) -q (this option suppresses normal output)
63+
if echo "$PR_BODY" | grep -iEq "$pattern1"; then
64+
### Here we are taking only the numerical value ie. issue number
65+
### head -n1 only prints the 1st line.
66+
### grep -o -E "[0-9]+ basically outputs only the number between [0-9]+
67+
issue_num=$(echo "$PR_BODY" | grep -iE "$pattern1" | head -n1 | grep -o -E "[0-9]+")
68+
echo "issue_num is : $issue_num"
69+
elif echo "$PR_BODY" | grep -iEq "$pattern2"; then
70+
issue_num=$(echo "$PR_BODY" | grep -iE "$pattern2" | head -n1 | awk -F '/' '{print $NF}')
71+
echo "issue_num is : $issue_num"
72+
elif echo "$PR_BODY" | grep -iEq "$pattern3"; then
73+
issue_num=$(echo "$PR_BODY" | grep -iE "$pattern3" | head -n1 | awk -F '#' '{print $NF}')
74+
else
75+
echo "No Issue number detected hence failing the PR Validation check."
76+
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
77+
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
78+
exit 1
79+
fi
80+
81+
### hardcoding the url here to devtron repo as issues will always be created inside devtron repo.
82+
url=https://api.github.com/repos/devtron-labs/devtron
83+
84+
# Add the issue number to the URL
85+
url="${url}/issues/${issue_num}"
86+
echo "$url"
87+
response_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
88+
if [[ "$response_code" -eq 200 ]]; then
89+
# Check if issue is open or closed
90+
text=$(curl -s "$url")
91+
echo "checking status of the issue"
92+
# Skipping this condition as the Issue can be in closed state as BE PRs are merged before FE
93+
# if [[ $(echo "$text" | jq -r '.state') == "open" ]]; then
94+
echo "Issue #$issue_num is open"
95+
echo "Issue reference found in the pull request body."
96+
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
97+
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
98+
exit 0
99+
# else
100+
# echo "Issue #$issue_num is not open"
101+
# exit 1
102+
# fi
103+
else
104+
echo "Invalid Response Code obtained - error code: $response_code"
105+
echo "No valid issue reference found in the pull request body."
106+
gh pr comment $PRNUM --body "PR is not linked to any issue, please make the corresponding changes in the body."
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

0 commit comments

Comments
 (0)