Skip to content

Commit 84e44f5

Browse files
authored
[Feature] Add Rule "HasChanges" (#15)
* Add Rule "HasChanges" * Add ApplicationFactory * Add InfoCommand * Add conditions "countEquals", "countNotEquals"
1 parent 936d92e commit 84e44f5

File tree

106 files changed

+1932
-327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1932
-327
lines changed

.mr-linter.php

-34
This file was deleted.

.mr-linter.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
rules:
2+
"@mr-linter/has_any_labels_of":
3+
labels:
4+
- Feature
5+
- Bug
6+
- Docs
7+
- Tests
8+
9+
"@mr-linter/title_must_starts_with_any_prefix":
10+
prefixes:
11+
- '[Feature]'
12+
- '[Bug]'
13+
- '[Docs]'
14+
- '[Tests]'
15+
16+
"@mr-linter/description_not_empty": {}
17+
18+
"@mr-linter/has_changes":
19+
changes:
20+
- file: "src/Console/Application/Application.php"
21+
updatedPhpConstant: "VERSION"
22+
when:
23+
targetBranch:
24+
equals: "master"
25+
26+
credentials:
27+
github_actions: "env(MR_LINTER_GITHUB_HTTP_TOKEN)"

bin/mr-linter

+2-35
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33

4-
use ArtARTs36\FileSystem\Local\LocalFileSystem;
5-
use ArtARTs36\MergeRequestLinter\CI\System\DefaultSystems;
6-
use ArtARTs36\MergeRequestLinter\Configuration\Loader\ArrayConfigLoaderFactory;
7-
use ArtARTs36\MergeRequestLinter\Configuration\Loader\CompositeLoader;
8-
use ArtARTs36\MergeRequestLinter\Configuration\Loader\ConfigLoaderProxy;
9-
use ArtARTs36\MergeRequestLinter\Configuration\Loader\PhpConfigLoader;
10-
use ArtARTs36\MergeRequestLinter\Configuration\Resolver\ConfigResolver;
11-
use ArtARTs36\MergeRequestLinter\Configuration\Resolver\PathResolver;
12-
use ArtARTs36\MergeRequestLinter\Console\DumpCommand;
13-
use ArtARTs36\MergeRequestLinter\Console\InstallCommand;
14-
use ArtARTs36\MergeRequestLinter\Console\LintCommand;
15-
use ArtARTs36\MergeRequestLinter\Environment\LocalEnvironment;
16-
use ArtARTs36\MergeRequestLinter\Linter\Runner\RunnerFactory;
17-
use Symfony\Component\Console\Application;
4+
use ArtARTs36\MergeRequestLinter\Console\Application\ApplicationFactory;
185

196
$loaded = false;
207

@@ -38,26 +25,6 @@ if ($loaded === false) {
3825
exit(1);
3926
}
4027

41-
$application = new Application('Merge Request Linter', '0.5.1');
42-
43-
$filesystem = new LocalFileSystem();
44-
$environment = new LocalEnvironment();
45-
$ciSystemsMap = DefaultSystems::map();
46-
$runnerFactory = new RunnerFactory($environment, $ciSystemsMap);
47-
48-
$arrayConfigLoaderFactory = new ArrayConfigLoaderFactory($filesystem, $environment);
49-
50-
$configLoader = new CompositeLoader([
51-
'php' => new PhpConfigLoader($filesystem),
52-
'json' => new ConfigLoaderProxy(static fn () => $arrayConfigLoaderFactory->create('json')),
53-
'yaml' => new ConfigLoaderProxy(static fn () => $arrayConfigLoaderFactory->create('yaml')),
54-
'yml' => new ConfigLoaderProxy(static fn () => $arrayConfigLoaderFactory->create('yaml')),
55-
]);
56-
57-
$configResolver = new ConfigResolver(new PathResolver($filesystem), $configLoader);
58-
59-
$application->add(new LintCommand($configResolver, $runnerFactory));
60-
$application->add(new InstallCommand());
61-
$application->add(new DumpCommand($configResolver));
28+
$application = (new ApplicationFactory())->create();
6229

6330
$application->run();

docs/Builder/ConfigJsonSchema/RuleSchemaGenerator.php

+20-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,26 @@ public function generate()
4242
];
4343

4444
if ($paramType->isGeneric()) {
45-
$typeSchema['items'] = [
46-
[
47-
'type' => $paramType->generic,
48-
],
49-
];
45+
if ($paramType->isGenericOfObject()) {
46+
$genericProps = [];
47+
48+
foreach (Reflector::mapPropertyTypes($paramType->generic) as $propertyName => $propertyType) {
49+
$genericProps[$propertyName] = [
50+
'type' => JsonType::to($propertyType->name),
51+
];
52+
}
53+
54+
$typeSchema['items'] = [
55+
'type' => 'object',
56+
'properties' => $genericProps,
57+
];
58+
} else {
59+
$typeSchema['items'] = [
60+
[
61+
'type' => $paramType->generic,
62+
],
63+
];
64+
}
5065
}
5166

