add protections to not to send messages about the same PR more than once

This commit is contained in:
b1ek 2023-08-05 23:57:56 +10:00
parent 1b668967d9
commit e3d9cf5398
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 24 additions and 3 deletions

View File

@ -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)]

View File

@ -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<SharedState>) -> 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::<Vec<u64>>(&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)