Skip to content

Commit

Permalink
feat(check): Support commit scope checking
Browse files Browse the repository at this point in the history
References: #42
Signed-off-by: Thomas Böhler <witcher@wiredspace.de>
  • Loading branch information
Witcher01 committed Mar 7, 2024
1 parent 3cd580e commit 802225f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
43 changes: 43 additions & 0 deletions cog.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
# A list of glob patterns describing branches on which semver bump are allowed
branch_whitelist = ["main"]

scopes = [
"README",
"bump",
"cd",
"changelog",
"check",
"ci",
"cli",
"coco",
"codecov",
"cog",
"commit",
"compat",
"conventional",
"coverage",
"edit",
"error",
"errors",
"formatting",
"git",
"git-hooks",
"github",
"gitignore",
"hook",
"hooks",
"lib",
"log",
"logo",
"monorepo",
"parser",
"partial",
"readme",
"revspec",
"scope",
"semver",
"settings",
"tag",
"template",
"tests",
"verify",
"version",
]

# Ignore merge commits from conventional commit checks
ignore_merge_commits = true

Expand Down
13 changes: 13 additions & 0 deletions src/conventional/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ impl Commit {
date,
};

if let (Some(scopes), Some(scope)) =
(&SETTINGS.commit_scopes(), &commit.conventional.scope)
{
if !scopes.contains(scope) {
return Err(Box::new(ConventionalCommitError::CommitScopeNotDefined {
oid: commit.oid.to_string(),
summary: format_summary(&commit.conventional),
scope: scope.to_string(),
author: commit.author,
}));
}
}

match &SETTINGS
.commit_types()
.get(&commit.conventional.commit_type)
Expand Down
26 changes: 26 additions & 0 deletions src/conventional/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub enum ConventionalCommitError {
commit_type: String,
author: String,
},
CommitScopeNotDefined {
oid: String,
summary: String,
scope: String,
author: String,
},
ParseError(ParseError),
}

Expand Down Expand Up @@ -126,6 +132,26 @@ impl Display for ConventionalCommitError {
commit_type = commit_type.red()
)
}
ConventionalCommitError::CommitScopeNotDefined {
oid,
summary,
scope,
author,
} => {
let error_header = "Errored commit: ".bold().red();
let author = format!("<{author}>").blue();
writeln!(
f,
"{}{} {}\n\t{message}'{summary}'\n\t{cause}Commit scope `{scope}` not allowed",
error_header,
oid,
author,
message = "Commit message:".yellow().bold(),
cause = "Error: ".yellow().bold(),
summary = summary.italic(),
scope = scope.red(),
)
}
ConventionalCommitError::ParseError(err) => {
let err = anyhow!(err.clone());
writeln!(f, "{err:?}")
Expand Down
10 changes: 10 additions & 0 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct Settings {
pub changelog: Changelog,
pub bump_profiles: HashMap<String, BumpProfile>,
pub packages: HashMap<String, MonoRepoPackage>,
pub scopes: Vec<String>,
}

impl Default for Settings {
Expand All @@ -70,6 +71,7 @@ impl Default for Settings {
changelog: Default::default(),
bump_profiles: Default::default(),
packages: Default::default(),
scopes: Default::default(),
}
}
}
Expand Down Expand Up @@ -315,6 +317,14 @@ impl Settings {
default_types
}

pub fn commit_scopes(&self) -> Option<Vec<String>> {
if self.scopes.is_empty() {
return None;
};

Some(self.scopes.clone())
}

fn default_commit_config() -> CommitsMetadata {
let mut default_types = HashMap::new();
default_types.insert(
Expand Down

0 comments on commit 802225f

Please sign in to comment.