Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add PR review labels automation #2972

Merged
merged 6 commits into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions .github/workflows/pr-labels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: PR Review Labels

on:
pull_request_target:
types: [opened, reopened, synchronize, ready_for_review]
pull_request_review:
types: [submitted, edited, dismissed]

permissions:
contents: read
pull-requests: write

jobs:
update-labels:
runs-on: ubuntu-latest
steps:
- name: Update PR labels based on reviews
uses: actions/github-script@v6
with:
script: |
const { owner, repo } = context.repo;
const pr_number = context.payload.pull_request.number;

// 获取 PR 的所有 reviews
const reviews = await github.rest.pulls.listReviews({
owner,
repo,
pull_number: pr_number
});

// 计算 approved 的数量
const approvedCount = reviews.data.filter(
review => review.state === 'APPROVED'
).length;

// 获取当前 PR 的标签
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: pr_number
});

// 移除已有的 action 标签
const labelsToRemove = currentLabels
.filter(label => label.name === 'action:merge' || label.name === 'action:review')
.map(label => label.name);

for (const label of labelsToRemove) {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: pr_number,
name: label
});
}

// 根据 approved 数量添加对应标签
const newLabel = approvedCount >= 2 ? 'action:merge' : 'action:review';
await github.rest.issues.addLabels({
owner,
repo,
issue_number: pr_number,
labels: [newLabel]
});
Loading