-
Notifications
You must be signed in to change notification settings - Fork 296
80 lines (68 loc) · 2.35 KB
/
pr-labels.yml
File metadata and controls
80 lines (68 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: PR Review Labels
on:
schedule:
- cron: '0 */3 * * *'
pull_request_target:
types:
- opened
- reopened
- synchronize
permissions:
pull-requests: write
issues: write
jobs:
update-labels:
runs-on: ubuntu-latest
steps:
- name: Update PR labels
uses: actions/github-script@v9
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
const { owner, repo } = context.repo;
// 获取所有开放的 PRs
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
state: 'open'
});
for (const pr of prs) {
// 获取 PR 的所有 reviews
const { data: reviews } = await github.rest.pulls.listReviews({
owner,
repo,
pull_number: pr.number
});
// 计算最新的 approved 数量
const latestReviews = new Map();
reviews.forEach(review => {
latestReviews.set(review.user.id, review);
});
const approvedCount = Array.from(latestReviews.values())
.filter(review => review.state === 'APPROVED')
.length;
console.log(`PR #${pr.number} - Approved count:`, approvedCount);
// 获取当前标签
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: pr.number
});
// 保留非 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;
}