Skip to content

Commit

Permalink
feat: Support checking allowed authors
Browse files Browse the repository at this point in the history
Closes #353
  • Loading branch information
scop committed Nov 30, 2024
1 parent c64be6f commit 6b408e8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/committed/src/checks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::report;
use committed::Style;
use regex::Regex;

pub(crate) fn check_message(
source: report::Source<'_>,
Expand Down Expand Up @@ -371,3 +372,24 @@ pub(crate) fn check_merge_commit(
Ok(false)
}
}

pub(crate) fn check_allowed_author(
source: report::Source<'_>,
commit: &git2::Commit<'_>,
re: &Regex,
report: report::Report,
) -> Result<bool, anyhow::Error> {
let author = commit.author().to_string();
if !re.is_match(&author) {
report(report::Message::error(
source,
report::DisallowedAuthor {
used: author,
allowed: re.as_str(),
},
));
Ok(true)
} else {
Ok(false)
}
}
9 changes: 9 additions & 0 deletions crates/committed/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) struct Config {
pub(crate) allowed_types: Option<Vec<String>>,
pub(crate) allowed_scopes: Option<Vec<String>>,
pub(crate) merge_commit: Option<bool>,
pub(crate) allowed_author_re: Option<String>,
}

impl Config {
Expand All @@ -49,6 +50,7 @@ impl Config {
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()),
allowed_author_re: empty.allowed_author_re().map(|s| s.to_owned()),
}
}

Expand Down Expand Up @@ -92,6 +94,9 @@ impl Config {
if let Some(source) = source.merge_commit {
self.merge_commit = Some(source);
}
if let Some(source) = source.allowed_author_re {
self.allowed_author_re = Some(source);
}
}

pub(crate) fn ignore_author_re(&self) -> Option<&str> {
Expand Down Expand Up @@ -157,4 +162,8 @@ impl Config {
pub(crate) fn merge_commit(&self) -> bool {
self.merge_commit.unwrap_or(true)
}

pub(crate) fn allowed_author_re(&self) -> Option<&str> {
self.allowed_author_re.as_deref()
}
}
13 changes: 13 additions & 0 deletions crates/committed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ fn run() -> proc_exit::ExitResult {
}
false
};
let allowed_author_re = config
.allowed_author_re()
.map(regex::Regex::new)
.transpose()
.with_code(proc_exit::sysexits::CONFIG_ERR)?;

let mut failed = false;
if let Some(output_path) = options.dump_config.as_ref() {
Expand Down Expand Up @@ -240,6 +245,10 @@ fn run() -> proc_exit::ExitResult {
log::trace!("Ignoring {}", source);
} else {
log::trace!("Processing {}", source);
if let Some(re) = allowed_author_re.as_ref() {
failed |= checks::check_allowed_author(source, &commit, re, report)
.with_code(UNKNOWN_ERR)?;
}
let message = commit.message().unwrap();
failed |= checks::check_message(source, message, &config, report)
.with_code(UNKNOWN_ERR)?;
Expand Down Expand Up @@ -272,6 +281,10 @@ fn run() -> proc_exit::ExitResult {
log::trace!("Ignoring {}", source);
} else {
log::trace!("Processing {}", source);
if let Some(re) = allowed_author_re.as_ref() {
failed |= checks::check_allowed_author(source, &commit, re, report)
.with_code(UNKNOWN_ERR)?;
}
let message = commit.message().unwrap();
failed |=
checks::check_message(source, message, &config, report).with_code(UNKNOWN_ERR)?;
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 @@ -68,6 +68,7 @@ pub(crate) enum Content<'s> {
DisallowedCommitType(DisallowedCommitType),
DisallowedCommitScope(DisallowedCommitScope),
MergeCommitDisallowed(MergeCommitDisallowed),
DisallowedAuthor(DisallowedAuthor<'s>),
}

#[derive(Clone, Debug, serde::Serialize)]
Expand Down Expand Up @@ -180,6 +181,15 @@ pub(crate) struct MergeCommitDisallowed {}
#[display("Empty commits are disallowed")]
pub(crate) struct EmptyCommit {}

#[derive(Clone, Debug, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[derive(derive_more::Display)]
#[display("Disallowed author `{}` used, please use one matching `{}`", used, allowed)]
pub(crate) struct DisallowedAuthor<'s> {
pub(crate) used: String,
pub(crate) allowed: &'s str,
}

pub(crate) type Report = fn(msg: Message<'_>);

pub(crate) fn print_silent(_: Message<'_>) {}
Expand Down
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Configuration is read from the following (in precedence order)
| Field | Argument | Format | Default | Description |
| ---------------------- | ----------------- | -------------------- | --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------- |
| ignore_author_re | \- | regx | (none) | Authors to ignore the commits for. Generally used with bots out of your control. |
| allowed_author_re | \- | regx | (none) | Require commit author to match this regular expression |
| 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 |
| hard_line_length | \- | number | 0 (none) | Max number of columns any line can occupy. |
Expand Down

0 comments on commit 6b408e8

Please sign in to comment.