Skip to content

Commit

Permalink
Merge pull request #24 from lartist/refacto
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
boherm committed Aug 3, 2023
2 parents e6d16bf + c35fb36 commit 4cfe5b3
Show file tree
Hide file tree
Showing 41 changed files with 111 additions and 286 deletions.
2 changes: 1 addition & 1 deletion config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ framework:
webhook:
routing:
github:
service: App\Shared\Webhook\GithubWebhookParser
service: App\Shared\Infrastructure\Webhook\GithubWebhookParser

when@test:
framework:
Expand Down
4 changes: 2 additions & 2 deletions config/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
controllers:
resource:
path: ../src/Shared/Controller/
namespace: App\Shared\Controller
path: ../src/Shared/Infrastructure/Controller/
namespace: App\Shared\Infrastructure\Controller
type: attribute
14 changes: 6 additions & 8 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

use App\PullRequest\Domain\Gateway\CommitterRepositoryInterface as PRCommitterRepositoryInterface;
use App\PullRequest\Domain\Gateway\PullRequestRepositoryInterface;
use App\PullRequest\Infrastructure\Adapter\RestGithubCommitterRepository as PRRestGithubCommitterRepository;
use App\PullRequest\Infrastructure\Adapter\RestPullRequestRepository;
use App\PullRequestDashboard\Domain\Gateway\CommitterRepositoryInterface;
use App\PullRequestDashboard\Domain\Gateway\PullRequestCardRepositoryInterface;
use App\PullRequestDashboard\Infrastructure\Adapter\GraphqlGithubPullRequestCardRepository;
use App\PullRequestDashboard\Infrastructure\Adapter\RestGithubCommitterRepository;
use App\Shared\Adapter\SpyMessageBus;
use App\Shared\Factory\CommandFactory\CommandFactory;
use App\Shared\Domain\Gateway\CommitterRepositoryInterface;
use App\Shared\Infrastructure\Adapter\RestGithubCommitterRepository;
use App\Shared\Infrastructure\Adapter\SpyMessageBus;
use App\Shared\Infrastructure\Factory\CommandFactory\CommandFactory;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Messenger\MessageBusInterface;

Expand All @@ -26,6 +24,7 @@
->autoconfigure(true)
->bind('$pullRequestDashboardNumber', '%pull_request_dashboard_number%')
->bind('$webhookSecret', '%env(WEBHOOK_SECRET)%')
->bind('$appVersion', '%app.version%')
;

$services->load('App\\', '../src/')
Expand All @@ -35,6 +34,7 @@
'../src/Kernel.php',
]);

// Note: The config about messenger.message_handler is not tested, be careful when you want to modify it
$services->load('App\\PullRequest\\Application\\CommandHandler\\', '../src/PullRequest/Application/CommandHandler/')
->tag('messenger.message_handler');

Expand All @@ -44,7 +44,6 @@
$services->alias(PullRequestRepositoryInterface::class, RestPullRequestRepository::class);
$services->alias(PullRequestCardRepositoryInterface::class, GraphqlGithubPullRequestCardRepository::class);
$services->alias(CommitterRepositoryInterface::class, RestGithubCommitterRepository::class);
$services->alias(PRCommitterRepositoryInterface::class, PRRestGithubCommitterRepository::class);
$services->set(CommandFactory::class)
->args([tagged_iterator('app.shared.command_strategy')])
;
Expand All @@ -66,7 +65,6 @@
$services->set(RestPullRequestRepository::class);
$services->set(GraphqlGithubPullRequestCardRepository::class);
$services->set(RestGithubCommitterRepository::class);
$services->set(PRRestGithubCommitterRepository::class);
$services->alias(MessageBusInterface::class, SpyMessageBus::class);
}
};
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<directory>tests/PullRequestDashboard/Infrastructure/Adapter</directory>
</testsuite>
<testsuite name="endtoend_tests">
<directory>tests/Shared/Controller</directory>
<directory>tests/Shared/Command</directory>
<directory>tests/Shared/Webhook</directory>
<directory>tests/Shared/Infrastructure/Controller</directory>
<directory>tests/Shared/Infrastructure/Command</directory>
<directory>tests/Shared/Infrastructure/Webhook</directory>
</testsuite>
</testsuites>
<coverage/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use App\PullRequest\Application\Command\AddLabelByApprovalCountCommand;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestId;
use App\PullRequest\Domain\Exception\PullRequestNotFoundException;
use App\PullRequest\Domain\Gateway\CommitterRepositoryInterface;
use App\PullRequest\Domain\Gateway\PullRequestRepositoryInterface;
use App\Shared\Domain\Gateway\CommitterRepositoryInterface;

