From 558137e2ee276371e584ff1dde4b3d83b511751f Mon Sep 17 00:00:00 2001
From: Valentin Brandl
Date: Mon, 16 Jan 2023 09:54:04 +0100
Subject: [PATCH] Allow customization of the badge label
---
src/lib.rs | 25 +++++++++++++++++--------
templates/index.rs.html | 4 ++++
templates/overview.rs.html | 4 ++--
3 files changed, 23 insertions(+), 10 deletions(-)
diff --git a/src/lib.rs b/src/lib.rs
index 178d239..888ba20 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -83,8 +83,14 @@ struct JsonResponse<'a> {
}
#[derive(Deserialize, Debug)]
-struct BranchQuery {
+struct BadgeQuery {
branch: Option,
+ #[serde(default = "default_label")]
+ label: String,
+}
+
+fn default_label() -> String {
+ "Hits-of-Code".to_string()
}
fn pull(path: impl AsRef) -> Result<()> {
@@ -298,7 +304,7 @@ pub(crate) async fn json_hoc(
state: web::Data,
repo_count: web::Data,
data: web::Path<(String, String)>,
- branch: web::Query,
+ branch: web::Query,
) -> Result {
let branch = branch.branch.as_deref().unwrap_or("master");
let rc_clone = repo_count.clone();
@@ -334,14 +340,15 @@ pub(crate) async fn calculate_hoc(
state: web::Data,
repo_count: web::Data,
data: web::Path<(String, String)>,
- branch: web::Query,
+ query: web::Query,
) -> HttpResponse {
let rc_clone = repo_count.clone();
+ let label = query.label.clone();
let mapper = move |r| match r {
HocResult::NotFound => p404(rc_clone),
HocResult::Hoc { hoc_pretty, .. } => {
let badge_opt = BadgeOptions {
- subject: "Hits-of-Code".to_string(),
+ subject: label,
color: "#007ec6".to_string(),
status: hoc_pretty,
};
@@ -352,10 +359,10 @@ pub(crate) async fn calculate_hoc(
Ok(no_cache_response(body))
}
};
- let branch = branch.branch.as_deref().unwrap_or("master");
+ let branch = query.branch.as_deref().unwrap_or("master");
let error_badge = |_| {
let error_badge = Badge::new(BadgeOptions {
- subject: "Hits-of-Code".to_string(),
+ subject: query.label.clone(),
color: "#ff0000".to_string(),
status: "error".to_string(),
})
@@ -372,9 +379,10 @@ async fn overview(
state: web::Data,
repo_count: web::Data,
data: web::Path<(String, String)>,
- branch: web::Query,
+ query: web::Query,
) -> Result {
- let branch = branch.branch.as_deref().unwrap_or("master");
+ let branch = query.branch.as_deref().unwrap_or("master");
+ let label = query.label.clone();
let base_url = state.settings.base_url.clone();
let rc_clone = repo_count.clone();
let mapper = move |r| match r {
@@ -405,6 +413,7 @@ async fn overview(
VERSION_INFO,
rc_clone.load(Ordering::Relaxed),
repo_info,
+ label,
)?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
diff --git a/templates/index.rs.html b/templates/index.rs.html
index aebc661..75934d4 100644
--- a/templates/index.rs.html
+++ b/templates/index.rs.html
@@ -51,6 +51,10 @@ in your repository or you want a badge for another branch of your repository, ju
?branch=<branch-name>
to the URL.
+
+The badge label can be customized using the label=<some-label>
query parameter. It defaults to Hits-of-Code
.
+
+
You can also request the HoC as JSON by appending /json
to the request path. This will return a JSON object
with three fields: count
(the HoC value), commits
(the number of commits) and
diff --git a/templates/overview.rs.html b/templates/overview.rs.html
index 13bc9e1..ef209a1 100644
--- a/templates/overview.rs.html
+++ b/templates/overview.rs.html
@@ -2,7 +2,7 @@
@use crate::statics::VersionInfo;
@use crate::template::RepoInfo;
-@(version_info: VersionInfo, repo_count: usize, repo_info: RepoInfo)
+@(version_info: VersionInfo, repo_count: usize, repo_info: RepoInfo, label: String)
@:base("Hits-of-Code Badges", "Overview", {
@@ -19,7 +19,7 @@ To include the badge in your readme, use the following markdown:
-[![Hits-of-Code](@repo_info.base_url/@repo_info.path?branch=@repo_info.branch)](@repo_info.base_url/@repo_info.path/view?branch=@repo_info.branch)
+[![@label](@repo_info.base_url/@repo_info.path?branch=@repo_info.branch&label=@label)](@repo_info.base_url/@repo_info.path/view?branch=@repo_info.branch&label=@label)