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

feat(config): Add allowed_scopes #402

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header -->
## [Unreleased] - ReleaseDate

### Features

- Allowed scopes support

## [1.0.20] - 2023-08-07

### Compatibility
Expand Down
30 changes: 30 additions & 0 deletions crates/committed/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<bool, anyhow::Error> {
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<regex::Regex> = once_cell::sync::Lazy::new(|| {
regex::Regex::new(r"^(wip\b|WIP\b|\[WIP\]|Draft\b|\[Draft\]|\(Draft\))").unwrap()
Expand Down
15 changes: 15 additions & 0 deletions crates/committed/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct Config {
pub(crate) line_length: Option<usize>,
pub(crate) style: Option<Style>,
pub(crate) allowed_types: Option<Vec<String>>,
pub(crate) allowed_scopes: Option<Vec<String>>,
pub(crate) merge_commit: Option<bool>,
}

Expand All @@ -46,6 +47,7 @@ impl Config {
line_length: Some(empty.line_length()),
style: Some(empty.style()),
allowed_types: Some(empty.allowed_types().map(|s| s.to_owned()).collect()),
allowed_scopes: Some(empty.allowed_scopes().map(|s| s.to_owned()).collect()),
merge_commit: Some(empty.merge_commit()),
}
}
Expand Down Expand Up @@ -84,6 +86,9 @@ impl Config {
if let Some(source) = source.allowed_types {
self.allowed_types = Some(source);
}
if let Some(source) = source.allowed_scopes {
self.allowed_scopes = Some(source);
}
if let Some(source) = source.merge_commit {
self.merge_commit = Some(source);
}
Expand Down Expand Up @@ -139,6 +144,16 @@ impl Config {
.unwrap_or_else(|| Box::new(DEFAULT_TYPES.iter().copied()))
}

pub(crate) fn allowed_scopes<'s>(&'s self) -> Box<dyn Iterator<Item = &str> + 's> {
self.allowed_scopes
.as_ref()
.map(|v| {
let b: Box<dyn Iterator<Item = &str>> = Box::new(v.iter().map(|s| s.as_str()));
b
})
.unwrap_or_else(|| Box::new([].iter().copied()))
}

pub(crate) fn merge_commit(&self) -> bool {
self.merge_commit.unwrap_or(true)
}
Expand Down
10 changes: 10 additions & 0 deletions crates/committed/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub(crate) enum Content<'s> {
Fixup(Fixup),
InvalidCommitFormat(InvalidCommitFormat),
DisallowedCommitType(DisallowedCommitType),
DisallowedCommitScope(DisallowedCommitScope),
MergeCommitDisallowed(MergeCommitDisallowed),
}

Expand Down Expand Up @@ -158,6 +159,15 @@ pub(crate) struct DisallowedCommitType {
pub(crate) allowed: Vec<String>,
}

#[derive(Clone, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(derive_more::Display)]
#[display("Disallowed scope `{}` used, please use one of {:?}", used, allowed)]
pub(crate) struct DisallowedCommitScope {
pub(crate) used: String,
pub(crate) allowed: Vec<String>,
}

#[derive(Clone, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(derive_more::Display)]
Expand Down
5 changes: 3 additions & 2 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ committed master..HEAD

- The range excludes the start commit
- This will Do The Right Thing even when `master` is ahead of when you
branched. `committed` will look for the merge-base between the range end
branched. `committed` will look for the merge-base between the range end
points.

### Commit Files and `stdin`
Expand Down Expand Up @@ -48,7 +48,7 @@ Configuration is read from the following (in precedence order)
### Config Fields

| Field | Argument | Format | Default | Description |
|------------------------|-------------------|----------------------|-----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------|
| ---------------------- | ----------------- | -------------------- | --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| ignore_author_re | \- | regx | (none) | Authors to ignore the commits for. Generally used with bots out of your control. |
| subject_length | \- | number | 50 | Number of columns the subject can occupy |
| line_length | \- | number | 72 | Number of columns any line with a break can occupy, including subject |
Expand All @@ -60,6 +60,7 @@ Configuration is read from the following (in precedence order)
| no_wip | \- | bool | true | Disallow WIP commits |
| style | \- | none, [conventional] | none | Commit style convention |
| allowed_types | \- | list of strings | fix, feat, chore, docs, style, refactor, perf, test | _(Conventional)_ Accepted commit types |
| allowed_scopes | \- | list of strings | none (all scopes allowed) | _(Conventional)_ Accepted commit scopes |
| merge_commit | --no-merge-commit | \- | true | Disallow merge commits. Argument is recommended over config file since there are times when merge-commits are wanted. |

[conventional]: https://www.conventionalcommits.org/
Loading