better error handling
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-12 01:21:07 +02:00
parent cfe0620aa9
commit addf6e0035
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 74 additions and 61 deletions

View file

@ -1,6 +1,6 @@
use core::panic;
use reqwest::header::ACCEPT;
use reqwest::{header::ACCEPT, Error};
use serde::Deserialize;
use crate::utils::get_reqwest_client;
@ -52,7 +52,7 @@ pub struct Project {
pub contrib_url: String,
}
pub async fn fetch_pr() -> Vec<Project> {
pub async fn fetch_pr() -> Result<Vec<Project>, Error> {
// Result<GithubResponse, Error>
let client = get_reqwest_client();
@ -60,11 +60,9 @@ pub async fn fetch_pr() -> Vec<Project> {
.get("https://api.github.com/search/issues?q=is:pr%20author:Mylloon")
.header(ACCEPT, "application/vnd.github.text-match+json")
.send()
.await
.unwrap()
.await?
.json::<GithubResponse>()
.await
.unwrap();
.await?;
let mut list = vec![];
resp.items.iter().for_each(|p| {
@ -86,5 +84,5 @@ pub async fn fetch_pr() -> Vec<Project> {
});
});
list
Ok(list)
}

View file

@ -16,6 +16,7 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
#[derive(Content)]
struct PortfolioTemplate {
error: bool,
projects: Vec<Project>,
waiting: Vec<Project>,
closed: Vec<Project>,
@ -41,65 +42,76 @@ struct Pull {
#[once(time = 120)]
pub async fn get_page(config: Config) -> std::string::String {
// Fetch latest data from github
let projects = fetch_pr().await;
let mut data: Vec<Project> = Vec::new();
let mut map: HashMap<&str, Vec<Pull>> = HashMap::new();
projects.iter().for_each(|p| {
let project = Pull {
url: p.contrib_url.clone(),
id: p.id,
name: p.project.clone(),
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.to_string(),
url: p.project_url.clone(),
pulls_merged: Vec::new(),
pulls_closed: Vec::new(),
pulls_open: Vec::new(),
let data = match fetch_pr().await {
Ok(projects) => {
let mut data: Vec<Project> = Vec::new();
let mut map: HashMap<&str, Vec<Pull>> = HashMap::new();
projects.iter().for_each(|p| {
let project = Pull {
url: p.contrib_url.clone(),
id: p.id,
name: p.project.clone(),
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.to_string(),
url: p.project_url.clone(),
pulls_merged: Vec::new(),
pulls_closed: Vec::new(),
pulls_open: Vec::new(),
});
map.insert(project_name, vec![project]);
}
});
map.insert(project_name, vec![project]);
}
});
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.clone()),
ProjectState::Merged => d.pulls_merged.push(p.clone()),
ProjectState::Open => d.pulls_open.push(p.clone()),
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.clone()),
ProjectState::Merged => d.pulls_merged.push(p.clone()),
ProjectState::Open => d.pulls_open.push(p.clone()),
}
});
let mut name: Vec<char> = d.name.replace('-', " ").chars().collect();
name[0] = name[0].to_uppercase().next().unwrap();
d.name = name.into_iter().collect();
});
PortfolioTemplate {
error: false,
projects: data
.iter()
.cloned()
.filter(|p| !p.pulls_merged.is_empty())
.collect(),
waiting: data
.iter()
.cloned()
.filter(|p| !p.pulls_open.is_empty())
.collect(),
closed: data
.iter()
.cloned()
.filter(|p| !p.pulls_closed.is_empty())
.collect(),
}
});
let mut name: Vec<char> = d.name.replace('-', " ").chars().collect();
name[0] = name[0].to_uppercase().next().unwrap();
d.name = name.into_iter().collect();
});
}
Err(_) => PortfolioTemplate {
error: true,
projects: Vec::new(),
waiting: Vec::new(),
closed: Vec::new(),
},
};
config.tmpl.render(
"contrib.html",
PortfolioTemplate {
projects: data
.iter()
.cloned()
.filter(|p| !p.pulls_merged.is_empty())
.collect(),
waiting: data
.iter()
.cloned()
.filter(|p| !p.pulls_open.is_empty())
.collect(),
closed: data
.iter()
.cloned()
.filter(|p| !p.pulls_closed.is_empty())
.collect(),
},
data,
Infos {
page_title: Some("Mes contributions".to_string()),
page_desc: Some("Contributions à l'opensource par Anri".to_string()),

View file

@ -4,6 +4,9 @@
<body>
{{#data}}
<div id="content">
{{#error}}
<p>Github ne veut pas que tu vois ces informations...</p>
{{/error}} {{^error}}
<h1 class="subtitle">Mes contributions</h1>
<!-- Add URL? -->
{{#projects}}
@ -31,6 +34,6 @@
</p>
{{/closed}}
</div>
{{/data}} {{> footer.html }}
{{/error}} {{/data}} {{> footer.html }}
</body>
</html>