Skip to content

Commit 5c4aeff

Browse files
committed
fix(vcs): Handle non-github workflow in find_base_sha
`find_head_sha` supported both Github workflow contexts and 'raw git' contexts however the matching function `find_base_sha` did not handle raw git contexts. Thi updates `find_base_sha` to return the equivalent of: `git merge-base HEAD origin/HEAD`. For: ``` o---o---o---origin/main / ---1---o---o---o---foo ``` We return the SHA of 1.
1 parent 4fee058 commit 5c4aeff

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Deprecated the `upload-proguard` subcommand's `--platform` flag ([#2863](https://github.com/getsentry/sentry-cli/pull/2863)). This flag appears to have been a no-op for some time, so we will remove it in the next major.
88
- Deprecated the `upload-proguard` subcommand's `--android-manifest` flag ([#2891](https://github.com/getsentry/sentry-cli/pull/2891)). This flag appears to have been a no-op for some time, so we will remove it in the next major.
99

10+
### Fixes
11+
12+
- Fix auto filled git metadata when using the `build upload` subcommand in non-github workflow contexts ([#2897](https://github.com/getsentry/sentry-cli/pull/2897))
13+
1014
## 2.57.0
1115

1216
### New Features

src/commands/build/upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
233233
.map(String::as_str)
234234
.map(Cow::Borrowed)
235235
.or_else(|| {
236-
vcs::find_base_sha()
236+
vcs::find_base_sha(&cached_remote)
237237
.inspect_err(|e| debug!("Error finding base SHA: {e}"))
238238
.ok()
239239
.flatten()

src/utils/vcs.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::fmt;
44
use std::path::PathBuf;
55

6-
use anyhow::{bail, format_err, Context as _, Error, Result};
6+
use anyhow::{bail, format_err, Error, Result};
77
use chrono::{DateTime, FixedOffset, TimeZone as _};
88
use git2::{Commit, Repository, Time};
99
use if_chain::if_chain;
@@ -569,13 +569,30 @@ pub fn find_head_sha() -> Result<String> {
569569
Ok(head.id().to_string())
570570
}
571571

572-
pub fn find_base_sha() -> Result<Option<String>> {
573-
let github_event = std::env::var("GITHUB_EVENT_PATH")
574-
.map_err(Error::from)
575-
.and_then(|event_path| std::fs::read_to_string(event_path).map_err(Error::from))
576-
.context("Failed to read GitHub event path")?;
572+
pub fn find_base_sha(remote_name: &str) -> Result<Option<String>> {
573+
if let Some(pr_base_sha) = std::env::var("GITHUB_EVENT_PATH")
574+
.ok()
575+
.and_then(|event_path| std::fs::read_to_string(event_path).ok())
576+
.and_then(|content| extract_pr_base_sha_from_event(&content))
577+
{
578+
debug!("Using GitHub Actions PR base SHA from event payload: {pr_base_sha}");
579+
return Ok(Some(pr_base_sha));
580+
}
581+
582+
let repo = git2::Repository::open_from_env()?;
583+
584+
let head_commit = repo.head()?.peel_to_commit()?;
585+
586+
let remote_branch_name = format!("refs/remotes/{remote_name}/HEAD");
587+
let remote_ref = repo
588+
.find_reference(&remote_branch_name)
589+
.map_err(|e| anyhow::anyhow!("Could default branch for {remote_name}: {e}"))?;
577590

578-
Ok(extract_pr_base_sha_from_event(&github_event))
591+
let remote_commit = remote_ref.peel_to_commit()?;
592+
let merge_base_oid = repo.merge_base(head_commit.id(), remote_commit.id())?;
593+
let merge_base_sha = merge_base_oid.to_string();
594+
debug!("Found merge-base commit as base reference: {merge_base_sha}");
595+
Ok(Some(merge_base_sha))
579596
}
580597

581598
/// Extracts the PR head SHA from GitHub Actions event payload JSON.
@@ -1773,15 +1790,15 @@ mod tests {
17731790
fs::write(&event_file, pr_json).expect("Failed to write event file");
17741791
std::env::set_var("GITHUB_EVENT_PATH", event_file.to_str().unwrap());
17751792

1776-
let result = find_base_sha();
1793+
let result = find_base_sha("origin");
17771794
assert_eq!(
17781795
result.unwrap().unwrap(),
17791796
"55e6bc8c264ce95164314275d805f477650c440d"
17801797
);
17811798

17821799
// Test without GITHUB_EVENT_PATH
17831800
std::env::remove_var("GITHUB_EVENT_PATH");
1784-
let result = find_base_sha();
1801+
let result = find_base_sha("origin");
17851802
assert!(result.is_err());
17861803
}
17871804
}

0 commit comments

Comments
 (0)