Skip to content

Commit

Permalink
send date into delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
FredrikAugust committed Jul 8, 2024
1 parent de4325d commit 41b28c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/delivery/api.rs
Original file line number Diff line number Diff line change
@@ -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<Utc>,
message: &str,
) -> Result<()>;
}
17 changes: 11 additions & 6 deletions src/delivery/slack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Utc>, 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");

Expand All @@ -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?;
Expand Down
8 changes: 6 additions & 2 deletions src/github/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<PullRequest>> {
pub async fn get_merged_pull_requests_from_last_working_day(
instance: &Octocrab,
organisation: &str,
repository: &str,
) -> Result<(DateTime<Utc>, Vec<PullRequest>)> {
let beginning_datetime = get_beginning_of_last_working_day();
info!("Getting all PRs merged after: {}", beginning_datetime.to_rfc2822());

Expand All @@ -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<PullRequest>) -> Vec<PullRequest> {
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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(())
}
Expand Down

0 comments on commit 41b28c9

Please sign in to comment.