From 80a67bba3da39f4a6b1d8ce0538c3f247d194428 Mon Sep 17 00:00:00 2001 From: Andy Lee Date: Fri, 22 Aug 2025 15:45:54 +0800 Subject: [PATCH] ci: auto add PR label workflow and script (#466) * ci: add auto PR label workflow and script Signed-off-by: Andy Lee * fix: bug fix prefix Signed-off-by: Andy Lee * refactor: update label mapping Signed-off-by: Andy Lee * refactor: read commitlint prefix from commitlint.config.js Signed-off-by: Andy Lee * refactor: rename to scripts/extract-release-label.mjs Signed-off-by: Andy Lee --------- Signed-off-by: Andy Lee (cherry picked from commit ea5e9aa1f41f875fd4e42c0accbb904bd032f570) --- .github/workflows/release-label.yaml | 33 ++++++++++++++++++++++++++++ package.json | 1 + scripts/extract-release-label.mjs | 16 ++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 .github/workflows/release-label.yaml create mode 100644 scripts/extract-release-label.mjs diff --git a/.github/workflows/release-label.yaml b/.github/workflows/release-label.yaml new file mode 100644 index 00000000..37cf830b --- /dev/null +++ b/.github/workflows/release-label.yaml @@ -0,0 +1,33 @@ +name: "[PR Management] Add PR Label" + +on: + pull_request_target: + types: [opened, reopened] + branches: + - main + - 'release-harvester-v*' + +permissions: + pull-requests: write + +jobs: + auto-assign-pr-label: + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: ${{ github.base_ref }} + - name: Setup Nodejs with yarn caching + uses: actions/setup-node@v4 + with: + node-version-file: '.nvmrc' + cache: yarn + - name: Set PR label + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + PR_LABEL=$(node ./scripts/extract-pr-label.js "${{ github.event.pull_request.title }}") + echo "PR_LABEL = $PR_LABEL" + gh pr edit ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --add-label "$PR_LABEL" diff --git a/package.json b/package.json index 0e12aa89..c1ff6425 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "prepare": "husky" }, "devDependencies": { + "@commitlint/load": "^19.8.1", "@commitlint/cli": "^19.8.1", "@commitlint/config-conventional": "^19.8.1", "eslint-plugin-node": "^11.1.0", diff --git a/scripts/extract-release-label.mjs b/scripts/extract-release-label.mjs new file mode 100644 index 00000000..93d13be0 --- /dev/null +++ b/scripts/extract-release-label.mjs @@ -0,0 +1,16 @@ +/* eslint-disable no-console */ +import load from "@commitlint/load"; + +if (process.argv.length < 3) { + console.error('No PR title as first argument'); + process.exit(1); +} + +const prTitle = process.argv[2]; +const prLabel = prTitle.split(':')[0].trim(); + +const config = await load({ extends: ["./commitlint.config.js"] }); +const commitPrefix = config?.rules?.['type-enum']?.[2] || []; +const label = commitPrefix.includes(prLabel) ? prLabel : 'other'; + +console.log(label);