class AddLabelByApprovalCountCommandHandler
{
Expand All @@ -24,7 +24,7 @@ public function __invoke(AddLabelByApprovalCountCommand $command): void
if (null === $pullRequest) {
throw new PullRequestNotFoundException();
}
$pullRequest->addLabelByApprovalCount($this->committerRepository->findAll($pullRequest->getId()));
$pullRequest->addLabelByApprovalCount($this->committerRepository->findAll($pullRequest->getId()->repositoryOwner));
$this->pullRequestRepository->update($pullRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function requestChanges(): void
*/
public function addLabelByApprovalCount(array $committers): void
{
// Todo: use enum instead
$validApprovals = array_filter(
$this->approvals,
static fn (Approval $approval) => in_array($approval->author, $committers, true)
Expand Down
17 changes: 0 additions & 17 deletions src/PullRequest/Domain/Gateway/CommitterRepositoryInterface.php

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use App\PullRequestDashboard\Application\Command\MovePullRequestCardToColumnByApprovalCountCommand;
use App\PullRequestDashboard\Domain\Aggregate\PullRequestCard\PullRequestCardId;
use App\PullRequestDashboard\Domain\Exception\PullRequestCardNotFoundException;
use App\PullRequestDashboard\Domain\Gateway\CommitterRepositoryInterface;
use App\PullRequestDashboard\Domain\Gateway\PullRequestCardRepositoryInterface;
use App\Shared\Domain\Gateway\CommitterRepositoryInterface;

class MovePullRequestCardToColumnByApprovalCountCommandHandler
{
Expand All @@ -33,7 +33,7 @@ public function __invoke(MovePullRequestCardToColumnByApprovalCountCommand $comm
throw new PullRequestCardNotFoundException();
}

$pullRequestCard->moveByApprovalCount($this->committersRepository->findAll($pullRequestCard->getId()));
$pullRequestCard->moveByApprovalCount($this->committersRepository->findAll($pullRequestCard->getId()->repositoryOwner));
$this->pullRequestCardRepository->update($pullRequestCard);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function moveColumnByLabel(string $label): void
*/
public function moveByApprovalCount(array $committers): void
{
// Todo: use enum
$validApprovals = array_filter(
$this->pullRequest->approvals,
static fn (Approval $approval) => in_array($approval->author, $committers, true)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use App\PullRequestDashboard\Domain\Gateway\PullRequestCardRepositoryInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

// Todo: to test and optimize
class GraphqlGithubPullRequestCardRepository implements PullRequestCardRepositoryInterface
{
public function __construct(private readonly HttpClientInterface $githubClient)
Expand Down

This file was deleted.

This file was deleted.

13 changes: 13 additions & 0 deletions src/Shared/Domain/Gateway/CommitterRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Shared\Domain\Gateway;

interface CommitterRepositoryInterface
{
/**
* @return string[]
*/
public function findAll(string $organisation): array;
}
18 changes: 18 additions & 0 deletions src/Shared/Infrastructure/Adapter/InMemoryCommitterRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\Adapter;

use App\Shared\Domain\Gateway\CommitterRepositoryInterface;

class InMemoryCommitterRepository implements CommitterRepositoryInterface
{
public function findAll(string $organisation): array
{
return [
'lartist',
'nicosomb',
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\Shared\Infrastructure\Adapter;

use App\Shared\Domain\Gateway\CommitterRepositoryInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class RestGithubCommitterRepository implements CommitterRepositoryInterface
{
public function __construct(private readonly HttpClientInterface $githubClient)
{
}

public function findAll(string $organisation): array
{
return array_map(
fn (array $committer) => $committer['login'],
$this->githubClient->request('GET', '/orgs/'.$organisation.'/teams/committers/members')->toArray()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Shared\Adapter;
namespace App\Shared\Infrastructure\Adapter;

use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\Messenger\Envelope;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace App\Shared\Command;
namespace App\Shared\Infrastructure\Command;

use App\Shared\Event\GithubEvent;
use App\Shared\Infrastructure\Event\GithubEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Shared\Controller;
namespace App\Shared\Infrastructure\Controller;

use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestId;
use App\PullRequest\Infrastructure\Adapter\RestPullRequestRepository;
Expand All @@ -19,17 +19,17 @@ public function __construct(
* Endpoint for monitoring team to check if our bot is working properly.
*/
#[Route('/healthcheck', name: 'healthcheck')]
public function healthcheck(): JsonResponse
public function healthcheck(string $appVersion): JsonResponse
{
return $this->json($this->checkBotStatus());
return $this->json($this->checkBotStatus($appVersion));
}

/**
* Function to check if our bot is working properly, and generate response.
*
* @return array<string, mixed>
*/
private function checkBotStatus(): array
private function checkBotStatus(string $appVersion): array
{
// Let's check all services in used.
// -> We can add more services if needed here.
Expand All @@ -40,7 +40,7 @@ private function checkBotStatus(): array
// Now, we build the global status.
$status = [
'status' => !in_array(false, array_values($checkServices)) ? 'OK' : 'KO',
'version' => $this->getParameter('app.version'),
'version' => $appVersion,
];
foreach ($checkServices as $serviceName => $serviceStatus) {
$status[$serviceName] = ['status' => $serviceStatus ? 'up' : 'down'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Shared\Event;
namespace App\Shared\Infrastructure\Event;

class GithubEvent
{
Expand Down

0 comments on commit 4cfe5b3

Please sign in to comment.