Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Update pr-issue-validator.yaml #2081

Closed
wants to merge 9 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 51 additions & 112 deletions .github/workflows/pr-issue-validator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ on:
branches:
- 'main'
- 'release-**'
# paths-ignore:
# - 'docs/**'
# - '.github/'
# - 'CHANGELOG/'
# - 'charts/'
# - 'manifests/'
# - 'sample-docker-templates/'


jobs:
validate-PR-issue:
runs-on: ubuntu-latest
Expand All @@ -32,127 +25,73 @@ jobs:
- name: Validate Issue Reference
env:
GITHUB_TOKEN: ${{ github.token }}
GIT_ADMIN_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }}
PR_BODY: ${{ github.event.pull_request.body }}
url: ${{ github.event.pull_request.url }}
PRNUM: ${{ github.event.pull_request.number }}
TITLE: ${{ github.event.pull_request.title }}
run: |
set -x
if [[ "$TITLE" == *"doc:"* || "$TITLE" == *"docs:"* || "$TITLE" == *"chore:"* ]]; then
echo "Skipping validation as this is a PR for documentation or chore."

# Skip validation for documentation or chore PRs
if [[ "$TITLE" =~ ^(doc:|docs:|chore:) ]]; then
echo "Skipping validation for docs/chore PR."
echo "PR NUMBER-: $PRNUM "
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
exit 0
fi

### For ex: Fixes #2123
pattern1="((Fixes|Resolves) #[0-9]+)"

### For ex: Resolves https://github.com/devtron-labs/devtron/issues/2123
pattern2="((Fixes|Resolves) https://github.com/devtron-labs/devtron/issues/[0-9]+)"

### For ex: Fixes devtron-labs/devtron#2123
pattern3="((Fixes|Resolves) devtron-labs/devtron#[0-9]+)"

### For ex: Fixes [#4839](https://github.com/devtron-labs/devtron/issues/4839)
pattern4="(Fixes|Resolves):?\s+\[#([0-9]+)\]"

### For ex: Fixes devtron-labs/devops-sprint#2123
pattern5="((Fixes|Resolves):? #devtron-labs/devops-sprint/issues/[0-9]+)"

### For ex: Fixes devtron-labs/sprint-tasks#2123
pattern6="((Fixes|Resolves):? #devtron-labs/sprint-tasks/issues/[0-9]+)"

### For ex: Resolves https://github.com/devtron-labs/devops-sprint/issues/2123
pattern7="((Fixes|Resolves) https://github.com/devtron-labs/devops-sprint/issues/[0-9]+)"

### For ex: Resolves https://github.com/devtron-labs/sprint-tasks/issues/2123
pattern8="((Fixes|Resolves) https://github.com/devtron-labs/sprint-tasks/issues/[0-9]+)"
# Define all issue matching patterns
patterns=(
"((Fixes|Resolves) #[0-9]+)"
"((Fixes|Resolves) https://github.com/devtron-labs/devtron/issues/[0-9]+)"
"((Fixes|Resolves) devtron-labs/devtron#[0-9]+)"
"(Fixes|Resolves):?\\s+\\[#([0-9]+)\\]"
"((Fixes|Resolves):? #devtron-labs/devops-sprint/issues/[0-9]+)"
"((Fixes|Resolves):? #devtron-labs/sprint-tasks/issues/[0-9]+)"
"((Fixes|Resolves) https://github.com/devtron-labs/devops-sprint/issues/[0-9]+)"
"((Fixes|Resolves) https://github.com/devtron-labs/sprint-tasks/issues/[0-9]+)"
)

# Get the pull request body
PR_BODY=$(jq -r '.pull_request.body' $GITHUB_EVENT_PATH)
echo "PR_BODY = $PR_BODY"
# Extract issue number from PR body
extract_issue_number() {
local pattern="$1"
if [[ "$PR_BODY" =~ $pattern ]]; then
echo "${BASH_REMATCH[0]}" | grep -oE "[0-9]+" | tr -d '\r\n'
return 0
fi
return 1
}

