fix: use branch ref instead of SHA for actions/checkout@v7 - #325
fix: use branch ref instead of SHA for actions/checkout@v7#325hudeng-go wants to merge 1 commit into
Conversation
Using a bare SHA as the ref parameter in actions/checkout@v7 causes
the getRefSpec function to only fetch the specific commit instead
of the full branch. This is problematic when git commit --amend is
used on a PR branch, because the amended commit's SHA is not present
in the initial fetch, and testRef() always returns true for SHA refs
(never triggering the second targeted fetch).
The fix is to use refs/heads/<branch> instead of the bare SHA, which
makes getRefSpecForAllHistory properly handle the ref and allows
testRef() to correctly detect when a force-push has moved the branch.
Log: 修复 actions/checkout@v7 在 git commit --amend 后无法正确获取
PR 分支最新 commit 的问题
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: hudeng-go The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Sorry @hudeng-go, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Reviewer's guide (collapsed on small PRs)Reviewer's GuideUpdates the cppcheck GitHub Actions workflow to checkout the pull request branch by ref name instead of a specific commit SHA, ensuring full branch history is fetched and avoiding failures after amended/force-pushed commits. Sequence diagram for updated checkout behavior in cppcheck workflowsequenceDiagram
actor Developer
participant GitHubActionsRunner as GitHubActionsRunner
participant actions_checkout_v7 as actions_checkout_v7
participant git as git
Developer->>GitHubActionsRunner: open pull_request
GitHubActionsRunner->>actions_checkout_v7: uses actions/checkout@v7
GitHubActionsRunner->>actions_checkout_v7: with ref=refs/heads/${{ github.head_ref }}
actions_checkout_v7->>git: getRefSpecForAllHistory(refs/heads/${{ github.head_ref }})
git-->>actions_checkout_v7: full branch history
actions_checkout_v7->>git: testRef(refs/heads/${{ github.head_ref }})
alt [branch force-pushed]
git-->>actions_checkout_v7: ref changed
actions_checkout_v7->>git: getRefSpecForAllHistory(refs/heads/${{ github.head_ref }})
else [no change]
git-->>actions_checkout_v7: ref unchanged
end
GitHubActionsRunner->>git: git diff base...head
git-->>GitHubActionsRunner: diff succeeds with full history
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
deepin pr auto review★ 总体评分:40分■ 【总体评价】
■ 【详细分析】
■ 【改进建议代码示例】 diff --git a/.github/workflows/cppcheck.yml b/.github/workflows/cppcheck.yml
index 5ae77f4a..85544fae 100644
--- a/.github/workflows/cppcheck.yml
+++ b/.github/workflows/cppcheck.yml
@@ -16,7 +16,7 @@ jobs:
- run: export
- uses: actions/checkout@v7
with:
- ref: refs/heads/${{ github.head_ref }}
+ ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
allow-unsafe-pr-checkout: true
- uses: linuxdeepin/action-cppcheck@main |
|
@hudeng-go: The following test failed, say
Full PR test history. Your PR dashboard. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
问题
actions/checkout@v7在使用ref: ${{ github.event.pull_request.head.sha }}时存在缺陷:getRefSpec对 SHA 只 fetch 单个 commit,不拉取完整分支历史testRef对 SHA 永远返回 true,不会触发第二次 targeted fetchgit commit --amend后,新的 commit SHA 不在本地 git 历史中,导致后续git diff base...head等命令失败详情见 #318 的 CI 失败记录。
修复
将
ref参数从裸 SHA 改为refs/heads/<branch>格式,使getRefSpecForAllHistory能正确处理分支 ref,testRef也能在 force-push 后正确检测到 ref 变化并触发第二次 fetch。变更文件
.github/workflows/cppcheck.yml:ref: ${{ github.event.pull_request.head.sha }}→ref: refs/heads/${{ github.head_ref }}其他受影响的工作流
linuxdeepin/.github仓库中的多个 reusable workflow 也存在同样问题,需要单独修复:.github/workflows/license-check.yml(3 处).github/workflows/commitlint.yml.github/workflows/doc-check.yml.github/workflows/dtk-unittest.yml.github/workflows/build-distribution.yml(4 处).github/workflows/auto-tag.ymlworkflow-templates/cppcheck.yml这些已准备好 patch,需要另提 PR 到
linuxdeepin/.github仓库。Summary by Sourcery
Bug Fixes: