Skip to content

Commit a37e148

Browse files
runningcodeclaude
andcommitted
feat: Add GitHub Actions base branch detection for EME-320
Implement GitHub Actions-aware base branch detection that works in detached HEAD states commonly found in CI/CD environments. Changes: - Add get_github_base_ref() function to read GITHUB_BASE_REF env var - Prioritize GitHub Actions env vars over git introspection for reliability - Update test snapshots to remove base branch detection warning - Maintain backward compatibility with existing git-based detection 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a81510b commit a37e148

6 files changed

Lines changed: 49 additions & 8 deletions

File tree

src/commands/build/upload.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ 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_pr_number, get_provider_from_remote, get_repo_from_remote, git_repo_base_ref,
28-
git_repo_base_repo_name, git_repo_head_ref, git_repo_remote_url,
27+
self, get_github_base_ref, get_github_pr_number, get_provider_from_remote,
28+
get_repo_from_remote, git_repo_base_ref, git_repo_base_repo_name, git_repo_head_ref,
29+
git_repo_remote_url,
2930
};
3031

3132
pub fn make_command(command: Command) -> Command {
@@ -175,8 +176,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
175176
.map(String::as_str)
176177
.map(Cow::Borrowed)
177178
.or_else(|| {
178-
// Try to get the base ref from the VCS if not provided
179-
// This attempts to find the merge-base with the remote tracking branch
179+
// First try GitHub Actions environment variables
180+
get_github_base_ref().map(Cow::Owned)
181+
})
182+
.or_else(|| {
183+
// Fallback to git repository introspection
180184
repo_ref
181185
.and_then(|r| match git_repo_base_ref(r, &cached_remote) {
182186
Ok(base_ref_name) => {

src/utils/vcs.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,21 @@ pub fn get_github_pr_number() -> Option<u32> {
338338
Some(pr_number)
339339
}
340340

341+
/// Attempts to get the base branch from GitHub Actions environment variables.
342+
/// Returns the base branch name if running in a GitHub Actions pull request environment.
343+
pub fn get_github_base_ref() -> Option<String> {
344+
let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?;
345+
346+
if event_name != "pull_request" {
347+
debug!("Not running in pull_request event, got: {}", event_name);
348+
return None;
349+
}
350+
351+
let base_ref = std::env::var("GITHUB_BASE_REF").ok()?;
352+
debug!("Auto-detected base ref from GitHub Actions: {}", base_ref);
353+
Some(base_ref)
354+
}
355+
341356
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {
342357
let mut non_git = false;
343358
for configured_repo in repos {
@@ -1351,4 +1366,30 @@ mod tests {
13511366
std::env::remove_var("GITHUB_EVENT_NAME");
13521367
std::env::remove_var("GITHUB_REF");
13531368
}
1369+
1370+
#[test]
1371+
fn test_get_github_base_ref() {
1372+
std::env::set_var("GITHUB_EVENT_NAME", "pull_request");
1373+
std::env::set_var("GITHUB_BASE_REF", "main");
1374+
let base_ref = get_github_base_ref();
1375+
assert_eq!(base_ref, Some("main".to_string()));
1376+
1377+
// Test with different base branch
1378+
std::env::set_var("GITHUB_BASE_REF", "develop");
1379+
let base_ref = get_github_base_ref();
1380+
assert_eq!(base_ref, Some("develop".to_string()));
1381+
1382+
// Test when not in pull_request event
1383+
std::env::set_var("GITHUB_EVENT_NAME", "push");
1384+
let base_ref = get_github_base_ref();
1385+
assert_eq!(base_ref, None);
1386+
1387+
// Test when GITHUB_BASE_REF is not set
1388+
std::env::set_var("GITHUB_EVENT_NAME", "pull_request");
1389+
std::env::remove_var("GITHUB_BASE_REF");
1390+
let base_ref = get_github_base_ref();
1391+
assert_eq!(base_ref, None);
1392+
1393+
std::env::remove_var("GITHUB_EVENT_NAME");
1394+
}
13541395
}

tests/integration/_cases/build/build-upload-apk-all-uploaded.trycmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
$ sentry-cli build upload tests/integration/_fixtures/build/apk.apk
33
? success
44
[..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release.
5-
WARN [..] Could not detect base branch reference: [..]
65
Successfully uploaded 1 file to Sentry
76
- tests/integration/_fixtures/build/apk.apk (http[..]/wat-org/preprod/wat-project/42)
87

tests/integration/_cases/build/build-upload-apk-no-token.trycmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
$ sentry-cli build upload tests/integration/_fixtures/build/apk.apk
33
? failed
44
[..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release.
5-
WARN [..] Could not detect base branch reference: [..]
65
error: Auth token is required for this request. Please run `sentry-cli login` and try again!
76

87
Add --log-level=[info|debug] or export SENTRY_LOG_LEVEL=[info|debug] to see more output.

tests/integration/_cases/build/build-upload-apk.trycmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
$ sentry-cli build upload tests/integration/_fixtures/build/apk.apk --head-sha test_head_sha
33
? success
44
[..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release.
5-
WARN [..] Could not detect base branch reference: [..]
65
Successfully uploaded 1 file to Sentry
76
- tests/integration/_fixtures/build/apk.apk (http[..]/wat-org/preprod/wat-project/42)
87

tests/integration/_cases/build/build-upload-ipa.trycmd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
$ sentry-cli build upload tests/integration/_fixtures/build/ipa.ipa --head-sha test_head_sha
33
? success
44
[..]WARN[..]EXPERIMENTAL: The build subcommand is experimental. The command is subject to breaking changes and may be removed without notice in any release.
5-
WARN [..] Could not detect base branch reference: [..]
65
Successfully uploaded 1 file to Sentry
76
- tests/integration/_fixtures/build/ipa.ipa (http[..]/wat-org/preprod/wat-project/some-text-id)
87

0 commit comments

Comments
 (0)