Skip to content

Commit

Permalink
Merge pull request #40 from boherm/add-check-table-description-and-mi…
Browse files Browse the repository at this point in the history
…lestone-missing-comment
  • Loading branch information
boherm authored May 28, 2024
2 parents 796340a + 3235c9b commit 14b6335
Show file tree
Hide file tree
Showing 22 changed files with 1,589 additions and 41 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"symfony/property-info": "~6.3.0",
"symfony/runtime": "~6.3.0",
"symfony/twig-bundle": "~6.3.0",
"symfony/validator": "~6.3.0",
"symfony/webhook": "~6.3.0",
"symfony/yaml": "~6.3.0"
},
Expand Down
98 changes: 97 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions config/packages/validator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
framework:
validation:
email_validation_mode: html5

# Enables validator auto-mapping support.
# For instance, basic validation constraints will be inferred from Doctrine's metadata.
#auto_mapping:
# App\Entity\: []

when@test:
framework:
validation:
not_compromised_password: false
15 changes: 15 additions & 0 deletions src/PullRequest/Application/Command/CheckMilestoneCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\PullRequest\Application\Command;

class CheckMilestoneCommand
{
public function __construct(
public readonly string $repositoryOwner,
public readonly string $repositoryName,
public readonly string $pullRequestNumber,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\PullRequest\Application\Command;

class CheckTableDescriptionCommand
{
public function __construct(
public readonly string $repositoryOwner,
public readonly string $repositoryName,
public readonly string $pullRequestNumber,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\PullRequest\Application\CommandHandler;

use App\PullRequest\Application\Command\CheckMilestoneCommand;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestId;
use App\PullRequest\Domain\Exception\PullRequestNotFoundException;
use App\PullRequest\Domain\Gateway\PullRequestRepositoryInterface;

class CheckMilestoneCommandHandler
{
public function __construct(
private readonly PullRequestRepositoryInterface $prRepository
) {
}

public function __invoke(CheckMilestoneCommand $command): void
{
// This command is only for PrestaShop/PrestaShop repository
if ('PrestaShop' !== $command->repositoryOwner || 'PrestaShop' !== $command->repositoryName) {
return;
}

// Retrieve the PullRequest object
$prId = new PullRequestId($command->repositoryOwner, $command->repositoryName, $command->pullRequestNumber);
$pullRequest = $this->prRepository->find($prId);
if (null === $pullRequest) {
throw new PullRequestNotFoundException();
}

// We check if the milestone is set
if (null === $pullRequest->getMilestoneNumber() && $pullRequest->isQAValidated()) {
$this->prRepository->addMissingMilestoneComment($prId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\PullRequest\Application\CommandHandler;

use App\PullRequest\Application\Command\CheckTableDescriptionCommand;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestDescription;
use App\PullRequest\Domain\Aggregate\PullRequest\PullRequestId;
use App\PullRequest\Domain\Exception\PullRequestNotFoundException;
use App\PullRequest\Domain\Gateway\PullRequestRepositoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class CheckTableDescriptionCommandHandler
{
public function __construct(
private readonly PullRequestRepositoryInterface $prRepository,
private readonly ValidatorInterface $validator,
) {
}

public function __invoke(CheckTableDescriptionCommand $command): void
{
// This command is only for PrestaShop/PrestaShop repository
if ('PrestaShop' !== $command->repositoryOwner || 'PrestaShop' !== $command->repositoryName) {
return;
}

// Retrieve the PullRequest object
$prId = new PullRequestId($command->repositoryOwner, $command->repositoryName, $command->pullRequestNumber);
$pullRequest = $this->prRepository->find($prId);
if (null === $pullRequest) {
throw new PullRequestNotFoundException();
}

// Create PulLRequestDescription object with the description
$prDescription = new PullRequestDescription($pullRequest->getBodyDescription());

// We check the description with the validator
$errors = $this->validator->validate($prDescription);

// If we have some errors, we need to add (or edit) the comment about this errors
if (count($errors) > 0 || $prDescription->isLinkedIssuesNeeded()) {
$this->prRepository->addTableDescriptionErrorsComment($prId, $errors, $prDescription->isLinkedIssuesNeeded());
} else {
$this->prRepository->removeTableDescriptionErrorsComment($prId);
}

// Then, we had the labels to the PR by PullRequestDescription object
$pullRequest->addLabelsByDescription($prDescription);
$this->prRepository->update($pullRequest);
}
}
56 changes: 54 additions & 2 deletions src/PullRequest/Domain/Aggregate/PullRequest/PullRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ private function __construct(
private array $labels,
private array $approvals,
private string $targetBranch,
private string $bodyDescription = '',
private ?int $milestoneNumber = null,
) {
}

/**
* @param string[] $labels
* @param Approval[] $approvals
*/
public static function create(PullRequestId $id, array $labels, array $approvals, string $targetBranch): self
public static function create(PullRequestId $id, array $labels, array $approvals, string $targetBranch, string $bodyDescription = '', ?int $milestoneNumber = null): self
{
return new self($id, $labels, $approvals, $targetBranch);
return new self($id, $labels, $approvals, $targetBranch, $bodyDescription, $milestoneNumber);
}

public function getId(): PullRequestId
Expand Down Expand Up @@ -83,4 +85,54 @@ public function getTargetBranch(): string
{
return $this->targetBranch;
}

public function getBodyDescription(): string
{
return $this->bodyDescription;
}

public function getMilestoneNumber(): ?int
{
return $this->milestoneNumber;
}

public function addLabelsByDescription(PullRequestDescription $description): void
{
// Remove some labels in labels list
$this->labels = array_diff($this->labels, [
'develop',
'8.1.x',
'Bug fix',
'Improvement',
'Feature',
'Refactoring',
'BC break',
]);

// Add label for branch
if ($description->getBranch()) {
$this->labels[] = $description->getBranch();
}

// Add label for PR type
if ($description->getType()) {
$mapLabelTypes = [
'bug fix' => 'Bug fix',
'improvement' => 'Improvement',
'new feature' => 'Feature',
'refacto' => 'Refactoring',
];
$this->labels[] = $mapLabelTypes[$description->getType()];
}

// Add label if BC break declared
if ($description->isBCBreak()) {
$this->labels[] = 'BC break';
}
}

public function isQAValidated(): bool
{
return in_array('QA ✔️', $this->labels);
}
}
Loading

0 comments on commit 14b6335

Please sign in to comment.