Skip to content

[todo] fix multiple tasks with GitHub links not persisting #27

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
},
"require-dev": {
"dbrekelmans/bdi": "^1.1",
"dg/bypass-finals": "^1.6",
"doctrine/doctrine-fixtures-bundle": "^3.5",
"phpunit/phpunit": "^11.0",
"symfony/browser-kit": "7.0.*",
Expand Down
95 changes: 74 additions & 21 deletions composer.lock

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

7 changes: 6 additions & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ parameters:
containerXmlPath: var/cache/test/App_KernelTestDebugContainer.xml
doctrine:
ormRepositoryClass: App\Repository\AbstractRepository
objectManagerLoader: tests/phpstan-bootstrap.php
objectManagerLoader: tests/phpstan-bootstrap.php
ignoreErrors:
-
message: '#Variable \$[a-zA-Z0-9]+ might not be defined\.#'
# identifier: variable.undefined // Available in phpstan 1.11.0
path: src/GraphQLRequest/*
5 changes: 5 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@

$config->symfonyContainerXml(__DIR__.'/var/cache/test/App_KernelTestDebugContainer.xml');
$config->symfonyContainerPhp(__DIR__.'/tests/rector-bootstrap.php');
// $config->singleton(\Zenstruck\Foundry\Utils\Rector\PersistenceResolver::class,
// static fn() => new \Zenstruck\Foundry\Utils\Rector\PersistenceResolver(
// (require __DIR__.'/tests/rector-bootstrap.php')->get('doctrine')->getManager())
// );

$config->sets([
// \Zenstruck\Foundry\Utils\Rector\FoundrySetList::UP_TO_FOUNDRY_2,
LevelSetList::UP_TO_PHP_83,
SetList::DEAD_CODE,
SymfonySetList::SYMFONY_64,
Expand Down
55 changes: 35 additions & 20 deletions src/Controller/TodoListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
use App\Entity\TodoList;
use App\Exception\HttpClientException;
use App\Form\TodoListType;
use App\Model\GitHubIssue;
use App\Model\GitHubPullRequest;
use App\Model\GitHubUrlData;
use App\Repository\TaskRepository;
use App\Repository\TodoListRepository;
use App\Security\Voter\TodoListVoter;
use App\Service\TaskService;
use App\Util\UrlParser;
use Doctrine\Common\Collections\Collection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -40,10 +41,8 @@ public function new(Request $request, TaskService $taskService): Response
if ($form->isSubmitted() && $form->isValid()) {
/** @var TodoList $list */
$list = $form->getData();

foreach ($list->getTasks() as $task) {
$this->checkForGitHub($task, $taskService);
}
// @TODO Fix this for new tasks...
$this->checkForGitHub($list->getTasks(), $taskService);

$this->listRepository->persist($list, true);

Expand All @@ -57,15 +56,13 @@ public function new(Request $request, TaskService $taskService): Response

#[IsGranted(TodoListVoter::EDIT, 'todoList')]
#[Route('/{id}/edit', name: 'list_edit', requirements: ['id' => Requirement::UUID], methods: ['GET', 'POST'])]
public function edit(Request $request, TodoList $todoList, TaskService $taskService): Response
public function edit(Request $request, TodoList $todoList, TaskService $taskService, TaskRepository $taskRepository): Response
{
$form = $this->createForm(TodoListType::class, $todoList);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
foreach ($todoList->getTasks() as $task) {
$this->checkForGitHub($task, $taskService);
}
$todoList->setTasks($this->checkForGitHub($todoList->getTasks(), $taskService));

$this->listRepository->flush();

Expand Down Expand Up @@ -105,24 +102,42 @@ public function removeTask(TodoList $todoList, Task $task): JsonResponse
return $this->json(['Ok']);
}

protected function checkForGitHub(Task $task, TaskService $taskService): void
/**
* @param Collection<int, Task> $tasks
*
* @return Collection<int, Task>
*/
protected function checkForGitHub(Collection $tasks, TaskService $taskService): Collection
{
$gitHubLink = UrlParser::getGitHubUrlFromText($task->getName());
$links = [];

foreach ($tasks as $task) {
$link = UrlParser::getGitHubUrlFromText($task->getName());

if (false === $gitHubLink) {
return;
if (!$link instanceof GitHubUrlData) {
continue;
}

$links[] = $link;
}

try {
// We want to query GitHub to grab the information about the link
$data = $taskService->getGitHubDataFromUrl($gitHubLink);
} catch (HttpClientException) {
// @TODO - Do something with this in the future...
return;
$objects = $taskService->getGitHubDataFromUrl($links, 'rushlow-development', 'big-desk');
} catch (HttpClientException $e) {
throw new \RuntimeException('getGitHubDataFromUrl() Failed.', previous: $e);
}

if ($data instanceof GitHubIssue || $data instanceof GitHubPullRequest) {
$task->addGitHub($data);
foreach ($objects as $gitHubObject) {
$tasks = $tasks->map(function (Task $task) use ($gitHubObject) {
if (str_contains($task->getName(), $gitHubObject->uri)) {
$task->addGitHub($gitHubObject);
}

return $task;
});
}

return $tasks;
}
}
8 changes: 8 additions & 0 deletions src/Entity/TodoList.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function addTask(Task $task): self
return $this;
}

/** @param Collection<int, Task> $tasks */
public function setTasks(Collection $tasks): self
{
$this->tasks = $tasks;

return $this;
}

public function removeTask(Task $task): self
{
$this->tasks->removeElement($task);
Expand Down
Loading
Loading