Skip to content

Commit 1750a0a

Browse files
authored
[Feature] [Logger] Print date and time in logs (#47)
* print date time * add notes to event rule was failed * Add ApplicationNotCreatedException * add exceptions * check tz in LocalClock * fix lint * update docs * update doc * update docs: add event descriptions * fix tests * add test * add test * add test * Add covers * add test * update version
1 parent 5dfc7e5 commit 1750a0a

File tree

28 files changed

+267
-31
lines changed

28 files changed

+267
-31
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ This file contains changelogs.
66

77
-----------------------------------------------------------------
88

9+
## [v0.13.1 (2023-05-26)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.13.0..0.13.1)
10+
11+
### Added
12+
* [Logger] Print date and time in logs
13+
* Added array of notes in event `rule_was_failed`
14+
15+
### Internal
16+
* [Docs] Added event descriptions into config JSON Schema
17+
18+
[💾 Assets](https://github.com/ArtARTs36/php-merge-request-linter/releases/tag/0.13.1)
19+
20+
-----------------------------------------------------------------
21+
922
## [v0.13.0 (2023-05-25)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.12.1..0.13.0)
1023

1124
### Added

bin/mr-linter

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<?php
33

44
use ArtARTs36\MergeRequestLinter\Presentation\Console\Application\ApplicationFactory;
5+
use ArtARTs36\MergeRequestLinter\Presentation\Console\Exceptions\ApplicationNotCreatedException;
56
use Symfony\Component\Console\Output\ConsoleOutput;
67

78
$loaded = false;
@@ -28,6 +29,15 @@ if ($loaded === false) {
2829

2930
$output = new ConsoleOutput();
3031

31-
$application = (new ApplicationFactory())->create($output);
32+
try {
33+
$application = (new ApplicationFactory())->create($output);
34+
} catch (ApplicationNotCreatedException $e) {
35+
$output->writeln(sprintf(
36+
'Error: Application not created: %s',
37+
$e->getMessage(),
38+
));
39+
40+
exit(1);
41+
}
3242

3343
$application->run(output: $output);

docs/Builder/ConfigJsonSchema/Generator.php

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use ArtARTs36\MergeRequestLinter\DocBuilder\EventFinder;
77
use ArtARTs36\MergeRequestLinter\Domain\Notifications\ChannelType;
88
use ArtARTs36\MergeRequestLinter\Domain\Request\MergeRequest;
9+
use ArtARTs36\MergeRequestLinter\Shared\Reflector\ClassSummary;
10+
use ArtARTs36\MergeRequestLinter\Shared\Reflector\Reflector;
911

1012
class Generator
1113
{
@@ -183,6 +185,13 @@ private function mapEvents(): array
183185
'when' => $this->operatorSchemaArrayGenerator->generate($class),
184186
],
185187
];
188+
189+
$reflector = new \ReflectionClass($class);
190+
$description = Reflector::findPHPDocSummary($reflector);
191+
192+
if (! empty($description)) {
193+
$map[$eventName]['description'] = $description;
194+
}
186195
}
187196

188197
return $map;

docs/notifications.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
#### The following events are currently available for notifications:
77

8-
| Event | Description | Arguments |
9-
|---------------|----------------------------------------------------------------------------|-----------------|
10-
| lint_started | The event is created at the moment when the linter has just started | request |
11-
| lint_finished | The event is created at the moment when the linter has completed its work. | request, result |
8+
| Event | Description | Arguments |
9+
|---------------------|----------------------------------------------------------------------------|-----------------|
10+
| lint_started | The event is created at the moment when the linter has just started. | request |
11+
| lint_finished | The event is created at the moment when the linter has completed its work. | request, result |
12+
| rule_was_failed | The event is created at the moment when the linter rule was failed. | rule, notes |
13+
| rule_was_successful | The event is created at the moment when the linter rule was successful. | rule |
1214

1315
#### Templating
1416

mr-linter-config-schema.json

+13-4
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,8 @@
12941294
},
12951295
"additionalProperties": false
12961296
}
1297-
}
1297+
},
1298+
"description": "The event is created at the moment when the linter has just started."
12981299
},
12991300
"rule_was_failed": {
13001301
"type": "object",
@@ -1403,11 +1404,17 @@
14031404
}
14041405
},
14051406
"additionalProperties": false
1407+
},
1408+
"notes": {
1409+
"type": "object",
1410+
"properties": [],
1411+
"additionalProperties": false
14061412
}
14071413
},
14081414
"additionalProperties": false
14091415
}
1410-
}
1416+
},
1417+
"description": "The event is created at the moment when the linter rule was failed."
14111418
},
14121419
"lint_finished": {
14131420
"type": "object",
@@ -2500,7 +2507,8 @@
25002507
},
25012508
"additionalProperties": false
25022509
}
2503-
}
2510+
},
2511+
"description": "The event is created at the moment when the linter has completed its work."
25042512
},
25052513
"rule_was_successful": {
25062514
"type": "object",
@@ -2613,7 +2621,8 @@
26132621
},
26142622
"additionalProperties": false
26152623
}
2616-
}
2624+
},
2625+
"description": "The event is created at the moment when the linter rule was successful."
26172626
}
26182627
}
26192628
}

src/Application/Linter/Linter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function run(MergeRequest $request): LintResult
5757
if (count($ruleNotes) === 0) {
5858
$this->events->dispatch(new RuleWasSuccessfulEvent($rule->getName()));
5959
} else {
60-
$this->events->dispatch(new RuleWasFailedEvent($rule->getName()));
60+
$this->events->dispatch(new RuleWasFailedEvent($rule->getName(), $ruleNotes));
6161
}
6262
} catch (EvaluatorCrashedException $e) {
6363
$notes[] = new LintNote(sprintf('[%s] Invalid condition value: %s', $rule->getName(), $e->getMessage()));

src/Domain/Linter/LintFinishedEvent.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArtARTs36\MergeRequestLinter\Domain\Request\MergeRequest;
66

77
/**
8+
* The event is created at the moment when the linter has completed its work.
89
* @codeCoverageIgnore
910
*/
1011
class LintFinishedEvent

src/Domain/Linter/LintStartedEvent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use ArtARTs36\MergeRequestLinter\Domain\Request\MergeRequest;
66

77
/**
8-
* Event for lint starting.
8+
* The event is created at the moment when the linter has just started.
99
* @codeCoverageIgnore
1010
*/
1111
class LintStartedEvent

src/Domain/Linter/RuleWasFailedEvent.php

+7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Domain\Linter;
44

5+
use ArtARTs36\MergeRequestLinter\Domain\Note\Note;
6+
57
/**
8+
* The event is created at the moment when the linter rule was failed.
69
* @codeCoverageIgnore
710
*/
811
class RuleWasFailedEvent
912
{
1013
public const NAME = 'rule_was_failed';
1114

15+
/**
16+
* @param array<Note> $notes
17+
*/
1218
public function __construct(
1319
public readonly string $ruleName,
20+
public readonly array $notes,
1421
) {
1522
//
1623
}

src/Domain/Linter/RuleWasSuccessfulEvent.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ArtARTs36\MergeRequestLinter\Domain\Linter;
44

55
/**
6+
* The event is created at the moment when the linter rule was successful.
67
* @codeCoverageIgnore
78
*/
89
class RuleWasSuccessfulEvent

src/Infrastructure/Contracts/Text/TextRenderer.php

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

33
namespace ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Text;
44

5+
use ArtARTs36\MergeRequestLinter\Infrastructure\Text\Exceptions\TextRenderingFailedException;
56
use ArtARTs36\MergeRequestLinter\Shared\DataStructure\Map;
67

78
/**
@@ -12,6 +13,7 @@ interface TextRenderer
1213
/**
1314
* Render text.
1415
* @param Map<string, mixed> $data
16+
* @throws TextRenderingFailedException
1517
*/
1618
public function render(string $text, Map $data): string;
1719
}

src/Infrastructure/NotificationEvent/Listener.php

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Listener
99
{
1010
/**
1111
* Handle event.
12+
* @throws NotifyFailedException
1213
*/
1314
public function handle(object $event): void;
1415
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Infrastructure\NotificationEvent;
4+
5+
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
6+
7+
final class NotifyFailedException extends MergeRequestLinterException
8+
{
9+
//
10+
}

src/Infrastructure/NotificationEvent/NotifyListener.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ArtARTs36\ContextLogger\Contracts\ContextLogger;
77
use ArtARTs36\MergeRequestLinter\Infrastructure\Notifications\Contracts\Notifier;
88
use ArtARTs36\MergeRequestLinter\Infrastructure\Notifications\Notifier\MessageCreator;
9+
use ArtARTs36\MergeRequestLinter\Infrastructure\Text\Exceptions\TextRenderingFailedException;
910
use ArtARTs36\MergeRequestLinter\Shared\DataStructure\ArrayMap;
1011

1112
class NotifyListener implements Listener
@@ -21,10 +22,18 @@ public function __construct(
2122

2223
public function handle(object $event): void
2324
{
24-
$message = $this->messageCreator->create(
25-
$this->message->template,
26-
new ArrayMap(get_object_vars($event)),
27-
);
25+
try {
26+
$message = $this->messageCreator->create(
27+
$this->message->template,
28+
new ArrayMap(get_object_vars($event)),
29+
);
30+
} catch (TextRenderingFailedException $e) {
31+
throw new NotifyFailedException(sprintf(
32+
'Rendering template for notification on event "%s" was failed: %s',
33+
$this->message->event,
34+
$e->getMessage(),
35+
), previous: $e);
36+
}
2837

2938
$this->logger->shareContext('message_id', $message->id);
3039

src/Infrastructure/Notifications/Notifier/MessageCreator.php

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ArtARTs36\MergeRequestLinter\Domain\Notifications\Message;
66
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Text\TextRenderer;
7+
use ArtARTs36\MergeRequestLinter\Infrastructure\Text\Exceptions\TextRenderingFailedException;
78
use ArtARTs36\MergeRequestLinter\Shared\DataStructure\Map;
89

910
class MessageCreator
@@ -16,6 +17,7 @@ public function __construct(
1617

1718
/**
1819
* @param Map<string, mixed> $data
20+
* @throws TextRenderingFailedException
1921
*/
2022
public function create(string $template, Map $data): Message
2123
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Infrastructure\Text\Exceptions;
4+
5+
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
6+
7+
class TextRenderingFailedException extends MergeRequestLinterException
8+
{
9+
//
10+
}

src/Infrastructure/Text/Renderer/TwigRenderer.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
namespace ArtARTs36\MergeRequestLinter\Infrastructure\Text\Renderer;
44

55
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Text\TextRenderer;
6+
use ArtARTs36\MergeRequestLinter\Infrastructure\Text\Exceptions\TextRenderingFailedException;
67
use ArtARTs36\MergeRequestLinter\Shared\DataStructure\Map;
78
use Twig\Environment;
9+
use Twig\Error\LoaderError;
10+
use Twig\Error\RuntimeError;
11+
use Twig\Error\SyntaxError;
812
use Twig\Loader\ArrayLoader;
913

1014
class TwigRenderer implements TextRenderer
@@ -29,6 +33,15 @@ public function render(string $text, Map $data): string
2933

3034
$this->loader->setTemplate($templateName, $text);
3135

32-
return $this->environment->render($templateName, $data->toArray());
36+
try {
37+
return $this->environment->render($templateName, $data->toArray());
38+
} catch (SyntaxError $e) {
39+
throw new TextRenderingFailedException(
40+
sprintf('invalid template: %s', $e->getMessage()),
41+
previous: $e,
42+
);
43+
} catch (LoaderError|RuntimeError|\TypeError $e) {
44+
throw new TextRenderingFailedException($e->getMessage(), previous: $e);
45+
}
3346
}
3447
}

src/Presentation/Console/Application/ApplicationFactory.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use ArtARTs36\MergeRequestLinter\Presentation\Console\Command\InfoCommand;
4444
use ArtARTs36\MergeRequestLinter\Presentation\Console\Command\InstallCommand;
4545
use ArtARTs36\MergeRequestLinter\Presentation\Console\Command\LintCommand;
46+
use ArtARTs36\MergeRequestLinter\Presentation\Console\Exceptions\ApplicationNotCreatedException;
4647
use ArtARTs36\MergeRequestLinter\Presentation\Console\Output\ConsoleLogger;
4748
use ArtARTs36\MergeRequestLinter\Shared\Events\CallbackListener;
4849
use ArtARTs36\MergeRequestLinter\Shared\Events\EventDispatcher;
@@ -65,6 +66,9 @@ public function __construct(
6566
//
6667
}
6768

69+
/**
70+
* @throws ApplicationNotCreatedException
71+
*/
6872
public function create(OutputInterface $output): Application
6973
{
7074
$clock = $this->registerClock();
@@ -125,7 +129,7 @@ public function create(OutputInterface $output): Application
125129
}
126130

127131
/**
128-
* @throws \Exception
132+
* @throws ApplicationNotCreatedException
129133
*/
130134
private function registerClock(): Clock
131135
{
@@ -135,12 +139,10 @@ private function registerClock(): Clock
135139
$tzId = $this->environment->getString('MR_LINTER_TIMEZONE');
136140

137141
try {
138-
$tz = new \DateTimeZone(trim($tzId));
139-
} catch (\Throwable) {
140-
throw new \Exception(sprintf('TimeZone "%s" invalid', $tzId));
142+
$clock = LocalClock::on(trim($tzId));
143+
} catch (\Throwable $e) {
144+
throw new ApplicationNotCreatedException($e->getMessage(), previous: $e);
141145
}
142-
143-
$clock = new LocalClock($tz);
144146
}
145147

146148
$this->container->set(ClockInterface::class, $clock);
@@ -234,7 +236,7 @@ private function createLogger(OutputInterface $output, MetricManager $metricMana
234236
{
235237
$loggers = [
236238
MetricableLogger::create($metricManager),
237-
new ConsoleLogger($output),
239+
new ConsoleLogger($output, $this->container->get(ClockInterface::class)),
238240
];
239241

240242
$compositeLogger = new CompositeLogger($loggers);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Presentation\Console\Exceptions;
4+
5+
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
6+
7+
final class ApplicationNotCreatedException extends MergeRequestLinterException
8+
{
9+
//
10+
}

0 commit comments

Comments
 (0)