Skip to content

Commit

Permalink
Merge pull request #10 from kudos-ink/change-lambda-execution-context
Browse files Browse the repository at this point in the history
use lambda runtime instead of http
  • Loading branch information
CJ13th authored Sep 23, 2024
2 parents f642b97 + fa21f81 commit 2dfb939
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/notification_triggered/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2021"
# and it will keep the alphabetic ordering for you.

[dependencies]
lambda_http = "0.13.0"
lambda_runtime = "0.13.0"
chrono = "0.4.38"
octocrab = "0.39.0"
serde = "1.0.205"
Expand Down
21 changes: 8 additions & 13 deletions src/notification_triggered/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use lambda_http::{run, service_fn, tracing, Body, Error, Request, Response};
use lambda_runtime::{run, service_fn, tracing, Error, LambdaEvent};
use octocrab::Octocrab;
use shared::functions::{extract_issue, get_username_map};
// use shared::functions::extract_issue;
// use shared::functions::get_username_map;
use shared::types::KudosIssue;
use shared::functions::get_username_map;
use shared::types::{AsyncLambdaPayload, KudosIssue, Res};
use sqlx::PgPool;
use sqlx::Row;
use std::env;
Expand All @@ -14,8 +12,8 @@ Fetches the latest version of that record from GitHub
Inserts it into the database
*/

async fn function_handler(event: Request) -> Result<Response<Body>, Error> {
let issue_details = extract_issue(event)?;
async fn function_handler(event: LambdaEvent<AsyncLambdaPayload>) -> Result<Res, Error> {
let issue_details = event.payload.response_payload;

let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
let mut tx: sqlx::Transaction<'_, sqlx::Postgres> = pool.begin().await?;
Expand Down Expand Up @@ -74,12 +72,9 @@ async fn function_handler(event: Request) -> Result<Response<Body>, Error> {

tx.commit().await?;

let resp = Response::builder()
.status(200)
.header("content-type", "text/html")
.body(Body::Text(format!("Insert count {}", insert_count)))
.map_err(Box::new)?;
Ok(resp)
Ok(Res {
message: format!("Insert count {}", insert_count),
})
}

#[tokio::main]
Expand Down
18 changes: 1 addition & 17 deletions src/shared/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{ImportType, KudosIssue, KudosIssuePayload, Project, RepoInfo, Repository};
use crate::types::{ImportType, KudosIssue, Project, RepoInfo, Repository};

use lambda_http::{
tracing::{error, info},
Expand Down Expand Up @@ -27,22 +27,6 @@ pub fn extract_project(event: Request) -> Result<Project, Error> {
Ok(project)
}

pub fn extract_issue(event: Request) -> Result<KudosIssuePayload, Error> {
let request_body = event.body();
let json_string = (match request_body {
Body::Text(json) => Some(json),
_ => None,
})
.ok_or_else(|| Error::from("Invalid request body type"))?;

let issue_details: KudosIssuePayload = serde_json::from_str(&json_string).map_err(|e| {
error!("Error parsing JSON: {}", e);
Error::from("Error parsing JSON")
})?;

Ok(issue_details)
}

pub async fn insert_project(
project: &Project,
tx: &mut Transaction<'_, Postgres>,
Expand Down
10 changes: 10 additions & 0 deletions src/shared/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ pub struct ProjectLinks {
pub repository: Vec<Repository>,
}

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AsyncLambdaPayload {
pub response_payload: KudosIssuePayload,
}

#[derive(Deserialize, Debug)]
pub struct KudosIssuePayload {
pub owner: String,
Expand All @@ -124,3 +130,7 @@ pub enum ImportType {
Import,
Sync,
}
#[derive(Serialize, Debug)]
pub struct Res {
pub message: String,
}

0 comments on commit 2dfb939

Please sign in to comment.