Skip to content
Snippets Groups Projects
Select Git revision
  • f4ed114bbd21f1ca202b0a5030f5edc7672bce5c
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • interactive-mode-preference
  • bedran_exercise-list
  • add_route_user
  • Jw_sonar_backup
  • exercise_list_filter
  • assignment_filter
  • add_route_assignments
  • move-to-esm-only
  • 6.0.0-dev
  • Pre-alpha
  • 5.0.0
  • Latest
  • 4.2.0
  • 4.1.1
  • 4.1.0
  • 4.0.1
  • 4.0.0
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.3.0
  • 3.2.3
  • 3.2.2
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
32 results

ExerciseHelper.ts

Blame
  • Joel von der Weid's avatar
    joel.vonderwe authored and Michaël Minelli committed
    677d512e
    History
    ExerciseHelper.ts 5.84 KiB
    import Exercise           from '../../sharedByClients/models/Exercise';
    import ora                from 'ora';
    import TextStyle          from '../../types/TextStyle';
    import inquirer           from 'inquirer';
    import Config             from '../../config/Config';
    import DojoBackendManager from '../../managers/DojoBackendManager';
    import SharedConfig       from '../../shared/config/SharedConfig';
    
    
    class ExerciseHelper {
        /**
         * Clone the exercise repository
         * @param exercise
         * @param providedPath If a string is provided, the repository will be cloned in the specified directory. If true, the repository will be cloned in the current directory. If false, the repository will not be cloned, and if undefined, the user will be prompted for the path.
         */
        async clone(exercise: Exercise, providedPath: string | boolean | undefined) {
            if ( providedPath === false ) {
                return;
            }
    
            let path: string | boolean = './';
            if ( providedPath === undefined ) {
                path = (await inquirer.prompt([ {
                    type   : 'input',
                    name   : 'path',
                    message: `Please enter the path (blank, '.' or './' for current directory):`
                } ])).path;
            } else {
                path = providedPath;
            }
    
            console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...'));
    
            await Config.gitlabManager.cloneRepository(path === '' ? true : path, exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise_${ exercise.assignmentName }`, true, 0);
        }
    
        async delete(exerciseIdOrUrl: string) {
            console.log(TextStyle.BLOCK('Please wait while we are deleting the exercise...'));
    
            await DojoBackendManager.deleteExercise(exerciseIdOrUrl);
        }
    
        async actionMenu(exercise: Exercise): Promise<void> {
            // eslint-disable-next-line no-constant-condition
            while ( true ) {
                const action: string = (await inquirer.prompt([ {
                    type   : 'list',
                    name   : 'action',
                    message: 'What action do you want to do on the exercise ?',
                    choices: [ {
                        name : 'Display details of the exercise',
                        value: 'info'
                    }, new inquirer.Separator(), {
                        name : 'Clone (SSH required) in current directory (will create a subdirectory)',
                        value: 'cloneInCurrentDirectory'
                    }, {
                        name : 'Clone (SSH required) in the specified directory (will create a subdirectory)',
                        value: 'clone'
                    }, new inquirer.Separator(), {
                        name : 'Delete the exercise',
                        value: 'delete'
                    }, new inquirer.Separator(), {
                        name : 'Exit',
                        value: 'exit'
                    }, new inquirer.Separator() ]
                } ])).action;
    
                switch ( action ) {
                    case 'info':
                        await this.displayDetails(exercise, false);
                        break;
                    case 'cloneInCurrentDirectory':
                        await this.clone(exercise, true);
                        break;
                    case 'clone':
                        await this.clone(exercise, undefined);
                        break;
                    case 'delete':
                        await this.delete(exercise.id);
                        return;
                    case 'exit':
                        return;
                    default:
                        ora().fail('Invalid option.');
                        return;
                }
            }
        }
    
        async displayDetails(exercise: Exercise, showActionMenu: boolean = false): Promise<void> {
            ora().info(`Details of the exercise:`);
    
            const oraInfo = (message: string, indent: number = 4) => {
                ora({
                        text  : message,
                        indent: indent
                    }).start().info();
            };
    
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('Id:') } ${ exercise.id }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('Name:') } ${ exercise.name }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('Assignment:') } ${ exercise.assignmentName }`);
    
            // Display exercise teachers
            if ( exercise.assignment?.staff && exercise.assignment?.staff.length > 0 ) {
                oraInfo(`${ TextStyle.LIST_ITEM_NAME('Teachers:') }`);
                exercise.assignment?.staff.forEach(staff => {
                    console.log(`        - ${ staff.gitlabUsername }`);
                });
            } else {
                ora({
                        text  : `${ TextStyle.LIST_ITEM_NAME('Teachers:') } No teachers found for this exercise.`,
                        indent: 4
                    }).start().warn();
            }
    
            // Display exercise members
            if ( exercise.members && exercise.members.length > 0 ) {
                oraInfo(`${ TextStyle.LIST_ITEM_NAME('Members:') }`);
                exercise.members.forEach(member => {
                    console.log(`        - ${ member.gitlabUsername }`);
                });
            } else {
                ora({
                        text  : `${ TextStyle.LIST_ITEM_NAME('Members:') } No members found for this exercise.`,
                        indent: 4
                    }).start().warn();
            }
    
            if ( exercise.assignment?.useSonar ) {
                oraInfo(`${ TextStyle.LIST_ITEM_NAME('Sonar project:') } ${ SharedConfig.sonar.url }/dashboard?id=${ exercise.sonarKey }`);
            }
    
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('Gitlab URL:') } ${ exercise.gitlabCreationInfo.web_url }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('HTTP Repo:') } ${ exercise.gitlabCreationInfo.http_url_to_repo }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('SSH Repo:') } ${ exercise.gitlabCreationInfo.ssh_url_to_repo }`);
    
            if ( showActionMenu ) {
                await this.actionMenu(exercise);
            }
        }
    }
    
    
    export default new ExerciseHelper();