Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tag to comments #858

Merged
merged 12 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/packages/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DR\Review\Doctrine\Type\CodeReviewType;
use DR\Review\Doctrine\Type\ColorThemeType;
use DR\Review\Doctrine\Type\CommentStateType;
use DR\Review\Doctrine\Type\CommentTagType;
use DR\Review\Doctrine\Type\DiffAlgorithmType;
use DR\Review\Doctrine\Type\FilterType;
use DR\Review\Doctrine\Type\FrequencyType;
Expand Down Expand Up @@ -36,6 +37,7 @@
$dbal->type(CodeReviewerStateType::TYPE)->class(CodeReviewerStateType::class);
$dbal->type(ColorThemeType::TYPE)->class(ColorThemeType::class);
$dbal->type(CommentStateType::TYPE)->class(CommentStateType::class);
$dbal->type(CommentTagType::TYPE)->class(CommentTagType::class);
$dbal->type(DiffAlgorithmType::TYPE)->class(DiffAlgorithmType::class);
$dbal->type(FilterType::TYPE)->class(FilterType::class);
$dbal->type(FrequencyType::TYPE)->class(FrequencyType::class);
Expand Down
31 changes: 31 additions & 0 deletions migrations/Version20240919190124.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240919190124 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE comment ADD tag ENUM(\'change_request\', \'explanation\', \'nice_to_have\', \'suggestion\') DEFAULT NULL');
$this->addSql('ALTER TABLE comment_reply ADD tag ENUM(\'change_request\', \'explanation\', \'nice_to_have\', \'suggestion\') DEFAULT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE comment DROP tag');
$this->addSql('ALTER TABLE comment_reply DROP tag');
}
}
24 changes: 8 additions & 16 deletions src/Controller/App/Review/Comment/AddCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
use DR\Review\Controller\AbstractController;
use DR\Review\Entity\Review\CodeReview;
use DR\Review\Entity\Review\Comment;
use DR\Review\Entity\Review\LineReference;
use DR\Review\Form\Review\AddCommentFormType;
use DR\Review\Repository\Review\CommentRepository;
use DR\Review\Security\Role\Roles;
use DR\Utils\Assert;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -28,27 +26,21 @@ public function __construct(private readonly CommentRepository $commentRepositor
#[IsGranted(Roles::ROLE_USER)]
public function __invoke(Request $request, #[MapEntity] CodeReview $review): JsonResponse
{
$form = $this->createForm(AddCommentFormType::class, null, ['review' => $review]);
$form->handleRequest($request);
if ($form->isSubmitted() === false || $form->isValid() === false) {
return $this->json(['success' => false], Response::HTTP_BAD_REQUEST);
}

/** @var array{lineReference: string, message: string} $data */
$data = $form->getData();

$lineReference = LineReference::fromString($data['lineReference']);

$user = $this->getUser();
$comment = new Comment();
$comment->setUser($user);
$comment->setMessage('');
$comment->setTag(null);
$comment->setReview($review);
$comment->setFilePath(Assert::notNull($lineReference->oldPath ?? $lineReference->newPath));
$comment->setLineReference($lineReference);
$comment->setMessage($data['message']);
$comment->setCreateTimestamp(time());
$comment->setUpdateTimestamp(time());

$form = $this->createForm(AddCommentFormType::class, $comment, ['review' => $review]);
$form->handleRequest($request);
if ($form->isSubmitted() === false || $form->isValid() === false) {
return $this->json(['success' => false], Response::HTTP_BAD_REQUEST);
}

$this->commentRepository->save($comment, true);

$url = $this->generateUrl(GetCommentThreadController::class, ['id' => (int)$comment->getId()]);
Expand Down
20 changes: 9 additions & 11 deletions src/Controller/App/Review/Comment/AddCommentReplyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,29 @@ public function __invoke(Request $request, #[MapEntity] ?Comment $comment): Json
return $this->json(['success' => false, 'error' => $this->translator->trans('comment.was.deleted.meanwhile')], Response::HTTP_NOT_FOUND);
}

$form = $this->createForm(AddCommentReplyFormType::class, null, ['comment' => $comment]);
$form->handleRequest($request);
if ($form->isSubmitted() === false || $form->isValid() === false) {
return $this->json(['success' => false], Response::HTTP_BAD_REQUEST);
}

/** @var array{message: string} $data */
$data = $form->getData();

$user = $this->getUser();
$reply = new CommentReply();
$reply->setUser($user);
$reply->setMessage('');
$reply->setTag(null);
$reply->setComment($comment);
$reply->setMessage($data['message']);
$reply->setCreateTimestamp(time());
$reply->setUpdateTimestamp(time());

$form = $this->createForm(AddCommentReplyFormType::class, $reply, ['comment' => $comment]);
$form->handleRequest($request);
if ($form->isSubmitted() === false || $form->isValid() === false) {
return $this->json(['success' => false], Response::HTTP_BAD_REQUEST);
}

$this->replyRepository->save($reply, true);

$this->bus->dispatch(
new CommentReplyAdded(
(int)$comment->getReview()->getId(),
(int)$reply->getId(),
$user->getId(),
$data['message'],
$reply->getMessage(),
$comment->getFilePath()
)
);
Expand Down
17 changes: 17 additions & 0 deletions src/Doctrine/Type/CommentTagType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

namespace DR\Review\Doctrine\Type;

use DR\Review\Entity\Review\CommentTagEnum;

class CommentTagType extends AbstractEnumType
{
public const TYPE = 'enum_comment_tag_type';
public const VALUES = [
CommentTagEnum::ChangeRequest->value,
CommentTagEnum::Explanation->value,
CommentTagEnum::NiceToHave->value,
CommentTagEnum::Suggestion->value
];
}
16 changes: 16 additions & 0 deletions src/Entity/Review/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use DR\Review\Doctrine\Type\CommentStateType;
use DR\Review\Doctrine\Type\CommentTagType;
use DR\Review\Entity\User\User;
use DR\Review\Repository\Review\CommentRepository;

Expand Down Expand Up @@ -36,6 +37,9 @@ class Comment
#[ORM\Column(type: Types::TEXT)]
private string $message;

#[ORM\Column(type: CommentTagType::TYPE, nullable: true, enumType: CommentTagEnum::class)]
private ?CommentTagEnum $tag;

#[ORM\Column]
private int $createTimestamp;

Expand Down Expand Up @@ -139,6 +143,18 @@ public function setMessage(string $message): self
return $this;
}

public function getTag(): ?CommentTagEnum
{
return $this->tag;
}

public function setTag(?CommentTagEnum $tag): self
{
$this->tag = $tag;

return $this;
}

public function getCreateTimestamp(): int
{
return $this->createTimestamp;
Expand Down
16 changes: 16 additions & 0 deletions src/Entity/Review/CommentReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use DR\Review\Doctrine\Type\CommentTagType;
use DR\Review\Entity\User\User;
use DR\Review\Repository\Review\CommentReplyRepository;

Expand All @@ -19,6 +20,9 @@ class CommentReply
#[ORM\Column(type: Types::TEXT)]
private string $message;

#[ORM\Column(type: CommentTagType::TYPE, nullable: true, enumType: CommentTagEnum::class)]
private ?CommentTagEnum $tag;

#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $extReferenceId = null;

Expand Down Expand Up @@ -61,6 +65,18 @@ public function setMessage(string $message): void
$this->message = $message;
}

public function getTag(): ?CommentTagEnum
{
return $this->tag;
}

public function setTag(?CommentTagEnum $tag): self
{
$this->tag = $tag;

return $this;
}

public function getExtReferenceId(): ?string
{
return $this->extReferenceId;
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/Review/CommentTagEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace DR\Review\Entity\Review;

enum CommentTagEnum: string
{
case Suggestion = 'suggestion';
case NiceToHave = 'nice_to_have';
case ChangeRequest = 'change_request';
case Explanation = 'explanation';
}
16 changes: 15 additions & 1 deletion src/Form/Review/AddCommentFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

use DR\Review\Controller\App\Review\Comment\AddCommentController;
use DR\Review\Entity\Review\CodeReview;
use DR\Review\Entity\Review\Comment;
use DR\Review\Entity\Review\LineReference;
use DR\Utils\Assert;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
Expand Down Expand Up @@ -38,7 +40,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

$builder->setAction($this->urlGenerator->generate(AddCommentController::class, ['id' => $review->getId()]));
$builder->setMethod('POST');
$builder->add('lineReference', HiddenType::class, ['data' => (string)$lineReference]);
$builder->add(
'lineReference',
HiddenType::class,
['data' => (string)$lineReference, 'setter' => $this->setter(...)]
);
$builder->add(
'message',
CommentType::class,
Expand All @@ -47,6 +53,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'attr' => ['placeholder' => 'leave.a.comment.on.line'],
]
);
$builder->add('tag', CommentTagType::class);
$builder->add('save', SubmitType::class, ['label' => 'add.comment']);
}

public function setter(Comment $comment, string $value): void
{
$lineReference = LineReference::fromString($value);
$comment->setLineReference($lineReference);
$comment->setFilePath(Assert::notNull($lineReference->oldPath ?? $lineReference->newPath));
}
}
1 change: 1 addition & 0 deletions src/Form/Review/AddCommentReplyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->setAction($this->urlGenerator->generate(AddCommentReplyController::class, ['id' => $comment->getId()]));
$builder->setMethod('POST');
$builder->add('message', CommentType::class, ['attr' => ['placeholder' => 'leave.a.reply']]);
$builder->add('tag', CommentTagType::class);
$builder->add('save', SubmitType::class, ['label' => 'reply']);
}
}
48 changes: 48 additions & 0 deletions src/Form/Review/CommentTagType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace DR\Review\Form\Review;

