Skip to content
Snippets Groups Projects
Commit 184385ce authored by kelly.nguyen's avatar kelly.nguyen
Browse files

add git clone to cmd export

parent 13474a47
Branches
Tags
No related merge requests found
Pipeline #33616 failed
...@@ -5,22 +5,26 @@ import SessionManager from "../../../managers/SessionManager"; ...@@ -5,22 +5,26 @@ import SessionManager from "../../../managers/SessionManager";
import ora from "ora"; import ora from "ora";
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
import TextStyle from "../../../types/TextStyle";
import GitlabManager from "../../../managers/GitlabManager";
import User from "../../../sharedByClients/models/User";
// type CommandOptions = { name: string }
// type CommandOptions = { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean }
class UserCreateResumeCommand extends CommanderCommand { class UserCreateResumeCommand extends CommanderCommand {
protected commandName : string = 'resume'; protected commandName : string = 'resume';
protected defineCommand(): void { protected defineCommand(): void {
this.command this.command
.description('list all the user') .description('export the portfolio of an user')
.argument('<userId>', 'id of the user') .argument('<userId>', 'id of the user')
.argument('<pathFile>', 'path of the file') .argument('<pathFile>', 'path of the file')
// .option('-n, --name <name>', 'name of the assignment')
.action(this.commandAction.bind(this)); .action(this.commandAction.bind(this));
// .action((options) => this.commandAction(options));
} }
// protected async commandAction(options: { name: string }): Promise<void> {
private sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
protected async commandAction(userId : string, pathFile : string): Promise<void> { protected async commandAction(userId : string, pathFile : string): Promise<void> {
// check privileges // check privileges
console.log(chalk.cyan('Please wait while we are exporting the resume...')); console.log(chalk.cyan('Please wait while we are exporting the resume...'));
...@@ -45,28 +49,60 @@ class UserCreateResumeCommand extends CommanderCommand { ...@@ -45,28 +49,60 @@ class UserCreateResumeCommand extends CommanderCommand {
return; return;
} }
const portfolioCreated = await DojoBackendManager.createPortfolio(userId); // START LIGHT EXPORT
// const portfolioCreated = await DojoBackendManager.createPortfolio(userId);
// if (!portfolioCreated) {
// resumeSpinner.fail('Failed to create portfolio');
// return;
// }
if (!portfolioCreated) { // await this.sleep(2000);
// END LIGHT EXPORT
// START COMPLET EXPORT
const userDataAssignment : User = await DojoBackendManager.getUserAssignments(userId);
const userDataExercise : User = await DojoBackendManager.getUserExercises(userId);
if (!userDataAssignment) {
resumeSpinner.fail('Failed to create portfolio'); resumeSpinner.fail('Failed to create portfolio');
return; return;
} }
if (!userDataExercise) {
resumeSpinner.fail('Failed to create portfolio');
return;
}
const pathAssignments = userDataAssignment.gitlabUsername + '/assignments'
const pathExercises = userDataAssignment.gitlabUsername + '/exercises'
userDataAssignment.assignments?.forEach(el => {
this.cloneRepository(pathAssignments, el.gitlabCreationInfo.ssh_url_to_repo, el.name);
});
userDataExercise.exercises?.forEach(el => {
this.cloneRepository(pathExercises, el.gitlabCreationInfo.ssh_url_to_repo, el.name);
});
// END COMPLET EXPORT
resumeSpinner.succeed('File downloaded and saved successfully.');
// this.downloadFile(userId, pathFile).then(() => {
// resumeSpinner.succeed('File downloaded and saved successfully.');
// }).catch((error) => {
// resumeSpinner.fail(`Error download : ${error}`);
// });
await this.sleep(2000);
this.downloadFile(userId, pathFile).then(() => {
// console.log('File downloaded and saved successfully.');
resumeSpinner.succeed('File downloaded and saved successfully.');
}).catch((error) => {
// console.error('Error:', error);
resumeSpinner.fail(`Error download : ${error}`);
});
} catch (error) { } catch (error) {
// console.error('Error during export process:', error); console.log(error);
resumeSpinner.fail('Error during export process'); resumeSpinner.fail('Error during export process');
} }
} }
private sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
private async downloadFile(id : string, pathFile : string) { private async downloadFile(id : string, pathFile : string) {
try { try {
const response = await DojoBackendManager.createZip(id); const response = await DojoBackendManager.createZip(id);
...@@ -84,6 +120,14 @@ class UserCreateResumeCommand extends CommanderCommand { ...@@ -84,6 +120,14 @@ class UserCreateResumeCommand extends CommanderCommand {
console.error('Error downloading file:', error); console.error('Error downloading file:', error);
} }
} }
private async cloneRepository(clonePath : string, url : string, folderName : string) {
if ( clonePath ) {
console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...\n'));
await GitlabManager.cloneRepositoryExport(clonePath, url, folderName, true, 0);
}
}
} }
export default new UserCreateResumeCommand(); export default new UserCreateResumeCommand();
\ No newline at end of file
...@@ -210,6 +210,44 @@ class GitlabManager extends SharedGitlabManager { ...@@ -210,6 +210,44 @@ class GitlabManager extends SharedGitlabManager {
} }
} }
} }
public async cloneRepositoryExport(clonePath: string | boolean, repositorySshUrl: string, folderName?: string, verbose: boolean = false, verboseIndent: number = 0) {
let path = '/tmp/';
if ( typeof clonePath === 'string' ) {
path = 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?.replace(' ', '_') ?? '' }"`, {
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');
}
}
}
} }
......
Subproject commit 0b209c36d0aad3a032c9bbe632207dc61a2a6cf8 Subproject commit 73bc34d30e3a247464e5529d6bbe24d741271842
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment