159 lines
4.8 KiB
Rust
159 lines
4.8 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use crate::{
|
|
config::Config,
|
|
misc::github::{fetch_pr, ProjectState},
|
|
template::{Infos, NavBar},
|
|
};
|
|
use actix_web::{get, web, HttpResponse, Responder};
|
|
use cached::proc_macro::once;
|
|
use ramhorns::Content;
|
|
|
|
#[get("/contrib")]
|
|
async fn page(config: web::Data<Config>) -> impl Responder {
|
|
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await)
|
|
}
|
|
|
|
#[derive(Content, Debug)]
|
|
struct PortfolioTemplate {
|
|
navbar: NavBar,
|
|
error: bool,
|
|
projects: Option<Vec<Project>>,
|
|
waiting: Option<Vec<Project>>,
|
|
closed: Option<Vec<Project>>,
|
|
}
|
|
|
|
#[derive(Content, Clone, Debug)]
|
|
struct Project {
|
|
name: String,
|
|
url: String,
|
|
pulls_merged: Vec<Pull>,
|
|
pulls_open: Vec<Pull>,
|
|
pulls_closed: Vec<Pull>,
|
|
}
|
|
|
|
#[derive(Content, Clone, Debug)]
|
|
struct Pull {
|
|
url: String,
|
|
id: u32,
|
|
name_repo: String,
|
|
title: String,
|
|
state: u8,
|
|
}
|
|
|
|
#[once(time = 600)] // 10min
|
|
async fn build_page(config: Config) -> String {
|
|
let navbar = NavBar {
|
|
contrib: true,
|
|
..NavBar::default()
|
|
};
|
|
|
|
// Fetch latest data from github
|
|
let data = match fetch_pr().await {
|
|
Ok(projects) => {
|
|
let mut data: Vec<Project> = Vec::new();
|
|
|
|
// Grouping PRs by projects
|
|
let mut map: HashMap<&str, Vec<Pull>> = HashMap::new();
|
|
projects.iter().for_each(|p| {
|
|
let project = Pull {
|
|
url: p.contrib_url.to_owned(),
|
|
id: p.id,
|
|
name_repo: p.project.to_owned(),
|
|
title: p.title.to_owned(),
|
|
state: p.status as u8,
|
|
};
|
|
let project_name = p.project.as_str();
|
|
if map.contains_key(project_name) {
|
|
map.entry(project_name).and_modify(|v| v.push(project));
|
|
} else {
|
|
data.push(Project {
|
|
name: project_name.into(),
|
|
url: p.project_url.to_owned(),
|
|
pulls_merged: Vec::new(),
|
|
pulls_closed: Vec::new(),
|
|
pulls_open: Vec::new(),
|
|
});
|
|
map.insert(project_name, vec![project]);
|
|
}
|
|
});
|
|
|
|
// Distributes each PR in the right vector
|
|
data.iter_mut().for_each(|d| {
|
|
map.get(d.name.as_str()).unwrap().iter().for_each(|p| {
|
|
let state = p.state.into();
|
|
match state {
|
|
ProjectState::Closed => d.pulls_closed.push(p.to_owned()),
|
|
ProjectState::Merged => d.pulls_merged.push(p.to_owned()),
|
|
ProjectState::Open => d.pulls_open.push(p.to_owned()),
|
|
}
|
|
});
|
|
let mut name: Vec<char> = d.name.replace('-', " ").chars().collect();
|
|
name[0] = name[0].to_uppercase().next().unwrap();
|
|
d.name = name.into_iter().collect();
|
|
});
|
|
|
|
// Ascending order by pulls IDs
|
|
data.iter_mut().for_each(|d| {
|
|
d.pulls_closed.reverse();
|
|
d.pulls_merged.reverse();
|
|
d.pulls_open.reverse();
|
|
});
|
|
|
|
// Ascending order by number of pulls
|
|
data.sort_by(|a, b| {
|
|
b.pulls_merged
|
|
.len()
|
|
.partial_cmp(&a.pulls_merged.len())
|
|
.unwrap()
|
|
});
|
|
|
|
PortfolioTemplate {
|
|
navbar,
|
|
error: false,
|
|
projects: Some(
|
|
data.iter()
|
|
.cloned()
|
|
.filter(|p| !p.pulls_merged.is_empty())
|
|
.collect(),
|
|
),
|
|
waiting: Some(
|
|
data.iter()
|
|
.cloned()
|
|
.filter(|p| !p.pulls_open.is_empty())
|
|
.collect(),
|
|
),
|
|
closed: Some(
|
|
data.iter()
|
|
.cloned()
|
|
.filter(|p| !p.pulls_closed.is_empty())
|
|
.collect(),
|
|
),
|
|
}
|
|
}
|
|
Err(e) => {
|
|
eprintln!("{}", e);
|
|
|
|
PortfolioTemplate {
|
|
navbar,
|
|
error: true,
|
|
projects: None,
|
|
waiting: None,
|
|
closed: None,
|
|
}
|
|
}
|
|
};
|
|
|
|
config.tmpl.render(
|
|
"contrib.html",
|
|
data,
|
|
Infos {
|
|
page_title: Some("Mes contributions".into()),
|
|
page_desc: Some(format!(
|
|
"Contributions à l'opensource par {}",
|
|
config.fc.name.unwrap_or_default()
|
|
)),
|
|
page_kw: None,
|
|
},
|
|
)
|
|
}
|