From 41b28c933b6b8454c7d581f15b153dd52b3c38be Mon Sep 17 00:00:00 2001 From: Fredrik August Madsen-Malmo Date: Mon, 8 Jul 2024 15:13:47 +0200 Subject: [PATCH] send date into delivery --- src/delivery/api.rs | 7 ++++++- src/delivery/slack.rs | 17 +++++++++++------ src/github/api.rs | 8 ++++++-- src/main.rs | 5 +++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/delivery/api.rs b/src/delivery/api.rs index 79ca2ca..ffddd44 100644 --- a/src/delivery/api.rs +++ b/src/delivery/api.rs @@ -1,7 +1,12 @@ use anyhow::Result; use async_trait::async_trait; +use chrono::{DateTime, Utc}; #[async_trait] pub trait DeliveryMechanism { - async fn deliver(&self, message: &str) -> Result<()>; + async fn deliver( + &self, + date_time: &DateTime, + message: &str, + ) -> Result<()>; } \ No newline at end of file diff --git a/src/delivery/slack.rs b/src/delivery/slack.rs index f2cd38b..dbc9655 100644 --- a/src/delivery/slack.rs +++ b/src/delivery/slack.rs @@ -2,13 +2,15 @@ use log::info; use crate::delivery::api::DeliveryMechanism; use anyhow::Result; use async_trait::async_trait; +use chrono::{DateTime, Utc}; use serde::Serialize; pub struct SlackDelivery {} #[async_trait] impl DeliveryMechanism for SlackDelivery { - async fn deliver(&self, message: &str) -> Result<()> { + async fn deliver(&self, date_time: &DateTime, message: &str) -> Result< + ()> { let slack_bot_token = std::env::var("SLACK_API_KEY").expect("Must provide slack API key"); let slack_channel = std::env::var("SLACK_CHANNEL").expect("Must provide slack channel"); @@ -24,17 +26,20 @@ impl DeliveryMechanism for SlackDelivery { r#type: "header".to_string(), text: Text { r#type: "plain_text".to_string(), - text: "Your daily digest 📜".to_string(), - } + text: format!( + "Digest for {}", + date_time.format("%d/%m/%Y") + ).to_string(), + }, }, Block { r#type: "section".to_string(), text: Text { r#type: "mrkdwn".to_string(), text: message.to_string(), - } - } - ] + }, + }, + ], }) .send() .await?; diff --git a/src/github/api.rs b/src/github/api.rs index bd5dc85..e4582c0 100644 --- a/src/github/api.rs +++ b/src/github/api.rs @@ -6,7 +6,11 @@ use chrono::{Datelike, DateTime, Duration, Timelike, Utc, Weekday}; use log::info; use octocrab::models::pulls::PullRequest; -pub async fn get_merged_pull_requests_from_last_working_day(instance: &Octocrab, organisation: &str, repository: &str) -> Result> { +pub async fn get_merged_pull_requests_from_last_working_day( + instance: &Octocrab, + organisation: &str, + repository: &str, +) -> Result<(DateTime, Vec)> { let beginning_datetime = get_beginning_of_last_working_day(); info!("Getting all PRs merged after: {}", beginning_datetime.to_rfc2822()); @@ -33,7 +37,7 @@ pub async fn get_merged_pull_requests_from_last_working_day(instance: &Octocrab, } } - Ok(merged_pull_requests) + Ok((beginning_datetime, merged_pull_requests)) } pub fn filter_out_renovate_pull_requests(pull_requests: Vec) -> Vec { diff --git a/src/main.rs b/src/main.rs index 58b35c0..4b39cfd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,8 @@ async fn main() -> Result<()> { info!("Github and ChatGPT instances created successfully"); - let all_pull_requests = github::api::get_merged_pull_requests_from_last_working_day(&instance, repository_owner.as_str(), repository_name.as_str()).await?; + let (date_time, all_pull_requests) = + github::api::get_merged_pull_requests_from_last_working_day(&instance, repository_owner.as_str(), repository_name.as_str()).await?; info!("{} pull requests fetched successfully", &all_pull_requests.len()); let filtered_pull_requests = github::api::filter_out_renovate_pull_requests(all_pull_requests); info!("{} pull request(s) left after filtering out dependency updates", &filtered_pull_requests.len()); @@ -44,7 +45,7 @@ async fn main() -> Result<()> { let delivery_mechanism = configure_delivery_mechanism()?; - delivery_mechanism.deliver(&chat_response).await?; + delivery_mechanism.deliver(&date_time, &chat_response).await?; Ok(()) }