From 77de790c9fe0ac4a1afc837121ab40de8ea3c67e Mon Sep 17 00:00:00 2001 From: Rizel Scarlett Date: Tue, 1 Oct 2024 23:00:34 -0400 Subject: [PATCH 1/4] Automating adding Hacktoberfest labels --- .github/PULL_REQUEST_TEMPLATE.md | 83 +++++++++++++++++++ .../workflows/add-hacktoberfest-labels.yml | 79 ++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md create mode 100644 .github/workflows/add-hacktoberfest-labels.yml diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..a676b8f --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,83 @@ + + +## What type of PR is this? (check all applicable) + +- [ ] โ™ป๏ธ Refactor +- [ ] โœจ New Feature +- [ ] ๐Ÿ› Bug Fix +- [ ] ๐Ÿ“ Documentation Update +- [ ] ๐Ÿ‘ท Example Application +- [ ] ๐Ÿง‘โ€๐Ÿ’ป Code Snippet +- [ ] ๐ŸŽจ Design +- [ ] ๐Ÿ“– Content +- [ ] ๐Ÿงช Tests +- [ ] ๐Ÿ”– Release +- [ ] ๐Ÿšฉ Other + +## Description + + + +This PR [adds/removes/fixes/replaces] this [feature/bug/etc]. + +## Related Tickets & Documents + +Resolves # + +## Mobile & Desktop Screenshots/Recordings + + + +## Added code snippets? +- [ ] ๐Ÿ‘ yes +- [ ] ๐Ÿ™… no, because they aren't needed + +## Added tests? + +- [ ] ๐Ÿ‘ yes +- [ ] ๐Ÿ™… no, because they aren't needed +- [ ] ๐Ÿ™‹ no, because I need help + +### No tests? Add a note + + +## Added to documentation? + +- [ ] ๐Ÿ“œ readme +- [ ] ๐Ÿ“œ contributing.md +- [ ] ๐Ÿ““ general documentation +- [ ] ๐Ÿ™… no documentation needed + +### No docs? Add a note + + +## [optional] Are there any post-deployment tasks we need to perform? + + + +## [optional] What gif best describes this PR or how it makes you feel? + + + + \ No newline at end of file diff --git a/.github/workflows/add-hacktoberfest-labels.yml b/.github/workflows/add-hacktoberfest-labels.yml new file mode 100644 index 0000000..fa0c888 --- /dev/null +++ b/.github/workflows/add-hacktoberfest-labels.yml @@ -0,0 +1,79 @@ +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 + uses: actions/github-script@v6 + 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 + id: issue_labels + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const issue_number = ${{ steps.issue_number.outputs.issue_number }}; + try { + const issue = await github.rest.issues.get({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: parseInt(issue_number) + }); + return issue.data.labels.map(label => label.name); + } catch (error) { + 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 + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const labels = ${{ steps.issue_labels.outputs.result }}; + 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 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const pr_number = context.issue.number; + const labels = ${{ steps.issue_labels.outputs.result }}; + try { + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pr_number, + labels: labels + }); + console.log('Labels added successfully'); + } catch (error) { + console.log(`Error adding labels: ${error}`); + } \ No newline at end of file From bd27b56502691842e5bdadae569977e7536a73b4 Mon Sep 17 00:00:00 2001 From: Rizel Scarlett Date: Wed, 2 Oct 2024 06:32:14 -0400 Subject: [PATCH 2/4] shouldn't fail if issue number isnt present --- .../workflows/add-hacktoberfest-labels.yml | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/.github/workflows/add-hacktoberfest-labels.yml b/.github/workflows/add-hacktoberfest-labels.yml index fa0c888..8b53244 100644 --- a/.github/workflows/add-hacktoberfest-labels.yml +++ b/.github/workflows/add-hacktoberfest-labels.yml @@ -1,33 +1,29 @@ 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 - uses: actions/github-script@v6 - 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'); - } + 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 + - name: Get labels from linked issue if: steps.issue_number.outputs.has_issue == 'true' uses: actions/github-script@v6 @@ -47,6 +43,7 @@ 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 @@ -58,6 +55,7 @@ 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 @@ -76,4 +74,4 @@ jobs: console.log('Labels added successfully'); } catch (error) { console.log(`Error adding labels: ${error}`); - } \ No newline at end of file + } From 14b5a54c3386448f3aa69f237417161fc7aaa18e Mon Sep 17 00:00:00 2001 From: Rizel Scarlett Date: Wed, 2 Oct 2024 06:36:09 -0400 Subject: [PATCH 3/4] 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 From d81faccba12ce6a889771fc9b5068a0f02a87a45 Mon Sep 17 00:00:00 2001 From: Rizel Scarlett Date: Wed, 2 Oct 2024 06:40:35 -0400 Subject: [PATCH 4/4] fix typo --- .github/workflows/add-hacktoberfest-labels.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/add-hacktoberfest-labels.yml b/.github/workflows/add-hacktoberfest-labels.yml index ca0a09f..14b7dff 100644 --- a/.github/workflows/add-hacktoberfest-labels.yml +++ b/.github/workflows/add-hacktoberfest-labels.yml @@ -10,7 +10,7 @@ jobs: uses: actions/checkout@v4 - name: Get issue number from PR body id: issue_number - uses: actions/github-script@v6gi + uses: actions/github-script@v6 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: |