mylloon.fr/src/misc/github.rs
Mylloon b0112c06d6
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending
use to_owned instead of clone
2023-04-24 12:18:21 +02:00

88 lines
2 KiB
Rust

use core::panic;
use reqwest::{header::ACCEPT, Error};
use serde::Deserialize;
use crate::misc::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() -> Result<Vec<Project>, Error> {
// 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?
.json::<GithubResponse>()
.await?;
let mut list = vec![];
resp.items.iter().for_each(|p| {
list.push(Project {
project: p.repository_url.split('/').last().unwrap().to_owned(),
project_url: p.repository_url.to_owned(),
status: if p.pull_request.merged_at.is_none() {
if p.state == "closed" {
ProjectState::Closed
} else {
ProjectState::Open
}
} else {
ProjectState::Merged
},
title: p.title.to_owned(),
id: p.number,
contrib_url: p.pull_request.html_url.to_owned(),
});
});
Ok(list)
}