mylloon.fr/src/misc/github.rs
2023-04-12 00:56:39 +02:00

90 lines
2 KiB
Rust

use core::panic;
use reqwest::header::ACCEPT;
use serde::Deserialize;
use crate::utils::get_reqwest_client;
#[derive(Deserialize)]
struct GithubResponse {
items: Vec<GithubProject>,
}
#[derive(Deserialize)]
struct GithubProject {
repository_url: String,
number: u32,
title: String,
state: String,
pull_request: GithubPullRequest,
}
#[derive(Deserialize)]
struct GithubPullRequest {
html_url: String,
merged_at: Option<String>,
}
#[derive(Clone, Copy)]
pub enum ProjectState {
Closed = 0,
Open = 1,
Merged = 2,
}
impl From<u8> for ProjectState {
fn from(orig: u8) -> Self {
match orig {
0 => Self::Closed,
1 => Self::Open,
2 => Self::Merged,
_ => panic!(),
}
}
}
pub struct Project {
pub project: String,
pub project_url: String,
pub status: ProjectState,
pub title: String,
pub id: u32,
pub contrib_url: String,
}
pub async fn fetch_pr() -> Vec<Project> {
// Result<GithubResponse, Error>
let client = get_reqwest_client();
let resp = client
.get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon")
.header(ACCEPT, "application/vnd.github.text-match+json")
.send()
.await
.unwrap()
.json::<GithubResponse>()
.await
.unwrap();
let mut list = vec![];
resp.items.iter().for_each(|p| {
list.push(Project {
project: p.repository_url.split('/').last().unwrap().to_string(),
project_url: p.repository_url.clone(),
status: if p.pull_request.merged_at.is_none() {
if p.state == "closed" {
ProjectState::Closed
} else {
ProjectState::Open
}
} else {
ProjectState::Merged
},
title: p.title.clone(),
id: p.number,
contrib_url: p.pull_request.html_url.clone(),
});
});
list
}