From 5c6dd397bcb5579cf1f9ceaa71de391c4fe3e36c Mon Sep 17 00:00:00 2001 From: Nolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:22:44 +0200 Subject: [PATCH] SONAR-22559 Update permissions during project onboarding --- .../java/org/sonar/db/ce/CeTaskTypes.java | 1 + server/sonar-server-common/build.gradle | 2 ++ .../server/project/VisibilityService.java | 8 ++++- .../server/project/VisibilityServiceTest.java | 34 +++++++++++-------- .../resources/org/sonar/l10n/core.properties | 1 + 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskTypes.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskTypes.java index 28fedd467d7..935f26187a1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskTypes.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskTypes.java @@ -27,6 +27,7 @@ public final class CeTaskTypes { public static final String PROJECT_EXPORT = "PROJECT_EXPORT"; public static final String GITHUB_PROJECT_PERMISSIONS_PROVISIONING = "GITHUB_PROJECT_PERMISSIONS_PROVISIONING"; + public static final String GITLAB_PROJECT_PERMISSIONS_PROVISIONING = "GITLAB_PROJECT_PERMISSIONS_PROVISIONING"; private CeTaskTypes() { // only statics diff --git a/server/sonar-server-common/build.gradle b/server/sonar-server-common/build.gradle index fd8f433ded3..1c817eb9aa4 100644 --- a/server/sonar-server-common/build.gradle +++ b/server/sonar-server-common/build.gradle @@ -46,6 +46,8 @@ dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api' testImplementation 'org.junit.jupiter:junit-jupiter-params' testImplementation 'org.mockito:mockito-core' + testImplementation 'org.mockito:mockito-junit-jupiter' + testImplementation 'org.junit.jupiter:junit-jupiter-params' testImplementation 'org.sonarsource.api.plugin:sonar-plugin-api-test-fixtures' testImplementation testFixtures(project(':server:sonar-db-dao')) testImplementation testFixtures(project(':server:sonar-webserver-auth')) diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/project/VisibilityService.java b/server/sonar-server-common/src/main/java/org/sonar/server/project/VisibilityService.java index 4cf54033289..9ac65cc7b06 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/project/VisibilityService.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/project/VisibilityService.java @@ -27,6 +27,7 @@ import org.sonar.core.util.UuidFactory; import org.sonar.core.util.Uuids; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.ce.CeQueueDto; import org.sonar.db.component.BranchDto; import org.sonar.db.entity.EntityDto; import org.sonar.db.permission.GroupPermissionDto; @@ -40,6 +41,7 @@ import static java.util.Optional.ofNullable; import static org.sonar.api.utils.Preconditions.checkState; import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS; import static org.sonar.db.ce.CeTaskTypes.GITHUB_PROJECT_PERMISSIONS_PROVISIONING; +import static org.sonar.db.ce.CeTaskTypes.GITLAB_PROJECT_PERMISSIONS_PROVISIONING; @ServerSide @ComputeEngineSide @@ -79,10 +81,14 @@ public class VisibilityService { EntityDto entityDto = dbClient.entityDao().selectByKey(dbSession, entityKey).orElseThrow(() -> new IllegalStateException("Can't find entity " + entityKey)); return dbClient.ceQueueDao().selectByEntityUuid(dbSession, entityDto.getUuid()) .stream() - .filter(task -> !task.getTaskType().equals(GITHUB_PROJECT_PERMISSIONS_PROVISIONING)) + .filter(task -> !hasDevopsProjectPermissionsProvisioningTaskRunning(task)) .count(); } + private static boolean hasDevopsProjectPermissionsProvisioningTaskRunning(CeQueueDto task) { + return task.getTaskType().equals(GITHUB_PROJECT_PERMISSIONS_PROVISIONING) || task.getTaskType().equals(GITLAB_PROJECT_PERMISSIONS_PROVISIONING); + } + private void setPrivateForRootComponentUuid(DbSession dbSession, EntityDto entity, boolean newIsPrivate) { Optional branchDto = dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, entity.getUuid()); String branchUuid = branchDto.isPresent() ? branchDto.get().getUuid() : entity.getUuid(); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/project/VisibilityServiceTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/project/VisibilityServiceTest.java index 054595a2b0d..e65c3a10210 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/project/VisibilityServiceTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/project/VisibilityServiceTest.java @@ -21,24 +21,28 @@ package org.sonar.server.project; import java.util.List; import java.util.Optional; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Answers; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.entity.EntityDto; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.db.ce.CeTaskTypes.GITHUB_PROJECT_PERMISSIONS_PROVISIONING; +import static org.sonar.db.ce.CeTaskTypes.GITLAB_PROJECT_PERMISSIONS_PROVISIONING; -@RunWith(MockitoJUnitRunner.class) -public class VisibilityServiceTest { +@ExtendWith(MockitoExtension.class) +class VisibilityServiceTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private DbClient dbClient; @@ -50,7 +54,7 @@ public class VisibilityServiceTest { private VisibilityService visibilityService; @Test - public void checkNoPendingTasks_whenEntityNotFound_throwsIae() { + void checkNoPendingTasks_whenEntityNotFound_throwsIae() { EntityDto entityDto = mockEntityDto(); when(dbClient.entityDao().selectByKey(dbSession, entityDto.getKey())).thenReturn(Optional.empty()); @@ -60,25 +64,27 @@ public class VisibilityServiceTest { } @Test - public void checkNoPendingTasks_whenEntityFoundAndNoTaskInQueue_doesNotThrow() { + void checkNoPendingTasks_whenEntityFoundAndNoTaskInQueue_doesNotThrow() { EntityDto entityDto = mockEntityDto(); when(dbClient.entityDao().selectByKey(dbSession, entityDto.getKey())).thenReturn(Optional.of(entityDto)); visibilityService.checkNoPendingTasks(dbSession, entityDto); } - @Test - public void checkNoPendingTasks_whenOneGithubSyncTaskInQueue_doesNotThrow() { + @ParameterizedTest + @ValueSource(strings = {GITHUB_PROJECT_PERMISSIONS_PROVISIONING, GITLAB_PROJECT_PERMISSIONS_PROVISIONING}) + void checkNoPendingTasks_whenOneDevopsProjectPermissionSyncInQueue_doesNotThrow(String taskType) { EntityDto entityDto = mockEntityDto(); when(dbClient.entityDao().selectByKey(dbSession, entityDto.getKey())).thenReturn(Optional.of(entityDto)); - mockCeQueueDto(GITHUB_PROJECT_PERMISSIONS_PROVISIONING, entityDto.getKey()); + mockCeQueueDto(taskType, entityDto.getUuid()); visibilityService.checkNoPendingTasks(dbSession, entityDto); } + @Test - public void checkNoPendingTasks_whenAnyOtherTaskInQueue_throws() { + void checkNoPendingTasks_whenAnyOtherTaskInQueue_throws() { EntityDto entityDto = mockEntityDto(); when(dbClient.entityDao().selectByKey(dbSession, entityDto.getKey())).thenReturn(Optional.of(entityDto)); @@ -89,16 +95,16 @@ public class VisibilityServiceTest { .withMessage("Component visibility can't be changed as long as it has background task(s) pending or in progress"); } - private void mockCeQueueDto(String taskType, String entityDto) { + private void mockCeQueueDto(String taskType, String entityDtoUuid) { CeQueueDto ceQueueDto = mock(CeQueueDto.class); when(ceQueueDto.getTaskType()).thenReturn(taskType); - when(dbClient.ceQueueDao().selectByEntityUuid(dbSession, entityDto)).thenReturn(List.of(ceQueueDto)); + when(dbClient.ceQueueDao().selectByEntityUuid(dbSession, entityDtoUuid)).thenReturn(List.of(ceQueueDto)); } private static EntityDto mockEntityDto() { EntityDto entityDto = mock(EntityDto.class); when(entityDto.getKey()).thenReturn("entityKey"); - when(entityDto.getUuid()).thenReturn("entityUuid"); + lenient().when(entityDto.getUuid()).thenReturn("entityUuid"); return entityDto; } } diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index 87f3cfecd7d..bec901fb25d 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -3800,6 +3800,7 @@ background_task.type.REPORT_SUBMIT=Report Email Submit background_task.type.GITHUB_AUTH_PROVISIONING=GitHub Provisioning background_task.type.GITHUB_PROJECT_PERMISSIONS_PROVISIONING=GitHub Project Permission Sync background_task.type.GITLAB_AUTH_PROVISIONING=GitLab Provisioning +background_task.type.GITLAB_PROJECT_PERMISSIONS_PROVISIONING=Gitlab Project Permission Sync background_tasks.page=Background Tasks background_tasks.page.description=This page allows monitoring of the queue of tasks running asynchronously on the server. It also gives access to the history of finished tasks and their status. Analysis report processing is the most common kind of background task. -- 2.39.5