From 7948a0309fbff8a1f3615170ff9a532b22e21706 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Fri, 13 Sep 2024 18:42:38 +0200 Subject: [PATCH] Feat: remove labels and assignees on issue close --- .../remove-labels-and-assignees-on-close.yml | 57 +++++++++++++++++++ .../remove-labels-assignees-on-close.yml | 38 ------------- 2 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 .github/workflows/remove-labels-and-assignees-on-close.yml delete mode 100644 .github/workflows/remove-labels-assignees-on-close.yml diff --git a/.github/workflows/remove-labels-and-assignees-on-close.yml b/.github/workflows/remove-labels-and-assignees-on-close.yml new file mode 100644 index 000000000..a2291f7f9 --- /dev/null +++ b/.github/workflows/remove-labels-and-assignees-on-close.yml @@ -0,0 +1,57 @@ +name: Remove Labels and Assignees on Issue Close + +on: + issues: + types: [closed] + pull_request: + types: [closed] + +jobs: + remove-labels-and-assignees: + runs-on: ubuntu-latest + steps: + - name: Remove labels and assignees + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const issue = context.payload.issue || context.payload.pull_request; + const { owner, repo } = context.repo; + + try { + const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ + owner, + repo, + issue_number: issue.number + }); + + const labelsToKeep = currentLabels + .filter(label => label.name === '⏱︎ Stale') + .map(label => label.name); + + await github.rest.issues.setLabels({ + owner, + repo, + issue_number: issue.number, + labels: labelsToKeep + }); + } catch (error) { + if (error.status !== 404) { + throw error; + } + } + + if (issue.assignees && issue.assignees.length > 0) { + try { + await github.rest.issues.removeAssignees({ + owner, + repo, + issue_number: issue.number, + assignees: issue.assignees.map(assignee => assignee.login) + }); + } catch (error) { + if (error.status !== 404) { + throw error; + } + } + } diff --git a/.github/workflows/remove-labels-assignees-on-close.yml b/.github/workflows/remove-labels-assignees-on-close.yml deleted file mode 100644 index 584842290..000000000 --- a/.github/workflows/remove-labels-assignees-on-close.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Remove Labels and Assignees on Issue Close - -on: - issues: - types: [closed] - pull_request: - types: [closed] - -jobs: - remove-labels-and-assignees: - runs-on: ubuntu-latest - steps: - - name: Remove all labels and assignees - uses: actions/github-script@v6 - with: - github-token: ${{secrets.GITHUB_TOKEN}} - script: | - const issue = context.payload.issue || context.payload.pull_request; - const repo = context.repo; - - // Remove all labels - await github.rest.issues.removeAllLabels({ - owner: repo.owner, - repo: repo.name, - issue_number: issue.number - }); - - // Remove all assignees - if (issue.assignees && issue.assignees.length > 0) { - await github.rest.issues.removeAssignees({ - owner: repo.owner, - repo: repo.name, - issue_number: issue.number, - assignees: issue.assignees.map(assignee => assignee.login) - }); - } - - console.log(`Removed all labels and assignees from issue/PR #${issue.number}`); \ No newline at end of file