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

ExerciseCreation => Add prompt when exercises already exist

parent 7169ca6b
No related branches found
No related tags found
No related merge requests found
Pipeline #38072 passed
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$/DojoExercise_Technique_de compilation - TP" vcs="Git" />
<mapping directory="$PROJECT_DIR$/src/shared" vcs="Git" /> <mapping directory="$PROJECT_DIR$/src/shared" vcs="Git" />
<mapping directory="$PROJECT_DIR$/src/sharedByClients" vcs="Git" /> <mapping directory="$PROJECT_DIR$/src/sharedByClients" vcs="Git" />
</component> </component>
......
import CommanderCommand from '../../CommanderCommand.js'; import CommanderCommand from '../../CommanderCommand.js';
import ora from 'ora'; import ora from 'ora';
import DojoBackendManager from '../../../managers/DojoBackendManager.js'; import DojoBackendManager from '../../../managers/DojoBackendManager.js';
import AccessesHelper from '../../../helpers/AccessesHelper.js'; import AccessesHelper from '../../../helpers/AccessesHelper.js';
import Assignment from '../../../sharedByClients/models/Assignment.js'; import Assignment from '../../../sharedByClients/models/Assignment.js';
import Exercise from '../../../sharedByClients/models/Exercise.js'; import Exercise from '../../../sharedByClients/models/Exercise.js';
import * as Gitlab from '@gitbeaker/rest'; import * as Gitlab from '@gitbeaker/rest';
import TextStyle from '../../../types/TextStyle.js'; import TextStyle from '../../../types/TextStyle.js';
import inquirer from 'inquirer'; import inquirer from 'inquirer';
import Config from '../../../config/Config'; import Config from '../../../config/Config';
import ClientsSharedConfig from '../../../sharedByClients/config/ClientsSharedConfig';
import { Option } from 'commander';
type CommandOptions = { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean } type CommandOptions = { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean, force?: boolean };
class ExerciseCreateCommand extends CommanderCommand { class ExerciseCreateCommand extends CommanderCommand {
...@@ -27,6 +29,7 @@ class ExerciseCreateCommand extends CommanderCommand { ...@@ -27,6 +29,7 @@ class ExerciseCreateCommand extends CommanderCommand {
.option('-i, --members_id <ids...>', 'list of gitlab members ids (group\'s student) to add to the repository') .option('-i, --members_id <ids...>', 'list of gitlab members ids (group\'s student) to add to the repository')
.option('-u, --members_username <usernames...>', 'list of gitlab members username (group\'s student) to add to the repository') .option('-u, --members_username <usernames...>', 'list of gitlab members username (group\'s student) to add to the repository')
.option('-c, --clone [string]', 'automatically clone the repository (SSH required) in the specified directory (this will create a subdirectory with the assignment name)') .option('-c, --clone [string]', 'automatically clone the repository (SSH required) in the specified directory (this will create a subdirectory with the assignment name)')
.addOption(new Option('-f, --force', 'don\'t ask for choice if there are already exercises for this assignment and try to create a new one anyway'))
.action(this.commandAction.bind(this)); .action(this.commandAction.bind(this));
} }
...@@ -70,25 +73,85 @@ class ExerciseCreateCommand extends CommanderCommand { ...@@ -70,25 +73,85 @@ class ExerciseCreateCommand extends CommanderCommand {
}).start().info(); }).start().info();
}; };
oraInfo(TextStyle.WARNING(`You already created ${ this.assignment.myExercises.length } exercises for this assignment:`), 4); oraInfo(TextStyle.WARNING(`You already created ${ this.assignment.myExercises.length } exercise${ this.assignment.myExercises.length > 1 ? 's' : '' } for this assignment${ this.assignment.myExercises.length >= ClientsSharedConfig.exercise.maxPerAssignment ? ' (which is the limit)' : '' }:`), 4);
type CreationChoice = { delete: string | false, deleteAll: boolean, create: boolean };
const presets: Array<{ name: string, value: CreationChoice } | inquirer.Separator> = [];
for ( const exercise of this.assignment.myExercises ) { for ( const exercise of this.assignment.myExercises ) {
oraInfo(`${ TextStyle.LIST_ITEM_NAME('Exercice Id:') } ${ exercise.id }`, 8); oraInfo(`${ TextStyle.LIST_ITEM_NAME('Exercice Id:') } ${ exercise.id }`, 8);
oraInfo(`${ TextStyle.LIST_ITEM_NAME('Name:') } ${ exercise.name }`);
oraInfo(`${ TextStyle.LIST_ITEM_NAME('Creation date:') } ${ exercise.gitlabCreationInfo.created_at }`); oraInfo(`${ TextStyle.LIST_ITEM_NAME('Creation date:') } ${ exercise.gitlabCreationInfo.created_at }`);
oraInfo(`${ TextStyle.LIST_ITEM_NAME('Repository:') } ${ exercise.gitlabCreationInfo.web_url }`); oraInfo(`${ TextStyle.LIST_ITEM_NAME('Repository:') } ${ exercise.gitlabCreationInfo.web_url }`);
oraInfo(`${ TextStyle.LIST_ITEM_NAME('Members:') } ${ exercise.members?.map(member => member.gitlabUsername).join(', ') ?? 'There is only you' }`); oraInfo(`${ TextStyle.LIST_ITEM_NAME('Members:') } ${ exercise.members?.map(member => member.gitlabUsername).join(', ') ?? 'There is only you' }`);
}
const confirm: boolean = (await inquirer.prompt({ presets.push({
name : 'confirm', name : `Delete "${ exercise.name }" and create a new one.`,
prefix : ' ', value: {
message: `${ TextStyle.QUESTION('?') } You already created ${ this.assignment.myExercises.length } exercises for this assignment (see above). Are you sure you want to create a new one?`, delete : exercise.id,
type : 'confirm', deleteAll: false,
default: false create : true
})).confirm; }
});
}
if ( !confirm ) { if ( !options.force ) {
throw new Error(); presets.push(new inquirer.Separator());
if ( this.assignment.myExercises.length > 1 ) {
presets.push({
name : `Delete all existing exercises for this assignment and create a new one.`,
value: {
delete : false,
deleteAll: true,
create : true
}
}, new inquirer.Separator());
}
if ( this.assignment.myExercises.length < ClientsSharedConfig.exercise.maxPerAssignment ) {
presets.push({
name : `Create a new one`,
value: {
delete : false,
deleteAll: false,
create : true
}
}, new inquirer.Separator());
}
presets.push({
name : `Cancel`,
value: {
delete : false,
deleteAll: false,
create : false
}
});
const creationChoice: CreationChoice = (await inquirer.prompt({
name : 'creationChoice',
message : `You already created ${ this.assignment.myExercises.length } exercise${ this.assignment.myExercises.length > 1 ? 's' : '' }${ this.assignment.myExercises.length >= ClientsSharedConfig.exercise.maxPerAssignment ? ' (which is the limit)' : '' } for this assignment (see above). What do you want to do?`,
type : 'list',
pageSize: 1000,
choices : presets
})).creationChoice;
if ( creationChoice.delete || creationChoice.deleteAll ) {
console.log(TextStyle.BLOCK(`Please wait while we are deleting the exercise${ creationChoice.deleteAll ? 's' : '' }...`));
if ( creationChoice.delete ) {
await DojoBackendManager.deleteExercise(creationChoice.delete);
} else if ( creationChoice.deleteAll ) {
for ( const exercise of this.assignment.myExercises ) {
await DojoBackendManager.deleteExercise(exercise.id);
}
}
}
if ( !creationChoice.create ) {
throw new Error();
}
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment