From fc2d9cf14f23e124e2de6526417e62dd33ca670f Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:33:59 +0530 Subject: [PATCH 1/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index 4fd7cfd828..0956fd8769 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -31,7 +31,7 @@ jobs: - name: Validate Issue Reference env: - GITHUB_TOKEN: ${{ github.token }} + GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }} PR_BODY: ${{ github.event.pull_request.body }} url: ${{ github.event.pull_request.url }} PRNUM: ${{ github.event.pull_request.number }} From 89ab83a255968a0186015df302f10c8fbd5b18b4 Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:40:13 +0530 Subject: [PATCH 2/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index 0956fd8769..e82fa22cbb 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -31,7 +31,7 @@ jobs: - name: Validate Issue Reference env: - GITHUB_TOKEN: ${{ secrets.GIT_TOKEN }} + GITHUB_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 }} From a2c402ae3f16476f941bbc9450c269d1717078e2 Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:51:46 +0530 Subject: [PATCH 3/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index e82fa22cbb..e96387c1ea 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -31,7 +31,7 @@ jobs: - name: Validate Issue Reference env: - GITHUB_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }} + GH_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 }} From 233e5bafbda2a22f1b0cc360c2899a5d3582a952 Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:52:50 +0530 Subject: [PATCH 4/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index e96387c1ea..28a1d33a14 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -31,7 +31,7 @@ jobs: - name: Validate Issue Reference env: - GH_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }} + GH_TOKEN: ${{ github.token }} PR_BODY: ${{ github.event.pull_request.body }} url: ${{ github.event.pull_request.url }} PRNUM: ${{ github.event.pull_request.number }} From 0f1f237b5068ab7da5e6b5642ab1912d7dc2a5cc Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 11:53:59 +0530 Subject: [PATCH 5/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index 28a1d33a14..4fd7cfd828 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -31,7 +31,7 @@ jobs: - name: Validate Issue Reference env: - GH_TOKEN: ${{ github.token }} + GITHUB_TOKEN: ${{ github.token }} PR_BODY: ${{ github.event.pull_request.body }} url: ${{ github.event.pull_request.url }} PRNUM: ${{ github.event.pull_request.number }} From 103fd7f66a30d10b1f4c45038633c27f96253418 Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:03:44 +0530 Subject: [PATCH 6/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 164 +++++++--------------- 1 file changed, 51 insertions(+), 113 deletions(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index 4fd7cfd828..5d16129624 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -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 @@ -31,128 +24,73 @@ jobs: - name: Validate Issue Reference env: - GITHUB_TOKEN: ${{ github.token }} + GITHUB_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }} + 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." 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 From aa3c75453b0d71e28fbe078bdf69a7b387ce7c8b Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:05:22 +0530 Subject: [PATCH 7/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index 5d16129624..fcc299a0f4 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -24,7 +24,7 @@ jobs: - name: Validate Issue Reference env: - GITHUB_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }} + GH_TOKEN: ${{ github.token }} GIT_ADMIN_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }} PR_BODY: ${{ github.event.pull_request.body }} PRNUM: ${{ github.event.pull_request.number }} From 334335cafcb618fd51b23af82392f3a8341213ec Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:18:46 +0530 Subject: [PATCH 8/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index fcc299a0f4..e21b2d7a64 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -35,6 +35,7 @@ jobs: # 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 From 9511f09a9075ac5a61a1b2f764fd69841279cc92 Mon Sep 17 00:00:00 2001 From: Badal Kumar <130441461+badal773@users.noreply.github.com> Date: Tue, 24 Sep 2024 15:19:54 +0530 Subject: [PATCH 9/9] Update pr-issue-validator.yaml --- .github/workflows/pr-issue-validator.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-issue-validator.yaml b/.github/workflows/pr-issue-validator.yaml index e21b2d7a64..0ccb4e09b8 100644 --- a/.github/workflows/pr-issue-validator.yaml +++ b/.github/workflows/pr-issue-validator.yaml @@ -24,7 +24,7 @@ jobs: - name: Validate Issue Reference env: - GH_TOKEN: ${{ github.token }} + GITHUB_TOKEN: ${{ github.token }} GIT_ADMIN_TOKEN: ${{ secrets.GH_SYSTEMSDT_TOKEN_ITEM_ISSUE_RW }} PR_BODY: ${{ github.event.pull_request.body }} PRNUM: ${{ github.event.pull_request.number }}