use DR\Review\Entity\Review\Comment;
use DR\Review\Entity\Review\CommentReply;
use DR\Review\Entity\Review\CommentTagEnum;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;

class CommentTagType extends AbstractType
{
public function __construct(private readonly TranslatorInterface $translator)
{
}

public function configureOptions(OptionsResolver $resolver): void
{
$choices = [$this->translator->trans('tag.none') => ''];
foreach (CommentTagEnum::cases() as $tag) {
$choices[$this->translator->trans('tag.' . strtolower($tag->value))] = $tag->value;
}

$resolver->setDefaults(
[
'getter' => static function (object $comment): string {
assert($comment instanceof Comment || $comment instanceof CommentReply);
return $comment->getTag()?->value ?? '';
},
'setter' => static function (object $comment, string $value): void {
assert($comment instanceof Comment || $comment instanceof CommentReply);
$comment->setTag($value === '' ? null : CommentTagEnum::from($value));
},
'label' => false,
'required' => false,
'choices' => $choices
]
);
}

public function getParent(): string
{
return ChoiceType::class;
}
}
1 change: 1 addition & 0 deletions src/Form/Review/EditCommentFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->setAction($this->urlGenerator->generate(UpdateCommentController::class, ['id' => $comment->getId()]));
$builder->setMethod('POST');
$builder->add('message', CommentType::class);
$builder->add('tag', CommentTagType::class);
$builder->add('save', SubmitType::class, ['label' => 'save']);
}
}
1 change: 1 addition & 0 deletions src/Form/Review/EditCommentReplyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->setAction($this->urlGenerator->generate(UpdateCommentReplyController::class, ['id' => $reply->getId()]));
$builder->setMethod('POST');
$builder->add('message', CommentType::class);
$builder->add('tag', CommentTagType::class);
$builder->add('save', SubmitType::class, ['label' => 'save']);
}
}
10 changes: 10 additions & 0 deletions templates/app/review/comment/comment.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@
<span class="comment__datetime" title="{{ comment.updateTimestamp|format_datetime('full', 'full') }}">
{{ 'on'|trans }} {{ comment.updateTimestamp|format_datetime('short', 'short') }}
</span>
{%- if comment.tag -%}
{%- if comment.tag.value == 'suggestion' -%}
<span class="badge rounded-pill text-bg-info">{{ 'tag.suggestion'|trans }}</span>
{%- elseif comment.tag.value == 'explanation' -%}
<span class="badge rounded-pill text-bg-primary">{{ 'tag.explanation'|trans }}</span>
{%- else -%}
<span class="badge rounded-pill text-bg-success">{{ ('tag.' ~ comment.tag.value)|trans }}</span>
{%- endif -%}
{%- endif -%}

