Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an endpoint to create shields.io badges #122

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::models::SubjectPath;
enum StatusFormat {
Html,
Svg,
ShieldsIoJson,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -63,6 +64,10 @@ impl App {
"/repo/:site/:qual/:name/status.svg",
Route::RepoStatus(StatusFormat::Svg),
);
router.add(
"/repo/:site/:qual/:name/shieldsio.json",
Route::RepoStatus(StatusFormat::ShieldsIoJson),
);

router.add("/crate/:name", Route::CrateRedirect);
router.add(
Expand All @@ -73,6 +78,10 @@ impl App {
"/crate/:name/:version/status.svg",
Route::CrateStatus(StatusFormat::Svg),
);
router.add(
"/crate/:name/:version/shieldsio.json",
Route::CrateStatus(StatusFormat::ShieldsIoJson),
);

App {
logger,
Expand Down Expand Up @@ -342,6 +351,7 @@ impl App {
match format {
StatusFormat::Svg => views::badge::response(analysis_outcome.as_ref()),
StatusFormat::Html => views::html::status::render(analysis_outcome, subject_path),
StatusFormat::ShieldsIoJson => views::shieldsio::response(analysis_outcome.as_ref()),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/server/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod badge;
pub mod html;
pub mod shieldsio;
70 changes: 70 additions & 0 deletions src/server/views/shieldsio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::borrow::Cow;

use hyper::header::CONTENT_TYPE;
use hyper::{Body, Response};
use serde_json::json;

use crate::engine::AnalyzeDependenciesOutcome;

struct EndpointData<'a> {
message: Cow<'a, str>,
color: &'a str,
}

fn data(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> EndpointData<'_> {
match analysis_outcome {
Some(outcome) => {
if outcome.any_always_insecure() {
EndpointData {
message: "insecure".into(),
color: "#e05d44",
}
} else {
let (outdated, total) = outcome.outdated_ratio();

if outdated > 0 {
EndpointData {
message: format!("{} of {} outdated", outdated, total).into(),
color: "#dfb317",
}
} else if total > 0 {
if outcome.any_insecure() {
EndpointData {
message: "maybe insecure".into(),
color: "#88bb11",
}
} else {
EndpointData {
message: "up to date".into(),
color: "#44cc11",
}
}
} else {
EndpointData {
message: "none".into(),
color: "#44cc11",
}
}
}
}
None => EndpointData {
message: "unknown".into(),
color: "#9f9f9f",
},
}
}

pub fn response(analysis_outcome: Option<&AnalyzeDependenciesOutcome>) -> Response<Body> {
let data = data(analysis_outcome);
let json = json! {{
"schemaVersion": 1,
"label": "dependencies",
"message": data.message,
"color": data.color
}};

Response::builder()
.header(CONTENT_TYPE, "application/json; charset=utf-8")
.body(Body::from(json.to_string()))
.unwrap()
}