rewrite from gritea to "dumb" implementation

This commit is contained in:
b1ek 2023-08-05 02:08:45 +10:00
parent e0435daafd
commit 761f33273e
Signed by: blek
GPG Key ID: 14546221E3595D0C
6 changed files with 1322 additions and 2 deletions

View File

@ -1,2 +1,3 @@
TG_KEY=
GITEA_KEY=
GITEA_URL=https://git.blek.codes

1235
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,3 +5,9 @@ edition = "2021"
[dependencies]
dotenvy_macro = "0.15.7"
femme = "2.2.1"
log = "0.4.19"
reqwest = "0.11.18"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.104"
tokio = { version = "1.29.1", features = [ "full" ] }

37
src/api.rs Normal file
View File

@ -0,0 +1,37 @@
use crate::{GITEA_KEY, gitea::{User, Repo}};
macro_rules! gitea_api {
($path: expr) => {
format!("{}{}?token={}", dotenvy_macro::dotenv!("GITEA_URL", "Gitea url is not set!"), $path, GITEA_KEY)
};
}
pub async fn get_user() -> User {
let user = reqwest::get(gitea_api!("/api/v1/user")).await.unwrap();
if user.status() != 200 {
panic!("Request failed with code {}", user.status());
}
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 fn filter_repos(repos: Vec<Repo>) -> Vec<Repo> {
repos.iter().filter_map(|x| { if (!x.archived) && x.has_pull_requests { Some(x) } else { None } }).collect::<Vec<Repo>>()
}

20
src/gitea.rs Normal file
View File

@ -0,0 +1,20 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Clone)]
pub struct User {
pub id: i64,
pub login: String,
pub full_name: String
}
#[derive(Serialize, Deserialize, Clone)]
pub struct Repo {
pub id: i64,
pub owner: User,
pub name: String,
pub full_name: String,
pub private: bool,
pub archived: bool,
pub has_pull_requests: bool,
pub has_issues: bool
}

View File

@ -1,8 +1,31 @@
#![forbid(unsafe_code)]
use std::process::exit;
use log;
mod gitea;
mod api;
const TG_KEY: &str = dotenvy_macro::dotenv!("TG_KEY", "Telegram key is not set!");
const GITEA_KEY: &str = dotenvy_macro::dotenv!("GITEA_KEY", "Gitea key is not set!");
const GITEA_URL: &str = dotenvy_macro::dotenv!("GITEA_URL", "Gitea url is not set!");
fn main() {
#[tokio::main]
async fn main() {
#[cfg(debug_assertions)]
femme::with_level(log::LevelFilter::Debug);
#[cfg(not(debug_assertions))] {
femme::with_level(log::LevelFilter::Info);
log::info!("Running in production");
}
let user = api::get_user().await;
log::info!("Logged into gitea as user \"{}\"", user.full_name);
let repos = api::get_org_repos("Tochka".into()).await;
log::info!("Found {} repositories", repos.len());
}