Skip to content

Commit 32a3450

Browse files
authored
[Optimization] Catch HTTP exceptions (#56)
* catch graphql not found exception * catch and log * remove exception * gitlab * bb * add http exceptions * update contract * add throws * add FetchMergeRequestException * catch http exceptions * fix catch * catch github * catch gitlab * add test * remove catch * fix lint * fix stat-analyse * add exceptions * fix lint * fix stat-analyse * fix test * fix test * fix test * fix lint * fix test * add test * add test * add test * add logs to http client * fix/add test * add test * add test * add test * fix lint * fix lint * fix test * up currently not merge request exception * unwrap sprintf * catch github update comment * catch post comment * catch post comment * catch auth map exceptions * remove unless class * catch load config exceptions * set as final * provide subjects * fix mock * add test * fix lint * add test * fix lint * fix mock * remove CiSystem::isCurrentlyMergeRequest * catch update comment bb * catch update comment gl * catch post comment gl * fix test * catch env exception ga * catch env exception ga * throw find comment bb * fix lint * fix test * catch get first comment ga * catch get first comment gl * fix lint * up * update changelog
1 parent a5131f4 commit 32a3450

File tree

82 files changed

+2112
-580
lines changed

Some content is hidden

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

82 files changed

+2112
-580
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ This file contains changelogs.
66

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

9+
## [v0.16.2 (2023-08-29)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.16.1..0.16.2)
10+
11+
## Optimized
12+
* Throw http exceptions and catch their in CI requests
13+
14+
[💾 Assets](https://github.com/ArtARTs36/php-merge-request-linter/releases/tag/0.16.1)
15+
16+
-----------------------------------------------------------------
17+
918
## [v0.16.1 (2023-08-13)](https://github.com/ArtARTs36/php-merge-request-linter/compare/0.16.0..0.16.1)
1019

1120
## Added

src/Application/Comments/Commenter/SingleCommenter.php

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private function updateCommentIfHaveNewMessage(string $requestId, Comment $first
7777
$this->updateComment($requestId, new Comment(
7878
$firstComment->id,
7979
$message,
80+
$requestId,
8081
));
8182
}
8283

src/Application/Linter/Runner.php

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

33
namespace ArtARTs36\MergeRequestLinter\Application\Linter;
44

5+
use ArtARTs36\MergeRequestLinter\Domain\Note\NoteSeverity;
6+
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
57
use ArtARTs36\MergeRequestLinter\Shared\Time\Timer;
68
use ArtARTs36\MergeRequestLinter\Domain\CI\CurrentlyNotMergeRequestException;
79
use ArtARTs36\MergeRequestLinter\Domain\Linter\LinterRunner;
810
use ArtARTs36\MergeRequestLinter\Domain\Linter\LintResult;
911
use ArtARTs36\MergeRequestLinter\Domain\Note\ExceptionNote;
1012
use ArtARTs36\MergeRequestLinter\Domain\Note\LintNote;
1113
use ArtARTs36\MergeRequestLinter\Domain\Request\MergeRequestFetcher;
12-
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\Exceptions\CiNotSupported;
13-
use ArtARTs36\MergeRequestLinter\Infrastructure\Http\Exceptions\InvalidCredentialsException;
1414
use ArtARTs36\MergeRequestLinter\Domain\Linter\Linter;
1515

1616
final class Runner implements LinterRunner
@@ -27,9 +27,11 @@ public function run(Linter $linter): LintResult
2727

2828
try {
2929
return $linter->run($this->requestFetcher->fetch());
30-
} catch (CurrentlyNotMergeRequestException) {
31-
return LintResult::successWithNote(new LintNote('Currently is not merge request'), $timer->finish());
32-
} catch (CiNotSupported|InvalidCredentialsException $e) {
30+
} catch (CurrentlyNotMergeRequestException $e) {
31+
return LintResult::successWithNote(new LintNote($e->getMessage()), $timer->finish());
32+
} catch (MergeRequestLinterException $e) {
33+
return LintResult::fail((new LintNote($e->getMessage()))->withSeverity(NoteSeverity::Fatal), $timer->finish());
34+
} catch (\Throwable $e) {
3335
return LintResult::fail(new ExceptionNote($e), $timer->finish());
3436
}
3537
}

src/Application/Rule/TaskHandlers/DumpTaskHandler.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArtARTs36\MergeRequestLinter\Application\Rule\Dumper\DumpInfo;
66
use ArtARTs36\MergeRequestLinter\Application\Rule\Dumper\RuleDumper;
77
use ArtARTs36\MergeRequestLinter\Application\Rule\Tasks\DumpTask;
8+
use ArtARTs36\MergeRequestLinter\Domain\Configuration\Config;
89
use ArtARTs36\MergeRequestLinter\Infrastructure\Configuration\User;
910
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Configuration\ConfigResolver;
1011

@@ -21,7 +22,7 @@ public function handle(DumpTask $task): DumpInfo
2122
{
2223
$config = $this
2324
->config
24-
->resolve(new User($task->workingDirectory, $task->customConfigPath));
25+
->resolve(new User($task->workingDirectory, $task->customConfigPath), Config::SUBJECT_RULES);
2526

2627
return new DumpInfo($config->path, $this->dumper->dump($config->config->getRules()));
2728
}

src/Domain/CI/CiSystem.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,9 @@ public function getName(): string;
2020
*/
2121
public function isCurrentlyWorking(): bool;
2222

23-
/**
24-
* Is Merge Request.
25-
*/
26-
public function isCurrentlyMergeRequest(): bool;
27-
2823
/**
2924
* Get current merge request.
3025
* @throws GettingMergeRequestException
31-
* @throws MergeRequestNotFoundException
3226
*/
3327
public function getCurrentlyMergeRequest(): MergeRequest;
3428

@@ -41,11 +35,13 @@ public function postCommentOnMergeRequest(MergeRequest $request, string $comment
4135
/**
4236
* Update comment.
4337
* @throws InvalidCommentException
38+
* @throws PostCommentException
4439
*/
4540
public function updateComment(Comment $comment): void;
4641

4742
/**
4843
* Get first comment on merge request by current user.
44+
* @throws FindCommentException
4945
*/
5046
public function getFirstCommentOnMergeRequestByCurrentUser(MergeRequest $request): ?Comment;
5147
}

src/Domain/CI/CurrentlyNotMergeRequestException.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,19 @@
66

77
class CurrentlyNotMergeRequestException extends MergeRequestLinterException implements GettingMergeRequestException
88
{
9-
//
9+
/**
10+
* @codeCoverageIgnore
11+
*/
12+
public static function createFrom(\Throwable $e): self
13+
{
14+
return new self(sprintf('Currently is not merge request: %s', $e->getMessage()), previous: $e);
15+
}
16+
17+
/**
18+
* @codeCoverageIgnore
19+
*/
20+
public static function create(): self
21+
{
22+
return new self('Currently is not merge request');
23+
}
1024
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Domain\CI;
4+
5+
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
6+
7+
class FetchMergeRequestException extends MergeRequestLinterException implements GettingMergeRequestException
8+
{
9+
//
10+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace ArtARTs36\MergeRequestLinter\Domain\CI;
4+
5+
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
6+
7+
final class FindCommentException extends MergeRequestLinterException
8+
{
9+
}

src/Domain/CI/MergeRequestNotFoundException.php

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

55
use ArtARTs36\MergeRequestLinter\Shared\Exceptions\MergeRequestLinterException;
66

7-
final class MergeRequestNotFoundException extends MergeRequestLinterException
7+
final class MergeRequestNotFoundException extends MergeRequestLinterException implements GettingMergeRequestException
88
{
9-
//
9+
/**
10+
* @codeCoverageIgnore
11+
*/
12+
public static function create(string $id, string $reason, ?\Throwable $previous = null): self
13+
{
14+
return new self(
15+
sprintf('Merge request with id %s not found: %s', $id, $reason),
16+
previous: $previous,
17+
);
18+
}
1019
}

src/Domain/CI/PostCommentException.php

-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@
66

77
final class PostCommentException extends MergeRequestLinterException
88
{
9-
//
109
}

src/Domain/Configuration/Config.php

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
*/
1111
class Config
1212
{
13+
public const SUBJECT_RULES = 1;
14+
public const SUBJECT_CI_SETTINGS = 2;
15+
public const SUBJECT_HTTP_CLIENT = 3;
16+
public const SUBJECT_NOTIFICATIONS = 4;
17+
public const SUBJECT_LINTER = 5;
18+
public const SUBJECT_COMMENTS = 6;
19+
public const SUBJECT_ALL = self::SUBJECT_RULES |
20+
self::SUBJECT_CI_SETTINGS |
21+
self::SUBJECT_HTTP_CLIENT |
22+
self::SUBJECT_NOTIFICATIONS |
23+
self::SUBJECT_LINTER |
24+
self::SUBJECT_COMMENTS;
25+
1326
/**
1427
* @param Map<CiName, CiSettings> $settings
1528
*/

src/Domain/Configuration/HttpClientConfig.php

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

55
/**
6-
* @phpstan-type Params array{base_uri: string, cookies: true, headers: array<string, string>}
6+
* @phpstan-type Params = array{base_uri?: string, cookies?: true, headers?: array<string, string>}
77
* @codeCoverageIgnore
88
*/
99
class HttpClientConfig

src/Domain/Request/Comment.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Comment
1010
public function __construct(
1111
public readonly string $id,
1212
public readonly string $message,
13+
public readonly string $mergeRequestId,
1314
) {
1415
//
1516
}

src/Domain/Request/MergeRequestFetcher.php

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

33
namespace ArtARTs36\MergeRequestLinter\Domain\Request;
44

5-
use ArtARTs36\MergeRequestLinter\Domain\CI\CurrentlyNotMergeRequestException;
5+
use ArtARTs36\MergeRequestLinter\Domain\CI\GettingMergeRequestException;
66

77
/**
88
* Interface for request fetching.
@@ -11,7 +11,7 @@ interface MergeRequestFetcher
1111
{
1212
/**
1313
* Fetch Merge Request.
14-
* @throws CurrentlyNotMergeRequestException
14+
* @throws GettingMergeRequestException
1515
*/
1616
public function fetch(): MergeRequest;
1717
}

src/Infrastructure/Ci/Credentials/AuthenticatorProxy.php

-34
This file was deleted.

src/Infrastructure/Ci/System/Bitbucket/API/Client.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,40 @@
99
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\System\Bitbucket\API\Objects\CommentList;
1010
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\System\Bitbucket\API\Objects\PullRequest;
1111
use ArtARTs36\MergeRequestLinter\Infrastructure\Ci\System\Bitbucket\API\Objects\User;
12+
use ArtARTs36\MergeRequestLinter\Infrastructure\Contracts\Http\RequestException;
1213

1314
/**
14-
* Interface for Client.
15+
* Interface for Bitbucket Client.
1516
*/
1617
interface Client
1718
{
1819
/**
1920
* Get pull request.
21+
* @throws RequestException
2022
*/
2123
public function getPullRequest(PullRequestInput $input): PullRequest;
2224

2325
/**
2426
* Get current user.
27+
* @throws RequestException
2528
*/
2629
public function getCurrentUser(): User;
2730

2831
/**
2932
* Get comments.
33+
* @throws RequestException
3034
*/
3135
public function getComments(PullRequestInput $input): CommentList;
3236

3337
/**
3438
* Update comment.
39+
* @throws RequestException
3540
*/
3641
public function updateComment(UpdateCommentInput $input): Comment;
3742

3843
/**
3944
* Post comment.
45+
* @throws RequestException
4046
*/
4147
public function postComment(CreateCommentInput $input): Comment;
4248
}

0 commit comments

Comments
 (0)