From 3a5fafc6c08c6b6d5897934686eb937fbea6e83d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 11 Apr 2023 23:26:54 +0200 Subject: [PATCH] use vec of projects --- src/misc/github.rs | 67 +++++++++++++++++++++++++++++++------------ src/routes/contrib.rs | 31 ++++++++------------ 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/src/misc/github.rs b/src/misc/github.rs index d8fcf03..83bad83 100644 --- a/src/misc/github.rs +++ b/src/misc/github.rs @@ -1,37 +1,68 @@ -use reqwest::{header::ACCEPT, Error}; +use ramhorns::Content; +use reqwest::header::ACCEPT; use serde::Deserialize; use crate::utils::get_reqwest_client; #[derive(Deserialize)] -pub struct GithubResponse { - pub total_count: u32, - pub items: Vec, +struct GithubResponse { + items: Vec, } #[derive(Deserialize)] -pub struct GithubProject { - pub repository_url: String, - pub number: u32, +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(Content, Debug)] +pub struct Project { + pub project: String, + pub project_url: String, + pub status: String, pub title: String, - pub state: String, - pub pull_request: GithubPullRequest, + pub id: u32, + pub contrib_url: String, } -#[derive(Deserialize)] -pub struct GithubPullRequest { - pub html_url: String, - pub merged_at: Option, -} - -pub async fn fetch_pr() -> Result { +pub async fn fetch_pr() -> Vec { + // Result let client = get_reqwest_client(); - 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? + .await + .unwrap() .json::() .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() { + p.state.clone() + } else { + "merged".to_string() + }, + title: p.title.clone(), + id: p.number, + contrib_url: p.pull_request.html_url.clone(), + }); + }); + + list } diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index dc778fb..bbd37c9 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -1,4 +1,8 @@ -use crate::{config::Config, github::fetch_pr, template::Infos}; +use crate::{ + config::Config, + github::{fetch_pr, Project}, + template::Infos, +}; use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; @@ -9,31 +13,20 @@ pub async fn page(config: web::Data) -> impl Responder { } #[derive(Content)] -struct PortfolioTemplate {} +struct PortfolioTemplate { + data: Vec, +} #[once(time = 60)] pub async fn get_page(config: Config) -> std::string::String { // Fetch latest data from github - if let Ok(resp) = fetch_pr().await { - println!("Nb: {}", resp.total_count); - resp.items.iter().for_each(|x| { - println!( - "[{} #{}]({}) - {}", - x.title, - x.number, - x.pull_request.html_url, - if x.pull_request.merged_at.is_none() { - &x.state - } else { - "merged" - } - ) - }); - }; + let projects = fetch_pr().await; + + println!("{:#?}", projects); config.tmpl.render( "contrib.html", - PortfolioTemplate {}, + PortfolioTemplate { data: projects }, Infos { page_title: Some("Mes contributions".to_string()), page_desc: Some("Contributions à l'opensource par Anri".to_string()),