Skip to content

Commit 8c6e63e

Browse files
committed
ref(vcs): Make extract_pr_base_sha_from_event like extract_pr_head_sha_from_event
1 parent 09e8b33 commit 8c6e63e

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/utils/vcs.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -575,17 +575,17 @@ pub fn find_base_sha() -> Result<Option<String>> {
575575
.and_then(|event_path| std::fs::read_to_string(event_path).map_err(Error::from))
576576
.context("Failed to read GitHub event path")?;
577577

578-
extract_pr_base_sha_from_event(&github_event)
578+
Ok(extract_pr_base_sha_from_event(&github_event))
579579
}
580580

581581
/// Extracts the PR head SHA from GitHub Actions event payload JSON.
582582
/// Returns None if not a PR event or if SHA cannot be extracted.
583+
/// Panics if json is malformed.
583584
fn extract_pr_head_sha_from_event(json_content: &str) -> Option<String> {
584585
let v: Value = match serde_json::from_str(json_content) {
585586
Ok(v) => v,
586587
Err(_) => {
587-
debug!("Failed to parse GitHub event payload as JSON");
588-
return None;
588+
panic!("Failed to parse GitHub event payload as JSON");
589589
}
590590
};
591591

@@ -595,15 +595,19 @@ fn extract_pr_head_sha_from_event(json_content: &str) -> Option<String> {
595595
}
596596

597597
/// Extracts the PR base SHA from GitHub Actions event payload JSON.
598-
/// Returns Ok(None) if not a PR event or if SHA cannot be extracted.
599-
/// Returns an error if we cannot parse the JSON.
600-
fn extract_pr_base_sha_from_event(json_content: &str) -> Result<Option<String>> {
601-
let v: Value = serde_json::from_str(json_content)
602-
.context("Failed to parse GitHub event payload as JSON")?;
598+
/// Returns None if not a PR event or if SHA cannot be extracted.
599+
/// Panics if json is malformed.
600+
fn extract_pr_base_sha_from_event(json_content: &str) -> Option<String> {
601+
let v: Value = match serde_json::from_str(json_content) {
602+
Ok(v) => v,
603+
Err(_) => {
604+
panic!("Failed to parse GitHub event payload as JSON");
605+
}
606+
};
603607

604-
Ok(v.pointer("/pull_request/base/sha")
608+
v.pointer("/pull_request/base/sha")
605609
.and_then(|s| s.as_str())
606-
.map(|s| s.to_owned()))
610+
.map(|s| s.to_owned())
607611
}
608612

609613
/// Given commit specs, repos and remote_name this returns a list of head
@@ -1594,6 +1598,7 @@ mod tests {
15941598
}"#;
15951599

15961600
assert_eq!(extract_pr_head_sha_from_event(push_json), None);
1601+
15971602
let malformed_json = r#"{
15981603
"pull_request": {
15991604
"id": 789,
@@ -1602,7 +1607,6 @@ mod tests {
16021607
}
16031608
}
16041609
}"#;
1605-
16061610
assert_eq!(extract_pr_head_sha_from_event(malformed_json), None);
16071611

16081612
assert_eq!(extract_pr_head_sha_from_event("{}"), None);
@@ -1655,8 +1659,19 @@ mod tests {
16551659
extract_pr_head_sha_from_event(any_sha_json),
16561660
Some("invalid-sha-123".to_owned())
16571661
);
1662+
}
16581663

1659-
assert_eq!(extract_pr_head_sha_from_event("invalid json {"), None);
1664+
#[test]
1665+
#[serial(github_env)]
1666+
#[should_panic]
1667+
fn test_extract_pr_base_sha_from_event_invalid() {
1668+
extract_pr_base_sha_from_event("invalid json {");
1669+
}
1670+
#[test]
1671+
#[serial(github_env)]
1672+
#[should_panic]
1673+
fn test_extract_pr_head_sha_from_event_invalid() {
1674+
extract_pr_head_sha_from_event("invalid json {");
16601675
}
16611676

16621677
#[test]
@@ -1705,7 +1720,7 @@ mod tests {
17051720
.to_string();
17061721

17071722
assert_eq!(
1708-
extract_pr_base_sha_from_event(&pr_json).unwrap(),
1723+
extract_pr_base_sha_from_event(&pr_json),
17091724
Some("55e6bc8c264ce95164314275d805f477650c440d".to_owned())
17101725
);
17111726

@@ -1719,10 +1734,7 @@ mod tests {
17191734
})
17201735
.to_string();
17211736

1722-
assert_eq!(extract_pr_base_sha_from_event(&push_json).unwrap(), None);
1723-
1724-
// Test with malformed JSON
1725-
assert!(extract_pr_base_sha_from_event("invalid json {").is_err());
1737+
assert_eq!(extract_pr_base_sha_from_event(&push_json), None);
17261738

17271739
// Test with missing base SHA
17281740
let incomplete_json = r#"{
@@ -1733,10 +1745,7 @@ mod tests {
17331745
}
17341746
}"#;
17351747

1736-
assert_eq!(
1737-
extract_pr_base_sha_from_event(incomplete_json).unwrap(),
1738-
None
1739-
);
1748+
assert_eq!(extract_pr_base_sha_from_event(incomplete_json), None);
17401749
}
17411750

17421751
#[test]

0 commit comments

Comments
 (0)