From db186366cb82cec6f562e93431ac618b79a67025 Mon Sep 17 00:00:00 2001 From: Simon Thulbourn Date: Fri, 8 Sep 2023 12:14:20 +0000 Subject: [PATCH] chore(automation): remove previous labels when PR is updated --- .github/scripts/label_pr_based_on_title.js | 16 ++++++++++++++++ .github/scripts/save_pr_details.js | 14 ++++++++++++-- .github/workflows/reusable_export_pr_details.yml | 7 +++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/.github/scripts/label_pr_based_on_title.js b/.github/scripts/label_pr_based_on_title.js index 95c33841f92..e2e208c2d78 100644 --- a/.github/scripts/label_pr_based_on_title.js +++ b/.github/scripts/label_pr_based_on_title.js @@ -17,6 +17,10 @@ module.exports = async ({github, context, core}) => { "deprecated": DEPRECATED_REGEX, } + // get PR labels from env + const prLabels = process.env.PR_LABELS.replaceAll("\"", "").split(","); + const labelKeys = Object.keys(labels); + // Maintenance: We should keep track of modified PRs in case their titles change let miss = 0; try { @@ -26,6 +30,18 @@ module.exports = async ({github, context, core}) => { if (matches != null) { core.info(`Auto-labeling PR ${PR_NUMBER} with ${label}`) + for (const prLabel of prLabels) { + if (labelKeys.includes(prLabel) && prLabel !== label) { + core.info(`PR previously tagged with: ${prLabel}, removing.`); + await github.rest.issues.removeLabel({ + issue_number: PR_NUMBER, + owner: context.repo.owner, + repo: context.repo.repo, + name: prLabel + }) + } + } + await github.rest.issues.addLabels({ issue_number: PR_NUMBER, owner: context.repo.owner, diff --git a/.github/scripts/save_pr_details.js b/.github/scripts/save_pr_details.js index 83bd3bf70d4..ba2de975b3c 100644 --- a/.github/scripts/save_pr_details.js +++ b/.github/scripts/save_pr_details.js @@ -1,9 +1,19 @@ -module.exports = async ({context, core}) => { +module.exports = async ({github, context, core}) => { const fs = require('fs'); const filename = "pr.txt"; + const labelsData = await github.rest.issues.listLabelsOnIssue({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: (context.payload.issue || context.payload.pull_request || context.payload).number, + }); + + const labels = labelsData.data.map((label) => { + return label['name']; + }); + try { - fs.writeFileSync(`./${filename}`, JSON.stringify(context.payload)); + fs.writeFileSync(`./${filename}`, JSON.stringify({...context.payload, ...{labels:labels.join(",")}})); return `PR successfully saved ${filename}` } catch (err) { diff --git a/.github/workflows/reusable_export_pr_details.yml b/.github/workflows/reusable_export_pr_details.yml index d942a156950..ad29d4c9bf1 100644 --- a/.github/workflows/reusable_export_pr_details.yml +++ b/.github/workflows/reusable_export_pr_details.yml @@ -49,6 +49,9 @@ on: prIsMerged: description: "Whether PR is merged" value: ${{ jobs.export_pr_details.outputs.prIsMerged }} + prLabels: + description: "PR Labels" + value: ${{ jobs.export_pr_details.outputs.prLabels }} permissions: contents: read @@ -70,6 +73,7 @@ jobs: prAuthor: ${{ steps.prAuthor.outputs.prAuthor }} prAction: ${{ steps.prAction.outputs.prAction }} prIsMerged: ${{ steps.prIsMerged.outputs.prIsMerged }} + prLabels: ${{ steps.prLabels.outputs.prLabels }} steps: - name: Checkout repository # in case caller workflow doesn't checkout thus failing with file not found uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 @@ -106,3 +110,6 @@ jobs: - name: "Export Pull Request Merged status" id: prIsMerged run: echo prIsMerged="$(jq -c '.pull_request.merged' "${FILENAME}")" >> "$GITHUB_OUTPUT" + - name: "Export Pull Request labels" + id: prLabels + run: echo prLabels="$(jq -c '.labels' "${FILENAME}")" >> "$GITHUB_OUTPUT" \ No newline at end of file