From e6211e59572ca0b04ec7ab20f419da60e354d252 Mon Sep 17 00:00:00 2001 From: "Alex.hxy" <1872591453@qq.com> Date: Tue, 11 Feb 2025 17:45:56 +0800 Subject: [PATCH 1/4] fix: adjust pr-label strategy --- .github/workflows/pr-labels.yml | 103 +++++++++++++++++--------------- 1 file changed, 56 insertions(+), 47 deletions(-) diff --git a/.github/workflows/pr-labels.yml b/.github/workflows/pr-labels.yml index 593d5802e1..1145698dfd 100644 --- a/.github/workflows/pr-labels.yml +++ b/.github/workflows/pr-labels.yml @@ -1,14 +1,15 @@ name: PR Review Labels on: + schedule: + - cron: '*/15 * * * *' pull_request_target: - types: [opened, reopened, synchronize, ready_for_review] - pull_request_review: - types: [submitted, edited, dismissed] + types: + - opened + - reopened + - synchronize permissions: - checks: write - contents: read pull-requests: write issues: write @@ -16,55 +17,63 @@ jobs: update-labels: runs-on: ubuntu-latest steps: - - name: Update PR labels based on reviews + - name: Update PR labels uses: actions/github-script@v6 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - const { owner, repo } = context.repo; - // 根据事件类型获取 PR number - const pr_number = context.payload.pull_request - ? context.payload.pull_request.number - : context.payload.review.pull_request_number; + try { + const { owner, repo } = context.repo; - // 获取 PR 的所有 reviews - const reviews = await github.rest.pulls.listReviews({ - owner, - repo, - pull_number: pr_number - }); + // 获取所有开放的 PRs + const { data: prs } = await github.rest.pulls.list({ + owner, + repo, + state: 'open' + }); - // 计算 approved 的数量 - const approvedCount = reviews.data.filter( - review => review.state === 'APPROVED' - ).length; + for (const pr of prs) { + // 获取 PR 的所有 reviews + const { data: reviews } = await github.rest.pulls.listReviews({ + owner, + repo, + pull_number: pr.number + }); - // 获取当前 PR 的标签 - const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ - owner, - repo, - issue_number: pr_number - }); + // 计算最新的 approved 数量 + const latestReviews = new Map(); + reviews.forEach(review => { + latestReviews.set(review.user.id, review); + }); - // 移除已有的 action 标签 - const labelsToRemove = currentLabels - .filter(label => label.name === 'action:merge' || label.name === 'action:review') - .map(label => label.name); + const approvedCount = Array.from(latestReviews.values()) + .filter(review => review.state === 'APPROVED') + .length; - for (const label of labelsToRemove) { - await github.rest.issues.removeLabel({ - owner, - repo, - issue_number: pr_number, - name: label - }); - } + console.log(`PR #${pr.number} - Approved count:`, approvedCount); + + // 获取当前标签 + const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ + owner, + repo, + issue_number: pr.number + }); - // 根据 approved 数量添加对应标签 - const newLabel = approvedCount >= 2 ? 'action:merge' : 'action:review'; - await github.rest.issues.addLabels({ - owner, - repo, - issue_number: pr_number, - labels: [newLabel] - }); + // 保留非 action 标签 + const labelsToKeep = currentLabels + .filter(label => label.name !== 'action:merge' && label.name !== 'action:review') + .map(label => label.name); + + // 添加新标签 + const newLabel = approvedCount >= 2 ? 'action:merge' : 'action:review'; + await github.rest.issues.setLabels({ + owner, + repo, + issue_number: pr.number, + labels: [...labelsToKeep, newLabel] + }); + } + } catch (error) { + console.error('Error:', error); + throw error; + } From c3e5625c63066647c37d540a27701fdf0d658184 Mon Sep 17 00:00:00 2001 From: "Alex.hxy" <1872591453@qq.com> Date: Wed, 12 Feb 2025 11:32:41 +0800 Subject: [PATCH 2/4] fix: adjust time interval --- .github/workflows/pr-labels.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-labels.yml b/.github/workflows/pr-labels.yml index 1145698dfd..436897fb1b 100644 --- a/.github/workflows/pr-labels.yml +++ b/.github/workflows/pr-labels.yml @@ -2,7 +2,7 @@ name: PR Review Labels on: schedule: - - cron: '*/15 * * * *' + - cron: '*/20 * * * *' pull_request_target: types: - opened From 687e25b5357ec53070a56d1612c25185bbfad5c1 Mon Sep 17 00:00:00 2001 From: "Alex.hxy" <1872591453@qq.com> Date: Thu, 13 Feb 2025 10:23:02 +0800 Subject: [PATCH 3/4] fix: adjust interval time --- .github/workflows/pr-labels.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-labels.yml b/.github/workflows/pr-labels.yml index 436897fb1b..fab9736c07 100644 --- a/.github/workflows/pr-labels.yml +++ b/.github/workflows/pr-labels.yml @@ -2,7 +2,7 @@ name: PR Review Labels on: schedule: - - cron: '*/20 * * * *' + - cron: '*/120 * * * *' pull_request_target: types: - opened From 43f6df5541437682182f4df5ecd5cd228aed30bf Mon Sep 17 00:00:00 2001 From: "Alex.hxy" <1872591453@qq.com> Date: Thu, 13 Feb 2025 10:45:48 +0800 Subject: [PATCH 4/4] fix: adjust interval time --- .github/workflows/pr-labels.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr-labels.yml b/.github/workflows/pr-labels.yml index fab9736c07..13d2435f66 100644 --- a/.github/workflows/pr-labels.yml +++ b/.github/workflows/pr-labels.yml @@ -2,7 +2,8 @@ name: PR Review Labels on: schedule: - - cron: '*/120 * * * *' + - cron: '0 */3 * * *' + pull_request_target: types: - opened