Skip to content
Snippets Groups Projects
Select Git revision
  • 5eb5f9f6220851e90f350eba0a10f07c4afe51c5
  • main default protected
  • v3.3.0
  • add_link_in_readme
  • v3.2.1
  • v3.2.0
  • v3.1.2
  • 3.3.0-dev
  • 3.2.0
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
  • 2.2.0
  • 2.1.0
  • 2.0.0
  • v1.0.1
18 results

ExerciseCreateCommand.ts

Blame
  • ExerciseCreateCommand.ts 3.93 KiB
    import CommanderCommand   from '../../CommanderCommand';
    import chalk              from 'chalk';
    import GitlabManager      from '../../../managers/GitlabManager';
    import GitlabUser         from '../../../shared/types/Gitlab/GitlabUser';
    import ora                from 'ora';
    import DojoBackendManager from '../../../managers/DojoBackendManager';
    import AccessesHelper     from '../../../helpers/AccessesHelper';
    import Assignment         from '../../../sharedByClients/models/Assignment';
    import Exercise           from '../../../sharedByClients/models/Exercise';
    
    
    class ExerciseCreateCommand extends CommanderCommand {
        protected commandName: string = 'create';
    
        protected defineCommand() {
            this.command
            .description('create a new exercise from an assignment')
            .requiredOption('-a, --assignment <value>', 'assignment source (Dojo assignment ID, Dojo assignment name or Gitlab assignment 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));
        }
    
        protected async commandAction(options: any): Promise<void> {
            let members!: Array<GitlabUser> | false;
            let assignment!: Assignment | undefined;
    
            // Check access and retrieve data
            {
                console.log(chalk.cyan('Please wait while we verify and retrieve data...'));
    
                if ( !await AccessesHelper.checkStudent() ) {
                    return;
                }
    
                members = await GitlabManager.fetchMembers(options);
                if ( !members ) {
                    return;
                }
    
                ora('Checking assignment:').start().info();
                const assignmentGetSpinner: ora.Ora = ora({
                                                              text: 'Checking if assignment exists', indent: 4
                                                          }).start();
                assignment = await DojoBackendManager.getAssignment(options.assignment);
                if ( !assignment ) {
                    assignmentGetSpinner.fail(`Assignment "${ options.assignment }" doesn't exists`);
                    return;
                }
                assignmentGetSpinner.succeed(`Assignment "${ options.assignment }" exists`);
    
                const assignmentPublishedSpinner: ora.Ora = ora({
                                                                    text: 'Checking if assignment is published', indent: 4
                                                                }).start();
                if ( !assignment.published ) {
                    assignmentPublishedSpinner.fail(`Assignment "${ assignment.name }" isn't published`);
                    return;
                }
                assignmentPublishedSpinner.succeed(`Assignment "${ assignment.name }" is published`);
            }
    
            //Create the exercise
            {
                console.log(chalk.cyan('Please wait while we are creating the exercise...'));
    
                try {
                    const exercise: Exercise = await DojoBackendManager.createExercise((assignment as Assignment).name, members);
    
                    const oraInfo = (message: string) => {
                        ora({
                                text: message, indent: 4
                            }).start().info();
                    };
    
                    oraInfo(`${ chalk.magenta('Id:') } ${ exercise.id }`);
                    oraInfo(`${ chalk.magenta('Name:') } ${ exercise.name }`);
                    oraInfo(`${ chalk.magenta('Web URL:') } ${ exercise.gitlabCreationInfo.web_url }`);
                    oraInfo(`${ chalk.magenta('HTTP Repo:') } ${ exercise.gitlabCreationInfo.http_url_to_repo }`);
                    oraInfo(`${ chalk.magenta('SSH Repo:') } ${ exercise.gitlabCreationInfo.ssh_url_to_repo }`);
                } catch ( error ) {
                    return;
                }
            }
        }
    }
    
    
    export default new ExerciseCreateCommand();