From 1b8b134acbd13455a7d19cdb758c381eae5673da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tja=C5=BE=20Vra=C4=8Dko?= Date: Mon, 14 Oct 2024 21:15:58 +0200 Subject: [PATCH] feat(config): Add allowed_scopes --- CHANGELOG.md | 4 ++++ crates/committed/src/checks.rs | 30 ++++++++++++++++++++++++++++++ crates/committed/src/config.rs | 15 +++++++++++++++ crates/committed/src/report.rs | 10 ++++++++++ docs/reference.md | 5 +++-- 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 657e1fd..6bc5b90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] - ReleaseDate +### Features + +- Allowed scopes support + ## [1.0.20] - 2023-08-07 ### Compatibility diff --git a/crates/committed/src/checks.rs b/crates/committed/src/checks.rs index 6ced058..46f964a 100644 --- a/crates/committed/src/checks.rs +++ b/crates/committed/src/checks.rs @@ -78,6 +78,13 @@ pub(crate) fn check_message( failed |= check_allowed_types(source, used_type, allowed_types, report)?; } } + + let allowed_scopes: Vec<_> = config.allowed_scopes().collect(); + if !allowed_scopes.is_empty() { + if let Some(used_scope) = parsed.scope() { + failed |= check_allowed_scopes(source, used_scope, allowed_scopes, report)?; + } + } } if config.subject_length() != 0 { @@ -295,6 +302,29 @@ fn check_allowed_types( Ok(true) } +fn check_allowed_scopes( + source: report::Source<'_>, + parsed: unicase::UniCase<&str>, + allowed_scopes: Vec<&str>, + report: report::Report, +) -> Result { + for allowed_scope in allowed_scopes.iter() { + let allowed_scope = unicase::UniCase::new(allowed_scope); + if allowed_scope == parsed { + return Ok(false); + } + } + + report(report::Message::error( + source, + report::DisallowedCommitScope { + used: parsed.as_ref().to_owned(), + allowed: allowed_scopes.iter().map(|s| (*s).to_owned()).collect(), + }, + )); + Ok(true) +} + // For Gitlab's rules, see https://docs.gitlab.com/ee/user/project/merge_requests/work_in_progress_merge_requests.html static WIP_RE: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { regex::Regex::new(r"^(wip\b|WIP\b|\[WIP\]|Draft\b|\[Draft\]|\(Draft\))").unwrap() diff --git a/crates/committed/src/config.rs b/crates/committed/src/config.rs index 698e36e..c6bed46 100644 --- a/crates/committed/src/config.rs +++ b/crates/committed/src/config.rs @@ -28,6 +28,7 @@ pub(crate) struct Config { pub(crate) line_length: Option, pub(crate) style: Option