From d0974325f5d8db47f787c2ea9d1c71847f6a9c95 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Thu, 19 May 2022 20:25:05 +0200 Subject: [PATCH] BUGFIX: sometimes I was still using IDs --- src/Controller/JobController.php | 20 +++++++++++++------- src/Entity/Job.php | 8 ++++---- src/Entity/Run.php | 8 ++++---- src/Repository/JobRepository.php | 6 +++--- src/Repository/RunRepository.php | 5 ++--- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/Controller/JobController.php b/src/Controller/JobController.php index 3e108fe..0b32169 100644 --- a/src/Controller/JobController.php +++ b/src/Controller/JobController.php @@ -21,27 +21,30 @@ class JobController extends AbstractController return $this->render('job/index.html.twig', ['jobs' => $jobs]); } - public function jobAction(Request $request, ManagerRegistry $doctrine, $id, $all = false): Response + public function jobAction(Request $request, ManagerRegistry $doctrine, int $id, mixed $all = false): Response { $jobRepo = $doctrine->getRepository(Job::class); $runRepo = $doctrine->getRepository(Run::class); if($request->getMethod() == 'GET') { - $job = $jobRepo->parseJob($id); - $runs = $runRepo->getRunsForJob($id, $all != 'all'); + $job = $jobRepo->find($id); + $jobRepo->parseJob($job); + $runs = $runRepo->getRunsForJob($job, $all != 'all'); return $this->render('job/view.html.twig', ['job' => $job, 'runs' => $runs, 'allruns' => $all == 'all']); } elseif($request->getMethod() == 'DELETE') { $success = $jobRepo->deleteJob($id); $this->addFlash('success', $success['message']); return new JsonResponse(['return_path' => $this->GenerateUrl('job_index')]); } + return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST); } - public function editAction(Request $request, ManagerRegistry $doctrine, $id) + public function editAction(Request $request, ManagerRegistry $doctrine, int $id): Response { if($request->getMethod() == 'GET') { $jobRepo = $doctrine->getRepository(Job::class); - $job = $jobRepo->parseJob($id, true); + $job = $jobRepo->find($id); + $jobRepo->parseJob($job, true); return $this->render('job/edit.html.twig', ['job' => $job]); } elseif($request->getMethod() == 'POST') { $allValues = $request->request->all(); @@ -56,9 +59,10 @@ class JobController extends AbstractController $this->addFlash('success', $joboutput['message']); return new RedirectResponse($this->GenerateUrl('job_index')); } + return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST); } - public function addAction(Request $request, ManagerRegistry $doctrine) + public function addAction(Request $request, ManagerRegistry $doctrine): Response { if($request->getMethod() == 'GET') { return $this->render('job/add.html.twig', ['data' => []]); @@ -78,11 +82,13 @@ class JobController extends AbstractController } } - public function runNowAction(Request $request, ManagerRegistry $doctrine, int $id) { + public function runNowAction(Request $request, ManagerRegistry $doctrine, int $id): JsonResponse + { if($request->getMethod() == 'GET') { $jobRepo = $doctrine->getRepository(Job::class); $job = $jobRepo->find($id); return new JsonResponse($jobRepo->runNow($job)); } + return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST); } } \ No newline at end of file diff --git a/src/Entity/Job.php b/src/Entity/Job.php index 4dd32da..50f8cd6 100644 --- a/src/Entity/Job.php +++ b/src/Entity/Job.php @@ -154,13 +154,13 @@ class Job { $names = is_array($name) ? $name : explode('.', $name); $current = $names[0]; - if(is_array($data[$current]) && isset($names[1])) { + if(!isset($data[$current])) { + return false; + } elseif(is_array($data[$current]) && isset($names[1])) { unset($names[0]); $this->removeDataItem($data[$current], array_values($names)); - } elseif(!isset($data[$current])) { - return false; } else { - unset($data[$names[0]]); + if(isset($data[$names[0]])) unset($data[$names[0]]); } return true; } diff --git a/src/Entity/Run.php b/src/Entity/Run.php index 9bbed11..f3623a5 100644 --- a/src/Entity/Run.php +++ b/src/Entity/Run.php @@ -78,18 +78,18 @@ class Run } /** - * @return CollectionAlias + * @return Job */ - public function getJob(): CollectionAlias + public function getJob(): Job { return $this->job; } /** - * @param CollectionAlias $job + * @param Job $job * @return Run */ - public function setJob(CollectionAlias $job): Run + public function setJob(Job $job): Run { $this->job = $job; return $this; diff --git a/src/Repository/JobRepository.php b/src/Repository/JobRepository.php index e45969b..975d675 100644 --- a/src/Repository/JobRepository.php +++ b/src/Repository/JobRepository.php @@ -88,9 +88,9 @@ class JobRepository extends EntityRepository $job->setData('service', $jobData['service'] ?? ''); $job->setData('norun', $job->getLastrun() !== null && $job->getNextrun() > $job->getLastrun()); $job->setData('running', $job->getRunning() != 0); - $failedruns = $runRepo->getRunsForJob($job->getId(), true, $jobData['fail-days']); + $failedruns = $runRepo->getRunsForJob($job, true, $jobData['fail-days']); $failed = count($failedruns); - $all = count($runRepo->getRunsForJob($job->getId(), false, $jobData['fail-days'])); + $all = count($runRepo->getRunsForJob($job, false, $jobData['fail-days'])); $job->setData('lastfail', $failedruns[0] ?? NULL); $job->setData('needschecking', $all > 0 && (($failed / $all) * 100) > $jobData['fail-pct']); if(!empty($jobData['containertype']) && $jobData['containertype'] != 'none') { @@ -457,7 +457,7 @@ class JobRepository extends EntityRepository // saving to database $em->getConnection()->close(); $runRepo = $em->getRepository(Run::class); - $runRepo->addRun($job->getId(), $result['exitcode'], floor($starttime), $runtime, $result['output'], $flags); + $runRepo->addRun($job, $result['exitcode'], floor($starttime), $runtime, $result['output'], $flags); if (!$manual){ // setting nextrun to next run $nextrun = $job->getNextrun(); diff --git a/src/Repository/RunRepository.php b/src/Repository/RunRepository.php index 654e4e1..b5a9250 100644 --- a/src/Repository/RunRepository.php +++ b/src/Repository/RunRepository.php @@ -15,10 +15,9 @@ class RunRepository extends EntityRepository const SUCCESS = 'S'; const MANUAL = 'M'; - public function getRunsForJob(int $id, bool $onlyfailed = false, int $maxage = NULL, bool $ordered = true): array + public function getRunsForJob(Job $job, bool $onlyfailed = false, int $maxage = NULL, bool $ordered = true): array { $qb = $this->createQueryBuilder('run'); - $job = $this->getEntityManager()->getRepository(Job::class)->find($id); $runs = $qb ->where('run.job = :job') ->setParameter(':job', $job); @@ -90,7 +89,7 @@ class RunRepository extends EntityRepository } } else { foreach($jobids as $jobid) { - $job = $em->find($jobid); + $job = $jobRepo->find($jobid); $jobRepo->parseJob($job); $allJobs[] = $job; }