1414 variant_size_differences
1515) ]
1616
17- mod models;
18- use crate :: models:: GithubHook ;
17+ use std:: env;
1918
20- use aws_lambda_events:: http:: HeaderMap ;
21- use aws_lambda_events:: http:: status:: StatusCode ;
2219use aws_lambda_events:: apigw:: { ApiGatewayProxyRequest , ApiGatewayProxyResponse } ;
2320use aws_lambda_events:: encodings:: Body ;
21+ use aws_lambda_events:: http:: status:: StatusCode ;
22+ use aws_lambda_events:: http:: HeaderMap ;
2423use github_webhook_message_validator:: validate as validate_gh;
2524use hex:: decode;
26- use lambda_http:: http:: HeaderValue ;
2725use lambda_runtime:: { run, service_fn, Error as LambdaError , LambdaEvent } ;
2826use lazy_static:: lazy_static;
2927use log:: { error, info} ;
30- use std:: env;
28+
29+ use crate :: models:: GithubHook ;
30+
31+ mod models;
3132
3233lazy_static ! {
3334 static ref WEBHOOK_SECRET : String = env:: var( "WEBHOOK_GH_SECRET" ) . unwrap( ) ;
@@ -64,14 +65,15 @@ fn validate(sig: &str, msg: &str) -> Option<ApiGatewayProxyResponse> {
6465
6566fn process_webhook ( payload : & str ) -> Option < GithubHook > {
6667 let decoded: GithubHook = serde_json:: from_str :: < GithubHook > ( payload) . unwrap ( ) ;
67- let decoded = decoded. clone ( ) ;
68-
69- if decoded. repository . name . contains ( "planet_" ) || decoded. repository . name . contains ( "abm_git_update_bot" ) || decoded
70- . head_commit
71- . message
72- . clone ( )
73- . unwrap ( )
74- . contains ( "Update submodule" )
68+
69+ if decoded. repository . full_name . contains ( "planet_" )
70+ || decoded. repository . full_name . contains ( "abm_git_update_bot" )
71+ || decoded
72+ . head_commit
73+ . message
74+ . as_ref ( )
75+ . unwrap ( )
76+ . contains ( "Update submodule" )
7577 {
7678 return None ;
7779 }
@@ -82,25 +84,22 @@ fn process_webhook(payload: &str) -> Option<GithubHook> {
8284async fn webhook_handler (
8385 evt : LambdaEvent < ApiGatewayProxyRequest > ,
8486) -> Result < ApiGatewayProxyResponse , LambdaError > {
85- let ctx = evt. context ;
86- let empty_header_value = HeaderValue :: from_str ( "" ) ?;
8787 let sig = evt
8888 . payload
8989 . headers
9090 . get ( "X-Hub-Signature" )
91- . unwrap_or ( & empty_header_value) ;
92- info ! ( "AWS Request ID: {}" , ctx. request_id) ;
93-
94- if let Some ( result) = validate (
95- sig. to_str ( ) . unwrap_or_default ( ) ,
96- evt. payload . body . clone ( ) . unwrap_or_default ( ) . as_str ( ) ,
97- ) {
98- return Ok ( result) ;
91+ . expect ( "No GitHub signature found in headers." )
92+ . to_str ( )
93+ . unwrap_or_default ( ) ;
94+ let body = evt. payload . body . unwrap_or_default ( ) ;
95+
96+ if let Some ( resp) = validate ( sig, & body) {
97+ return Ok ( resp) ;
9998 }
10099
101100 info ! ( "Webhook validated, signature confirmed OK." ) ;
102101
103- if process_webhook ( & evt . payload . body . clone ( ) . unwrap_or_default ( ) ) . is_none ( ) {
102+ if process_webhook ( & body) . is_none ( ) {
104103 info ! ( "Can't act on this event - it is suppressed." ) ;
105104 return Ok ( ApiGatewayProxyResponse {
106105 status_code : i64:: from ( StatusCode :: OK . as_u16 ( ) ) ,
0 commit comments