Conversation
|
""" Walkthrough添加了一个新的 GitHub Actions 工作流文件 Changes
Sequence Diagram(s)sequenceDiagram
participant EVT as Pull Request Review Event
participant GH as GitHub Actions
participant JOB as Approved Job
participant STEP as Echo Step
participant LABEL as Add Label Step
EVT->>GH: 触发 pull_request_review 事件
GH->>JOB: 启动 approved job (ubuntu-latest)
JOB->>STEP: 执行 echo 输出 review 状态
JOB->>LABEL: 检查并添加标签
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## feat_v3.x #2989 +/- ##
==========================================
Coverage 85.75% 85.75%
==========================================
Files 277 277
Lines 18110 18110
Branches 2735 2735
==========================================
Hits 15531 15531
Misses 2574 2574
Partials 5 5 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (3)
.github/workflows/test-pull_request_review.yml (3)
1-9: 建议添加权限声明工作流配置基本正确,但建议明确声明所需权限以遵循最小权限原则。
建议添加如下权限配置:
name: Test Pull Request Review on: pull_request_review: types: - submitted - edited - dismissed +permissions: + pull-requests: read
11-12: 建议修改任务名称当前任务名称
approved可能造成误解,因为该任务会在所有类型的评审事件(包括驳回)时触发,而不仅仅是批准时触发。建议修改为更准确的名称:
jobs: - approved: + process-review: runs-on: ubuntu-latest
13-14: 建议增强步骤实现当前实现过于简单,建议添加更多上下文信息并进行错误处理。
建议修改为:
steps: - - run: echo "${{ github.event.review.state }}" + - name: 处理评审状态 + run: | + if [ -n "${{ github.event.review.state }}" ]; then + echo "评审状态: ${{ github.event.review.state }}" + echo "评审者: ${{ github.event.review.user.login }}" + echo "评审时间: ${{ github.event.review.submitted_at }}" + else + echo "错误:无法获取评审状态" + exit 1 + fi
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.github/workflows/test-pull_request_review.yml(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build
- GitHub Check: test
🔇 Additional comments (1)
.github/workflows/test-pull_request_review.yml (1)
16-16: 文件结尾换行符设置正确!保留了文件末尾的单个换行符,符合最佳实践。
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
.github/workflows/test-pull_request_review.yml (2)
1-9: 建议添加工作流程说明为了提高可维护性,建议在工作流程名称下方添加对该工作流程功能和目的的简要说明。
建议添加如下注释:
name: Test Pull Request Review +# 该工作流程用于在 PR 审查时自动添加标签,以跟踪审查状态 on: pull_request_review:
11-12: 任务名称不准确当前任务名称
approved可能造成误解,因为该工作流程会处理所有类型的审查事件(已提交、已编辑、已驳回),而不仅仅是批准的审查。建议修改为更准确的名称:
jobs: - approved: + handle_review:
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.github/workflows/test-pull_request_review.yml(1 hunks)
🧰 Additional context used
🪛 actionlint (1.7.4)
.github/workflows/test-pull_request_review.yml
18-18: the runner of "actions/github-script@v5" action is too old to run on GitHub Actions. update the action's version to fix this issue
(action)
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.github/workflows/test-pull_request_review.yml(1 hunks)
🧰 Additional context used
🪛 actionlint (1.7.4)
.github/workflows/test-pull_request_review.yml
22-22: the runner of "actions/github-script@v6" action is too old to run on GitHub Actions. update the action's version to fix this issue
(action)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: test
🔇 Additional comments (3)
.github/workflows/test-pull_request_review.yml (3)
1-13: 工作流配置正确!工作流触发器和权限设置都配置得当。
22-22: 更新 GitHub Script action 版本当前使用的
actions/github-script@v6版本过旧,可能存在兼容性问题。建议更新到最新版本。- uses: actions/github-script@v6 + uses: actions/github-script@v7🧰 Tools
🪛 actionlint (1.7.4)
22-22: the runner of "actions/github-script@v6" action is too old to run on GitHub Actions. update the action's version to fix this issue
(action)
24-24: 验证 GIT_ACTION 令牌权限建议验证
GIT_ACTION令牌的权限范围是否符合最小权限原则。如果只需要管理PR标签,可以考虑使用默认的GITHUB_TOKEN。- github-token: ${{ secrets.GIT_ACTION }} + 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; | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
优化脚本实现
当前实现有以下几点可以改进:
- 审批阈值(当前硬编码为2)应该通过工作流输入参数配置
- 标签名称('action:merge' 和 'action:review')应该可配置
- 代码注释应使用英文以保持一致性
- 错误处理可以更详细
建议按如下方式重构:
with:
github-token: ${{ secrets.GIT_ACTION }}
+ inputs:
+ approval-threshold:
+ description: 'Number of approvals required for merge'
+ default: '2'
+ merge-label:
+ description: 'Label to apply when approval threshold is met'
+ default: 'action:merge'
+ review-label:
+ description: 'Label to apply when approval threshold is not met'
+ default: 'action:review'
script: |
try {
const { owner, repo } = context.repo;
+ const approvalThreshold = parseInt(core.getInput('approval-threshold'));
+ const mergeLabel = core.getInput('merge-label');
+ const reviewLabel = core.getInput('review-label');
- // 获取所有开放的 PRs
+ // Get all open PRs
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
state: 'open'
});
for (const pr of prs) {
- // 获取 PR 的所有 reviews
+ // Get all reviews for the PR
const { data: reviews } = await github.rest.pulls.listReviews({
owner,
repo,
pull_number: pr.number
});
- // 计算最新的 approved 数量
+ // Calculate latest approval count
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);
+ core.info(`PR #${pr.number} - Approved count: ${approvedCount}`);
- // 获取当前标签
+ // Get current labels
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: pr.number
});
- // 保留非 action 标签
+ // Keep non-action labels
const labelsToKeep = currentLabels
- .filter(label => label.name !== 'action:merge' && label.name !== 'action:review')
+ .filter(label => label.name !== mergeLabel && label.name !== reviewLabel)
.map(label => label.name);
- // 添加新标签
- const newLabel = approvedCount >= 2 ? 'action:merge' : 'action:review';
+ // Add new label
+ const newLabel = approvedCount >= approvalThreshold ? mergeLabel : reviewLabel;
await github.rest.issues.setLabels({
owner,
repo,
issue_number: pr.number,
labels: [...labelsToKeep, newLabel]
});
}
} catch (error) {
- console.error('Error:', error);
+ core.error(`Failed to process PR labels: ${error.message}`);
+ core.setFailed(error.message);
throw error;
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| 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; | |
| } | |
| with: | |
| github-token: ${{ secrets.GIT_ACTION }} | |
| inputs: | |
| approval-threshold: | |
| description: 'Number of approvals required for merge' | |
| default: '2' | |
| merge-label: | |
| description: 'Label to apply when approval threshold is met' | |
| default: 'action:merge' | |
| review-label: | |
| description: 'Label to apply when approval threshold is not met' | |
| default: 'action:review' | |
| script: | | |
| try { | |
| const { owner, repo } = context.repo; | |
| const approvalThreshold = parseInt(core.getInput('approval-threshold')); | |
| const mergeLabel = core.getInput('merge-label'); | |
| const reviewLabel = core.getInput('review-label'); | |
| // Get all open PRs | |
| const { data: prs } = await github.rest.pulls.list({ | |
| owner, | |
| repo, | |
| state: 'open' | |
| }); | |
| for (const pr of prs) { | |
| // Get all reviews for the PR | |
| const { data: reviews } = await github.rest.pulls.listReviews({ | |
| owner, | |
| repo, | |
| pull_number: pr.number | |
| }); | |
| // Calculate latest approval count | |
| 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; | |
| core.info(`PR #${pr.number} - Approved count: ${approvedCount}`); | |
| // Get current labels | |
| const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ | |
| owner, | |
| repo, | |
| issue_number: pr.number | |
| }); | |
| // Keep non-action labels | |
| const labelsToKeep = currentLabels | |
| .filter(label => label.name !== mergeLabel && label.name !== reviewLabel) | |
| .map(label => label.name); | |
| // Add new label | |
| const newLabel = approvedCount >= approvalThreshold ? mergeLabel : reviewLabel; | |
| await github.rest.issues.setLabels({ | |
| owner, | |
| repo, | |
| issue_number: pr.number, | |
| labels: [...labelsToKeep, newLabel] | |
| }); | |
| } | |
| } catch (error) { | |
| core.error(`Failed to process PR labels: ${error.message}`); | |
| core.setFailed(error.message); | |
| throw error; | |
| } |
oasis-cloud
left a comment
There was a problem hiding this comment.
FOR_PULL_REVIEW
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
.github/workflows/test-pull_request_review.yml (1)
21-31: 🛠️ Refactor suggestion优化标签更新实现
当前实现存在以下问题:
- 使用 curl 命令不如使用
github-scriptaction 更易维护- 标签名称被硬编码
- 缺少错误处理
- 注释使用中文不符合国际化最佳实践
建议参考之前的评审意见,使用
github-scriptaction 重构实现:- name: Update PR labels uses: actions/github-script@v7 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const label = core.getInput('label') || 'Test'; const { context } = require('@actions/github'); const core = require('@actions/core'); try { const existingLabels = context.payload.pull_request.labels.map(l => l.name); if (!existingLabels.includes(label)) { await github.rest.issues.addLabels({ ...context.repo, issue_number: context.payload.pull_request.number, labels: [label], }); core.info(`Label "${label}" added to PR #${context.payload.pull_request.number}`); } else { core.info(`Label "${label}" already exists on PR #${context.payload.pull_request.number}`); } } catch (error) { core.setFailed(`Failed to add label: ${error.message}`); }另外,请添加一个换行符到文件末尾。
🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 31-31: no new line character at the end of file
(new-line-at-end-of-file)
🧹 Nitpick comments (1)
.github/workflows/test-pull_request_review.yml (1)
14-20: 建议修改任务名称以更好地反映其功能当前任务名称
approved可能会造成误解,因为该任务处理所有审查状态(已提交、已编辑、已驳回)。建议修改为更准确的名称:
jobs: - approved: + handle_review: runs-on: ubuntu-latest
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.github/workflows/test-pull_request_review.yml(1 hunks)
🧰 Additional context used
🪛 YAMLlint (1.35.1)
.github/workflows/test-pull_request_review.yml
[error] 31-31: no new line character at the end of file
(new-line-at-end-of-file)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build
- GitHub Check: test
🔇 Additional comments (1)
.github/workflows/test-pull_request_review.yml (1)
1-13: 工作流触发器和权限配置正确!工作流程正确配置了拉取请求审查事件的触发条件和所需的权限。
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
.github/workflows/test-pull_request_review.yml (1)
14-31: 🛠️ Refactor suggestion优化标签更新实现
当前实现存在以下问题:
- 使用 curl 命令不如使用
github-scriptaction 稳定可靠- 标签名称被硬编码
- 缺少错误处理
- 包含中文注释
建议重构为:
jobs: approved: runs-on: ubuntu-latest steps: - name: print run: echo "${{ toJSON(github.event.review) }}" - - name: Update PR labels - run: | - LABEL_NAME="Test" # 替换为你想要添加的标签名称 - PR_NUMBER=${{ github.event.pull_request.number }} - REPO=${{ github.repository }} - - curl -X POST \ - -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ - -H "Accept: application/vnd.github.v3+json" \ - -d "{\"labels\": [\"$LABEL_NAME\"]}" \ - https://api.github.com/repos/$REPO/issues/$PR_NUMBER/labels + - name: Update PR labels + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + try { + const label = core.getInput('label') || 'reviewed'; + const { context } = require('@actions/github'); + const core = require('@actions/core'); + + // Check if label already exists + const existingLabels = context.payload.pull_request.labels.map(l => l.name); + if (!existingLabels.includes(label)) { + await github.rest.issues.addLabels({ + ...context.repo, + issue_number: context.payload.pull_request.number, + labels: [label] + }); + core.info(`Label "${label}" added to PR #${context.payload.pull_request.number}`); + } else { + core.info(`Label "${label}" already exists on PR #${context.payload.pull_request.number}`); + } + } catch (error) { + core.setFailed(`Failed to add label: ${error.message}`); + }🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 31-31: no new line character at the end of file
(new-line-at-end-of-file)
🧹 Nitpick comments (2)
.github/workflows/test-pull_request_review.yml (2)
1-13: 建议添加工作流程说明为了提高可维护性,建议:
- 在工作流程名称中更具体地说明其功能
- 添加描述性注释说明工作流程的目的和功能
name: Test Pull Request Review +# 描述:此工作流程用于处理拉取请求的审查事件,并根据审查状态更新标签 on: pull_request_review:
31-31: 添加文件结尾的换行符YAML 文件应该以换行符结尾。
https://api.github.com/repos/$REPO/issues/$PR_NUMBER/labels +🧰 Tools
🪛 YAMLlint (1.35.1)
[error] 31-31: no new line character at the end of file
(new-line-at-end-of-file)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
.github/workflows/test-pull_request_review.yml(1 hunks)
🧰 Additional context used
🪛 YAMLlint (1.35.1)
.github/workflows/test-pull_request_review.yml
[error] 31-31: no new line character at the end of file
(new-line-at-end-of-file)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: test
- GitHub Check: build
Summary by CodeRabbit