Skip to content
Snippets Groups Projects
Select Git revision
  • 8afcb347749b77205329b172292a2416468567c1
  • main default protected
  • jw_sonar
  • v6.0.0 protected
  • bedran_exercise-list
  • ask-user-to-delete-exercises-on-duplicates
  • update-dependencies
  • jw_sonar_backup
  • add_route_assignments
  • 6.0.0-dev
  • 5.0.1
  • 5.0.0
  • 4.1.0
  • 4.0.0
  • 3.5.3
  • 3.5.2
  • 3.5.1
  • 3.5.0
  • 3.4.2
  • 3.4.1
  • 3.4.0
  • 3.3.0
  • 3.2.0
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 3.0.1
  • 3.0.0
29 results

DojoModelsHelper.ts

Blame
  • AssignmentCreateCommand.ts 4.54 KiB
    import CommanderCommand   from '../../CommanderCommand';
    import ora                from 'ora';
    import AccessesHelper     from '../../../helpers/AccessesHelper';
    import Assignment         from '../../../sharedByClients/models/Assignment';
    import DojoBackendManager from '../../../managers/DojoBackendManager';
    import Toolbox            from '../../../shared/helpers/Toolbox';
    import * as Gitlab        from '@gitbeaker/rest';
    import TextStyle          from '../../../types/TextStyle';
    import GitlabManager      from '../../../managers/GitlabManager';
    
    
    type CommandOptions = { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean }
    
    
    class AssignmentCreateCommand extends CommanderCommand {
        protected commandName: string = 'create';
    
        private members!: Array<Gitlab.UserSchema> | undefined;
        private templateIdOrNamespace: string | null = null;
        private assignment!: Assignment;
    
        protected defineCommand() {
            this.command
                .description('create a new repository for an assignment')
                .requiredOption('-n, --name <name>', 'name of the assignment')
                .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)')
                .option('-c, --clone [string]', 'automatically clone the repository (SSH required) in the specified directory (this will create a subdirectory with the assignment name)')
                .action(this.commandAction.bind(this));
        }
    
        private async dataRetrieval(options: CommandOptions) {
            console.log(TextStyle.BLOCK('Please wait while we verify and retrieve data...'));
    
            if ( !await AccessesHelper.checkTeachingStaff() ) {
                throw new Error();
            }
    
            this.members = await GitlabManager.fetchMembers(options);
            if ( !this.members ) {
                throw new Error();
            }
    
            const assignmentGetSpinner: ora.Ora = ora('Checking assignment name availability').start();
            if ( await DojoBackendManager.getAssignment(options.name) ) {
                assignmentGetSpinner.fail(`Assignment name "${ options.name }" is already taken. Please choose another one.`);
                throw new Error();
            }
            assignmentGetSpinner.succeed(`Assignment name "${ options.name }" is available`);
    
            if ( options.template ) {
                this.templateIdOrNamespace = options.template;
    
                if ( Number.isNaN(Number(this.templateIdOrNamespace)) ) {
                    this.templateIdOrNamespace = Toolbox.urlToPath(this.templateIdOrNamespace);
                }
    
                if ( !await DojoBackendManager.checkTemplateAccess(encodeURIComponent(this.templateIdOrNamespace)) ) {
                    throw new Error();
                }
            }
        }
    
        private async createAssignment(options: CommandOptions) {
            console.log(TextStyle.BLOCK('Please wait while we are creating the assignment (approximately 10 seconds)...'));
    
            this.assignment = await DojoBackendManager.createAssignment(options.name, this.members!, this.templateIdOrNamespace);
    
            const oraInfo = (message: string) => {
                ora({
                        text  : message,
                        indent: 4
                    }).start().info();
            };
    
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('Name:') } ${ this.assignment.name }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('Web URL:') } ${ this.assignment.gitlabCreationInfo.web_url }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('HTTP Repo:') } ${ this.assignment.gitlabCreationInfo.http_url_to_repo }`);
            oraInfo(`${ TextStyle.LIST_ITEM_NAME('SSH Repo:') } ${ this.assignment.gitlabCreationInfo.ssh_url_to_repo }`);
        }
    
        private async cloneRepository(options: CommandOptions) {
            if ( options.clone ) {
                console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...'));
    
                await GitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0);
            }
        }
    
        protected async commandAction(options: CommandOptions): Promise<void> {
            try {
                await this.dataRetrieval(options);
                await this.createAssignment(options);
                await this.cloneRepository(options);
            } catch ( e ) { /* Do nothing */ }
        }
    }
    
    
    export default new AssignmentCreateCommand();