From 1636ce4826d7150299cad2038db7df05f1be540d Mon Sep 17 00:00:00 2001 From: mohammed_9456 <41828058+Marco-9456@users.noreply.github.com> Date: Sun, 27 Aug 2023 12:16:58 +0200 Subject: [PATCH] Rewrite of Issue-Handler.yaml - Renamed the action to Issue-Handler - Rewritten the whole script and simplified it. --- .github/workflows/Issue-Handler.yaml | 73 ++++++++++++ .github/workflows/MetaIssueHandler.yaml | 144 ------------------------ 2 files changed, 73 insertions(+), 144 deletions(-) create mode 100644 .github/workflows/Issue-Handler.yaml delete mode 100644 .github/workflows/MetaIssueHandler.yaml diff --git a/.github/workflows/Issue-Handler.yaml b/.github/workflows/Issue-Handler.yaml new file mode 100644 index 000000000..2738babea --- /dev/null +++ b/.github/workflows/Issue-Handler.yaml @@ -0,0 +1,73 @@ +name: Check and Close Issues + +on: + issues: + types: [opened] + +jobs: + handle-issues: + if: github.event_name == 'issues' + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + + - name: Check and close issues + id: close-issues + uses: actions/github-script@v3 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const keywordsToCheck = ['instagram', 'facebook', 'twitter']; + const requiredLabels = ['bug', 'enhancement']; + const referenceIssueNumber = 733; + + async function processIssue(issue) { + const issueBody = issue.body.toLowerCase(); + const issueLabels = issue.labels.map(label => label.name); + + const shouldCloseIssue = keywordsToCheck.some(keyword => issueBody.includes(keyword)) && + requiredLabels.some(label => issueLabels.includes(label)); + + if (shouldCloseIssue) { + await github.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + state: 'closed' + }); + + await github.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + labels: ['wontfix'] + }); + + await github.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue.number, + body: `This issue has been closed and labeled as wontfix. Please see issue #${referenceIssueNumber} for more details.` + }); + } + } + + // Process newly opened issues + if (context.payload.action === 'opened') { + await processIssue(context.payload.issue); + } + + // Process all existing open issues + const issues = await github.issues.listForRepo({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open' + }); + + for (const issue of issues.data) { + await processIssue(issue); + } diff --git a/.github/workflows/MetaIssueHandler.yaml b/.github/workflows/MetaIssueHandler.yaml deleted file mode 100644 index a05f0b8bf..000000000 --- a/.github/workflows/MetaIssueHandler.yaml +++ /dev/null @@ -1,144 +0,0 @@ -on: - - issues: - - types: [opened] - - workflow_dispatch: - -jobs: - - auto_close_issues: - - runs-on: ubuntu-latest - - if: ${{ github.event_name == 'issues' }} - - steps: - - - name: 'Close issue and add wontfix label' - - uses: actions/github-script@v3 - - with: - - github-token: ${{secrets.GITHUB_TOKEN}} - - script: | - - const issueBody = context.payload.issue.body; - - const labels = context.payload.issue.labels.map(label => label.name); - - if ((issueBody.includes('instagram') || issueBody.includes('facebook') || issueBody.includes('twitter')) && labels.includes('bug')) { - - await github.issues.update({ - - issue_number: context.issue.number, - - owner: context.repo.owner, - - repo: context.repo.repo, - - state: 'closed' - - }); - - await github.issues.addLabels({ - - issue_number: context.issue.number, - - owner: context.repo.owner, - - repo: context.repo.repo, - - labels: ['wontfix'] - - }); - - await github.issues.createComment({ - - issue_number: context.issue.number, - - owner: context.repo.owner, - - repo: context.repo.repo, - - body: 'Please check issue https://github.com/JunkFood02/Seal/issues/733 for more info.' - - }); - - } - - label_and_close_old_issues: - - runs-on: ubuntu-latest - - if: ${{ github.event_name == 'workflow_dispatch' }} - - steps: - - - name: 'Label and close old issues' - - uses: actions/github-script@v3 - - with: - - github-token: ${{secrets.GITHUB_TOKEN}} - - script: | - - const q = 'instagram in:body label:bug repo:${{ github.repository }} OR facebook in:body label:bug repo:${{ github.repository }} state:open'; - - github.paginate(github.search.issuesAndPullRequests, { q }, (response) => { - - response.data.forEach(async issue => { - - const labels = issue.labels.map(label => label.name); - - - - if (!labels.includes('wontfix')) { - - await github.issues.addLabels({ - - issue_number: issue.number, - - owner: context.repo.owner, - - repo: context.repo.repo, - - labels: ['wontfix'] - - }); - - } - - await github.issues.update({ - - issue_number: issue.number, - - owner: context.repo.owner, - - repo: context.repo.repo, - - state: 'closed' - - }); - - await github.issues.createComment({ - - issue_number: issue.number, - - owner: context.repo.owner, - - repo: context.repo.repo, - - body: 'Please check issue https://github.com/JunkFood02/Seal/issues/733 for more info.' - - }); - - }) - - }) -