### Checks if PR_BODY matches pattern1 or pattern2 or pattern3 or none
### grep -i (case insensitive) -E (enables extended regular expression in grep) -q (this option suppresses normal output)
if echo "$PR_BODY" | grep -iEq "$pattern1"; then
### Here we are taking only the numerical value ie. issue number
### head -n1 only prints the 1st line.
### grep -o -E "[0-9]+ basically outputs only the number between [0-9]+
echo "$PR_BODY" | grep -iE "$pattern1" | head -n1 | grep -o -E "[0-9]+" | tr -d '\r\n' > issue_num
issue_num=$(cat issue_num)
echo "issue_num is : $issue_num"
elif echo "$PR_BODY" | grep -iEq "$pattern2"; then
echo "$PR_BODY" | grep -iE "$pattern2" | head -n1 | awk -F '/' '{print $NF}' | tr -d '\r\n' > issue_num
issue_num=$(cat issue_num)
echo "issue_num is : $issue_num"
elif echo "$PR_BODY" | grep -iEq "$pattern3"; then
echo "$PR_BODY" | grep -iE "$pattern3" | head -n1 | awk -F '#' '{print $NF}' | tr -d '\r\n' > issue_num
issue_num=$(cat issue_num)
echo "issue_num is : $issue_num"
elif echo "$PR_BODY" | grep -iEq "$pattern4"; then
echo "$PR_BODY" | grep -oP "$pattern4" | head -n1 | grep -oP '#\K[0-9]+' | tr -d '\r\n' > issue_num
issue_num=$(cat issue_num)
echo "issue_num is : $issue_num"
elif echo "$PR_BODY" | grep -iEq "$pattern5|$pattern6"; then
url_1="$(echo "$PR_BODY" | grep -iE "$pattern5" | head -n1 | awk '{print $2}')"
url_2=${url_1:1}
url=https://github.com/$url_2
echo "$PR_BODY" | grep -oP "$pattern4" | head -n1 | grep -oP '#\K[0-9]+' | tr -d '\r\n' > issue_num
issue_num=$(cat issue_num)
echo "issue_num is : $issue_num"
elif echo "$PR_BODY" | grep -iEq "$pattern7|$pattern8"; then
url="$(echo "$PR_BODY" | grep -iE "$pattern7|$pattern8" | head -n1 | awk '{print $2}')"
echo "$PR_BODY" | grep -oP "$pattern4" | head -n1 | grep -oP '#\K[0-9]+' | tr -d '\r\n' > issue_num
issue_num=$(cat issue_num)
echo "issue_num is : $issue_num"
else
echo "No Issue number detected hence failing the PR Validation check."
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
exit 1
issue_num=""
for pattern in "${patterns[@]}"; do
issue_num=$(extract_issue_number "$pattern") && break
done

if [[ -z "$issue_num" ]]; then
echo "No valid issue number found."
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
exit 1
fi

if echo "$PR_BODY" | grep -iEq "$pattern5|$pattern6|$pattern7|$pattern8"; then
echo $url
# Form the issue URL
if [[ "$PR_BODY" =~ (https://github.com/.*/issues/[0-9]+) ]]; then
url="${BASH_REMATCH[0]}"
else
### Here we are setting the Internal Field Separator to "/"
### read -r -> reads input from variable $url
### -a url_parts -> tells read command to store input into an array named url_parts[]
IFS="/" read -r -a url_parts <<< "$url"

# Remove the last two elements (repos and the issue number)
unset url_parts[-1]
unset url_parts[-1]
# Reattach the URL pieces
url=$(IFS=/; echo "${url_parts[*]}")

# Add the issue number to the URL
url="${url}/issues/${issue_num}"
echo "$url"
url="https://github.com/devtron-labs/devtron/issues/$issue_num"
fi


# Check if the issue exists
response_code=$(curl -s -o /dev/null -w "%{http_code}" "$url")
if [[ "$response_code" -eq 200 ]]; then
# Check if issue is open or closed
text=$(curl -s "$url")
echo "checking status of the issue"
# Skipping this condition as the Issue can be in closed state as BE PRs are merged before FE
# if [[ $(echo "$text" | jq -r '.state') == "open" ]]; then
echo "Issue #$issue_num is open"
echo "Issue reference found in the pull request body."
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
exit 0
# else
# echo "Issue #$issue_num is not open"
# exit 1
# fi
echo "Issue #$issue_num is valid and exists."
gh pr edit $PRNUM --remove-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --add-label "PR:Ready-to-Review"
else
echo "Invalid Response Code obtained - error code: $response_code"
echo "No valid issue reference found in the pull request body."
gh pr comment $PRNUM --body "PR is not linked to any issue, please make the corresponding changes in the body."
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
exit 1
echo "Issue not found. Invalid URL or issue number."
gh pr comment $PRNUM --body "PR is not linked to a valid issue. Please update."
gh pr edit $PRNUM --add-label "PR:Issue-verification-failed"
gh pr edit $PRNUM --remove-label "PR:Ready-to-Review"
exit 1
fi
Loading