Skip to content
Snippets Groups Projects
Select Git revision
  • ab22d6874a5554417b8b09466ab959d2f4b52d6d
  • main default protected
2 results

SolarSystem.c

Blame
  • ExerciceCreateCommand.ts 4.53 KiB
    import CommanderCommand   from '../CommanderCommand';
    import chalk              from 'chalk';
    import GitlabManager      from '../../managers/GitlabManager';
    import SessionManager     from '../../managers/SessionManager';
    import GitlabUser         from '../../shared/types/Gitlab/GitlabUser';
    import Enonce             from '../../types/Enonce';
    import ora                from 'ora';
    import DojoBackendManager from '../../managers/DojoBackendManager';
    import Exercice           from '../../types/Exercice';
    
    
    class ExerciceCreateCommand extends CommanderCommand {
        protected commandName: string = 'create';
    
        private static _instance: ExerciceCreateCommand;
    
        private constructor() { super(); }
    
        public static get instance(): ExerciceCreateCommand {
            if ( !ExerciceCreateCommand._instance ) {
                ExerciceCreateCommand._instance = new ExerciceCreateCommand();
            }
    
            return ExerciceCreateCommand._instance;
        }
    
        protected defineCommand() {
            this.command
            .description('create a new exercice from an enonce')
            .requiredOption('-e, --enonce <value>', 'enonce source (Dojo enonce ID, Dojo enonce name or Gitlab enonce URL)')
            .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')
            .action(this.commandAction.bind(this));
        }
    
        private async checkAccesses(): Promise<boolean> {
            let sessionResult = await SessionManager.testSession(true, [ 'student' ]);
    
            if ( !sessionResult ) {
                return false;
            }
    
            return (await GitlabManager.testToken(true)).every(result => result);
        }
    
        protected async commandAction(options: any): Promise<void> {
            let members!: Array<GitlabUser> | false;
            let enonce!: Enonce | undefined;
    
            // Check access and retrieve data
            {
                console.log(chalk.cyan('Please wait while we verify and retrieve data...'));
    
                if ( !await this.checkAccesses() ) {
                    return;
                }
    
                members = await GitlabManager.fetchMembers(options);
                if ( !members ) {
                    return;
                }
    
                ora('Checking enonce:').start().info();
                const enonceGetSpinner: ora.Ora = ora({
                                                          text  : 'Checking if enonce exists',
                                                          indent: 4
                                                      }).start();
                enonce = await DojoBackendManager.getEnonce(options.enonce);
                if ( !enonce ) {
                    enonceGetSpinner.fail(`Enonce "${ options.enonce }" doesn't exists`);
                    return;
                }
                enonceGetSpinner.succeed(`Enonce "${ options.enonce }" exists`);
    
                const enoncePublishedSpinner: ora.Ora = ora({
                                                                text  : 'Checking if enonce is published',
                                                                indent: 4
                                                            }).start();
                //TODO : Check if the enonce is published
                //if ( false ) {
                //enoncePublishedSpinner.fail(`Enonce "${ enonce.name }" isn't published`);
                //return;
                //}
                enoncePublishedSpinner.succeed(`Enonce "${ enonce.name }" is published`);
            }
    
            //Create the exercice
            {
                console.log(chalk.cyan('Please wait while we are creating the exercice...'));
    
                try {
                    const exercice: Exercice = await DojoBackendManager.createExercice((enonce as Enonce).name, members);
    
                    const oraInfo = (message: string) => {
                        ora({
                                text  : message,
                                indent: 4
                            }).start().info();
                    };
    
                    oraInfo(`${ chalk.magenta('Id:') } ${ exercice.id }`);
                    oraInfo(`${ chalk.magenta('Name:') } ${ exercice.name }`);
                    oraInfo(`${ chalk.magenta('Web URL:') } ${ exercice.gitlabCreationInfo.web_url }`);
                    oraInfo(`${ chalk.magenta('HTTP Repo:') } ${ exercice.gitlabCreationInfo.http_url_to_repo }`);
                    oraInfo(`${ chalk.magenta('SSH Repo:') } ${ exercice.gitlabCreationInfo.ssh_url_to_repo }`);
                } catch ( error ) {
                    return;
                }
            }
        }
    }
    
    
    export default ExerciceCreateCommand.instance;