-
Notifications
You must be signed in to change notification settings - Fork 280
fix: adjust pr-label strategy #2984
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,80 @@ | ||
name: PR Review Labels | ||
|
||
on: | ||
schedule: | ||
- cron: '0 */3 * * *' | ||
|
||
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 | ||
|
||
jobs: | ||
update-labels: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Update PR labels based on reviews | ||
- name: Update PR labels | ||
uses: actions/github-script@v6 | ||
Miles-hxy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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' | ||
}); | ||
Miles-hxy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 计算 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; | ||
} | ||
Miles-hxy marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.