5267
$ruleSchema['properties'][$paramName] = $typeSchema;

docs/rules.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Currently is available that rules:
1111
| 5 | @mr-linter/has_all_labels | ArtARTs36\MergeRequestLinter\Rule\HasAllLabelsOfRule | Merge Request must have all {labels} |
1212
| 6 | @mr-linter/has_any_labels_of | ArtARTs36\MergeRequestLinter\Rule\HasAnyLabelsOfRule | Merge Request must have any {labels}. |
1313
| 7 | @mr-linter/has_any_labels | ArtARTs36\MergeRequestLinter\Rule\HasAnyLabelsRule | Merge Request must have any labels. |
14-
| 8 | @mr-linter/jira/has_issue_link | ArtARTs36\MergeRequestLinter\Rule\HasLinkToJiraTaskRule | The description must have a link to Jira on a {domain} with {projectCode}. |
15-
| 9 | @mr-linter/youtrack/has_issue_link | ArtARTs36\MergeRequestLinter\Rule\HasLinkToYouTrackIssueRule | The description must have a link to YouTrack issue on a {domain} with {projectCode}. |
16-
| 10 | @mr-linter/title_matches_expression | ArtARTs36\MergeRequestLinter\Rule\TitleMatchesExpressionRule | The title must match the expression: {regex} |
17-
| 11 | @mr-linter/title_must_starts_with_any_prefix | ArtARTs36\MergeRequestLinter\Rule\TitleStartsWithAnyPrefixRule | The title must starts with any {prefixes} |
14+
| 8 | @mr-linter/has_changes | ArtARTs36\MergeRequestLinter\Rule\HasChangesRule | Merge Request must have changes in {files}. |
15+
| 9 | @mr-linter/jira/has_issue_link | ArtARTs36\MergeRequestLinter\Rule\HasLinkToJiraTaskRule | The description must have a link to Jira on a {domain} with {projectCode}. |
16+
| 10 | @mr-linter/youtrack/has_issue_link | ArtARTs36\MergeRequestLinter\Rule\HasLinkToYouTrackIssueRule | The description must have a link to YouTrack issue on a {domain} with {projectCode}. |
17+
| 11 | @mr-linter/title_matches_expression | ArtARTs36\MergeRequestLinter\Rule\TitleMatchesExpressionRule | The title must match the expression: {regex} |
18+
| 12 | @mr-linter/title_must_starts_with_any_prefix | ArtARTs36\MergeRequestLinter\Rule\TitleStartsWithAnyPrefixRule | The title must starts with any {prefixes} |

mr-linter-config-schema.json

+61-44
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,38 @@
185185
"required": [
186186
"prefixes"
187187
]
188+
},
189+
"@mr-linter/has_changes": {
190+
"type": "object",
191+
"description": "Merge Request must have changes in {files}.",
192+
"properties": {
193+
"when": {
194+
"$ref": "#/definitions/rule_conditions"
195+
},
196+
"changes": {
197+
"type": "array",
198+
"items": {
199+
"type": "object",
200+
"properties": {
201+
"file": {
202+
"type": "string"
203+
},
204+
"contains": {
205+
"type": "string"
206+
},
207+
"containsRegex": {
208+
"type": "string"
209+
},
210+
"updatedPhpConstant": {
211+
"type": "string"
212+
}
213+
}
214+
}
215+
}
216+
},
217+
"required": [
218+
"changes"
219+
]
188220
}
189221
}
190222
},
@@ -312,6 +344,14 @@
312344
"description": "Check the maximum number of elements in a field.",
313345
"type": "integer"
314346
},
347+
"countEquals": {
348+
"description": "Check count equals.",
349+
"type": "integer"
350+
},
351+
"countNotEquals": {
352+
"description": "Check count not equals.",
353+
"type": "integer"
354+
},
315355
"has": {
316356
"description": "Check if an array contains some value.",
317357
"type": "string"
@@ -446,50 +486,6 @@
446486
}
447487
}
448488
},
449-
"changedFilesCount": {
450-
"type": "object",
451-
"properties": {
452-
"equals": {
453-
"description": "Check if value are equal.",
454-
"type": "integer"
455-
},
456-
"=": {
457-
"description": "Check if value are equal.",
458-
"type": "integer"
459-
},
460-
"lte": {
461-
"description": "Check number is equal to or less than.",
462-
"type": "integer"
463-
},
464-
"<=": {
465-
"description": "Check number is equal to or less than.",
466-
"type": "integer"
467-
},
468-
"gte": {
469-
"description": "Check if a number is greater than or less than.",
470-
"type": "integer"
471-
},
472-
">=": {
473-
"description": "Check if a number is greater than or less than.",
474-
"type": "integer"
475-
},
476-
"notEquals": {
477-
"description": "Check if value are not equal.",
478-
"type": "integer"
479-
},
480-
"!=": {
481-
"description": "Check if value are not equal.",
482-
"type": "integer"
483-
},
484-
"equalsAny": {
485-
"description": "Check if the field is equal to one of the values.",
486-
"type": "array",
487-
"items": {
488-
"type": "string"
489-
}
490-
}
491-
}
492-
},
493489
"author.login": {
494490
"type": "object",
495491
"properties": {
@@ -579,6 +575,27 @@
579575
"type": "boolean"
580576
}
581577
}
578+
},
579+
"changes": {
580+
"type": "object",
581+
"properties": {
582+
"countMin": {
583+
"description": "Check the minimum number of elements in a field.",
584+
"type": "integer"
585+
},
586+
"countMax": {
587+
"description": "Check the maximum number of elements in a field.",
588+
"type": "integer"
589+
},
590+
"countEquals": {
591+
"description": "Check count equals.",
592+
"type": "integer"
593+
},
594+
"countNotEquals": {
595+
"description": "Check count not equals.",
596+
"type": "integer"
597+
}
598+
}
582599
}
583600
}
584601
}

