|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ArtARTs36\MergeRequestLinter\Application\Rule\Rules; |
| 4 | + |
| 5 | +use ArtARTs36\MergeRequestLinter\Application\Rule\Definition\Definition; |
| 6 | +use ArtARTs36\MergeRequestLinter\Domain\Note\LintNote; |
| 7 | +use ArtARTs36\MergeRequestLinter\Domain\Request\MergeRequest; |
| 8 | +use ArtARTs36\MergeRequestLinter\Domain\Rule\RuleDefinition; |
| 9 | +use ArtARTs36\MergeRequestLinter\Shared\Attributes\Description; |
| 10 | +use ArtARTs36\MergeRequestLinter\Shared\Attributes\Example; |
| 11 | +use ArtARTs36\MergeRequestLinter\Shared\Attributes\Generic; |
| 12 | +use ArtARTs36\MergeRequestLinter\Shared\DataStructure\Arrayee; |
| 13 | + |
| 14 | +/** |
| 15 | + * The title must match conventional commit pattern https://www.conventionalcommits.org/en/v1.0.0. |
| 16 | + * @link https://www.conventionalcommits.org/en/v1.0.0 |
| 17 | + */ |
| 18 | +final class TitleConventionalRule extends NamedRule |
| 19 | +{ |
| 20 | + public const NAME = '@mr-linter/title_conventional'; |
| 21 | + |
| 22 | + private const REGEX = '/^([a-z]+){1}(\([\w\-\.]+\))?(!)?: ([\w ])+([\s\S]*)/mis'; |
| 23 | + |
| 24 | + private const DEFAULT_TYPES = [ |
| 25 | + 'build', |
| 26 | + 'chore', |
| 27 | + 'ci', |
| 28 | + 'docs', |
| 29 | + 'feat', |
| 30 | + 'fix', |
| 31 | + 'perf', |
| 32 | + 'refactor', |
| 33 | + 'revert', |
| 34 | + 'style', |
| 35 | + 'test', |
| 36 | + ]; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param Arrayee<int, string> $types |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + #[Generic(Generic::OF_STRING)] |
| 43 | + private readonly Arrayee $types, |
| 44 | + ) { |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param Arrayee<int, string>|null $types |
| 49 | + */ |
| 50 | + public static function make( |
| 51 | + #[Generic(Generic::OF_STRING)] |
| 52 | + #[Description('Commit types')] |
| 53 | + #[Example('build')] |
| 54 | + #[Example('chore')] |
| 55 | + #[Example('ci')] |
| 56 | + #[Example('docs')] |
| 57 | + #[Example('feat')] |
| 58 | + #[Example('fix')] |
| 59 | + #[Example('perf')] |
| 60 | + #[Example('refactor')] |
| 61 | + #[Example('revert')] |
| 62 | + #[Example('style')] |
| 63 | + #[Example('test')] |
| 64 | + ?Arrayee $types = null, |
| 65 | + ): self { |
| 66 | + $types ??= new Arrayee(self::DEFAULT_TYPES); |
| 67 | + |
| 68 | + return new self($types); |
| 69 | + } |
| 70 | + |
| 71 | + public function lint(MergeRequest $request): array |
| 72 | + { |
| 73 | + $matches = []; |
| 74 | + |
| 75 | + preg_match(self::REGEX, $request->title, $matches); |
| 76 | + |
| 77 | + if (! array_key_exists(1, $matches) || ! is_string($matches[1])) { |
| 78 | + return [new LintNote('The title must matches with conventional commit')]; |
| 79 | + } |
| 80 | + |
| 81 | + $type = $matches[1]; |
| 82 | + |
| 83 | + if (! $this->types->contains($type)) { |
| 84 | + return [new LintNote(sprintf('Title conventional: type "%s" is unknown', $type))]; |
| 85 | + } |
| 86 | + |
| 87 | + return []; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @codeCoverageIgnore |
| 92 | + */ |
| 93 | + public function getDefinition(): RuleDefinition |
| 94 | + { |
| 95 | + return new Definition(sprintf( |
| 96 | + 'The title must matches with conventional commit with types: [%s]', |
| 97 | + $this->types->implode(', '), |
| 98 | + )); |
| 99 | + } |
| 100 | +} |
0 commit comments