diff --git a/src/commands/build/upload.rs b/src/commands/build/upload.rs index 46e663d53d..11bb45f2df 100644 --- a/src/commands/build/upload.rs +++ b/src/commands/build/upload.rs @@ -24,9 +24,9 @@ use crate::utils::fs::TempDir; use crate::utils::fs::TempFile; use crate::utils::progress::ProgressBar; use crate::utils::vcs::{ - self, 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, + self, get_github_base_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, }; pub fn make_command(command: Command) -> Command { @@ -176,8 +176,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> { .map(String::as_str) .map(Cow::Borrowed) .or_else(|| { - // Try to get the base ref from the VCS if not provided - // This attempts to find the merge-base with the remote tracking branch + // First try GitHub Actions environment variables + get_github_base_ref().map(Cow::Owned) + }) + .or_else(|| { + // Fallback to git repository introspection repo_ref .and_then(|r| match git_repo_base_ref(r, &cached_remote) { Ok(base_ref_name) => { diff --git a/src/utils/vcs.rs b/src/utils/vcs.rs index bc76f41333..757a13d53f 100644 --- a/src/utils/vcs.rs +++ b/src/utils/vcs.rs @@ -359,6 +359,21 @@ pub fn get_github_pr_number() -> Option { Some(pr_number) } +/// Attempts to get the base branch from GitHub Actions environment variables. +/// Returns the base branch name if running in a GitHub Actions pull request environment. +pub fn get_github_base_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 base_ref = std::env::var("GITHUB_BASE_REF").ok()?; + debug!("Auto-detected base ref from GitHub Actions: {}", base_ref); + Some(base_ref) +} + fn find_reference_url(repo: &str, repos: &[Repo]) -> Result> { let mut non_git = false; for configured_repo in repos { @@ -1466,4 +1481,30 @@ mod tests { std::env::remove_var("GITHUB_EVENT_NAME"); std::env::remove_var("GITHUB_REF"); } + + #[test] + fn test_get_github_base_ref() { + std::env::set_var("GITHUB_EVENT_NAME", "pull_request"); + std::env::set_var("GITHUB_BASE_REF", "main"); + let base_ref = get_github_base_ref(); + assert_eq!(base_ref, Some("main".to_owned())); + + // Test with different base branch + std::env::set_var("GITHUB_BASE_REF", "develop"); + let base_ref = get_github_base_ref(); + assert_eq!(base_ref, Some("develop".to_owned())); + + // Test when not in pull_request event + std::env::set_var("GITHUB_EVENT_NAME", "push"); + let base_ref = get_github_base_ref(); + assert_eq!(base_ref, None); + + // Test when GITHUB_BASE_REF is not set + std::env::set_var("GITHUB_EVENT_NAME", "pull_request"); + std::env::remove_var("GITHUB_BASE_REF"); + let base_ref = get_github_base_ref(); + assert_eq!(base_ref, None); + + std::env::remove_var("GITHUB_EVENT_NAME"); + } }