Skip to content

Commit c7c6890

Browse files
runningcodeclaude
andauthored
feat(build): Set head-ref in non-PR GitHub workflows (EME-654) (#2976)
## Summary - Extends `get_github_head_ref()` to support non-PR GitHub Actions workflows - For PR workflows: continues to use `GITHUB_HEAD_REF` (unchanged) - For non-PR workflows (push, release, etc.): now uses `GITHUB_REF_NAME` - Adds comprehensive tests for all workflow types ## Changes Modified `src/utils/vcs.rs`: - Updated `get_github_head_ref()` to handle non-PR events by reading `GITHUB_REF_NAME` - Added test coverage for push workflows, feature branches, tags, and edge cases ## Impact Build uploads in non-PR workflows (like Android release builds) will now automatically have the correct branch or tag reference set, without requiring manual `--head-ref` flags. Fixes EME-654 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <[email protected]>
1 parent dc3078a commit c7c6890

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Improvements
6+
7+
- The `sentry-cli build upload` command now automatically detects the correct branch or tag reference in non-PR GitHub Actions workflows ([#2976](https://github.com/getsentry/sentry-cli/pull/2976)). Previously, `--head-ref` was only auto-detected for pull request workflows. Now it works for push, release, and other workflow types by using the `GITHUB_REF_NAME` environment variable.
8+
39
## 2.58.2
410

511
### Improvements

src/utils/vcs.rs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,18 +363,23 @@ pub fn get_github_base_ref() -> Option<String> {
363363
}
364364

365365
/// Attempts to get the head branch from GitHub Actions environment variables.
366-
/// Returns the head branch name if running in a GitHub Actions pull request environment.
366+
/// Returns the head branch name if running in a GitHub Actions environment.
367+
/// For pull requests, returns the source branch. For other workflows, returns the branch or tag being built.
367368
pub fn get_github_head_ref() -> Option<String> {
368369
let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?;
369370

370-
if event_name != "pull_request" {
371-
debug!("Not running in pull_request event, got: {event_name}");
372-
return None;
371+
if event_name == "pull_request" {
372+
// For PRs, use GITHUB_HEAD_REF which contains the source branch
373+
let head_ref = std::env::var("GITHUB_HEAD_REF").ok()?;
374+
debug!("Auto-detected head ref from GitHub Actions PR: {head_ref}");
375+
Some(head_ref)
376+
} else {
377+
// For non-PR workflows (push, release, etc.), use GITHUB_REF_NAME
378+
// which contains the short name of the branch or tag (e.g., "main", "v1.0.0")
379+
let ref_name = std::env::var("GITHUB_REF_NAME").ok()?;
380+
debug!("Auto-detected head ref from GitHub Actions: {ref_name}");
381+
Some(ref_name)
373382
}
374-
375-
let head_ref = std::env::var("GITHUB_HEAD_REF").ok()?;
376-
debug!("Auto-detected head ref from GitHub Actions: {head_ref}");
377-
Some(head_ref)
378383
}
379384

380385
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> {
@@ -1585,6 +1590,51 @@ mod tests {
15851590
std::env::remove_var("GITHUB_EVENT_NAME");
15861591
}
15871592

1593+
#[test]
1594+
#[serial(github_env)]
1595+
fn test_get_github_head_ref() {
1596+
// Test PR workflow - should use GITHUB_HEAD_REF
1597+
std::env::set_var("GITHUB_EVENT_NAME", "pull_request");
1598+
std::env::set_var("GITHUB_HEAD_REF", "feature-branch");
1599+
std::env::set_var("GITHUB_REF_NAME", "123/merge"); // PR workflows also set this, but we prefer HEAD_REF
1600+
let head_ref = get_github_head_ref();
1601+
assert_eq!(head_ref, Some("feature-branch".to_owned()));
1602+
1603+
// Test push workflow - should use GITHUB_REF_NAME
1604+
std::env::set_var("GITHUB_EVENT_NAME", "push");
1605+
std::env::remove_var("GITHUB_HEAD_REF"); // Not set for non-PR workflows
1606+
std::env::set_var("GITHUB_REF_NAME", "main");
1607+
let head_ref = get_github_head_ref();
1608+
assert_eq!(head_ref, Some("main".to_owned()));
1609+
1610+
// Test push to feature branch
1611+
std::env::set_var("GITHUB_EVENT_NAME", "push");
1612+
std::env::set_var("GITHUB_REF_NAME", "feature/my-feature");
1613+
let head_ref = get_github_head_ref();
1614+
assert_eq!(head_ref, Some("feature/my-feature".to_owned()));
1615+
1616+
// Test release workflow with tag
1617+
std::env::set_var("GITHUB_EVENT_NAME", "release");
1618+
std::env::set_var("GITHUB_REF_NAME", "v1.0.0");
1619+
let head_ref = get_github_head_ref();
1620+
assert_eq!(head_ref, Some("v1.0.0".to_owned()));
1621+
1622+
// Test when GITHUB_REF_NAME is not set for non-PR workflow
1623+
std::env::set_var("GITHUB_EVENT_NAME", "push");
1624+
std::env::remove_var("GITHUB_REF_NAME");
1625+
let head_ref = get_github_head_ref();
1626+
assert_eq!(head_ref, None);
1627+
1628+
// Test when GITHUB_EVENT_NAME is not set (not in GitHub Actions)
1629+
std::env::remove_var("GITHUB_EVENT_NAME");
1630+
let head_ref = get_github_head_ref();
1631+
assert_eq!(head_ref, None);
1632+
1633+
// Clean up
1634+
std::env::remove_var("GITHUB_HEAD_REF");
1635+
std::env::remove_var("GITHUB_REF_NAME");
1636+
}
1637+
15881638
#[test]
15891639
fn test_extract_pr_head_sha_from_event() {
15901640
let pr_json = serde_json::json!({

0 commit comments

Comments
 (0)