Skip to content
Snippets Groups Projects
Select Git revision
  • 437e7ac97f7642954af977cdc3912a3f5555686a
  • 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

EnonceCreateCommand.ts

Blame
  • EnonceCreateCommand.ts 3.58 KiB
    import CommanderCommand   from '../CommanderCommand';
    import chalk              from 'chalk';
    import ora                from 'ora';
    import GitlabManager      from '../../managers/GitlabManager';
    import GitlabUser         from '../../shared/types/Gitlab/GitlabUser';
    import DojoBackendManager from '../../managers/DojoBackendManager';
    import Enonce             from '../../types/Enonce';
    import Toolbox            from '../../shared/helpers/Toolbox';
    import AccessesHelper     from '../../helpers/AccessesHelper';
    
    
    class EnonceCreateCommand extends CommanderCommand {
        protected commandName: string = 'create';
        
        protected defineCommand() {
            this.command
            .description('create a new repository for an enonce')
            .requiredOption('-n, --name <name>', 'name of the enonce')
            .option('-i, --members_id <ids...>', 'list of gitlab members ids (teaching staff) to add to the repository')
            .option('-u, --members_username <usernames...>', 'list of gitlab members username (teaching staff) to add to the repository')
            .option('-t, --template <string>', 'id or url of the template (http/s and ssh urls are possible)')
            .action(this.commandAction.bind(this));
        }
    
        protected async commandAction(options: any): Promise<void> {
            let members!: Array<GitlabUser> | false;
            let templateIdOrNamespace: string | null = null;
    
            // Check access and retrieve data
            {
                console.log(chalk.cyan('Please wait while we verify and retrieve data...'));
    
                if ( !await AccessesHelper.checkTeachingStaff() ) {
                    return;
                }
    
                members = await GitlabManager.fetchMembers(options);
                if ( !members ) {
                    return;
                }
    
                const enonceGetSpinner: ora.Ora = ora('Checking enonce name availability').start();
                if ( await DojoBackendManager.getEnonce(options.name) ) {
                    enonceGetSpinner.fail(`Enonce name "${ options.name }" is already taken. Please choose another one.`);
                    return;
                }
                enonceGetSpinner.succeed(`Enonce name "${ options.name }" is available`);
    
                if ( options.template ) {
                    templateIdOrNamespace = options.template;
    
                    if ( Number.isNaN(Number(templateIdOrNamespace)) ) {
                        templateIdOrNamespace = Toolbox.urlToPath(templateIdOrNamespace as string);
                    }
    
                    if ( !await DojoBackendManager.checkTemplateAccess(encodeURIComponent(templateIdOrNamespace as string)) ) {
                        return;
                    }
                }
            }
    
            // Create the enonce
            {
                console.log(chalk.cyan('Please wait while we are creating the enonce...'));
    
                try {
                    const enonce: Enonce = await DojoBackendManager.createEnonce(options.name, members, templateIdOrNamespace);
    
                    const oraInfo = (message: string) => {
                        ora({
                                text  : message,
                                indent: 4
                            }).start().info();
                    };
    
                    oraInfo(`${ chalk.magenta('Name:') } ${ enonce.name }`);
                    oraInfo(`${ chalk.magenta('Web URL:') } ${ enonce.gitlabCreationInfo.web_url }`);
                    oraInfo(`${ chalk.magenta('HTTP Repo:') } ${ enonce.gitlabCreationInfo.http_url_to_repo }`);
                    oraInfo(`${ chalk.magenta('SSH Repo:') } ${ enonce.gitlabCreationInfo.ssh_url_to_repo }`);
                } catch ( error ) {
                    return;
                }
            }
        }
    }
    
    
    export default new EnonceCreateCommand();