2023-11-09 16:31:17 +00:00
|
|
|
from gitlint.rules import LineRule, RuleViolation, CommitMessageTitle
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class TitleStartsWithComponent(LineRule):
|
2023-11-17 15:06:13 +00:00
|
|
|
"""A rule to enforce valid commit message title
|
|
|
|
|
|
|
|
Valid title format:
|
|
|
|
component1[, component2, componentN]: submodule: summary
|
|
|
|
|
|
|
|
Title should have at least one component
|
|
|
|
Components are separated by comma+space: ", "
|
|
|
|
Components are validated to be in valid_components
|
|
|
|
Components list is ended by a colon
|
|
|
|
Submodules are not validated
|
|
|
|
|
2023-11-09 16:31:17 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# A rule MUST have a human friendly name
|
|
|
|
name = "title-has-valid-component"
|
|
|
|
|
|
|
|
# A rule MUST have a *unique* id.
|
|
|
|
# We recommend starting with UL (for User-defined Line-rule)
|
|
|
|
id = "UL1"
|
|
|
|
|
|
|
|
# A line-rule MUST have a target (not required for CommitRules).
|
|
|
|
target = CommitMessageTitle
|
|
|
|
|
|
|
|
def validate(self, line, _commit):
|
2023-11-17 15:06:13 +00:00
|
|
|
valid_components = (
|
2023-11-09 16:31:17 +00:00
|
|
|
'api_client',
|
|
|
|
'arch',
|
|
|
|
'block',
|
|
|
|
'build',
|
|
|
|
'ch-remote',
|
|
|
|
'ci',
|
|
|
|
'devices',
|
|
|
|
'docs',
|
|
|
|
'event_monitor',
|
|
|
|
'fuzz',
|
|
|
|
'github',
|
|
|
|
'gitignore',
|
2023-11-16 18:59:38 +00:00
|
|
|
'gitlint',
|
2023-11-09 16:31:17 +00:00
|
|
|
'hypervisor',
|
|
|
|
'Jenkinsfile',
|
|
|
|
'misc',
|
|
|
|
'net_gen',
|
|
|
|
'net_util',
|
|
|
|
'option_parser',
|
|
|
|
'pci',
|
|
|
|
'performance-metrics',
|
|
|
|
'rate_limiter',
|
|
|
|
'README',
|
|
|
|
'resources',
|
|
|
|
'scripts',
|
|
|
|
'serial_buffer',
|
|
|
|
'test_data',
|
|
|
|
'test_infra',
|
|
|
|
'tests',
|
|
|
|
'tpm',
|
|
|
|
'tracer',
|
|
|
|
'vhost_user_block',
|
|
|
|
'vhost_user_net',
|
|
|
|
'virtio-devices',
|
|
|
|
'vm-allocator',
|
|
|
|
'vm-device',
|
|
|
|
'vmm',
|
|
|
|
'vm-migration',
|
2023-11-17 15:06:13 +00:00
|
|
|
'vm-virtio')
|
2023-11-09 16:31:17 +00:00
|
|
|
|
2023-11-17 15:06:13 +00:00
|
|
|
ptrn_title = re.compile(r'^(.+?):\s(.+)$')
|
|
|
|
match = ptrn_title.match(line)
|
2023-11-09 16:31:17 +00:00
|
|
|
|
|
|
|
if not match:
|
|
|
|
self.log.debug("Invalid commit title {}", line)
|
|
|
|
return [RuleViolation(self.id, "Commit title does not comply with "
|
|
|
|
"rule: 'component: change summary'")]
|
2023-11-17 15:06:13 +00:00
|
|
|
components = match.group(1)
|
2023-11-09 16:31:17 +00:00
|
|
|
summary = match.group(2)
|
2023-11-17 15:06:13 +00:00
|
|
|
self.log.debug(f"\nComponents: {components}\nSummary: {summary}")
|
|
|
|
|
|
|
|
ptrn_components = re.compile(r',\s')
|
|
|
|
components_list = re.split(ptrn_components, components)
|
|
|
|
self.log.debug("components list: %s" % components_list)
|
2023-11-09 16:31:17 +00:00
|
|
|
|
2023-11-17 15:06:13 +00:00
|
|
|
for component in components_list:
|
|
|
|
if component not in valid_components:
|
|
|
|
return [RuleViolation(self.id,
|
|
|
|
f"Invalid component: {component}, "
|
|
|
|
"\nValid components are: {}".format(
|
|
|
|
" ".join(valid_components)))]
|