Skip to content

Commit

Permalink
[Feature] Add Rule "description template" (#65)
Browse files Browse the repository at this point in the history
* Add DescriptionTemplateRule

* remove has_any_labels_of

* up doc

* up doc

* add release tag placeholder

* clean

* up self config

* up version
  • Loading branch information
ArtARTs36 committed Sep 12, 2023
1 parent 872ab1a commit cb205ae
Show file tree
Hide file tree
Showing 23 changed files with 270 additions and 168 deletions.
41 changes: 18 additions & 23 deletions .mr-linter.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
rules:
"@mr-linter/has_any_labels_of":
"@mr-linter/has_any_labels":
labels:
- Feature
- Bug
- Docs
- Tests
- Optimization

"@mr-linter/description_template":
- definition: description must have list of features
template: |
## Added
{text_multiline}
when:
labels:
has: "Feature"

- definition: description must have list of fixed bugs
template: |
## Fixed
{text_multiline}
when:
labels:
has: "Bug"

"@mr-linter/title_must_starts_with_any_prefix":
prefixes:
- '[Feature]'
Expand Down Expand Up @@ -53,28 +70,6 @@ rules:
sourceBranch:
isKebabCase: true

- definition: "Labels must be in StudlyCase"
rules:
labels:
$all:
isStudlyCase: true

- definition: "Description must have list of fixed bugs"
rules:
descriptionMarkdown:
containsHeading2: "Fixed"
when:
labels:
has: "Bug"

- definition: "Description must have list of features"
rules:
descriptionMarkdown:
containsHeading2: "Added"
when:
labels:
has: "Feature"

- definition: "Split feature / bug PR"
rules:
labels:
Expand Down
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ This file contains changelogs.

## [Unreleased](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.19.1..master)

## [v0.20.0 (2023-09-12)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.19.1..0.20.0)

### Added
* Added Rule `@mr-linter/description_template` for check description on defined template

### Removed
* Removed rule `@mr-linter/has_any_labels_of`

[💾 Assets](https://github.com/ArtARTs36/php-merge-request-linter/releases/tag/0.20.0)

## [v0.19.1 (2023-09-12)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.19.0..0.19.1)

### Added
Expand Down Expand Up @@ -181,7 +191,6 @@ This file contains changelogs.

[💾 Assets](https://github.com/ArtARTs36/php-merge-request-linter/releases/tag/0.14.1)


-----------------------------------------------------------------

## [v0.14.0 (2023-05-27)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.13.1..0.14.0)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ docker-pub-try:
-v "${PWD}/.mr-linter.yml:/app/.mr-linter.yml:ro" \
artarts36/merge-request-linter:${MR_LINTER_VERSION} lint

docs: ## Build documentation
docs: docker-build ## Build documentation
docker run \
--rm \
--volume ./:/app \
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"require": {
"php": "^8.2",
"artarts36/str": "^2.5.0",
"artarts36/str": "^2.6.0",
"symfony/console": "^4.0 | ^5.0 | ^6.0",
"guzzlehttp/psr7": "^2",
"artarts36/local-file-system": "^0.1.2",
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ You can also specify a list of allowed labels.

```yaml
rules:
"@mr-linter/has_any_labels_of":
"@mr-linter/has_any_labels":
labels:
- Feature
- Bug
Expand Down
20 changes: 13 additions & 7 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ The following rules are available:
| @mr-linter/description_contains_links_of_all_domains | Merge Request must contain links of all {domains}. |
| @mr-linter/description_not_empty | The description must be filled. |
| @mr-linter/has_all_labels | Merge Request must have all {labels} |
| @mr-linter/has_any_labels | Merge Request must have any labels. |
| @mr-linter/has_any_labels_of | Merge Request must have any {labels}. |
| @mr-linter/has_any_labels | Merge Request must have any {labels}. |
| @mr-linter/jira/has_issue_link | The description must have a link to Jira on a {domain} with {projectCode}. |
| @mr-linter/youtrack/has_issue_link | The description must have a link to YouTrack issue on a {domain} with {projectCode}. |
| @mr-linter/title_must_starts_with_any_prefix | The title must starts with any {prefixes} |
Expand All @@ -23,6 +22,7 @@ The following rules are available:
| @mr-linter/no_ssh_keys | Prevent ssh keys from being included in the merge request. |
| @mr-linter/disable_file_extensions | Disable adding files of certain extensions. |
| @mr-linter/title_conventional | The title must match conventional commit pattern https://www.conventionalcommits.org/en/v1.0.0. |
| @mr-linter/description_template | The description must match defined template. Available placeholders: {text}, {text_multiline}, {number}, {word}, {release_tag} |

## Global parameters

Expand Down Expand Up @@ -79,11 +79,6 @@ Merge Request must have all {labels}

## @mr-linter/has_any_labels

Merge Request must have any labels.


## @mr-linter/has_any_labels_of

Merge Request must have any {labels}.

### Parameters
Expand Down Expand Up @@ -223,3 +218,14 @@ The title must match conventional commit pattern https://www.conventionalcommits
| types | Commit types | array of strings | false | `[build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]` | "build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test" |
| task | Check if title contains task number | object | false | NULL | |
| task.projectCodes | Project codes. Empty list allowed for any projects | array of strings | false | | "ABC" |

## @mr-linter/description_template

The description must match defined template. Available placeholders: {text}, {text_multiline}, {number}, {word}, {release_tag}

### Parameters

| Name | Description | Type | Required | Default value |
|------|-------------|------|----------|---------------|
| template | Template for description | string | true | |
| definition | Custom definition | string | false | NULL |
71 changes: 41 additions & 30 deletions mr-linter-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
]
},
"@mr-linter/has_any_labels": {
"description": "Merge Request must have any labels.",
"description": "Merge Request must have any {labels}.",
"oneOf": [
{
"$ref": "#/definitions/rules_properties_@mr-linter_has_any_labels"
Expand All @@ -95,21 +95,6 @@
}
]
},
"@mr-linter/has_any_labels_of": {
"description": "Merge Request must have any {labels}.",
"oneOf": [
{
"$ref": "#/definitions/rules_properties_@mr-linter_has_any_labels_of"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/rules_properties_@mr-linter_has_any_labels_of"
},
"minItems": 1
}
]
},
"@mr-linter/jira/has_issue_link": {
"description": "The description must have a link to Jira on a {domain} with {projectCode}.",
"oneOf": [
Expand Down Expand Up @@ -304,6 +289,21 @@
"minItems": 1
}
]
},
"@mr-linter/description_template": {
"description": "The description must match defined template. Available placeholders: {text}, {text_multiline}, {number}, {word}, {release_tag}",
"oneOf": [
{
"$ref": "#/definitions/rules_properties_@mr-linter_description_template"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/rules_properties_@mr-linter_description_template"
},
"minItems": 1
}
]
}
},
"additionalProperties": false
Expand Down Expand Up @@ -6273,20 +6273,6 @@
]
},
"rules_properties_@mr-linter_has_any_labels": {
"type": "object",
"properties": {
"critical": {
"type": "boolean",
"default": true
},
"when": {
"description": "Conditions that determine whether the rule should run.",
"$ref": "#/definitions/rule_conditions"
}
},
"additionalProperties": false
},
"rules_properties_@mr-linter_has_any_labels_of": {
"type": "object",
"properties": {
"critical": {
Expand Down Expand Up @@ -6725,6 +6711,31 @@
},
"additionalProperties": false
},
"rules_properties_@mr-linter_description_template": {
"type": "object",
"properties": {
"critical": {
"type": "boolean",
"default": true
},
"when": {
"description": "Conditions that determine whether the rule should run.",
"$ref": "#/definitions/rule_conditions"
},
"template": {
"type": "string",
"description": "Template for description"
},
"definition": {
"type": "string",
"description": "Custom definition"
}
},
"additionalProperties": false,
"required": [
"template"
]
},
"notifications_channel_telegram_bot": {
"properties": {
"type": {
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Rule/Rules/DefaultRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ final class DefaultRules
DescriptionNotEmptyRule::NAME => DescriptionNotEmptyRule::class,
HasAllLabelsOfRule::NAME => HasAllLabelsOfRule::class,
HasAnyLabelsRule::NAME => HasAnyLabelsRule::class,
HasAnyLabelsOfRule::NAME => HasAnyLabelsOfRule::class,
HasLinkToJiraTaskRule::NAME => HasLinkToJiraTaskRule::class,
HasLinkToYouTrackIssueRule::NAME => HasLinkToYouTrackIssueRule::class,
TitleStartsWithAnyPrefixRule::NAME => TitleStartsWithAnyPrefixRule::class,
Expand All @@ -33,6 +32,7 @@ final class DefaultRules
NoSshKeysRule::NAME => NoSshKeysRule::class,
DisableFileExtensionsRule::NAME => DisableFileExtensionsRule::class,
TitleConventionalRule::NAME => TitleConventionalRule::class,
DescriptionTemplateRule::NAME => DescriptionTemplateRule::class,
];

/**
Expand Down
50 changes: 50 additions & 0 deletions src/Application/Rule/Rules/DescriptionTemplateRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace ArtARTs36\MergeRequestLinter\Application\Rule\Rules;

use ArtARTs36\MergeRequestLinter\Application\Rule\Definition\Definition;
use ArtARTs36\MergeRequestLinter\Domain\Request\MergeRequest;
use ArtARTs36\MergeRequestLinter\Domain\Rule\RuleDefinition;
use ArtARTs36\MergeRequestLinter\Shared\Attributes\Description;
use ArtARTs36\Str\Facade\Str;
use ArtARTs36\Str\Template\TemplatePlaceholders;

#[Description('The description must match defined template. Available placeholders: {text}, {text_multiline}, {number}, {word}, {release_tag}')]
final class DescriptionTemplateRule extends AbstractRule
{
public const NAME = '@mr-linter/description_template';
private const TAG_PLACEHOLDER_REGEX = '(v?[0-9]+.[0-9]+.[0-9]+)(\-(\w+))?';

public function __construct(
#[Description('Template for description')]
private readonly string $template,
#[Description('Custom definition')]
private readonly ?string $definition = null,
) {
}

protected function doLint(MergeRequest $request): bool
{
return $request
->descriptionMarkdown
->str()
->trim()
->replace([
"\r\n" => "\n",
])
->matchTemplate(
Str::trim($this->template),
TemplatePlaceholders::default()
->add('release_tag', self::TAG_PLACEHOLDER_REGEX),
)
->matched;
}

public function getDefinition(): RuleDefinition
{
return new Definition(sprintf(
'Description template: %s',
$this->definition ?? 'the description must match template',
));
}
}
27 changes: 0 additions & 27 deletions src/Application/Rule/Rules/HasAnyLabelsOfRule.php

This file was deleted.

Loading

0 comments on commit cb205ae

Please sign in to comment.