Skip to content
Snippets Groups Projects
Select Git revision
  • 90a9ce23fd9043f9204afd0baa03e883cb367f32
  • 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.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) => {