Skip to content

Commit 5e5933f

Browse files
ref(vcs): Improve find_base_sha debuggability (#2806)
1 parent 268f375 commit 5e5933f

File tree

2 files changed

+32
-32
lines changed

2 files changed

+32
-32
lines changed

src/commands/build/upload.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,13 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
235235
.get_one("base_sha")
236236
.map(String::as_str)
237237
.map(Cow::Borrowed)
238-
.or_else(|| vcs::find_base_sha().ok().map(Cow::Owned));
238+
.or_else(|| {
239+
vcs::find_base_sha()
240+
.inspect_err(|e| debug!("Error finding base SHA: {}", e))
241+
.ok()
242+
.flatten()
243+
.map(Cow::Owned)
244+
});
239245
let pr_number = matches
240246
.get_one("pr_number")
241247
.copied()

src/utils/vcs.rs

Lines changed: 25 additions & 31 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, Error, Result};
6+
use anyhow::{bail, format_err, Context as _, Error, Result};
77
use chrono::{DateTime, FixedOffset, TimeZone as _};
88
use git2::{Commit, Repository, Time};
99
use if_chain::if_chain;
@@ -584,20 +584,13 @@ pub fn find_head() -> Result<String> {
584584
Ok(head.id().to_string())
585585
}
586586

587-
pub fn find_base_sha() -> Result<String> {
588-
if let Some(pr_base_sha) = std::env::var("GITHUB_EVENT_PATH")
589-
.ok()
590-
.and_then(|event_path| std::fs::read_to_string(event_path).ok())
591-
.and_then(|content| extract_pr_base_sha_from_event(&content))
592-
{
593-
debug!(
594-
"Using GitHub Actions PR base SHA from event payload: {}",
595-
pr_base_sha
596-
);
597-
return Ok(pr_base_sha);
598-
}
587+
pub fn find_base_sha() -> Result<Option<String>> {
588+
let github_event = std::env::var("GITHUB_EVENT_PATH")
589+
.map_err(Error::from)
590+
.and_then(|event_path| std::fs::read_to_string(event_path).map_err(Error::from))
591+
.context("Failed to read GitHub event path")?;
599592

600-
Err(anyhow::anyhow!("No base SHA available"))
593+
extract_pr_base_sha_from_event(&github_event)
601594
}
602595

603596
/// Extracts the PR head SHA from GitHub Actions event payload JSON.
@@ -617,19 +610,15 @@ fn extract_pr_head_sha_from_event(json_content: &str) -> Option<String> {
617610
}
618611

619612
/// Extracts the PR base SHA from GitHub Actions event payload JSON.
620-
/// Returns None if not a PR event or if SHA cannot be extracted.
621-
fn extract_pr_base_sha_from_event(json_content: &str) -> Option<String> {
622-
let v: Value = match serde_json::from_str(json_content) {
623-
Ok(v) => v,
624-
Err(_) => {
625-
debug!("Failed to parse GitHub event payload as JSON");
626-
return None;
627-
}
628-
};
613+
/// Returns Ok(None) if not a PR event or if SHA cannot be extracted.
614+
/// Returns an error if we cannot parse the JSON.
615+
fn extract_pr_base_sha_from_event(json_content: &str) -> Result<Option<String>> {
616+
let v: Value = serde_json::from_str(json_content)
617+
.context("Failed to parse GitHub event payload as JSON")?;
629618

630-
v.pointer("/pull_request/base/sha")
619+
Ok(v.pointer("/pull_request/base/sha")
631620
.and_then(|s| s.as_str())
632-
.map(|s| s.to_owned())
621+
.map(|s| s.to_owned()))
633622
}
634623

635624
/// Given commit specs, repos and remote_name this returns a list of head
@@ -1727,7 +1716,7 @@ mod tests {
17271716
.to_string();
17281717

17291718
assert_eq!(
1730-
extract_pr_base_sha_from_event(&pr_json),
1719+
extract_pr_base_sha_from_event(&pr_json).unwrap(),
17311720
Some("55e6bc8c264ce95164314275d805f477650c440d".to_owned())
17321721
);
17331722

@@ -1741,10 +1730,10 @@ mod tests {
17411730
})
17421731
.to_string();
17431732

1744-
assert_eq!(extract_pr_base_sha_from_event(&push_json), None);
1733+
assert_eq!(extract_pr_base_sha_from_event(&push_json).unwrap(), None);
17451734

17461735
// Test with malformed JSON
1747-
assert_eq!(extract_pr_base_sha_from_event("invalid json {"), None);
1736+
assert!(extract_pr_base_sha_from_event("invalid json {").is_err());
17481737

17491738
// Test with missing base SHA
17501739
let incomplete_json = r#"{
@@ -1755,7 +1744,10 @@ mod tests {
17551744
}
17561745
}"#;
17571746

1758-
assert_eq!(extract_pr_base_sha_from_event(incomplete_json), None);
1747+
assert_eq!(
1748+
extract_pr_base_sha_from_event(incomplete_json).unwrap(),
1749+
None
1750+
);
17591751
}
17601752

17611753
#[test]
@@ -1784,8 +1776,10 @@ mod tests {
17841776
std::env::set_var("GITHUB_EVENT_PATH", event_file.to_str().unwrap());
17851777

17861778
let result = find_base_sha();
1787-
assert!(result.is_ok());
1788-
assert_eq!(result.unwrap(), "55e6bc8c264ce95164314275d805f477650c440d");
1779+
assert_eq!(
1780+
result.unwrap().unwrap(),
1781+
"55e6bc8c264ce95164314275d805f477650c440d"
1782+
);
17891783

17901784
// Test without GITHUB_EVENT_PATH
17911785
std::env::remove_var("GITHUB_EVENT_PATH");

0 commit comments

Comments
 (0)