Skip to content

Commit a1254ec

Browse files
committed
Revert "revert: Revert GHA base branch detection (#2776) (#2789)"
This reverts commit c0beb47.
1 parent 6bab90f commit a1254ec

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/commands/build/upload.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +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_preserve_case,
28-
git_repo_base_ref, git_repo_base_repo_name_preserve_case, git_repo_head_ref,
29-
git_repo_remote_url,
27+
self, get_github_base_ref, get_github_pr_number, get_provider_from_remote,
28+
get_repo_from_remote_preserve_case, git_repo_base_ref, git_repo_base_repo_name_preserve_case,
29+
git_repo_head_ref, git_repo_remote_url,
3030
};
3131

3232
pub fn make_command(command: Command) -> Command {
@@ -176,8 +176,11 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
176176
.map(String::as_str)
177177
.map(Cow::Borrowed)
178178
.or_else(|| {
179-
// Try to get the base ref from the VCS if not provided
180-
// 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
181184
repo_ref
182185
.and_then(|r| match git_repo_base_ref(r, &cached_remote) {
183186
Ok(base_ref_name) => {

src/utils/vcs.rs

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

362+
/// Attempts to get the base branch from GitHub Actions environment variables.
363+
/// Returns the base branch name if running in a GitHub Actions pull request environment.
364+
pub fn get_github_base_ref() -> Option<String> {
365+
let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?;
366+
367+
if event_name != "pull_request" {
368+
debug!("Not running in pull_request event, got: {}", event_name);
369+
return None;
370+
}
371+
372+
let base_ref = std::env::var("GITHUB_BASE_REF").ok()?;
373+
debug!("Auto-detected base ref from GitHub Actions: {}", base_ref);
374+
Some(base_ref)
375+
}
376+
362377
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {
363378
let mut non_git = false;
364379
for configured_repo in repos {
@@ -1466,4 +1481,30 @@ mod tests {
14661481
std::env::remove_var("GITHUB_EVENT_NAME");
14671482
std::env::remove_var("GITHUB_REF");
14681483
}
1484+
1485+
#[test]
1486+
fn test_get_github_base_ref() {
1487+
std::env::set_var("GITHUB_EVENT_NAME", "pull_request");
1488+
std::env::set_var("GITHUB_BASE_REF", "main");
1489+
let base_ref = get_github_base_ref();
1490+
assert_eq!(base_ref, Some("main".to_owned()));
1491+
1492+
// Test with different base branch
1493+
std::env::set_var("GITHUB_BASE_REF", "develop");
1494+
let base_ref = get_github_base_ref();
1495+
assert_eq!(base_ref, Some("develop".to_owned()));
1496+
1497+
// Test when not in pull_request event
1498+
std::env::set_var("GITHUB_EVENT_NAME", "push");
1499+
let base_ref = get_github_base_ref();
1500+
assert_eq!(base_ref, None);
1501+
1502+
// Test when GITHUB_BASE_REF is not set
1503+
std::env::set_var("GITHUB_EVENT_NAME", "pull_request");
1504+
std::env::remove_var("GITHUB_BASE_REF");
1505+
let base_ref = get_github_base_ref();
1506+
assert_eq!(base_ref, None);
1507+
1508+
std::env::remove_var("GITHUB_EVENT_NAME");
1509+
}
14691510
}

0 commit comments

Comments
 (0)