Allow customization of the badge label

This commit is contained in:
Valentin Brandl 2023-01-16 09:54:04 +01:00
parent 4708c19eb1
commit 558137e2ee
3 changed files with 23 additions and 10 deletions

View File

@ -83,8 +83,14 @@ struct JsonResponse<'a> {
}
#[derive(Deserialize, Debug)]
struct BranchQuery {
struct BadgeQuery {
branch: Option<String>,
#[serde(default = "default_label")]
label: String,
}
fn default_label() -> String {
"Hits-of-Code".to_string()
}
fn pull(path: impl AsRef<Path>) -> Result<()> {
@ -298,7 +304,7 @@ pub(crate) async fn json_hoc<T: Service>(
state: web::Data<State>,
repo_count: web::Data<AtomicUsize>,
data: web::Path<(String, String)>,
branch: web::Query<BranchQuery>,
branch: web::Query<BadgeQuery>,
) -> Result<HttpResponse> {
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<T: Service>(
state: web::Data<State>,
repo_count: web::Data<AtomicUsize>,
data: web::Path<(String, String)>,
branch: web::Query<BranchQuery>,
query: web::Query<BadgeQuery>,
) -> 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<T: Service>(
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<T: Service>(
state: web::Data<State>,
repo_count: web::Data<AtomicUsize>,
data: web::Path<(String, String)>,
branch: web::Query<BranchQuery>,
query: web::Query<BadgeQuery>,
) -> Result<HttpResponse> {
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<T: Service>(
VERSION_INFO,
rc_clone.load(Ordering::Relaxed),
repo_info,
label,
)?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf))

View File

@ -51,6 +51,10 @@ in your repository or you want a badge for another branch of your repository, ju
<code>?branch=&lt;branch-name&gt;</code> to the URL.
</p>
<p>
The badge label can be customized using the <code>label=&lt;some-label&gt;</code> query parameter. It defaults to <code>Hits-of-Code</code>.
</p>
<p>
You can also request the HoC as JSON by appending <code>/json</code> to the request path. This will return a JSON object
with three fields: <code>count</code> (the HoC value), <code>commits</code> (the number of commits) and

View File

@ -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:
</p>
<pre>
[![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)
</pre>