diff --git a/src/commands/build/upload.rs b/src/commands/build/upload.rs index 260767b4b0..834cc6accf 100644 --- a/src/commands/build/upload.rs +++ b/src/commands/build/upload.rs @@ -24,8 +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, git_repo_base_ref, - git_repo_base_repo_name, 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, git_repo_base_ref, git_repo_base_repo_name, git_repo_head_ref, + git_repo_remote_url, }; pub fn make_command(command: Command) -> Command { @@ -175,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 2ec789f0e4..c7a4c8fa68 100644 --- a/src/utils/vcs.rs +++ b/src/utils/vcs.rs @@ -338,6 +338,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 { @@ -1351,4 +1366,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"); + } } diff --git a/tests/integration/_cases/build/build-upload-apk-all-uploaded.trycmd b/tests/integration/_cases/build/build-upload-apk-all-uploaded.trycmd index 3a5a317409..095710d2e5 100644 --- a/tests/integration/_cases/build/build-upload-apk-all-uploaded.trycmd +++ b/tests/integration/_cases/build/build-upload-apk-all-uploaded.trycmd @@ -2,7 +2,6 @@ $ sentry-cli build upload tests/integration/_fixtures/build/apk.apk ? success [..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release. - WARN [..] Could not detect base branch reference: [..] Successfully uploaded 1 file to Sentry - tests/integration/_fixtures/build/apk.apk (http[..]/wat-org/preprod/wat-project/42) diff --git a/tests/integration/_cases/build/build-upload-apk-no-token.trycmd b/tests/integration/_cases/build/build-upload-apk-no-token.trycmd index 448173f456..a29ebd4323 100644 --- a/tests/integration/_cases/build/build-upload-apk-no-token.trycmd +++ b/tests/integration/_cases/build/build-upload-apk-no-token.trycmd @@ -2,7 +2,6 @@ $ sentry-cli build upload tests/integration/_fixtures/build/apk.apk ? failed [..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release. - WARN [..] Could not detect base branch reference: [..] error: Auth token is required for this request. Please run `sentry-cli login` and try again! Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output. diff --git a/tests/integration/_cases/build/build-upload-apk.trycmd b/tests/integration/_cases/build/build-upload-apk.trycmd index 9b869af97a..97868f4ae6 100644 --- a/tests/integration/_cases/build/build-upload-apk.trycmd +++ b/tests/integration/_cases/build/build-upload-apk.trycmd @@ -2,7 +2,6 @@ $ sentry-cli build upload tests/integration/_fixtures/build/apk.apk --head-sha test_head_sha ? success [..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release. - WARN [..] Could not detect base branch reference: [..] Successfully uploaded 1 file to Sentry - tests/integration/_fixtures/build/apk.apk (http[..]/wat-org/preprod/wat-project/42) diff --git a/tests/integration/_cases/build/build-upload-ipa.trycmd b/tests/integration/_cases/build/build-upload-ipa.trycmd index c259620e4f..8d0bde6566 100644 --- a/tests/integration/_cases/build/build-upload-ipa.trycmd +++ b/tests/integration/_cases/build/build-upload-ipa.trycmd @@ -2,7 +2,6 @@ $ sentry-cli build upload tests/integration/_fixtures/build/ipa.ipa --head-sha test_head_sha ? success [..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release. - WARN [..] Could not detect base branch reference: [..] Successfully uploaded 1 file to Sentry - tests/integration/_fixtures/build/ipa.ipa (http[..]/wat-org/preprod/wat-project/some-text-id)