Skip to content
Snippets Groups Projects
Commit 1122dcaf authored by michael.minelli's avatar michael.minelli
Browse files

Add step 6 => check content requirements and size

parent f0366f12
No related branches found
No related tags found
No related merge requests found
Pipeline #25650 passed
...@@ -4,6 +4,8 @@ const path = require('node:path'); ...@@ -4,6 +4,8 @@ const path = require('node:path');
require('dotenv').config({ path: path.join(__dirname, '../.env') }); require('dotenv').config({ path: path.join(__dirname, '../.env') });
require('./shared/helpers/TypeScriptExtensions'); // ATTENTION : This line MUST be the second of this file 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 ExerciceCheckerError from './types/ExerciceCheckerError';
import { exec, spawn } from 'child_process'; import { exec, spawn } from 'child_process';
import util from 'util'; import util from 'util';
...@@ -56,7 +58,7 @@ import Config from './config/Config'; ...@@ -56,7 +58,7 @@ import Config from './config/Config';
const containerExitStatus = await new Promise<[ number, string ]>((resolve, reject) => { const containerExitStatus = await new Promise<[ number, string ]>((resolve, reject) => {
let logs = '####################################################### Docker Compose & Main Container Logs #######################################################\n'; 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, cwd : Config.folders.project,
shell: true, shell: true,
env : { env : {
...@@ -96,9 +98,36 @@ import Config from './config/Config'; ...@@ -96,9 +98,36 @@ import Config from './config/Config';
// Step 6: Check content requirements and content size // 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 7: Upload and show the results
// Step 8: Exit with container exit code // Step 8: Exit with container exit code
console.log(chalk.green(`- Good bye (container's exit code : ${ containerExitStatus[0] })`));
process.exit(containerExitStatus[0]); process.exit(containerExitStatus[0]);
})(); })();
...@@ -3,10 +3,16 @@ import path from 'path'; ...@@ -3,10 +3,16 @@ import path from 'path';
class Config { class Config {
public readonly resultsFolderMaxSizeInBytes: number;
public readonly folders: { public readonly folders: {
project: string; resultsVolume: string; resultsDojo: string; resultsExercice: string; project: string; resultsVolume: string; resultsDojo: string; resultsExercice: string;
}; };
public readonly filenames: {
results: string;
};
public readonly exercice: { public readonly exercice: {
id: string; secret: string; id: string; secret: string;
}; };
...@@ -16,6 +22,8 @@ class Config { ...@@ -16,6 +22,8 @@ class Config {
}; };
constructor() { constructor() {
this.resultsFolderMaxSizeInBytes = Number(process.env.RESULTS_FOLDER_MAX_SIZE_IN_BYTES || 0);
this.folders = { this.folders = {
project : process.env.FILES_FOLDER?.convertWithEnvVars() ?? './', project : process.env.FILES_FOLDER?.convertWithEnvVars() ?? './',
resultsVolume : process.env.RESULTS_VOLUME?.convertWithEnvVars() ?? '', resultsVolume : process.env.RESULTS_VOLUME?.convertWithEnvVars() ?? '',
...@@ -24,6 +32,10 @@ class Config { ...@@ -24,6 +32,10 @@ class Config {
}; };
this.resetResultsVolume(); this.resetResultsVolume();
this.filenames = {
results: process.env.RESULTS_FILENAME || ''
};
this.exercice = { this.exercice = {
id : process.env.DOJO_EXERCICE_ID || '', id : process.env.DOJO_EXERCICE_ID || '',
secret: process.env.DOJO_SECRET || '' secret: process.env.DOJO_SECRET || ''
......
...@@ -2,6 +2,9 @@ enum ExerciceCheckerError { ...@@ -2,6 +2,9 @@ enum ExerciceCheckerError {
EXERCICE_ENONCE_GET_ERROR = 1000, EXERCICE_ENONCE_GET_ERROR = 1000,
DOCKER_COMPOSE_UP_ERROR = 1001, DOCKER_COMPOSE_UP_ERROR = 1001,
DOCKER_COMPOSE_LOGS_ERROR = 1002, DOCKER_COMPOSE_LOGS_ERROR = 1002,
EXERCICE_RESULTS_FOLDER_TOO_BIG = 1003,
EXERCICE_RESULTS_FILE_NOT_FOUND = 1004,
EXERCICE_RESULTS_FILE_SCHEMA_NOT_VALID = 1005
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment