From 332fe760a1b8ffd0dc6e416f5213190e75a705c7 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Mon, 29 Sep 2025 09:20:12 +0200 Subject: [PATCH] feat: auto-fetch head-ref from GitHub Actions in detached HEAD state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `get_github_head_ref()` function to automatically detect head branch from GitHub Actions environment variables when running in PR workflows. This resolves detached HEAD scenarios by using GITHUB_HEAD_REF instead of requiring manual --head-ref specification. Resolves EME-367 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/commands/build/upload.rs | 8 ++++++-- src/utils/vcs.rs | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/commands/build/upload.rs b/src/commands/build/upload.rs index ab2de1d462..630e904e2a 100644 --- a/src/commands/build/upload.rs +++ b/src/commands/build/upload.rs @@ -24,7 +24,7 @@ use crate::utils::fs::TempDir; use crate::utils::fs::TempFile; use crate::utils::progress::ProgressBar; use crate::utils::vcs::{ - self, get_github_base_ref, get_github_pr_number, get_provider_from_remote, + self, get_github_base_ref, get_github_head_ref, get_github_pr_number, get_provider_from_remote, get_repo_from_remote_preserve_case, git_repo_base_ref, git_repo_base_repo_name_preserve_case, git_repo_head_ref, git_repo_remote_url, }; @@ -150,7 +150,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> { .map(String::as_str) .map(Cow::Borrowed) .or_else(|| { - // Try to get the current ref from the VCS if not provided + // First try GitHub Actions environment variables + get_github_head_ref().map(Cow::Owned) + }) + .or_else(|| { + // Fallback to git repository introspection // Note: git_repo_head_ref will return an error for detached HEAD states, // which the error handling converts to None - this prevents sending "HEAD" as a branch name // In that case, the user will need to provide a valid branch name. diff --git a/src/utils/vcs.rs b/src/utils/vcs.rs index 93f190650f..1b436205ca 100644 --- a/src/utils/vcs.rs +++ b/src/utils/vcs.rs @@ -375,6 +375,21 @@ pub fn get_github_base_ref() -> Option { Some(base_ref) } +/// Attempts to get the head branch from GitHub Actions environment variables. +/// Returns the head branch name if running in a GitHub Actions pull request environment. +pub fn get_github_head_ref() -> Option { + let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?; + + if event_name != "pull_request" { + debug!("Not running in pull_request event, got: {}", event_name); + return None; + } + + let head_ref = std::env::var("GITHUB_HEAD_REF").ok()?; + debug!("Auto-detected head ref from GitHub Actions: {}", head_ref); + Some(head_ref) +} + fn find_reference_url(repo: &str, repos: &[Repo]) -> Result> { let mut non_git = false; for configured_repo in repos {