Skip to content

Commit eb5e71d

Browse files
authored
[Feature] Add support many configurations for Rule (#18)
* loading many configurations for rule * fix stat-analyse * add countEqualsAny * up schema * move Collection contract * Fix Collection * Add 'check' command * up version
1 parent e2a0829 commit eb5e71d

26 files changed

+557
-196
lines changed

README.MD

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ credentials:
177177
| make try-gitlab MR_ID=10 | Run MR-Linter on really merge request |
178178
| composer build-config-json-schema | Build JSON Schema for config |
179179
| composer deptrac | Run deptrac |
180+
| composer check | Run test, lint, stat-analyse, deptrac |
180181

181182
## Console output example
182183

composer.json

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
],
6262
"deptrac": [
6363
"./vendor/bin/deptrac"
64+
],
65+
"check": [
66+
"@lint",
67+
"@stat-analyse",
68+
"@deptrac",
69+
"@test"
6470
]
6571
}
6672
}

docs/Builder/ConfigJsonSchema/Generator.php

+22-26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema;
44

5+
use ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema\Schema\JsonSchema;
6+
57
class Generator
68
{
79
public function __construct(
@@ -11,37 +13,31 @@ public function __construct(
1113
//
1214
}
1315

14-
public function generate(): array
16+
public function generate(): JsonSchema
1517
{
16-
return [
17-
'$schema' => 'http://json-schema.org/draft-04/schema#',
18+
$schema = new JsonSchema();
19+
20+
$schema->addDefinition('rule_conditions', $this->operatorSchemaArrayGenerator->generate());
21+
22+
$schema->addProperty('rules', [
23+
'type' => 'object',
24+
'properties' => $this->ruleSchemaGenerator->generate($schema),
25+
]);
26+
27+
$schema->addProperty('credentials', [
1828
'type' => 'object',
1929
'properties' => [
20-
'rules' => [
21-
'type' => 'object',
22-
'properties' => $this->ruleSchemaGenerator->generate(),
30+
'gitlab_ci' => [
31+
'description' => 'Token',
32+
'type' => 'string',
2333
],
24-
'credentials' => [
25-
'type' => 'object',
26-
'properties' => [
27-
'gitlab_ci' => [
28-
'description' => 'Token',
29-
'type' => 'string',
30-
],
31-
'github_actions' => [
32-
'description' => 'Token',
33-
'type' => 'string',
34-
],
35-
],
34+
'github_actions' => [
35+
'description' => 'Token',
36+
'type' => 'string',
3637
],
3738
],
38-
'definitions' => [
39-
'rule_conditions' => $this->operatorSchemaArrayGenerator->generate(),
40-
],
41-
'required' => [
42-
'rules',
43-
'credentials',
44-
],
45-
];
39+
]);
40+
41+
return $schema;
4642
}
4743
}

docs/Builder/ConfigJsonSchema/JsonType.php

+6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
namespace ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema;
44

5+
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Arrayee;
6+
use ArtARTs36\MergeRequestLinter\Support\DataStructure\Set;
57
use ArtARTs36\Str\Str;
68

79
class JsonType
810
{
11+
public const OBJECT = 'object';
12+
913
private const MAP = [
1014
Str::class => 'string',
15+
Set::class => 'array',
16+
Arrayee::class => 'array',
1117
'int' => 'integer',
1218
'iterable' => 'array',
1319
'float' => 'number',

docs/Builder/ConfigJsonSchema/RuleSchemaGenerator.php

+32-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema;
44

55
use ArtARTs36\MergeRequestLinter\Contracts\Rule\RuleConstructorFinder;
6+
use ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema\Schema\JsonSchema;
67
use ArtARTs36\MergeRequestLinter\Rule\DefaultRules;
78
use ArtARTs36\MergeRequestLinter\Rule\Factory\Constructor\ConstructorFinder;
89
use ArtARTs36\MergeRequestLinter\Support\Reflector\Reflector;
10+
use ArtARTs36\Str\Facade\Str;
911

1012
class RuleSchemaGenerator
1113
{
@@ -15,27 +17,29 @@ public function __construct(
1517
//
1618
}
1719

18-
public function generate()
20+
public function generate(JsonSchema $jsonSchema): array
1921
{
2022
$rules = DefaultRules::map();
2123
$schema = [];
2224

2325
foreach ($rules as $ruleName => $rule) {
2426
$ruleSchema = [
25-
'type' => 'object',
2627
'description' => Reflector::findPHPDocSummary(new \ReflectionClass($rule)),
28+
];
29+
30+
$constructor = $this->constructorFinder->find($rule);
31+
$params = $constructor->params();
32+
33+
$definition = [
34+
'type' => 'object',
2735
'properties' => [
2836
'when' => [
2937
'$ref' => '#/definitions/rule_conditions',
3038
],
31-
]
39+
],
3240
];
3341

34-
$constructor = $this->constructorFinder->find($rule);
35-
$params = $constructor->params();
36-
3742
if (count($params) > 0) {
38-
$ruleSchema['required'] = [];
3943
foreach ($constructor->params() as $paramName => $paramType) {
4044
$typeSchema = [
4145
'type' => JsonType::to($paramType->name),
@@ -64,12 +68,30 @@ public function generate()
6468
}
6569
}
6670

67-
$ruleSchema['properties'][$paramName] = $typeSchema;
68-
69-
$ruleSchema['required'][] = $paramName;
71+
$definition['properties'][$paramName] = $typeSchema;
72+
$definition['required'][] = $paramName;
7073
}
7174
}
7275

76+
$definitionName = Str::replace('rules_properties_' . $ruleName, [
77+
'/' => '_',
78+
]);
79+
80+
$propertyDefinitionRef = $jsonSchema->addDefinition($definitionName, $definition);
81+
82+
$ruleSchema['oneOf'] = [
83+
[
84+
'$ref' => $propertyDefinitionRef,
85+
],
86+
[
87+
'type' => 'array',
88+
'items' => [
89+
'$ref' => $propertyDefinitionRef,
90+
],
91+
'minItems' => 1,
92+
],
93+
];
94+
7395
$schema[$ruleName] = $ruleSchema;
7496
}
7597

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema\Schema;
4+
5+
use ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema\JsonType;
6+
7+
class JsonSchema
8+
{
9+
private array $schema = [
10+
'$schema' => 'http://json-schema.org/draft-04/schema#',
11+
'type' => JsonType::OBJECT,
12+
'properties' => [],
13+
'definitions' => [],
14+
'required' => [],
15+
];
16+
17+
public function addDefinition(string $name, array $definition): string
18+
{
19+
$this->schema['definitions'][$name] = $definition;
20+
21+
return '#/definitions/' . $name;
22+
}
23+
24+
public function addProperty(string $name, array $prop): void
25+
{
26+
$this->schema['properties'][$name] = $prop;
27+
$this->schema['required'][] = $name;
28+
}
29+
30+
public function toJson(): string
31+
{
32+
return json_encode($this->schema, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
33+
}
34+
}

docs/Builder/build_config_json_schema.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
$jsonSchema = new \ArtARTs36\MergeRequestLinter\DocBuilder\ConfigJsonSchema\Generator();
66

7-
$json = json_encode($jsonSchema->generate(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
8-
9-
file_put_contents(__DIR__ . '/../../mr-linter-config-schema.json', $json);
7+
file_put_contents(__DIR__ . '/../../mr-linter-config-schema.json', $jsonSchema->generate()->toJson());

0 commit comments

Comments
 (0)