Skip to content

Commit 254f4e6

Browse files
committed
ref(vcs): Make extract_pr_base_sha_from_event like extract_pr_head_sha_from_event
1 parent 0a539c6 commit 254f4e6

File tree

1 file changed

+17
-31
lines changed

1 file changed

+17
-31
lines changed

src/utils/vcs.rs

Lines changed: 17 additions & 31 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 {json_content}");
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 {json_content}");
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,16 +1598,6 @@ mod tests {
15941598
}"#;
15951599

15961600
assert_eq!(extract_pr_head_sha_from_event(push_json), None);
1597-
let malformed_json = r#"{
1598-
"pull_request": {
1599-
"id": 789,
1600-
"head": {
1601-
"ref": "feature-branch"
1602-
}
1603-
}
1604-
}"#;
1605-
1606-
assert_eq!(extract_pr_head_sha_from_event(malformed_json), None);
16071601

16081602
assert_eq!(extract_pr_head_sha_from_event("{}"), None);
16091603
let real_gh_json = r#"{
@@ -1655,8 +1649,6 @@ mod tests {
16551649
extract_pr_head_sha_from_event(any_sha_json),
16561650
Some("invalid-sha-123".to_owned())
16571651
);
1658-
1659-
assert_eq!(extract_pr_head_sha_from_event("invalid json {"), None);
16601652
}
16611653

16621654
#[test]
@@ -1705,7 +1697,7 @@ mod tests {
17051697
.to_string();
17061698

17071699
assert_eq!(
1708-
extract_pr_base_sha_from_event(&pr_json).unwrap(),
1700+
extract_pr_base_sha_from_event(&pr_json),
17091701
Some("55e6bc8c264ce95164314275d805f477650c440d".to_owned())
17101702
);
17111703

@@ -1719,10 +1711,7 @@ mod tests {
17191711
})
17201712
.to_string();
17211713

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());
1714+
assert_eq!(extract_pr_base_sha_from_event(&push_json), None);
17261715

17271716
// Test with missing base SHA
17281717
let incomplete_json = r#"{
@@ -1733,10 +1722,7 @@ mod tests {
17331722
}
17341723
}"#;
17351724

1736-
assert_eq!(
1737-
extract_pr_base_sha_from_event(incomplete_json).unwrap(),
1738-
None
1739-
);
1725+
assert_eq!(extract_pr_base_sha_from_event(incomplete_json), None);
17401726
}
17411727

17421728
#[test]

0 commit comments

Comments
 (0)