From 20414d96593293dd800b054a364cf3f92f662445 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:01:33 +0100 Subject: [PATCH] [gh] better handling of labels (#2517) * Update autolabeler.yml * Update changelog-pr-config.json --- .github/changelog-pr-config.json | 36 ++++++++++++--------- .github/workflows/autolabeler.yml | 54 ++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/.github/changelog-pr-config.json b/.github/changelog-pr-config.json index 911098d9..c552c6a3 100644 --- a/.github/changelog-pr-config.json +++ b/.github/changelog-pr-config.json @@ -1,34 +1,38 @@ [ { - "title": "💥 Breaking Changes", - "labels": ["breaking change"] + "title": "💥 Breaking Changes", + "labels": ["breaking change"] }, { - "title": "✨ New Scripts", - "labels": ["new script"] + "title": "✨ New Scripts", + "labels": ["new script"] }, { - "title": "🚀 Updated Scripts", - "labels": ["update script"] + "title": "🚀 Updated Scripts", + "labels": ["update script"] }, { - "title": "🌐 Website", - "labels": ["website"] + "title": "🆕 Features", + "labels": ["new feature"] }, { - "title": "🐞 Bug Fixes", - "labels": ["bug fix"] + "title": "🌐 Website", + "labels": ["website"] }, { - "title": "🧰 Maintenance", - "labels": ["maintenance"] + "title": "🐞 Bug Fixes", + "labels": ["bug fix"] }, { - "title": "📡 API", - "labels": ["api"] + "title": "🧰 Maintenance", + "labels": ["maintenance"] }, { - "title": "❔ Unlabelled", - "labels": [] + "title": "📡 API", + "labels": ["api"] + }, + { + "title": "❔ Unlabelled", + "labels": [] } ] diff --git a/.github/workflows/autolabeler.yml b/.github/workflows/autolabeler.yml index 660135f5..4ef4f162 100644 --- a/.github/workflows/autolabeler.yml +++ b/.github/workflows/autolabeler.yml @@ -19,7 +19,7 @@ jobs: - name: Install minimatch run: npm install minimatch - - name: Label PR based on config rules + - name: Label PR based on file changes uses: actions/github-script@v7 with: script: | @@ -39,6 +39,8 @@ jobs: }); const prFiles = prListFilesResponse.data; + let labelsToAdd = new Set(); + for (const [label, rules] of Object.entries(autolabelerConfig)) { const shouldAddLabel = prFiles.some((prFile) => { return rules.some((rule) => { @@ -51,12 +53,48 @@ jobs: }); if (shouldAddLabel) { - console.log(`Adding label ${label} to PR ${prNumber}`); - await github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: prNumber, - labels: [label], - }); + labelsToAdd.add(label); } } + + if (labelsToAdd.size > 0) { + console.log(`Adding labels ${Array.from(labelsToAdd).join(", ")} to PR ${prNumber}`); + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + labels: Array.from(labelsToAdd), + }); + } + + - name: Label PR based on PR template selections + uses: actions/github-script@v7 + with: + script: | + const prBody = context.payload.pull_request.body.toLowerCase(); + const prNumber = context.payload.pull_request.number; + const labelMappings = { + "🐞 bug fix": "bug fix", + "✨ new feature": "new feature", + "💥 breaking change": "breaking change", + "🆕 new script": "new script" + }; + + let labelsToAdd = new Set(); + + for (const [checkbox, label] of Object.entries(labelMappings)) { + const regex = new RegExp(`- \\[(.*?)\\] ${checkbox}`, "i"); + if (regex.test(prBody)) { + labelsToAdd.add(label); + } + } + + if (labelsToAdd.size > 0) { + console.log(`Adding labels ${Array.from(labelsToAdd).join(", ")} to PR ${prNumber}`); + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + labels: Array.from(labelsToAdd), + }); + }