Skip to content

fix: handle detached HEAD in PR workflows#94

Open
danielsuguimoto wants to merge 1 commit intokompassdev:mainfrom
danielsuguimoto:fix-pr-commands
Open

fix: handle detached HEAD in PR workflows#94
danielsuguimoto wants to merge 1 commit intokompassdev:mainfrom
danielsuguimoto:fix-pr-commands

Conversation

@danielsuguimoto
Copy link
Copy Markdown
Contributor

@danielsuguimoto danielsuguimoto commented Apr 10, 2026

Ticket

SKIPPED

Summary

Fixes a failure in pr/review and pr/fix commands when run inside GitHub Actions CI/CD pipelines. By default, actions/checkout@v4 on pull_request events checks out the PR merge commit in detached HEAD mode — meaning git branch --show-current returns empty. The branch alignment logic previously treated any mismatch as a checkout target, but without the branch being fetched locally the checkout would fail and the command would stop.

Root Cause

GitHub Actions pull_request workflows run on a detached HEAD pointing to the synthetic merge commit (refs/pull/N/merge), not the PR head branch. The old logic compared <current-branch> against <pr-branch> and immediately tried git checkout <pr-branch> on mismatch — which fails because the branch ref is not available locally in a shallow CI clone.

Changes

  • Added git rev-parse HEAD<current-head> to the load-pr component to capture the current commit SHA alongside the branch name
  • Rewrote branch alignment in pr/review and pr/fix from a two-way to a three-way check:
    1. <current-branch> equals <pr-branch> → already on the correct branch, proceed
    2. <current-branch> is empty (detached HEAD) and <current-head> matches pr.headRefOid → already at the correct commit, skip checkout
    3. Otherwise → run git fetch origin <pr-branch> first to ensure the branch is available locally, then checkout
  • The explicit fetch before checkout makes the commands self-sufficient regardless of how shallow the CI clone is, removing the dependency on ref: ${{ github.head_ref }} in the caller's workflow
  • Added ref: ${{ github.head_ref }} to the checkout step in the Kompass CI workflow so the runner checks out the named branch directly, avoiding detached HEAD entirely and hitting the fast path in the alignment logic

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.

1 participant