src/CI/System/DefaultSystems.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
use ArtARTs36\MergeRequestLinter\CI\System\Github\GithubActions;
66
use ArtARTs36\MergeRequestLinter\CI\System\Gitlab\GitlabCi;
77
use ArtARTs36\MergeRequestLinter\Contracts\CI\CiSystem;
8-
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Map;
8+
use ArtARTs36\MergeRequestLinter\Contracts\DataStructure\Map;
9+
use ArtARTs36\MergeRequestLinter\Support\DataStructure\ArrayMap;
910

1011
final class DefaultSystems
1112
{
@@ -20,7 +21,7 @@ final class DefaultSystems
2021
*/
2122
public static function map(): Map
2223
{
23-
return new Map(self::$map);
24+
return new ArrayMap(self::$map);
2425
}
2526

2627
private function __construct()

src/CI/System/Github/GithubActions.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
namespace ArtARTs36\MergeRequestLinter\CI\System\Github;
44

55
use ArtARTs36\MergeRequestLinter\CI\System\Github\Env\GithubEnvironment;
6+
use ArtARTs36\MergeRequestLinter\CI\System\Github\GraphQL\PullRequest\PullRequest;
67
use ArtARTs36\MergeRequestLinter\CI\System\Github\GraphQL\PullRequest\PullRequestInput;
7-
use ArtARTs36\MergeRequestLinter\CI\System\InteractsWithResponse;
88
use ArtARTs36\MergeRequestLinter\Contracts\CI\CiSystem;
99
use ArtARTs36\MergeRequestLinter\Contracts\CI\GithubClient;
10+
use ArtARTs36\MergeRequestLinter\Contracts\DataStructure\Map;
1011
use ArtARTs36\MergeRequestLinter\Exception\EnvironmentVariableNotFound;
1112
use ArtARTs36\MergeRequestLinter\Request\Data\Author;
13+
use ArtARTs36\MergeRequestLinter\Request\Data\Diff\Diff;
1214
use ArtARTs36\MergeRequestLinter\Request\Data\MergeRequest;
15+
use ArtARTs36\MergeRequestLinter\Support\DataStructure\ArrayMap;
16+
use ArtARTs36\MergeRequestLinter\Support\DataStructure\MapProxy;
1317
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Set;
1418
use ArtARTs36\Str\Str;
1519

1620
class GithubActions implements CiSystem
1721
{
18-
use InteractsWithResponse;
19-
2022
public const NAME = 'github_actions';
2123

2224
public function __construct(
@@ -60,10 +62,29 @@ public function getCurrentlyMergeRequest(): MergeRequest
6062
$pullRequest->hasConflicts(),
6163
Str::make($pullRequest->headRefName),
6264
Str::make($pullRequest->baseRefName),
63-
$pullRequest->changedFiles,
6465
new Author($pullRequest->authorLogin),
6566
$pullRequest->isDraft,
6667
$pullRequest->canMerge(),
68+
$this->mapChanges($pullRequest),
6769
);
6870
}
71+
72+
/**
73+
* @return Map<string, \ArtARTs36\MergeRequestLinter\Request\Data\Change>
74+
*/
75+
private function mapChanges(PullRequest $request): Map
76+
{
77+
return new MapProxy(function () use ($request) {
78+
$changes = [];
79+
80+
foreach ($request->changes as $change) {
81+
$changes[$change->filename] = new \ArtARTs36\MergeRequestLinter\Request\Data\Change(
82+
$change->filename,
83+
new Diff($change->diff),
84+
);
85+
}
86+
87+
return new ArrayMap($changes);
88+
}, $request->changedFiles);
89+
}
6990
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\CI\System\Github\GraphQL\Change;
4+
5+
use ArtARTs36\MergeRequestLinter\Request\Data\Diff\Line;
6+
7+
class Change
8+
{
9+
/**
10+
* @param array<Line> $diff
11+
*/
12+
public function __construct(
13+
public readonly string $filename,
14+
public readonly array $diff,
15+
public readonly Status $status,
16+
) {
17+
//
18+
}
19+
}

0 commit comments

Comments
 (0)