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

AssignmentCreateCommand.ts

Blame
  • AssignmentCreateCommand.ts 4.49 KiB
    import CommanderCommand   from '../../CommanderCommand.js';
    import ora                from 'ora';
    import AccessesHelper     from '../../../helpers/AccessesHelper.js';
    import Assignment         from '../../../sharedByClients/models/Assignment.js';
    import DojoBackendManager from '../../../managers/DojoBackendManager.js';
    import Toolbox            from '../../../shared/helpers/Toolbox.js';
    import * as Gitlab        from '@gitbeaker/rest';
    import TextStyle          from '../../../types/TextStyle.js';
    import Config             from '../../../config/Config';
    
    
    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...'));
    
            await AccessesHelper.checkTeachingStaff();
    
            this.members = await Config.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(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 Config.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();