diff options
author | Nolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com> | 2024-08-14 16:22:44 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-08-19 20:02:45 +0000 |
commit | 5c6dd397bcb5579cf1f9ceaa71de391c4fe3e36c (patch) | |
tree | 77fa175a886cda73d3448fe171c22685715ea1cd /server | |
parent | b24b45c140ff48bdc89f8daf85a4f9182f5ef2fb (diff) | |
download | sonarqube-5c6dd397bcb5579cf1f9ceaa71de391c4fe3e36c.tar.gz sonarqube-5c6dd397bcb5579cf1f9ceaa71de391c4fe3e36c.zip |
SONAR-22559 Update permissions during project onboarding
Diffstat (limited to 'server')
4 files changed, 30 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> 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; } } |