Skip to content
Snippets Groups Projects
Select Git revision
  • 7c642e2573255bdbdf6b19291199d68bf3b3795f
  • main default protected
  • add_export_route
  • add_route_assignments
  • add_route_user
  • 4.1.0-dev
  • Pre-alpha
  • 4.0.1
  • Latest
  • 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
  • 3.0.0
  • 2.2.0
  • 2.1.0
  • 2.0.0
25 results

EnonceCreateCommand.ts

Blame
  • Forked from Dojo Project (HES-SO) / Projects / UI / DojoCLI
    Source project has a limited visibility.
    EnonceCreateCommand.ts 5.31 KiB
    import CommanderCommand   from '../CommanderCommand';
    import chalk              from 'chalk';
    import ora                from 'ora';
    import GitlabManager      from '../../managers/GitlabManager';
    import SessionManager     from '../../managers/SessionManager';
    import GitlabUser         from '../../shared/types/Gitlab/GitlabUser';
    import DojoBackendManager from '../../managers/DojoBackendManager';
    import Toolbox            from '../../shared/Toolbox';
    import Enonce             from '../../types/Enonce';
    
    
    class EnonceCreateCommand extends CommanderCommand {
        protected commandName: string = 'create';
    
        private static _instance: EnonceCreateCommand;
    
        private constructor() { super(); }
    
        public static get instance(): EnonceCreateCommand {
            if ( !EnonceCreateCommand._instance ) {
                EnonceCreateCommand._instance = new EnonceCreateCommand();
            }
    
            return EnonceCreateCommand._instance;
        }
    
        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 members ids (teaching staff) to add to the repository.')
            .option('-m, --members_username <usernames...>', 'list of 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));
        }
    
        private async checkAccesses(): Promise<boolean> {
            let sessionResult = await SessionManager.testSession(true, [ 'teachingStaff' ]);
    
            if ( !sessionResult || !sessionResult.teachingStaff ) {
                return false;
            }
    
            return (await GitlabManager.testToken(true)).every(result => result);
        }
    
        private async fetchMembers(options: any): Promise<Array<GitlabUser> | false> {
            ora('Checking Gitlab members:').start().info();
    
            let members: Array<GitlabUser> = [];
    
            async function getMembers<T>(context: any, functionName: string, paramsToSearch: Array<T>): Promise<boolean> {
                const result = await (context[functionName] as (arg: Array<T>, verbose: boolean, verboseIndent: number) => Promise<Array<GitlabUser | undefined>>)(paramsToSearch, true, 8);
    
                if ( result.every(user => user) ) {
                    members = members.concat(result as Array<GitlabUser>);
                    return true;
                } else {
                    return false;
                }
            }
    
            let result = true;
    
            if ( options.members_id ) {
                ora({
                        text  : 'Fetching members by id:',
                        indent: 4
                    }).start().info();
    
                result = await getMembers(GitlabManager, 'getUsersById', options.members_id);
            }
    
            if ( options.members_username ) {
                ora({
                        text  : 'Fetching members by username:',
                        indent: 4
                    }).start().info();
    
                result = result && await getMembers(GitlabManager, 'getUsersByUsername', options.members_username);
            }
    
            if ( !result ) {
                return false;
            }
    
            members = members.reduce((unique, user) => (unique.findIndex(uniqueUser => uniqueUser.id === user.id) !== -1 ? unique : [ ...unique, user ]), Array<GitlabUser>());
    
            return members;
        }
    
        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 this.checkAccesses() ) {
                    return;
                }
    
                members = await this.fetchMembers(options);
                if ( !members ) {
                    return;
                }
    
                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.createProject(options.name, members, templateIdOrNamespace);
    
                    const oraInfo = (message: string) => {
                        ora({
                                text  : message,
                                indent: 4
                            }).start().info();
                    };
    
                    oraInfo(`Dojo ID: ${ enonce.id }`);
                    oraInfo(`Name: ${ enonce.name }`);
                    oraInfo(`Web URL: ${ enonce.gitlabCreationInfo.web_url }`);
                    oraInfo(`HTTP Repo: ${ enonce.gitlabCreationInfo.http_url_to_repo }`);
                    oraInfo(`SSH Repo: ${ enonce.gitlabCreationInfo.ssh_url_to_repo }`);
                } catch ( error ) {
                    return;
                }
            }
        }
    }
    
    
    export default EnonceCreateCommand.instance;