From e3d9cf5398f5f1eb5f08b7e317c9074f7c3dc082 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sat, 5 Aug 2023 23:57:56 +1000 Subject: [PATCH] add protections to not to send messages about the same PR more than once --- src/gitea.rs | 5 +++-- src/main.rs | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/gitea.rs b/src/gitea.rs index 0948d79..8e6d433 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -36,10 +36,11 @@ pub struct Pull { pub body: String, pub state: String, pub mergeable: bool, + pub merge_base: String, + pub merged: bool, pub base: Branch, pub head: Branch, - pub merge_base: String, - pub user: User + pub user: User, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/src/main.rs b/src/main.rs index c2c6d96..2348d3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use log; use teloxide_core::{prelude::*, types::{Recipient, Chat}}; use teloxide::Bot; use tide::{Server, Request, Response}; +use tokio::fs; mod gitea; @@ -98,10 +99,29 @@ async fn webhook(mut req: Request) -> tide::Result { ) } + if ! fs::try_exists(".pr-cache").await.unwrap() { + fs::write(".pr-cache", "[]").await.unwrap(); + } + let cache = fs::read_to_string(".pr-cache").await.unwrap(); + let cache = serde_json::from_str::>(&cache).unwrap(); + + if cache.iter().find(|x| **x == pr.pull_request.id as u64).is_some() { + return Ok( + Response::builder(200) + .body("{\"status\":\"ignoring known PR\"}") + .content_type("application/json") + .build() + ) + } else { + let mut cache = cache; + cache.push(pr.pull_request.id as u64); + fs::write(".pr-cache", serde_json::to_string(&cache).unwrap()).await.unwrap(); + } + let state = req.state().clone(); state.bot.send_message(state.chat.id, format!("New PR\n{} ({}#{}) by {}\n{}", pr.pull_request.title, pr.pull_request.head.repo.name, pr.pull_request.number, pr.pull_request.user.login, pr.pull_request.url)).await.unwrap(); - state.bot.send_message(state.chat.id, "апрувните пж @bleki42 @balistiktw @x3paerz").await.unwrap(); + // state.bot.send_message(state.chat.id, "апрувните пж @bleki42 @balistiktw @x3paerz").await.unwrap(); Ok( Response::builder(200)