Skip to content
Snippets Groups Projects
Commit 1c5039cf authored by michael.minelli's avatar michael.minelli
Browse files

Repo creation => Add possibility to auto clone the repo

parent e022a904
No related branches found
No related tags found
No related merge requests found
Pipeline #27268 passed
import CommanderCommand from '../../CommanderCommand';
import chalk from 'chalk';
import ora from 'ora';
import GitlabManager from '../../../managers/GitlabManager';
import AccessesHelper from '../../../helpers/AccessesHelper';
import Assignment from '../../../sharedByClients/models/Assignment';
import GitlabUser from '../../../shared/types/Gitlab/GitlabUser';
import GitlabManager from '../../../managers/GitlabManager';
import DojoBackendManager from '../../../managers/DojoBackendManager';
import Toolbox from '../../../shared/helpers/Toolbox';
import AccessesHelper from '../../../helpers/AccessesHelper';
import Assignment from '../../../sharedByClients/models/Assignment';
class AssignmentCreateCommand extends CommanderCommand {
......@@ -19,12 +19,14 @@ class AssignmentCreateCommand extends CommanderCommand {
.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));
}
protected async commandAction(options: { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string> }): Promise<void> {
protected async commandAction(options: { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean }): Promise<void> {
let members!: Array<GitlabUser> | false;
let templateIdOrNamespace: string | null = null;
let assignment!: Assignment;
// Check access and retrieve data
{
......@@ -64,7 +66,7 @@ class AssignmentCreateCommand extends CommanderCommand {
console.log(chalk.cyan('Please wait while we are creating the assignment (approximately 10 seconds)...'));
try {
const assignment: Assignment = await DojoBackendManager.createAssignment(options.name, members, templateIdOrNamespace);
assignment = await DojoBackendManager.createAssignment(options.name, members, templateIdOrNamespace);
const oraInfo = (message: string) => {
ora({
......@@ -81,6 +83,15 @@ class AssignmentCreateCommand extends CommanderCommand {
return;
}
}
// Clone the repository
{
if ( options.clone ) {
console.log(chalk.cyan('Please wait while we are cloning the repository...'));
await GitlabManager.cloneRepository(options.clone, assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0);
}
}
}
}
......
......@@ -18,12 +18,14 @@ class ExerciseCreateCommand extends CommanderCommand {
.requiredOption('-a, --assignment <value>', 'assignment source (Dojo assignment ID, Dojo assignment name or Gitlab assignment URL)')
.option('-i, --members_id <ids...>', 'list of gitlab members ids (group\'s student) to add to the repository')
.option('-u, --members_username <usernames...>', 'list of gitlab members username (group\'s student) to add to the repository')
.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));
}
protected async commandAction(options: { assignment: string, members_id?: Array<number>, members_username?: Array<string> }): Promise<void> {
protected async commandAction(options: { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean }): Promise<void> {
let members!: Array<GitlabUser> | false;
let assignment!: Assignment | undefined;
let exercise!: Exercise;
// Check access and retrieve data
{
......@@ -66,7 +68,7 @@ class ExerciseCreateCommand extends CommanderCommand {
console.log(chalk.cyan('Please wait while we are creating the exercise (approximately 10 seconds)...'));
try {
const exercise: Exercise = await DojoBackendManager.createExercise((assignment as Assignment).name, members);
exercise = await DojoBackendManager.createExercise((assignment as Assignment).name, members);
const oraInfo = (message: string) => {
ora({
......@@ -84,6 +86,15 @@ class ExerciseCreateCommand extends CommanderCommand {
return;
}
}
// Clone the repository
{
if ( options.clone ) {
console.log(chalk.cyan('Please wait while we are cloning the repository...'));
await GitlabManager.cloneRepository(options.clone, assignment.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise - ${ exercise.assignmentName }`, true, 0);
}
}
}
}
......
......@@ -4,6 +4,8 @@ import GitlabUser from '../shared/types/Gitlab/GitlabUser';
import GitlabRoute from '../shared/types/Gitlab/GitlabRoute';
import SharedConfig from '../shared/config/SharedConfig';
import GitlabRepository from '../shared/types/Gitlab/GitlabRepository';
import fs from 'fs-extra';
import { spawn } from 'child_process';
class GitlabManager {
......@@ -179,6 +181,44 @@ class GitlabManager {
return members;
}
public async cloneRepository(clonePath: string | boolean, repositorySshUrl: string, folderName?: string, verbose: boolean = false, verboseIndent: number = 0) {
let path = './';
if ( typeof clonePath === 'string' ) {
path = clonePath;
fs.mkdirSync(path, { recursive: true });
}
let cloningSpinner!: ora.Ora;
if ( verbose ) {
cloningSpinner = ora({
text : 'Cloning the repository...',
indent: verboseIndent
}).start();
}
try {
await new Promise<void>((resolve, reject) => {
const gitClone = spawn(`git clone ${ repositorySshUrl } "${ folderName ?? '' }"`, {
cwd : path,
shell: true
});
gitClone.on('exit', (code) => {
code !== null && code == 0 ? resolve() : reject();
});
});
if ( verbose ) {
cloningSpinner.succeed('Repository cloned');
}
} catch ( error ) {
if ( verbose ) {
cloningSpinner.fail('Error while cloning the repository');
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment