Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/commands/build/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ pub fn execute(matches: &ArgMatches) -> Result<()> {
.get_one("base_sha")
.map(String::as_str)
.map(Cow::Borrowed)
.or_else(|| vcs::find_base_sha().ok().map(Cow::Owned));
.or_else(|| {
vcs::find_base_sha()
.inspect_err(|e| debug!("Error finding base SHA: {}", e))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL inspect_err. nice!

.ok()
.flatten()
.map(Cow::Owned)
});
let pr_number = matches
.get_one("pr_number")
.copied()
Expand Down
56 changes: 25 additions & 31 deletions src/utils/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fmt;
use std::path::PathBuf;

use anyhow::{bail, format_err, Error, Result};
use anyhow::{bail, format_err, Context as _, Error, Result};
use chrono::{DateTime, FixedOffset, TimeZone as _};
use git2::{Commit, Repository, Time};
use if_chain::if_chain;
Expand Down Expand Up @@ -569,20 +569,13 @@ pub fn find_head() -> Result<String> {
Ok(head.id().to_string())
}

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

Err(anyhow::anyhow!("No base SHA available"))
extract_pr_base_sha_from_event(&github_event)
}

/// Extracts the PR head SHA from GitHub Actions event payload JSON.
Expand All @@ -602,19 +595,15 @@ fn extract_pr_head_sha_from_event(json_content: &str) -> Option<String> {
}

/// Extracts the PR base SHA from GitHub Actions event payload JSON.
/// Returns None if not a PR event or if SHA cannot be extracted.
fn extract_pr_base_sha_from_event(json_content: &str) -> Option<String> {
let v: Value = match serde_json::from_str(json_content) {
Ok(v) => v,
Err(_) => {
debug!("Failed to parse GitHub event payload as JSON");
return None;
}
};
/// Returns Ok(None) if not a PR event or if SHA cannot be extracted.
/// Returns an error if we cannot parse the JSON.
fn extract_pr_base_sha_from_event(json_content: &str) -> Result<Option<String>> {
let v: Value = serde_json::from_str(json_content)
.context("Failed to parse GitHub event payload as JSON")?;

v.pointer("/pull_request/base/sha")
Ok(v.pointer("/pull_request/base/sha")
.and_then(|s| s.as_str())
.map(|s| s.to_owned())
.map(|s| s.to_owned()))
}

/// Given commit specs, repos and remote_name this returns a list of head
Expand Down Expand Up @@ -1712,7 +1701,7 @@ mod tests {
.to_string();

assert_eq!(
extract_pr_base_sha_from_event(&pr_json),
extract_pr_base_sha_from_event(&pr_json).unwrap(),
Some("55e6bc8c264ce95164314275d805f477650c440d".to_owned())
);

Expand All @@ -1726,10 +1715,10 @@ mod tests {
})
.to_string();

assert_eq!(extract_pr_base_sha_from_event(&push_json), None);
assert_eq!(extract_pr_base_sha_from_event(&push_json).unwrap(), None);

// Test with malformed JSON
assert_eq!(extract_pr_base_sha_from_event("invalid json {"), None);
assert!(extract_pr_base_sha_from_event("invalid json {").is_err());

// Test with missing base SHA
let incomplete_json = r#"{
Expand All @@ -1740,7 +1729,10 @@ mod tests {
}
}"#;

assert_eq!(extract_pr_base_sha_from_event(incomplete_json), None);
assert_eq!(
extract_pr_base_sha_from_event(incomplete_json).unwrap(),
None
);
}

#[test]
Expand Down Expand Up @@ -1769,8 +1761,10 @@ mod tests {
std::env::set_var("GITHUB_EVENT_PATH", event_file.to_str().unwrap());

let result = find_base_sha();
assert!(result.is_ok());
assert_eq!(result.unwrap(), "55e6bc8c264ce95164314275d805f477650c440d");
assert_eq!(
result.unwrap().unwrap(),
"55e6bc8c264ce95164314275d805f477650c440d"
);

// Test without GITHUB_EVENT_PATH
std::env::remove_var("GITHUB_EVENT_PATH");
Expand Down