Select Git revision
AssignmentCreateCommand.ts
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) => {