From d87452277407ef6ab3a368d4b0434b8b80eb7a64 Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Wed, 12 Jul 2023 11:09:29 +0200
Subject: Avoid update remote runner error

---
 server/middlewares/validators/runners/jobs.ts | 66 ++++++++++++++-------------
 1 file changed, 34 insertions(+), 32 deletions(-)

(limited to 'server/middlewares/validators')

diff --git a/server/middlewares/validators/runners/jobs.ts b/server/middlewares/validators/runners/jobs.ts
index 75cc0bdbb..384b209ba 100644
--- a/server/middlewares/validators/runners/jobs.ts
+++ b/server/middlewares/validators/runners/jobs.ts
@@ -159,44 +159,46 @@ export const runnerJobGetValidator = [
   }
 ]
 
-export const jobOfRunnerGetValidator = [
-  param('jobUUID').custom(isUUIDValid),
+export function jobOfRunnerGetValidatorFactory (allowedStates: RunnerJobState[]) {
+  return [
+    param('jobUUID').custom(isUUIDValid),
 
-  body('runnerToken').custom(isRunnerTokenValid),
-  body('jobToken').custom(isRunnerJobTokenValid),
+    body('runnerToken').custom(isRunnerTokenValid),
+    body('jobToken').custom(isRunnerJobTokenValid),
 
-  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
-    if (areValidationErrors(req, res, { tags })) return cleanUpReqFiles(req)
+    async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+      if (areValidationErrors(req, res, { tags })) return cleanUpReqFiles(req)
 
-    const runnerJob = await RunnerJobModel.loadByRunnerAndJobTokensWithRunner({
-      uuid: req.params.jobUUID,
-      runnerToken: req.body.runnerToken,
-      jobToken: req.body.jobToken
-    })
+      const runnerJob = await RunnerJobModel.loadByRunnerAndJobTokensWithRunner({
+        uuid: req.params.jobUUID,
+        runnerToken: req.body.runnerToken,
+        jobToken: req.body.jobToken
+      })
 
-    if (!runnerJob) {
-      cleanUpReqFiles(req)
+      if (!runnerJob) {
+        cleanUpReqFiles(req)
 
-      return res.fail({
-        status: HttpStatusCode.NOT_FOUND_404,
-        message: 'Unknown runner job',
-        tags
-      })
-    }
+        return res.fail({
+          status: HttpStatusCode.NOT_FOUND_404,
+          message: 'Unknown runner job',
+          tags
+        })
+      }
 
-    if (runnerJob.state !== RunnerJobState.PROCESSING) {
-      cleanUpReqFiles(req)
+      if (!allowedStates.includes(runnerJob.state)) {
+        cleanUpReqFiles(req)
 
-      return res.fail({
-        status: HttpStatusCode.BAD_REQUEST_400,
-        type: ServerErrorCode.RUNNER_JOB_NOT_IN_PROCESSING_STATE,
-        message: 'Job is not in "processing" state',
-        tags
-      })
-    }
+        return res.fail({
+          status: HttpStatusCode.BAD_REQUEST_400,
+          type: ServerErrorCode.RUNNER_JOB_NOT_IN_PROCESSING_STATE,
+          message: 'Job is not in "processing" state',
+          tags
+        })
+      }
 
-    res.locals.runnerJob = runnerJob
+      res.locals.runnerJob = runnerJob
 
-    return next()
-  }
-]
+      return next()
+    }
+  ]
+}
-- 
cgit v1.2.3