Skip to content

fix: use branch ref instead of SHA for actions/checkout@v7 - #797

Merged
BLumia merged 1 commit into
linuxdeepin:masterfrom
hudeng-go:fix/checkout-v7-sha-ref
Jul 29, 2026
Merged

fix: use branch ref instead of SHA for actions/checkout@v7#797
BLumia merged 1 commit into
linuxdeepin:masterfrom
hudeng-go:fix/checkout-v7-sha-ref

Conversation

@hudeng-go

@hudeng-go hudeng-go commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

问题

actions/checkout@v7 在使用 ref: ${{ github.event.pull_request.head.sha }} 时存在缺陷:

  1. getRefSpec 对 SHA 只 fetch 单个 commit,不拉取完整分支历史
  2. testRef 对 SHA 永远返回 true,不会触发第二次 targeted fetch
  3. 当 PR 分支使用 git commit --amend 后,新的 commit SHA 不在本地 git 历史中,导致后续 git diff base...head 等命令失败

修复

ref 参数从裸 SHA 改为 refs/heads/<branch> 格式,使 getRefSpecForAllHistory 能正确处理分支 ref,testRef 也能在 force-push 后正确检测到 ref 变化并触发第二次 fetch。

变更文件

文件 修改处数
.github/workflows/license-check.yml 3
.github/workflows/build-distribution.yml 4
.github/workflows/auto-tag.yml 1
.github/workflows/commitlint.yml 1
.github/workflows/doc-check.yml 1
.github/workflows/dtk-unittest.yml 1
workflow-templates/cppcheck.yml 1

相关 PR

Summary by Sourcery

Update GitHub Actions workflows to check out pull request branches by ref name instead of commit SHA to ensure full history and correct behavior after force-pushes.

Bug Fixes:

  • Fix pull request checkout issues caused by using head commit SHA, which could leave new commits missing from local history and break diff-based steps.

CI:

  • Adjust multiple GitHub Actions workflows to pass branch refs (refs/heads/) to actions/checkout@v7 when handling pull requests.

Using bare SHA as ref parameter in actions/checkout@v7 causes
getRefSpec to only fetch the specific commit instead of the full
branch. This breaks when git commit --amend is used on a PR branch,
because testRef() always returns true for SHA refs, never triggering
the second targeted fetch.

Fix all affected workflows to use refs/heads/<branch> instead of
the bare SHA, consistent with the upstream fix guidance.

Affected workflows:
- license-check.yml (3 occurrences)
- build-distribution.yml (4 occurrences)
- auto-tag.yml
- commitlint.yml
- doc-check.yml
- dtk-unittest.yml
- workflow-templates/cppcheck.yml

Log: 修复 actions/checkout@v7 在 git commit --amend 后无法正确获取
      PR 分支最新 commit 的问题

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @hudeng-go, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@sourcery-ai

sourcery-ai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaces all usages of actions/checkout@v7 that checked out a PR head by bare SHA with branch ref-based checkouts so that full branch history is fetched and force-pushed PRs are handled correctly across all CI workflows.

Sequence diagram for actions_checkout@v7 using branch ref instead of SHA

sequenceDiagram
  actor GitHubActionsRunner
  participant actions_checkout_v7
  participant remote_repo
  participant local_git

  GitHubActionsRunner->>actions_checkout_v7: uses actions/checkout@v7
  actions_checkout_v7->>remote_repo: getRefSpecForAllHistory(refs/heads/github.event.pull_request.head.ref)
  remote_repo-->>actions_checkout_v7: full branch history
  actions_checkout_v7->>local_git: testRef(refs/heads/github.event.pull_request.head.ref)
  alt [ref changed after force-push]
    actions_checkout_v7->>remote_repo: getRefSpecForAllHistory(refs/heads/github.event.pull_request.head.ref)
    remote_repo-->>actions_checkout_v7: updated branch history
  end
  actions_checkout_v7->>local_git: git diff base...head
  local_git-->>GitHubActionsRunner: diff succeeds with full history
