Merge pull request #546 from vbrandl/feature/badge-label
All checks were successful
continuous-integration/drone/push Build is passing

Allow customization of the badge label
This commit is contained in:
Valentin Brandl 2023-01-16 10:05:36 +01:00 committed by GitHub
commit 8b058d9c53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 12 deletions

View File

@ -5,6 +5,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.30.0] 2023-01-16
### New Features
* Allow customization of the badge label ([#546])
### Dependency Updates ### Dependency Updates
* Updated [`tracing-actix-web`](https://github.com/LukeMathWalker/tracing-actix-web) from 0.7.1 to 0.7.2 ([#542]) * Updated [`tracing-actix-web`](https://github.com/LukeMathWalker/tracing-actix-web) from 0.7.1 to 0.7.2 ([#542])
@ -13,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#542]: https://github.com/vbrandl/hoc/pull/542 [#542]: https://github.com/vbrandl/hoc/pull/542
[#544]: https://github.com/vbrandl/hoc/pull/544 [#544]: https://github.com/vbrandl/hoc/pull/544
[#546]: https://github.com/vbrandl/hoc/pull/546
## [0.29.0] 2023-01-07 ## [0.29.0] 2023-01-07

2
Cargo.lock generated
View File

@ -895,7 +895,7 @@ dependencies = [
[[package]] [[package]]
name = "hoc" name = "hoc"
version = "0.29.0" version = "0.30.0"
dependencies = [ dependencies = [
"actix-rt", "actix-rt",
"actix-web", "actix-web",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "hoc" name = "hoc"
version = "0.29.0" version = "0.30.0"
authors = ["Valentin Brandl <vbrandl@riseup.net>"] authors = ["Valentin Brandl <vbrandl@riseup.net>"]
edition = "2018" edition = "2018"
build = "build.rs" build = "build.rs"

View File

@ -83,8 +83,14 @@ struct JsonResponse<'a> {
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct BranchQuery { struct BadgeQuery {
branch: Option<String>, 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<()> { fn pull(path: impl AsRef<Path>) -> Result<()> {
@ -298,7 +304,7 @@ pub(crate) async fn json_hoc<T: Service>(
state: web::Data<State>, state: web::Data<State>,
repo_count: web::Data<AtomicUsize>, repo_count: web::Data<AtomicUsize>,
data: web::Path<(String, String)>, data: web::Path<(String, String)>,
branch: web::Query<BranchQuery>, branch: web::Query<BadgeQuery>,
) -> Result<HttpResponse> { ) -> Result<HttpResponse> {
let branch = branch.branch.as_deref().unwrap_or("master"); let branch = branch.branch.as_deref().unwrap_or("master");
let rc_clone = repo_count.clone(); let rc_clone = repo_count.clone();
@ -334,14 +340,15 @@ pub(crate) async fn calculate_hoc<T: Service>(
state: web::Data<State>, state: web::Data<State>,
repo_count: web::Data<AtomicUsize>, repo_count: web::Data<AtomicUsize>,
data: web::Path<(String, String)>, data: web::Path<(String, String)>,
branch: web::Query<BranchQuery>, query: web::Query<BadgeQuery>,
) -> HttpResponse { ) -> HttpResponse {
let rc_clone = repo_count.clone(); let rc_clone = repo_count.clone();
let label = query.label.clone();
let mapper = move |r| match r { let mapper = move |r| match r {
HocResult::NotFound => p404(rc_clone), HocResult::NotFound => p404(rc_clone),
HocResult::Hoc { hoc_pretty, .. } => { HocResult::Hoc { hoc_pretty, .. } => {
let badge_opt = BadgeOptions { let badge_opt = BadgeOptions {
subject: "Hits-of-Code".to_string(), subject: label,
color: "#007ec6".to_string(), color: "#007ec6".to_string(),
status: hoc_pretty, status: hoc_pretty,
}; };
@ -352,10 +359,10 @@ pub(crate) async fn calculate_hoc<T: Service>(
Ok(no_cache_response(body)) 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 = |_| {
let error_badge = Badge::new(BadgeOptions { let error_badge = Badge::new(BadgeOptions {
subject: "Hits-of-Code".to_string(), subject: query.label.clone(),
color: "#ff0000".to_string(), color: "#ff0000".to_string(),
status: "error".to_string(), status: "error".to_string(),
}) })
@ -372,9 +379,10 @@ async fn overview<T: Service>(
state: web::Data<State>, state: web::Data<State>,
repo_count: web::Data<AtomicUsize>, repo_count: web::Data<AtomicUsize>,
data: web::Path<(String, String)>, data: web::Path<(String, String)>,
branch: web::Query<BranchQuery>, query: web::Query<BadgeQuery>,
) -> Result<HttpResponse> { ) -> 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 base_url = state.settings.base_url.clone();
let rc_clone = repo_count.clone(); let rc_clone = repo_count.clone();
let mapper = move |r| match r { let mapper = move |r| match r {
@ -405,6 +413,7 @@ async fn overview<T: Service>(
VERSION_INFO, VERSION_INFO,
rc_clone.load(Ordering::Relaxed), rc_clone.load(Ordering::Relaxed),
repo_info, repo_info,
label,
)?; )?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf)) 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. <code>?branch=&lt;branch-name&gt;</code> to the URL.
</p> </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> <p>
You can also request the HoC as JSON by appending <code>/json</code> to the request path. This will return a JSON object 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 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::statics::VersionInfo;
@use crate::template::RepoInfo; @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", { @:base("Hits-of-Code Badges", "Overview", {
@ -19,7 +19,7 @@ To include the badge in your readme, use the following markdown:
</p> </p>
<pre> <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> </pre>