{% if comment.user == app.user %}
<span class="comment__edit">
{# edit comment #}
Expand Down
1 change: 1 addition & 0 deletions templates/app/review/comment/comment.modify.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<span class="float-end text-secondary">
{{ 'comment.markdown.examples'|trans }}
</span>
{{- form_widget(form.tag, {attr: {class: 'd-inline-block w-auto ms-5'}}) }}
</div>
{{- form_end(form) -}}
</div>
Expand Down
9 changes: 9 additions & 0 deletions templates/app/review/comment/comment.reply.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<span class="comment__datetime" title="{{ reply.updateTimestamp|format_datetime('full', 'full') }}">
{{ 'on'|trans }} {{ reply.updateTimestamp|format_datetime('short', 'short') }}
</span>
{%- if reply.tag -%}
{%- if reply.tag.value == 'suggestion' -%}
<span class="badge rounded-pill text-bg-info">{{ 'tag.suggestion'|trans }}</span>
{%- elseif reply.tag.value == 'explanation' -%}
<span class="badge rounded-pill text-bg-primary">{{ 'tag.explanation'|trans }}</span>
{%- else -%}
<span class="badge rounded-pill text-bg-success">{{ ('tag.' ~ reply.tag.value)|trans }}</span>
{%- endif -%}
{%- endif -%}
{% if reply.user == app.user %}
<span class="comment__edit">
{# edit reply #}
Expand Down
Loading