add feature to ping only required reviewers

This commit is contained in:
b1ek 2023-08-06 00:29:17 +10:00
parent e687b3fda3
commit dd957e1772
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 20 additions and 99 deletions

View File

@ -1,102 +1,19 @@
use crate::gitea::{User, Repo, Pull};
pub async fn get_user() -> User {
let user = reqwest::get(gitea_api!("/api/v1/user")).await.unwrap();
pub fn get_reviewers_pings_inner(author_login: String) -> Vec<String> {
let mut reviewers: Vec<String> = vec![
"@bleki42".into(),
"@x3paerz".into(),
"@balistiktw".into()
];
if user.status() != 200 {
panic!("Request failed with code {}", user.status());
let map: Vec<String> = vec!["blek".into(), "balistik".into(), "xepaerz".into()];
if ! map.contains(&author_login) {
return reviewers
}
let user = user.bytes().await.unwrap();
let user = String::from_utf8(user.to_vec()).unwrap();
serde_json::from_str::<User>(user.as_str()).unwrap()
}
pub async fn get_org_repos(org: String) -> Vec<Repo> {
let repos = reqwest::get(gitea_api!(format!("/api/v1/orgs/{org}/repos"))).await.unwrap();
if repos.status() != 200 {
panic!("Request failed with code {}", repos.status());
}
let repos = repos.bytes().await.unwrap();
let repos = String::from_utf8(repos.to_vec()).unwrap();
serde_json::from_str::<Vec<Repo>>(repos.as_str()).unwrap()
}
pub async fn get_repo_pulls(org: String, repo: String) -> Vec<Pull> {
let pulls = reqwest::get(gitea_api!(format!("/api/v1/repos/{org}/{repo}/pulls"))).await.unwrap();
if pulls.status() != 200 {
panic!("Request failed with code {}", pulls.status());
}
let pulls = pulls.bytes().await.unwrap();
let pulls = String::from_utf8(pulls.to_vec()).unwrap();
let parsed = serde_json::from_str(pulls.as_str());
if parsed.is_err() {
println!("{pulls}");
}
parsed.unwrap()
}
pub async fn get_all_prs(repos: Vec<Repo>) -> Vec<Pull> {
let mut out: Vec<Pull> = vec![];
let futures: Vec<_> =
repos.iter()
.map(|x| get_repo_pulls(x.owner.login.clone(), x.name.clone()))
.collect();
let mut set = tokio::task::JoinSet::new();
for future in futures {
set.spawn(future);
}
while let Some(res) = set.join_next().await {
let res = res.unwrap();
for pull in res.iter() {
out.push(pull.clone());
}
}
out
}
/**
The first one is the never seen before PRs and the 2nd is just the data
*/
pub async fn get_new_prs(old: Vec<Pull>, repos: Vec<Repo>) -> ( Vec<Pull>, Vec<Pull> ) {
log::debug!("Getting all PRs now");
let data = get_all_prs(repos.clone()).await;
let mut new_pulls: Vec<Pull> = vec![];
for pull in data.iter() {
let mut alr_exists = false;
for old_pull in old.iter() {
if pull.id == old_pull.id {
alr_exists = true
}
}
if ! alr_exists {
new_pulls.push(pull.clone());
}
}
(new_pulls, data)
}
pub fn filter_repos(repos: Vec<Repo>) -> Vec<Repo> {
repos.iter()
.filter(|x| (!x.archived) && x.has_pull_requests)
.cloned()
.collect()
reviewers.remove(map.iter().position(|x| **x == author_login).unwrap());
reviewers
}

View File

@ -7,6 +7,7 @@ use tide::{Server, Request, Response};
use tokio::fs;
mod gitea;
mod api;
const CHAT_ID: &str = dotenvy_macro::dotenv!("CHAT_ID", "Chat ID is not set!");
const TG_KEY: &str = dotenvy_macro::dotenv!("TG_KEY", "Telegram key is not set!");
@ -121,13 +122,16 @@ async fn webhook(mut req: Request<SharedState>) -> tide::Result {
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,
format!("апрувните пж{}", api::get_reviewers_pings_inner(pr.sender.login).iter().map(|x| " ".to_string() + x).collect::<Vec<String>>().concat())
).await.unwrap();
Ok(
Response::builder(200)
.body("{ \"status\": \"sent\" }")
.content_type("application/json")
.build()
.body("{ \"status\": \"sent\" }")
.content_type("application/json")
.build()
)
}