Skip to content

Commit b11b9de

Browse files
committed
Refactor Lambda
- Simplify Lambda logic - Use Rustic approach for borrowing. - Reduce `.clone` calls. - Update Dependabot. - Update models. - Lint config files. - Run IDEA/cargo fmt formatter.
1 parent 55e2d5e commit b11b9de

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

.github/workflows/aws.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: AWS Lambda function deploy workflow
33
on:
44
workflow_dispatch:
55
push:
6-
paths: ["src/**", "Cargo.toml"]
6+
paths: [ "src/**", "Cargo.toml" ]
77

88
jobs:
99
build-and-deploy:

dependabot.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "cargo"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"
7+
commit-message:
8+
prefix: "chore(deps)"
9+
groups:
10+
cargo-deps:
11+
patterns:
12+
- "*"
13+
14+
- package-ecosystem: "github-actions"
15+
directory: "/"
16+
schedule:
17+
interval: "weekly"
18+
commit-message:
19+
prefix: "chore(ci-deps)"
20+
groups:
21+
gh-actions-deps:
22+
patterns:
23+
- "*"
24+
25+
- package-ecosystem: "docker"
26+
directory: "/"
27+
schedule:
28+
interval: "weekly"
29+
commit-message:
30+
prefix: "chore(deps)"
31+
groups:
32+
docker-deps:
33+
patterns:
34+
- "*"

src/main.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
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;
2219
use aws_lambda_events::apigw::{ApiGatewayProxyRequest, ApiGatewayProxyResponse};
2320
use aws_lambda_events::encodings::Body;
21+
use aws_lambda_events::http::status::StatusCode;
22+
use aws_lambda_events::http::HeaderMap;
2423
use github_webhook_message_validator::validate as validate_gh;
2524
use hex::decode;
26-
use lambda_http::http::HeaderValue;
2725
use lambda_runtime::{run, service_fn, Error as LambdaError, LambdaEvent};
2826
use lazy_static::lazy_static;
2927
use log::{error, info};
30-
use std::env;
28+
29+
use crate::models::GithubHook;
30+
31+
mod models;
3132

3233
lazy_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

6566
fn 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> {
8284
async 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()),

src/models.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::Deserialize;
66
#[derive(Debug, Clone, Deserialize)]
77
pub(crate) struct GithubHook {
88
#[serde(rename = "ref")]
9-
pub(crate) _reference: String,
9+
pub(crate) reference: String,
1010
pub(crate) repository: Repository,
1111
pub(crate) pusher: Pusher,
1212
pub(crate) head_commit: Commit,
@@ -15,8 +15,7 @@ pub(crate) struct GithubHook {
1515

1616
#[derive(Clone, Debug, Deserialize)]
1717
pub(crate) struct Repository {
18-
#[serde(rename = "full_name")]
19-
pub(crate) name: String,
18+
pub(crate) full_name: String,
2019
}
2120

2221
#[derive(Debug, Clone, Deserialize)]

0 commit comments

Comments
 (0)