@@ -19,6 +19,9 @@ use crate::git::{get_diff, open_repo, parse_diff, parse_diff_from_buf};
19
19
20
20
use super :: RestApiClient ;
21
21
22
+ static USER_AGENT : & str =
23
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0" ;
24
+
22
25
/// A structure to work with Github REST API.
23
26
pub struct GithubApiClient {
24
27
/// The HTTP request client to be used for all REST API calls.
@@ -54,12 +57,11 @@ impl GithubApiClient {
54
57
GithubApiClient {
55
58
client : reqwest:: blocking:: Client :: new ( ) ,
56
59
event_payload : {
57
- let event_payload_path = env:: var ( "GITHUB_EVENT_PATH" ) ;
58
- if event_payload_path. is_ok ( ) {
60
+ if let Ok ( event_payload_path) = env:: var ( "GITHUB_EVENT_PATH" ) {
59
61
let file_buf = & mut String :: new ( ) ;
60
62
OpenOptions :: new ( )
61
63
. read ( true )
62
- . open ( event_payload_path. unwrap ( ) )
64
+ . open ( event_payload_path)
63
65
. unwrap ( )
64
66
. read_to_string ( file_buf)
65
67
. unwrap ( ) ;
@@ -103,7 +105,7 @@ impl RestApiClient for GithubApiClient {
103
105
. expect ( "GITHUB_OUTPUT file could not be opened" ) ;
104
106
if let Err ( e) = writeln ! (
105
107
gh_out_file,
106
- "checks-failed={}\n format-checks-failed={}\n tidy-checks-failed={}" ,
108
+ "checks-failed={}\n format-checks-failed={}\n tidy-checks-failed={}\n " ,
107
109
checks_failed,
108
110
format_checks_failed. unwrap_or( 0 ) ,
109
111
tidy_checks_failed. unwrap_or( 0 ) ,
@@ -124,7 +126,6 @@ impl RestApiClient for GithubApiClient {
124
126
}
125
127
126
128
fn make_headers ( & self , use_diff : Option < bool > ) -> HeaderMap < HeaderValue > {
127
- let gh_token = env:: var ( "GITHUB_TOKEN" ) ;
128
129
let mut headers = HeaderMap :: new ( ) ;
129
130
let return_fmt = "application/vnd.github." . to_owned ( )
130
131
+ if use_diff. is_some_and ( |val| val) {
@@ -133,10 +134,8 @@ impl RestApiClient for GithubApiClient {
133
134
"text+json"
134
135
} ;
135
136
headers. insert ( "Accept" , return_fmt. parse ( ) . unwrap ( ) ) ;
136
- let user_agent =
137
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0" ;
138
- headers. insert ( "User-Agent" , user_agent. parse ( ) . unwrap ( ) ) ;
139
- if let Ok ( token) = gh_token {
137
+ headers. insert ( "User-Agent" , USER_AGENT . parse ( ) . unwrap ( ) ) ;
138
+ if let Ok ( token) = env:: var ( "GITHUB_TOKEN" ) {
140
139
headers. insert ( "Authorization" , token. parse ( ) . unwrap ( ) ) ;
141
140
}
142
141
headers
@@ -471,3 +470,137 @@ struct User {
471
470
pub login : String ,
472
471
pub id : u64 ,
473
472
}
473
+
474
+ #[ cfg( test) ]
475
+ mod test {
476
+ use std:: { env, io:: Read , path:: PathBuf } ;
477
+
478
+ use tempfile:: { tempdir, NamedTempFile } ;
479
+
480
+ use super :: { GithubApiClient , USER_AGENT } ;
481
+ use crate :: {
482
+ clang_tools:: capture_clang_tools_output, common_fs:: FileObj , rest_api:: RestApiClient ,
483
+ } ;
484
+
485
+ // ************************** tests for GithubApiClient::make_headers()
486
+
487
+ #[ test]
488
+ fn get_headers_json_token ( ) {
489
+ let rest_api_client = GithubApiClient :: new ( ) ;
490
+ env:: set_var ( "GITHUB_TOKEN" , "123456" ) ;
491
+ let headers = rest_api_client. make_headers ( None ) ;
492
+ assert ! ( headers. contains_key( "User-Agent" ) ) ;
493
+ assert_eq ! ( headers. get( "User-Agent" ) . unwrap( ) , USER_AGENT ) ;
494
+ assert ! ( headers. contains_key( "Accept" ) ) ;
495
+ assert ! ( headers
496
+ . get( "Accept" )
497
+ . unwrap( )
498
+ . to_str( )
499
+ . unwrap( )
500
+ . ends_with( "text+json" ) ) ;
501
+ assert ! ( headers. contains_key( "Authorization" ) ) ;
502
+ assert_eq ! ( headers. get( "Authorization" ) . unwrap( ) , "123456" ) ;
503
+ }
504
+
505
+ #[ test]
506
+ fn get_headers_diff ( ) {
507
+ let rest_api_client = GithubApiClient :: new ( ) ;
508
+ let headers = rest_api_client. make_headers ( Some ( true ) ) ;
509
+ assert ! ( headers. contains_key( "User-Agent" ) ) ;
510
+ assert_eq ! ( headers. get( "User-Agent" ) . unwrap( ) , USER_AGENT ) ;
511
+ assert ! ( headers. contains_key( "Accept" ) ) ;
512
+ assert ! ( headers
513
+ . get( "Accept" )
514
+ . unwrap( )
515
+ . to_str( )
516
+ . unwrap( )
517
+ . ends_with( "diff" ) ) ;
518
+ }
519
+
520
+ // ************************** tests for GithubApiClient::set_exit_code()
521
+
522
+ #[ test]
523
+ fn set_exit_code ( ) {
524
+ let rest_api_client = GithubApiClient :: new ( ) ;
525
+ let checks_failed = 3 ;
526
+ let format_checks_failed = 2 ;
527
+ let tidy_checks_failed = 1 ;
528
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
529
+ let mut tmp_file = NamedTempFile :: new_in ( tmp_dir. path ( ) ) . unwrap ( ) ;
530
+ env:: set_var ( "GITHUB_OUTPUT" , tmp_file. path ( ) ) ;
531
+ assert_eq ! (
532
+ checks_failed,
533
+ rest_api_client. set_exit_code(
534
+ checks_failed,
535
+ Some ( format_checks_failed) ,
536
+ Some ( tidy_checks_failed)
537
+ )
538
+ ) ;
539
+ let mut output_file_content = String :: new ( ) ;
540
+ tmp_file. read_to_string ( & mut output_file_content) . unwrap ( ) ;
541
+ assert ! ( output_file_content. contains(
542
+ format!(
543
+ "checks-failed={}\n format-checks-failed={}\n tidy-checks-failed={}\n " ,
544
+ 3 , 2 , 1
545
+ )
546
+ . as_str( )
547
+ ) ) ;
548
+ println ! ( "temp file used: {:?}" , tmp_file. path( ) ) ;
549
+ drop ( tmp_file) ;
550
+ drop ( tmp_dir) ;
551
+ }
552
+
553
+ // ************************* tests for comment output
554
+
555
+ #[ test]
556
+ fn check_comment_concerns ( ) {
557
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
558
+ let mut tmp_file = NamedTempFile :: new_in ( tmp_dir. path ( ) ) . unwrap ( ) ;
559
+ let rest_api_client = GithubApiClient :: new ( ) ;
560
+ let files = vec ! [ FileObj :: new( PathBuf :: from( "tests/demo/demo.cpp" ) ) ] ;
561
+ let ( format_advice, tidy_advice) = capture_clang_tools_output (
562
+ & files,
563
+ env:: var ( "CLANG-VERSION" ) . unwrap_or ( "" . to_string ( ) ) . as_str ( ) ,
564
+ "readability-*" ,
565
+ "file" ,
566
+ 0 ,
567
+ None ,
568
+ None ,
569
+ ) ;
570
+ let ( comment, format_checks_failed, tidy_checks_failed) =
571
+ rest_api_client. make_comment ( & files, & format_advice, & tidy_advice) ;
572
+ assert ! ( format_checks_failed > 0 ) ;
573
+ assert ! ( tidy_checks_failed > 0 ) ;
574
+ env:: set_var ( "GITHUB_STEP_SUMMARY" , tmp_file. path ( ) ) ;
575
+ rest_api_client. post_step_summary ( & comment) ;
576
+ let mut output_file_content = String :: new ( ) ;
577
+ tmp_file. read_to_string ( & mut output_file_content) . unwrap ( ) ;
578
+ assert_eq ! ( format!( "\n {comment}\n \n " ) , output_file_content) ;
579
+ }
580
+
581
+ #[ test]
582
+ fn check_comment_lgtm ( ) {
583
+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
584
+ let mut tmp_file = NamedTempFile :: new_in ( tmp_dir. path ( ) ) . unwrap ( ) ;
585
+ let rest_api_client = GithubApiClient :: new ( ) ;
586
+ let files = vec ! [ FileObj :: new( PathBuf :: from( "tests/demo/demo.cpp" ) ) ] ;
587
+ let ( format_advice, tidy_advice) = capture_clang_tools_output (
588
+ & files,
589
+ env:: var ( "CLANG-VERSION" ) . unwrap_or ( "" . to_string ( ) ) . as_str ( ) ,
590
+ "-*" ,
591
+ "" ,
592
+ 0 ,
593
+ None ,
594
+ None ,
595
+ ) ;
596
+ let ( comment, format_checks_failed, tidy_checks_failed) =
597
+ rest_api_client. make_comment ( & files, & format_advice, & tidy_advice) ;
598
+ assert_eq ! ( format_checks_failed, 0 ) ;
599
+ assert_eq ! ( tidy_checks_failed, 0 ) ;
600
+ env:: set_var ( "GITHUB_STEP_SUMMARY" , tmp_file. path ( ) ) ;
601
+ rest_api_client. post_step_summary ( & comment) ;
602
+ let mut output_file_content = String :: new ( ) ;
603
+ tmp_file. read_to_string ( & mut output_file_content) . unwrap ( ) ;
604
+ assert_eq ! ( format!( "\n {comment}\n \n " ) , output_file_content) ;
605
+ }
606
+ }
0 commit comments