use core::panic; use reqwest::{header::ACCEPT, Error}; use serde::Deserialize; use crate::utils::get_reqwest_client; #[derive(Deserialize)] struct GithubResponse { items: Vec, } #[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, } #[derive(Clone, Copy)] pub enum ProjectState { Closed = 0, Open = 1, Merged = 2, } impl From 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, Error> { // Result 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::() .await?; 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(), }); }); Ok(list) }