Skip to content

Commit 268f375

Browse files
runningcodeclaude
andauthored
feat: auto-fetch head-ref from GitHub Actions in detached HEAD state (#2805)
## Summary - Add `get_github_head_ref()` function to automatically detect head branch from GitHub Actions environment variables - Integrate with upload command fallback chain to resolve detached HEAD scenarios - Use `GITHUB_HEAD_REF` for automatic head-ref detection in PR workflows See context reference: https://docs.github.com/en/actions/reference/workflows-and-actions/contexts ## Context Resolves EME-367 - Auto-fetch correct head-ref value in detached HEAD state using GitHub Actions env variables. Follows the same pattern as recent work on base SHA auto-fetching (#2799). 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds get_github_head_ref() (reads GITHUB_HEAD_REF in pull_request) and makes build upload prefer it before git head ref to handle detached HEAD. > > - **Build Upload**: > - Update `src/commands/build/upload.rs` head-ref resolution to first use `get_github_head_ref()`; fall back to `git_repo_head_ref`. > - **VCS Utils**: > - Add `get_github_head_ref()` to read `GITHUB_HEAD_REF` when `GITHUB_EVENT_NAME` is `pull_request` with debug logging. > - Export and import updated in `utils::vcs` and upload command. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 332fe76. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6080089 commit 268f375

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/commands/build/upload.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::utils::fs::TempDir;
2424
use crate::utils::fs::TempFile;
2525
use crate::utils::progress::ProgressBar;
2626
use crate::utils::vcs::{
27-
self, get_github_base_ref, get_github_pr_number, get_provider_from_remote,
27+
self, get_github_base_ref, get_github_head_ref, get_github_pr_number, get_provider_from_remote,
2828
get_repo_from_remote_preserve_case, git_repo_base_ref, git_repo_base_repo_name_preserve_case,
2929
git_repo_head_ref, git_repo_remote_url,
3030
};
@@ -150,7 +150,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
150150
.map(String::as_str)
151151
.map(Cow::Borrowed)
152152
.or_else(|| {
153-
// Try to get the current ref from the VCS if not provided
153+
// First try GitHub Actions environment variables
154+
get_github_head_ref().map(Cow::Owned)
155+
})
156+
.or_else(|| {
157+
// Fallback to git repository introspection
154158
// Note: git_repo_head_ref will return an error for detached HEAD states,
155159
// which the error handling converts to None - this prevents sending "HEAD" as a branch name
156160
// In that case, the user will need to provide a valid branch name.

src/utils/vcs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,21 @@ pub fn get_github_base_ref() -> Option<String> {
375375
Some(base_ref)
376376
}
377377

378+
/// Attempts to get the head branch from GitHub Actions environment variables.
379+
/// Returns the head branch name if running in a GitHub Actions pull request environment.
380+
pub fn get_github_head_ref() -> Option<String> {
381+
let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?;
382+
383+
if event_name != "pull_request" {
384+
debug!("Not running in pull_request event, got: {}", event_name);
385+
return None;
386+
}
387+
388+
let head_ref = std::env::var("GITHUB_HEAD_REF").ok()?;
389+
debug!("Auto-detected head ref from GitHub Actions: {}", head_ref);
390+
Some(head_ref)
391+
}
392+
378393
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {
379394
let mut non_git = false;
380395
for configured_repo in repos {

0 commit comments

Comments
 (0)