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

add function for export full repo

parent 213bf1dc
No related branches found
No related tags found
No related merge requests found
Pipeline #33112 failed
-- AlterTable
ALTER TABLE `Exercise` ADD COLUMN `correctionCommit` JSON NULL;
...@@ -51,6 +51,8 @@ model Exercise { ...@@ -51,6 +51,8 @@ model Exercise {
gitlabLastInfo Json @db.Json gitlabLastInfo Json @db.Json
gitlabLastInfoDate DateTime gitlabLastInfoDate DateTime
correctionCommit Json? @db.Json
assignment Assignment @relation(fields: [assignmentName], references: [name], onDelete: NoAction, onUpdate: Cascade) assignment Assignment @relation(fields: [assignmentName], references: [name], onDelete: NoAction, onUpdate: Cascade)
members User[] members User[]
......
...@@ -286,6 +286,23 @@ class GitlabManager { ...@@ -286,6 +286,23 @@ class GitlabManager {
}); });
return response.data; return response.data;
} }
async scheduleExportRepository(repoId : number) {
const response = await axios.post(this.getApiUrl(GitlabRoute.REPOSITORY_SCHEDULE_EXPORT).replace('{{id}}', String(repoId)));
return response.data;
}
async exportStatusRepository(repoId : number) {
const response = await axios.get(this.getApiUrl(GitlabRoute.REPOSITORY_EXPORT_STATUS).replace('{{id}}', String(repoId)));
return response.data;
}
async exportDownloadRepository(repoId : number) {
const response = await axios.get(this.getApiUrl(GitlabRoute.REPOSITORY_DOWNLOAD).replace('{{id}}', String(repoId)), {
responseType: 'arraybuffer',
});
return response.data;
}
} }
......
...@@ -52,6 +52,8 @@ class AssignmentRoutes implements RoutesManager { ...@@ -52,6 +52,8 @@ class AssignmentRoutes implements RoutesManager {
backend.patch('/assignments/:assignmentNameOrUrl/unpublish', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.unpublishAssignment.bind(this)); backend.patch('/assignments/:assignmentNameOrUrl/unpublish', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.unpublishAssignment.bind(this));
backend.patch('/assignments/:assignmentNameOrUrl/deleted', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.deleteAssignment.bind(this)); backend.patch('/assignments/:assignmentNameOrUrl/deleted', SecurityMiddleware.check(true, SecurityCheckType.ASSIGNMENT_STAFF), this.deleteAssignment.bind(this));
backend.get('/assignments/:assignmentNameOrUrl/export', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), this.exportAssign.bind(this));
backend.get('/assignments/:assignmentNameOrUrl/createSummary', SecurityMiddleware.check(true, SecurityCheckType.TEACHING_STAFF), this.createSummary.bind(this));
} }
// Get an assignment by its name or gitlab url // Get an assignment by its name or gitlab url
private async getAssignment(req: express.Request, res: express.Response) { private async getAssignment(req: express.Request, res: express.Response) {
...@@ -256,6 +258,91 @@ class AssignmentRoutes implements RoutesManager { ...@@ -256,6 +258,91 @@ class AssignmentRoutes implements RoutesManager {
return req.session.sendResponse(res, StatusCodes.OK); return req.session.sendResponse(res, StatusCodes.OK);
} }
// private async exportAssign(req : express.Request, res : express.Response) {
// const nameAssignment = req.params.assignmentNameOrUrl;
// let exportFinished = false;
// const repo = await db.assignment.findUnique({
// where : {
// name : String(nameAssignment)
// }
// });
// if (!repo) {
// return req.session.sendResponse(res, StatusCodes.NOT_FOUND);
// }
// const repoId = repo['gitlabId'];
// try {
// const resExport = await GitlabManager.scheduleExportRepository(repoId);
// if (resExport['message'] == '202 Accepted') {
// while (!exportFinished) {
// const exportStatus = await GitlabManager.exportStatusRepository(repoId);
// if (exportStatus['export_status'] == 'finished') {
// exportFinished = true;
// const repoData = await GitlabManager.exportDownloadRepository(repo['gitlabId']);
// logger.debug(repoData);
// // fs.writeFileSync('./tmprepo.tar', repoData.data);
// return req.session.sendResponse(res, StatusCodes.OK, exportStatus['_links']['web_url']);
// }
// }
// }
// } catch (error) {
// logger.error(`Error while downloading the repo with the error : ${error}`);
// }
// return req.session.sendResponse(res, StatusCodes.OK);
// }
private async exportAssign(req: express.Request, res: express.Response) {
const nameAssignment = req.params.assignmentNameOrUrl;
let exportFinished = false;
const repo = await db.assignment.findUnique({
where: { name: String(nameAssignment) }
});
if (!repo) {
return req.session.sendResponse(res, StatusCodes.NOT_FOUND);
}
const repoId = repo['gitlabId'];
try {
const resExport = await GitlabManager.scheduleExportRepository(repoId);
if (resExport['message'] === '202 Accepted') {
while (!exportFinished) {
const exportStatus = await GitlabManager.exportStatusRepository(repoId);
if (exportStatus['export_status'] === 'finished') {
exportFinished = true;
const repoData = await GitlabManager.exportDownloadRepository(repoId);
logger.debug(repoData);
if (!repoData.data || repoData.data.length === 0) {
logger.error('The downloaded export file is empty.');
return req.session.sendResponse(res, StatusCodes.INTERNAL_SERVER_ERROR, 'Export file is empty.');
}
// Écriture du fichier exporté
// fs.writeFileSync('./tmprepo.tar', repoData.data);
return req.session.sendResponse(res, StatusCodes.OK, exportStatus['_links']['web_url']);
} else {
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
} else {
logger.error(`Failed to initiate export: ${resExport['message']}`);
return req.session.sendResponse(res, StatusCodes.INTERNAL_SERVER_ERROR, 'Failed to initiate export.');
}
} catch (error) {
logger.error(`Error while downloading the repo: ${error}`);
return req.session.sendResponse(res, StatusCodes.INTERNAL_SERVER_ERROR, `Error while downloading the repo: ${error}`);
}
return req.session.sendResponse(res, StatusCodes.OK);
}
} }
......
...@@ -19,6 +19,9 @@ enum GitlabRoute { ...@@ -19,6 +19,9 @@ enum GitlabRoute {
REPOSITORY_PIPELINES = '/projects/{{id}}/pipelines', REPOSITORY_PIPELINES = '/projects/{{id}}/pipelines',
REPOSITORY_MEMBER_DELETE = '/projects/{{id}}/members/{{user_id}}', REPOSITORY_MEMBER_DELETE = '/projects/{{id}}/members/{{user_id}}',
REPOSITORY_MOVE_SUBGROUP = '/projects/{{id}}/transfer', REPOSITORY_MOVE_SUBGROUP = '/projects/{{id}}/transfer',
REPOSITORY_SCHEDULE_EXPORT = '/projects/{{id}}/export',
REPOSITORY_EXPORT_STATUS = '/projects/{{id}}/export',
REPOSITORY_DOWNLOAD = '/projects/{{id}}/export/download',
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment