diff --git a/ExerciceChecker/src/app.ts b/ExerciceChecker/src/app.ts index 23cecd27bc9ba695cb2697ad3f64a648da726202..0a68467ce0f19cb4f370c13101aeea25950b6857 100644 --- a/ExerciceChecker/src/app.ts +++ b/ExerciceChecker/src/app.ts @@ -4,6 +4,8 @@ const path = require('node:path'); require('dotenv').config({ path: path.join(__dirname, '../.env') }); require('./shared/helpers/TypeScriptExtensions'); // ATTENTION : This line MUST be the second of this file +import Toolbox from './shared/helpers/Toolbox'; +import ExerciceHelper from './shared/helpers/ExerciceHelper'; import ExerciceCheckerError from './types/ExerciceCheckerError'; import { exec, spawn } from 'child_process'; import util from 'util'; @@ -56,7 +58,7 @@ import Config from './config/Config'; const containerExitStatus = await new Promise<[ number, string ]>((resolve, reject) => { let logs = '####################################################### Docker Compose & Main Container Logs #######################################################\n'; - const dockerCompose = spawn(`${ dockerComposeCommand } run ${ exerciceEnonce.enonceFile.result.container }`, { + const dockerCompose = spawn(`${ dockerComposeCommand } run --build ${ exerciceEnonce.enonceFile.result.container }`, { cwd : Config.folders.project, shell: true, env : { @@ -96,9 +98,36 @@ import Config from './config/Config'; // Step 6: Check content requirements and content size + console.log(chalk.green('- Validating results folder size')); + const resultsFolderSize = await Toolbox.fs.getTotalSize(Config.folders.resultsExercice); + if ( resultsFolderSize > Config.resultsFolderMaxSizeInBytes ) { + console.error(chalk.red(`X Results folder size is too big (bigger than ${ Config.resultsFolderMaxSizeInBytes / 1000000 })`)); + process.exit(ExerciceCheckerError.EXERCICE_RESULTS_FOLDER_TOO_BIG); + } + + console.log(chalk.green('- Checking results file')); + const resultsFileOriginPath = path.join(Config.folders.resultsExercice, Config.filenames.results); + const resultsFilePath = path.join(Config.folders.resultsDojo, Config.filenames.results); + + if ( !fs.existsSync(resultsFileOriginPath) ) { + console.error(chalk.red(`X Results file not found.`)); + process.exit(ExerciceCheckerError.EXERCICE_RESULTS_FILE_NOT_FOUND); + } + + fs.moveSync(resultsFileOriginPath, resultsFilePath, { overwrite: true }); + + const validationResults = ExerciceHelper.validateResultFile(resultsFilePath); + + if ( !validationResults.isValid ) { + console.error(chalk.red(`X Results file is not valid. Here are the errors :`)); + console.error(chalk.red(JSON.stringify(validationResults.errors))); + process.exit(ExerciceCheckerError.EXERCICE_RESULTS_FILE_SCHEMA_NOT_VALID); + } + // Step 7: Upload and show the results // Step 8: Exit with container exit code + console.log(chalk.green(`- Good bye (container's exit code : ${ containerExitStatus[0] })`)); process.exit(containerExitStatus[0]); })(); diff --git a/ExerciceChecker/src/config/Config.ts b/ExerciceChecker/src/config/Config.ts index e50b50abb18c2eea92392bf01598328803a504fd..2b4706498c8c9a848656e215bdf6b9ce457ec8d4 100644 --- a/ExerciceChecker/src/config/Config.ts +++ b/ExerciceChecker/src/config/Config.ts @@ -3,10 +3,16 @@ import path from 'path'; class Config { + public readonly resultsFolderMaxSizeInBytes: number; + public readonly folders: { project: string; resultsVolume: string; resultsDojo: string; resultsExercice: string; }; + public readonly filenames: { + results: string; + }; + public readonly exercice: { id: string; secret: string; }; @@ -16,6 +22,8 @@ class Config { }; constructor() { + this.resultsFolderMaxSizeInBytes = Number(process.env.RESULTS_FOLDER_MAX_SIZE_IN_BYTES || 0); + this.folders = { project : process.env.FILES_FOLDER?.convertWithEnvVars() ?? './', resultsVolume : process.env.RESULTS_VOLUME?.convertWithEnvVars() ?? '', @@ -24,6 +32,10 @@ class Config { }; this.resetResultsVolume(); + this.filenames = { + results: process.env.RESULTS_FILENAME || '' + }; + this.exercice = { id : process.env.DOJO_EXERCICE_ID || '', secret: process.env.DOJO_SECRET || '' diff --git a/ExerciceChecker/src/types/ExerciceCheckerError.ts b/ExerciceChecker/src/types/ExerciceCheckerError.ts index c035c2b9d181b1e35d5fe4e07bf9acc3bc05e542..c687bc94c7d67baf48acb5541fd869663a37695e 100644 --- a/ExerciceChecker/src/types/ExerciceCheckerError.ts +++ b/ExerciceChecker/src/types/ExerciceCheckerError.ts @@ -1,7 +1,10 @@ enum ExerciceCheckerError { - EXERCICE_ENONCE_GET_ERROR = 1000, - DOCKER_COMPOSE_UP_ERROR = 1001, - DOCKER_COMPOSE_LOGS_ERROR = 1002, + EXERCICE_ENONCE_GET_ERROR = 1000, + DOCKER_COMPOSE_UP_ERROR = 1001, + DOCKER_COMPOSE_LOGS_ERROR = 1002, + EXERCICE_RESULTS_FOLDER_TOO_BIG = 1003, + EXERCICE_RESULTS_FILE_NOT_FOUND = 1004, + EXERCICE_RESULTS_FILE_SCHEMA_NOT_VALID = 1005 }