From 4c832ad98440e5f56a490e5f884a218cbfd2056d Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Sun, 26 May 2024 22:00:15 -0400 Subject: [PATCH] ci: update status labels on issue change --- .github/workflows/label-on-change.yml | 105 ++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 .github/workflows/label-on-change.yml diff --git a/.github/workflows/label-on-change.yml b/.github/workflows/label-on-change.yml new file mode 100644 index 0000000000..d0278d8482 --- /dev/null +++ b/.github/workflows/label-on-change.yml @@ -0,0 +1,105 @@ +name: label-on-change + +on: + # https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target + issues: + types: + - assigned + - unassigned + - closed + - edited + - labeled + - reopened + + # TODO: Handle labeling on comment + +jobs: + on-labeled-ensure-one-status: + runs-on: ubuntu-latest + permissions: + issues: write + # Only run on issue labeled and if label starts with 'status:' + if: github.event.action == 'labeled' && startsWith(github.event.label.name, 'status:') + steps: + - name: Ensure only one status label + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + // Get all labels that start with 'status:' and are not the incoming label + const incomingLabelName = context.payload.label.name; + const labelNamesToRemove = context.payload.issue.labels + .filter(label => label.name.startsWith('status:') && label.name !== incomingLabelName) + .map(label => label.name); + + if (!labelNamesToRemove.length) { + console.log('No labels to remove'); + return; + } + + console.log(`Labels to remove: '${labelNamesToRemove}'`); + + // If there is more than one status label, remove all but the incoming label + for (const labelName of labelNamesToRemove) { + await github.rest.issues.removeLabel({ + issue_number: context.issue.number, + name: labelName, + owner: context.repo.owner, + repo: context.repo.repo, + }); + console.log(`Removed '${labelName}' label`); + } + + on-issue-close: + runs-on: ubuntu-latest + permissions: + issues: write + if: github.event.action == 'closed' + steps: + - name: Remove all labels on issue close + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + // Get all labels that start with 'status:' and 'stale' + const labelNamesToRemove = context.payload.issue.labels + .filter(label => label.name.startsWith('status:') || label.name === 'stale') + .map(label => label.name); + + if (!labelNamesToRemove.length) { + console.log('No labels to remove'); + return; + } + + console.log(`Labels to remove: '${labelNamesToRemove}'`); + + for (const labelName of labelNamesToRemove) { + await github.rest.issues.removeLabel({ + issue_number: context.issue.number, + name: labelName, + owner: context.repo.owner, + repo: context.repo.repo, + }); + console.log(`Removed '${labelName}' label`); + } + + on-issue-reopen: + runs-on: ubuntu-latest + permissions: + issues: write + if: github.event.action == 'reopened' + steps: + - name: Add needs-triage label on issue reopen + uses: actions-ecosystem/action-add-labels@v1 + with: + labels: 'status: needs-triage' + + # on-pr-merge: + # runs-on: ubuntu-latest + # if: github.event.pull_request.merged == true + # steps: + + # on-pr-close: + # runs-on: ubuntu-latest + # if: github.event_name == 'pull_request_target' && github.event.pull_request.merged == false + # steps: