@@ -11,6 +11,7 @@ use lazy_static::lazy_static;
1111use log:: { debug, info, warn} ;
1212use regex:: Regex ;
1313use serde_json:: Value ;
14+ use sha1_smol:: Digest ;
1415
1516use crate :: api:: { GitCommit , PatchSet , Ref , Repo } ;
1617
@@ -550,7 +551,7 @@ fn find_matching_revs(
550551 Ok ( ( prev_rev, rev) )
551552}
552553
553- pub fn find_head_sha ( ) -> Result < String > {
554+ pub fn find_head_sha ( ) -> Result < Digest > {
554555 if let Some ( pr_head_sha) = std:: env:: var ( "GITHUB_EVENT_PATH" )
555556 . ok ( )
556557 . and_then ( |event_path| std:: fs:: read_to_string ( event_path) . ok ( ) )
@@ -562,10 +563,14 @@ pub fn find_head_sha() -> Result<String> {
562563
563564 let repo = git2:: Repository :: open_from_env ( ) ?;
564565 let head = repo. revparse_single ( "HEAD" ) ?;
565- Ok ( head. id ( ) . to_string ( ) )
566+ Ok ( head
567+ . id ( )
568+ . to_string ( )
569+ . parse ( )
570+ . expect ( "Repo SHA should be a valid SHA1 digest" ) )
566571}
567572
568- pub fn find_base_sha ( remote_name : & str ) -> Result < Option < String > > {
573+ pub fn find_base_sha ( remote_name : & str ) -> Result < Option < Digest > > {
569574 if let Some ( pr_base_sha) = std:: env:: var ( "GITHUB_EVENT_PATH" )
570575 . ok ( )
571576 . and_then ( |event_path| std:: fs:: read_to_string ( event_path) . ok ( ) )
@@ -587,15 +592,19 @@ pub fn find_base_sha(remote_name: &str) -> Result<Option<String>> {
587592 Ok ( remote_ref
588593 . peel_to_commit ( )
589594 . and_then ( |remote_commit| repo. merge_base ( head_commit. id ( ) , remote_commit. id ( ) ) )
590- . map ( |oid| oid. to_string ( ) )
595+ . map ( |oid| {
596+ oid. to_string ( )
597+ . parse ( )
598+ . expect ( "Repo SHA should be a valid SHA1 digest" )
599+ } )
591600 . ok ( )
592601 . inspect ( |sha| debug ! ( "Found merge-base commit as base reference: {sha}" ) ) )
593602}
594603
595604/// Extracts the PR head SHA from GitHub Actions event payload JSON.
596605/// Returns None if not a PR event or if SHA cannot be extracted.
597606/// Panics if json is malformed.
598- fn extract_pr_head_sha_from_event ( json_content : & str ) -> Option < String > {
607+ fn extract_pr_head_sha_from_event ( json_content : & str ) -> Option < Digest > {
599608 let v: Value = match serde_json:: from_str ( json_content) {
600609 Ok ( v) => v,
601610 Err ( _) => {
@@ -605,13 +614,13 @@ fn extract_pr_head_sha_from_event(json_content: &str) -> Option<String> {
605614
606615 v. pointer ( "/pull_request/head/sha" )
607616 . and_then ( |s| s. as_str ( ) )
608- . map ( |s| s. to_owned ( ) )
617+ . map ( |s| s. parse ( ) . expect ( "GitHub Actions provided an invalid SHA" ) )
609618}
610619
611620/// Extracts the PR base SHA from GitHub Actions event payload JSON.
612621/// Returns None if not a PR event or if SHA cannot be extracted.
613622/// Panics if json is malformed.
614- fn extract_pr_base_sha_from_event ( json_content : & str ) -> Option < String > {
623+ fn extract_pr_base_sha_from_event ( json_content : & str ) -> Option < Digest > {
615624 let v: Value = match serde_json:: from_str ( json_content) {
616625 Ok ( v) => v,
617626 Err ( _) => {
@@ -621,7 +630,7 @@ fn extract_pr_base_sha_from_event(json_content: &str) -> Option<String> {
621630
622631 v. pointer ( "/pull_request/base/sha" )
623632 . and_then ( |s| s. as_str ( ) )
624- . map ( |s| s. to_owned ( ) )
633+ . map ( |s| s. parse ( ) . expect ( "GitHub Actions provided an invalid SHA" ) )
625634}
626635
627636/// Given commit specs, repos and remote_name this returns a list of head
@@ -810,10 +819,7 @@ mod tests {
810819 crate :: api:: RepoProvider ,
811820 insta:: { assert_debug_snapshot, assert_yaml_snapshot} ,
812821 serial_test:: serial,
813- std:: fs:: File ,
814- std:: io:: Write as _,
815- std:: path:: Path ,
816- std:: process:: Command ,
822+ std:: { fs:: File , io:: Write as _, path:: Path , process:: Command } ,
817823 tempfile:: { tempdir, TempDir } ,
818824 } ;
819825
@@ -1600,7 +1606,7 @@ mod tests {
16001606
16011607 assert_eq ! (
16021608 extract_pr_head_sha_from_event( & pr_json) ,
1603- Some ( "19ef6adc4dbddf733db6e833e1f96fb056b6dba5" . to_owned ( ) )
1609+ Some ( "19ef6adc4dbddf733db6e833e1f96fb056b6dba5" . parse ( ) . unwrap ( ) )
16041610 ) ;
16051611
16061612 let push_json = r#"{
@@ -1642,7 +1648,7 @@ mod tests {
16421648
16431649 assert_eq ! (
16441650 extract_pr_head_sha_from_event( real_gh_json) ,
1645- Some ( "19ef6adc4dbddf733db6e833e1f96fb056b6dba4" . to_owned ( ) )
1651+ Some ( "19ef6adc4dbddf733db6e833e1f96fb056b6dba4" . parse ( ) . unwrap ( ) )
16461652 ) ;
16471653 let malicious_json = r#"{
16481654 "action": "opened",
@@ -1658,20 +1664,23 @@ mod tests {
16581664
16591665 assert_eq ! (
16601666 extract_pr_head_sha_from_event( malicious_json) ,
1661- Some ( "19ef6adc4dbddf733db6e833e1f96fb056b6dba5" . to_owned ( ) )
1667+ Some ( "19ef6adc4dbddf733db6e833e1f96fb056b6dba5" . parse ( ) . unwrap ( ) )
16621668 ) ;
1663- let any_sha_json = r#"{
1664- "pull_request": {
1665- "head": {
1666- "sha": "invalid-sha-123"
16671669 }
1668- }
1669- }"# ;
16701670
1671- assert_eq ! (
1672- extract_pr_head_sha_from_event( any_sha_json) ,
1673- Some ( "invalid-sha-123" . to_owned( ) )
1674- ) ;
1671+ #[ test]
1672+ #[ should_panic]
1673+ fn test_extract_pr_head_sha_from_event_invalid_sha ( ) {
1674+ let any_sha_json = serde_json:: json!( {
1675+ "pull_request" : {
1676+ "head" : {
1677+ "sha" : "invalid-sha-123"
1678+ }
1679+ }
1680+ } )
1681+ . to_string ( ) ;
1682+
1683+ extract_pr_head_sha_from_event ( & any_sha_json) ;
16751684 }
16761685
16771686 #[ test]
@@ -1710,7 +1719,10 @@ mod tests {
17101719 std:: env:: remove_var ( "GITHUB_EVENT_PATH" ) ;
17111720
17121721 assert ! ( result. is_ok( ) ) ;
1713- assert_eq ! ( result. unwrap( ) , "19ef6adc4dbddf733db6e833e1f96fb056b6dba5" ) ;
1722+ assert_eq ! (
1723+ result. unwrap( ) ,
1724+ "19ef6adc4dbddf733db6e833e1f96fb056b6dba5" . parse( ) . unwrap( )
1725+ ) ;
17141726 }
17151727
17161728 #[ test]
@@ -1734,7 +1746,7 @@ mod tests {
17341746
17351747 assert_eq ! (
17361748 extract_pr_base_sha_from_event( & pr_json) ,
1737- Some ( "55e6bc8c264ce95164314275d805f477650c440d" . to_owned ( ) )
1749+ Some ( "55e6bc8c264ce95164314275d805f477650c440d" . parse ( ) . unwrap ( ) )
17381750 ) ;
17391751
17401752 // Test with push event (should return None)
@@ -1790,7 +1802,7 @@ mod tests {
17901802 let result = find_base_sha ( "origin" ) ;
17911803 assert_eq ! (
17921804 result. unwrap( ) . unwrap( ) ,
1793- "55e6bc8c264ce95164314275d805f477650c440d"
1805+ "55e6bc8c264ce95164314275d805f477650c440d" . parse ( ) . unwrap ( )
17941806 ) ;
17951807
17961808 // Test without GITHUB_EVENT_PATH
0 commit comments