diff --git a/ExpressAPI/prisma/seed.ts b/ExpressAPI/prisma/seed.ts index 20249cf1ee8d9779400f3483413eb0b10d643205..6656ebd99d7922f2148740eb336da0bdad74d464 100644 --- a/ExpressAPI/prisma/seed.ts +++ b/ExpressAPI/prisma/seed.ts @@ -338,7 +338,8 @@ async function assignments() { 'autoclose_referenced_issues' : true }, gitlabLastInfoDate: new Date('2021-10-14T14:20:15.239Z'), - published : false + published : false, + secret : '77B9BB29-82F8-4D1F-B6D2-A201ABFB8509' } }); await db.assignment.upsert({ @@ -593,7 +594,8 @@ async function assignments() { 'autoclose_referenced_issues' : true }, gitlabLastInfoDate: new Date('2023-11-07T20:35:54.692Z'), - published : true + published : true, + secret : '1127A3E7-4461-43B9-85DE-13C2317617AA' } }); } diff --git a/ExpressAPI/src/helpers/GlobalHelper.ts b/ExpressAPI/src/helpers/GlobalHelper.ts index 76c85b016cc04f7b280e4c3aefd8c3cc5847f357..377e8de060533bd91738af68d2e66a3d86bb033a 100644 --- a/ExpressAPI/src/helpers/GlobalHelper.ts +++ b/ExpressAPI/src/helpers/GlobalHelper.ts @@ -5,9 +5,11 @@ import GitlabManager from '../managers/GitlabManager'; import { StatusCodes } from 'http-status-codes'; import DojoStatusCode from '../shared/types/Dojo/DojoStatusCode'; import { AxiosError } from 'axios'; +import SonarManager from '../managers/SonarManager'; +import SonarProjectCreation from '../shared/types/Sonar/SonarProjectCreation'; class GlobalHelper { - async repositoryCreationError(message: string, error: unknown, req: express.Request, res: express.Response, gitlabError: DojoStatusCode, internalError: DojoStatusCode, repositoryToRemove?: GitlabRepository): Promise<void> { + async repositoryCreationError(message: string, error: unknown, req: express.Request, res: express.Response, gitlabError: DojoStatusCode, internalError: DojoStatusCode, repositoryToRemove?: GitlabRepository, sonarToRemove?: SonarProjectCreation): Promise<void> { logger.error(message); logger.error(error); @@ -15,6 +17,9 @@ class GlobalHelper { if ( repositoryToRemove ) { await GitlabManager.deleteRepository(repositoryToRemove.id); } + if ( sonarToRemove ) { + await SonarManager.deleteProject(sonarToRemove.project.key); + } } catch ( error ) { logger.error('Repository deletion error'); logger.error(error); diff --git a/ExpressAPI/src/managers/GitlabManager.ts b/ExpressAPI/src/managers/GitlabManager.ts index 3455f8b7accad04dc7c09ace93b1c297cb633d49..e138386718d29fc2ac100db79e642bb8e7e0945c 100644 --- a/ExpressAPI/src/managers/GitlabManager.ts +++ b/ExpressAPI/src/managers/GitlabManager.ts @@ -148,6 +148,7 @@ class GitlabManager { } async addRepositoryVariable(repoId: number, key: string, value: string, isProtected: boolean, isMasked: boolean): Promise<GitlabMember> { + console.log(key, value); const response = await axios.post<GitlabMember>(this.getApiUrl(GitlabRoute.REPOSITORY_VARIABLES_ADD).replace('{{id}}', String(repoId)), { key : key, variable_type: 'env_var', diff --git a/ExpressAPI/src/managers/SonarManager.ts b/ExpressAPI/src/managers/SonarManager.ts index 88320a4cca56bc618ef58c066a70e5df661a13ba..9c8946ecaaa17cf974934a717ed613d58a0344c2 100644 --- a/ExpressAPI/src/managers/SonarManager.ts +++ b/ExpressAPI/src/managers/SonarManager.ts @@ -88,14 +88,14 @@ class SonarManager { return await GlobalHelper.repositoryCreationError('Sonar error', undefined, req, res, DojoStatusCode.ASSIGNMENT_CREATION_SONAR_ERROR, DojoStatusCode.ASSIGNMENT_CREATION_SONAR_ERROR, gitlabRepository); } } catch ( error ) { - return await GlobalHelper.repositoryCreationError('Sonar project creation error', error, req, res, DojoStatusCode.ASSIGNMENT_CREATION_SONAR_ERROR, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, gitlabRepository); + return await GlobalHelper.repositoryCreationError('Sonar project creation error', error, req, res, DojoStatusCode.ASSIGNMENT_CREATION_SONAR_ERROR, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, gitlabRepository, sonarProject); } // Add gate and profiles to sonar project if ( qualityGate != undefined && qualityGate != "" ) { try { await this.addQualityGate(sonarProject.project.key, qualityGate); } catch ( error ) { - return await GlobalHelper.repositoryCreationError('Sonar gate error', error, req, res, DojoStatusCode.ASSIGNMENT_SONAR_GATE_NOT_FOUND, DojoStatusCode.ASSIGNMENT_SONAR_GATE_NOT_FOUND, gitlabRepository); + return await GlobalHelper.repositoryCreationError('Sonar gate error', error, req, res, DojoStatusCode.ASSIGNMENT_SONAR_GATE_NOT_FOUND, DojoStatusCode.ASSIGNMENT_SONAR_GATE_NOT_FOUND, gitlabRepository, sonarProject); } } @@ -106,10 +106,10 @@ class SonarManager { if (lang.trim() != '' && name.trim() != '') { await this.addQualityProfile(sonarProject.project.key, name.trim(), lang.trim()); } else { - return await GlobalHelper.repositoryCreationError('Sonar profile invalid', undefined, req, res, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, gitlabRepository); + return await GlobalHelper.repositoryCreationError('Sonar profile invalid', undefined, req, res, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, gitlabRepository, sonarProject); } } catch ( error ) { - return await GlobalHelper.repositoryCreationError('Sonar profile not found', error, req, res, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, gitlabRepository); + return await GlobalHelper.repositoryCreationError('Sonar profile not found', error, req, res, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, DojoStatusCode.ASSIGNMENT_SONAR_PROFILE_NOT_FOUND, gitlabRepository, sonarProject); } } } @@ -117,6 +117,13 @@ class SonarManager { return sonarProject; } + async deleteProject(projectKey: string) { + const formData = new FormData(); + formData.append('project', projectKey); + + return await this.executePostRequest<SonarProjectCreation>(this.getApiUrl(SonarRoute.PROJECT_DELETE), formData) + } + async getLanguages() { const resp = await this.executeGetRequest<{ languages: { key: string, name: string }[]}>(this.getApiUrl(SonarRoute.GET_LANGUAGES)) return resp.languages.map(l => l.key) diff --git a/ExpressAPI/src/routes/AssignmentRoutes.ts b/ExpressAPI/src/routes/AssignmentRoutes.ts index 3229bb16b93c8f1e78e8a13b812dc0581203c61e..ed956528054a27055fcbe2ccb040fbaa3f191cca 100644 --- a/ExpressAPI/src/routes/AssignmentRoutes.ts +++ b/ExpressAPI/src/routes/AssignmentRoutes.ts @@ -137,10 +137,11 @@ class AssignmentRoutes implements RoutesManager { try { await GitlabManager.protectBranch(repository.id, '*', true, GitlabAccessLevel.DEVELOPER, GitlabAccessLevel.DEVELOPER, GitlabAccessLevel.OWNER); - await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_ASSIGNMENT_NAME', repository.name, false, true); + await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_ASSIGNMENT_NAME', repository.name, false, false); await GitlabManager.addRepositoryVariable(repository.id, 'DOJO_ASSIGNMENT_SECRET', secret, false, true); await GitlabManager.addRepositoryBadge(repository.id, Config.gitlab.badges.pipeline.link, Config.gitlab.badges.pipeline.imageUrl, 'Pipeline Status'); + } catch ( error ) { return GlobalHelper.repositoryCreationError('Repo params error', error, req, res, DojoStatusCode.ASSIGNMENT_CREATION_GITLAB_ERROR, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, repository); } @@ -209,13 +210,14 @@ class AssignmentRoutes implements RoutesManager { } }; }) ] - } + }, + secret: secret } }) as unknown as Assignment; return req.session.sendResponse(res, StatusCodes.OK, assignment); } catch ( error ) { - return GlobalHelper.repositoryCreationError('DB error', error, req, res, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, repository); + return GlobalHelper.repositoryCreationError('DB error', error, req, res, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, DojoStatusCode.ASSIGNMENT_CREATION_INTERNAL_ERROR, repository, sonarProject); } } diff --git a/ExpressAPI/src/routes/ExerciseRoutes.ts b/ExpressAPI/src/routes/ExerciseRoutes.ts index f56c3c4fd3e431d2a858f94abc46a6bef9659969..b8a89d769c5e33214ab49a1db62b99deb559f65f 100644 --- a/ExpressAPI/src/routes/ExerciseRoutes.ts +++ b/ExpressAPI/src/routes/ExerciseRoutes.ts @@ -111,6 +111,7 @@ class ExerciseRoutes implements RoutesManager { const exerciseId: string = uuidv4(); const secret: string = uuidv4(); let repository!: GitlabRepository; + let sonarProject: SonarProjectCreation | undefined = undefined; let suffix: number = 0; do { @@ -170,7 +171,6 @@ class ExerciseRoutes implements RoutesManager { })); // Create Sonar project - let sonarProject: SonarProjectCreation | undefined = undefined; console.log(assignment, repository); if (assignment.useSonar && assignment.sonarProfiles != null) { const profiles: string[] = JSON.parse(assignment.sonarProfiles as string); @@ -213,7 +213,7 @@ class ExerciseRoutes implements RoutesManager { return req.session.sendResponse(res, StatusCodes.OK, exercise); } catch ( error ) { - return GlobalHelper.repositoryCreationError('DB error', error, req, res, DojoStatusCode.EXERCISE_CREATION_GITLAB_ERROR, DojoStatusCode.EXERCISE_CREATION_INTERNAL_ERROR, repository); + return GlobalHelper.repositoryCreationError('DB error', error, req, res, DojoStatusCode.EXERCISE_CREATION_GITLAB_ERROR, DojoStatusCode.EXERCISE_CREATION_INTERNAL_ERROR, repository, sonarProject); } } diff --git a/ExpressAPI/src/shared b/ExpressAPI/src/shared index 102e79d9b78d79e495f7b82e5e767eb7898248db..44b2ae365423ca96ee035d0c21bf36a27141aa79 160000 --- a/ExpressAPI/src/shared +++ b/ExpressAPI/src/shared @@ -1 +1 @@ -Subproject commit 102e79d9b78d79e495f7b82e5e767eb7898248db +Subproject commit 44b2ae365423ca96ee035d0c21bf36a27141aa79