use vec of projects
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-11 23:26:54 +02:00
parent e42430d24b
commit 3a5fafc6c0
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 61 additions and 37 deletions

View file

@ -1,37 +1,68 @@
use reqwest::{header::ACCEPT, Error}; use ramhorns::Content;
use reqwest::header::ACCEPT;
use serde::Deserialize; use serde::Deserialize;
use crate::utils::get_reqwest_client; use crate::utils::get_reqwest_client;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GithubResponse { struct GithubResponse {
pub total_count: u32, items: Vec<GithubProject>,
pub items: Vec<GithubProject>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GithubProject { struct GithubProject {
pub repository_url: String, repository_url: String,
pub number: u32, number: u32,
title: String,
state: String,
pull_request: GithubPullRequest,
}
#[derive(Deserialize)]
struct GithubPullRequest {
html_url: String,
merged_at: Option<String>,
}
#[derive(Content, Debug)]
pub struct Project {
pub project: String,
pub project_url: String,
pub status: String,
pub title: String, pub title: String,
pub state: String, pub id: u32,
pub pull_request: GithubPullRequest, pub contrib_url: String,
} }
#[derive(Deserialize)] pub async fn fetch_pr() -> Vec<Project> {
pub struct GithubPullRequest { // Result<GithubResponse, Error>
pub html_url: String,
pub merged_at: Option<String>,
}
pub async fn fetch_pr() -> Result<GithubResponse, Error> {
let client = get_reqwest_client(); let client = get_reqwest_client();
client let resp = client
.get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon") .get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon")
.header(ACCEPT, "application/vnd.github.text-match+json") .header(ACCEPT, "application/vnd.github.text-match+json")
.send() .send()
.await? .await
.unwrap()
.json::<GithubResponse>() .json::<GithubResponse>()
.await .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
} }

View file

@ -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 actix_web::{get, web, HttpResponse, Responder};
use cached::proc_macro::once; use cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
@ -9,31 +13,20 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
} }
#[derive(Content)] #[derive(Content)]
struct PortfolioTemplate {} struct PortfolioTemplate {
data: Vec<Project>,
}
#[once(time = 60)] #[once(time = 60)]
pub async fn get_page(config: Config) -> std::string::String { pub async fn get_page(config: Config) -> std::string::String {
// Fetch latest data from github // Fetch latest data from github
if let Ok(resp) = fetch_pr().await { let projects = fetch_pr().await;
println!("Nb: {}", resp.total_count);
resp.items.iter().for_each(|x| { println!("{:#?}", projects);
println!(
"[{} #{}]({}) - {}",
x.title,
x.number,
x.pull_request.html_url,
if x.pull_request.merged_at.is_none() {
&x.state
} else {
"merged"
}
)
});
};
config.tmpl.render( config.tmpl.render(
"contrib.html", "contrib.html",
PortfolioTemplate {}, PortfolioTemplate { data: projects },
Infos { Infos {
page_title: Some("Mes contributions".to_string()), page_title: Some("Mes contributions".to_string()),
page_desc: Some("Contributions à l'opensource par Anri".to_string()), page_desc: Some("Contributions à l'opensource par Anri".to_string()),