Loading

File-Level Changes

Change Details Files
Use PR head branch ref instead of bare SHA when checking out pull request code to ensure full history and proper ref updates.
  • Update checkout ref from github.event.pull_request.head.sha to refs/heads/${{ github.event.pull_request.head.ref }} for PR-triggered jobs.
  • Preserve existing checkout options such as fetch-depth, repository, allow-unsafe-pr-checkout, and persist-credentials while changing only the ref parameter.
  • Apply the same ref change consistently across all affected workflows and workflow templates that operate on PR heads.
.github/workflows/build-distribution.yml
.github/workflows/license-check.yml
.github/workflows/auto-tag.yml
.github/workflows/commitlint.yml
.github/workflows/doc-check.yml
.github/workflows/dtk-unittest.yml
workflow-templates/cppcheck.yml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown
Contributor

deepin pr auto review

★ 总体评分:60分

■ 【总体评价】

代码通过修改 checkout 的 ref 引用方式提升了 CI 流水线拉取 PR 代码的稳定性,但保留了不安全的 PR 检出配置。
逻辑正确且修复了潜在的构建失败问题,但因保留不安全的 checkout 配置导致存在中危安全漏洞,触发安全上限规则扣分。

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

修改涉及多个 YAML 文件,将 ref 的值从 ${{ github.event.pull_request.head.sha }} 统一替换为 refs/heads/${{ github.event.pull_request.head.ref }},YAML 语法正确,字符串拼接逻辑无误。
潜在问题:无
建议:无

  • 2.代码质量(良好)✓

修改在所有相关的工作流文件中保持了一致性,未引入冗余代码,符合 GitHub Actions 的配置规范。
潜在问题:无
建议:无

  • 3.代码性能(无性能问题)✓

使用分支名替代 SHA 进行 checkout 不会对性能产生负面影响,反而可能减少因 SHA 不可达导致的重试开销。
潜在问题:无
建议:无

  • 4.代码安全(存在 1 个安全漏洞)✕

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 1 个
本次 diff 未引入新的安全漏洞,但保留了原有的不安全配置。在多个工作流文件中,allow-unsafe-pr-checkout: true 允许检出来自 Fork 仓库的 PR 代码,若工作流后续步骤执行了该代码中的构建脚本或测试命令,攻击面将扩展至整个 CI 运行环境。

  • 安全漏洞1(中危):不安全的 PR 检出配置 在 actions/checkout 步骤中,配置 allow-unsafe-pr-checkout: true 允许检出不受信任的 Fork 仓库代码。若工作流(如 cppcheck.yml)在检出后执行了代码中包含的构建脚本或命令,攻击者可通过提交恶意 PR 触发远程代码执行,危害 CI 环境及可能泄露仓库密钥 ——非常重要

  • 建议:移除 allow-unsafe-pr-checkout: true 配置,或确保在检出 Fork 仓库代码后,工作流不执行任何来自不受信任源的代码或脚本;对于需要执行构建的流程,应限制仅允许来自同组织或信任仓库的 PR。

■ 【改进建议代码示例】

--- a/.github/workflows/auto-tag.yml
+++ b/.github/workflows/auto-tag.yml
@@ -18,8 +18,7 @@ jobs:
     steps:
       - uses: actions/checkout@v7
         with:
           ref: refs/heads/${{ github.event.pull_request.head.ref }}
           repository: ${{ github.event.pull_request.head.repo.full_name }}
-          allow-unsafe-pr-checkout: true
           persist-credentials: false

@deepin-ci-robot

Copy link
Copy Markdown
Contributor

@hudeng-go: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
github-pr-review-ci 65060f0 link true /test github-pr-review-ci

Full PR test history. Your PR dashboard.

Details

Instructions 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.

@deepin-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia, hudeng-go

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@BLumia
BLumia merged commit 826bedc into linuxdeepin:master Jul 29, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants