From 14b5a54c3386448f3aa69f237417161fc7aaa18e Mon Sep 17 00:00:00 2001 From: Rizel Scarlett Date: Wed, 2 Oct 2024 06:36:09 -0400 Subject: [PATCH] won't fail if issue number isnt found --- .../workflows/add-hacktoberfest-labels.yml | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/.github/workflows/add-hacktoberfest-labels.yml b/.github/workflows/add-hacktoberfest-labels.yml index 8b53244..ca0a09f 100644 --- a/.github/workflows/add-hacktoberfest-labels.yml +++ b/.github/workflows/add-hacktoberfest-labels.yml @@ -1,29 +1,33 @@ name: Propagate Issue Labels to PR - on: pull_request: types: [opened, synchronize] - jobs: copy_labels: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - - name: Get issue number from PR body id: issue_number - run: | - issue_number=$(echo "${{ github.event.pull_request.body }}" | grep -oP '(?<=#)\d+' | head -n1) - if [ -z "$issue_number" ]; then - echo "No issue number found in PR body" - echo "has_issue=false" >> $GITHUB_OUTPUT - else - echo "Issue number found: $issue_number" - echo "has_issue=true" >> $GITHUB_OUTPUT - echo "issue_number=$issue_number" >> $GITHUB_OUTPUT - fi - + uses: actions/github-script@v6gi + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const prBody = context.payload.pull_request.body || ''; + // Remove HTML comments + const bodyWithoutComments = prBody.replace(//g, ''); + // Find issue number + const match = bodyWithoutComments.match(/(?:Resolves|Closes) #(\d+)/); + const issueNumber = match ? match[1] : null; + if (issueNumber) { + console.log(`Issue number found: ${issueNumber}`); + core.setOutput('has_issue', 'true'); + core.setOutput('issue_number', issueNumber); + } else { + console.log('No issue number found in PR body'); + core.setOutput('has_issue', 'false'); + } - name: Get labels from linked issue if: steps.issue_number.outputs.has_issue == 'true' uses: actions/github-script@v6 @@ -43,7 +47,6 @@ jobs: console.log(`Error fetching issue labels: ${error}`); return []; } - - name: Check for required labels if: steps.issue_number.outputs.has_issue == 'true' && steps.issue_labels.outputs.result != '[]' id: check_labels @@ -55,7 +58,6 @@ jobs: const hacktoberfestLabel = labels.some(label => label.toLowerCase().includes('hacktoberfest')); const sizeLabelPresent = labels.some(label => ['small', 'medium', 'large'].includes(label.toLowerCase())); return hacktoberfestLabel || sizeLabelPresent; - - name: Add labels to PR if: steps.issue_number.outputs.has_issue == 'true' && steps.check_labels.outputs.result == 'true' uses: actions/github-script@v6