]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20699 Local projects always apply permissions template
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Fri, 20 Oct 2023 08:42:48 +0000 (10:42 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 20 Oct 2023 20:02:41 +0000 (20:02 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/project/CreationMethod.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/ReportSubmitterIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/GithubProjectCreator.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/queue/ReportSubmitter.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/ProjectCreator.java

index cc9d89b98b45280c0447f9a67d83768a3e895864..5f96e42f9e609c138adaaefcf752b3732a8b851b 100644 (file)
@@ -22,20 +22,22 @@ package org.sonar.db.project;
 import java.util.Arrays;
 
 public enum CreationMethod {
-  UNKNOWN(Category.UNKNOWN, false),
-  LOCAL_API(Category.LOCAL, false),
-  LOCAL_BROWSER(Category.LOCAL, true),
-  ALM_IMPORT_API(Category.ALM_IMPORT, false),
-  ALM_IMPORT_BROWSER(Category.ALM_IMPORT, true),
-  SCANNER_API(Category.SCANNER, false),
-  SCANNER_API_DEVOPS_AUTO_CONFIG(Category.SCANNER, false);
+  UNKNOWN(Category.UNKNOWN, false, true),
+  LOCAL_API(Category.LOCAL, false, true),
+  LOCAL_BROWSER(Category.LOCAL, true, true),
+  ALM_IMPORT_API(Category.ALM_IMPORT, false, false),
+  ALM_IMPORT_BROWSER(Category.ALM_IMPORT, true, false),
+  SCANNER_API(Category.SCANNER, false, true),
+  SCANNER_API_DEVOPS_AUTO_CONFIG(Category.SCANNER, false, false);
 
-  private final boolean isCreatedViaBrowser;
   private final Category category;
+  private final boolean isCreatedViaBrowser;
+  private final boolean isLocal;
 
-  CreationMethod(Category category, boolean isCreatedViaBrowser) {
+  CreationMethod(Category category, boolean isCreatedViaBrowser, boolean isLocal) {
     this.isCreatedViaBrowser = isCreatedViaBrowser;
     this.category = category;
+    this.isLocal = isLocal;
   }
 
   public static CreationMethod getCreationMethod(Category category, boolean isBrowserCall) {
@@ -54,5 +56,9 @@ public enum CreationMethod {
     return category;
   }
 
+  public boolean isLocal() {
+    return isLocal;
+  }
+
   public enum Category {UNKNOWN, LOCAL, ALM_IMPORT, SCANNER}
 }
index 09068d6440f12108c2426748eff6e66b312141d1..9b2d4d514421b4454cfee6aca8c21b9b78541cf2 100644 (file)
@@ -56,7 +56,6 @@ import org.sonar.server.almsettings.ws.DevOpsProjectDescriptor;
 import org.sonar.server.almsettings.ws.GithubProjectCreationParameters;
 import org.sonar.server.almsettings.ws.GithubProjectCreator;
 import org.sonar.server.almsettings.ws.GithubProjectCreatorFactory;
-import org.sonar.server.project.ws.ProjectCreator;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.es.TestIndexers;
 import org.sonar.server.exceptions.BadRequestException;
@@ -72,6 +71,7 @@ import org.sonar.server.permission.UserPermissionChange;
 import org.sonar.server.project.DefaultBranchNameResolver;
 import org.sonar.server.project.ProjectDefaultVisibility;
 import org.sonar.server.project.Visibility;
+import org.sonar.server.project.ws.ProjectCreator;
 import org.sonar.server.tester.UserSessionRule;
 
 import static java.lang.String.format;
@@ -86,6 +86,7 @@ import static org.mockito.ArgumentMatchers.argThat;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
@@ -278,7 +279,8 @@ public class ReportSubmitterIT {
 
     underTest.submit(PROJECT_KEY, PROJECT_NAME, emptyMap(), IOUtils.toInputStream("{binary}", UTF_8));
 
-    assertLocalProjectWasCreated();
+    ProjectDto projectDto = assertLocalProjectWasCreated();
+    verify(permissionTemplateService).applyDefaultToNewComponent(any(DbSession.class), eq(projectDto), eq(userSession.getUuid()));
   }
 
   @Test
@@ -295,6 +297,7 @@ public class ReportSubmitterIT {
     underTest.submit(PROJECT_KEY, PROJECT_NAME, CHARACTERISTICS, IOUtils.toInputStream("{binary}", UTF_8));
 
     assertProjectWasCreatedWithBinding();
+    verify(permissionTemplateService, never()).applyDefaultToNewComponent(any(), any(), any());
   }
 
   @Test
@@ -310,6 +313,7 @@ public class ReportSubmitterIT {
     underTest.submit(PROJECT_KEY, PROJECT_NAME, CHARACTERISTICS, IOUtils.toInputStream("{binary}", UTF_8));
 
     assertProjectWasCreatedWithBinding();
+    verify(permissionTemplateService, never()).applyDefaultToNewComponent(any(), any(), any());
   }
 
   @Test
@@ -321,10 +325,11 @@ public class ReportSubmitterIT {
     mockSuccessfulPrepareSubmitCall();
 
     underTest.submit(PROJECT_KEY, PROJECT_NAME, CHARACTERISTICS, IOUtils.toInputStream("{binary}", UTF_8));
-    assertLocalProjectWasCreated();
+    ProjectDto projectDto = assertLocalProjectWasCreated();
+    verify(permissionTemplateService).applyDefaultToNewComponent(any(DbSession.class), eq(projectDto), eq(userSession.getUuid()));
   }
 
-  private void assertLocalProjectWasCreated() {
+  private ProjectDto assertLocalProjectWasCreated() {
     ProjectDto projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), PROJECT_KEY).orElseThrow();
     assertThat(projectDto.getCreationMethod()).isEqualTo(CreationMethod.SCANNER_API);
     assertThat(projectDto.getName()).isEqualTo(PROJECT_NAME);
@@ -333,6 +338,7 @@ public class ReportSubmitterIT {
     assertThat(branchDto.isMain()).isTrue();
 
     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.getUuid())).isEmpty();
+    return projectDto;
   }
 
   @Test
index 3171aae5b720113712f3285baa255119771225f1..df7a6b58cbda75fc9a1be66fea669854c53a4353 100644 (file)
@@ -25,11 +25,11 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.alm.client.github.AppInstallationToken;
 import org.sonar.alm.client.github.GithubApplicationClient;
+import org.sonar.alm.client.github.GithubPermissionConverter;
 import org.sonar.alm.client.github.api.GsonRepositoryCollaborator;
 import org.sonar.alm.client.github.api.GsonRepositoryTeam;
 import org.sonar.alm.client.github.security.AccessToken;
 import org.sonar.api.web.UserRole;
-import org.sonar.alm.client.github.GithubPermissionConverter;
 import org.sonar.auth.github.GsonRepositoryPermissions;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
index f20502fccd35a23d9d3a0e16f373931d3249f135..2129cd8813855afc3ec27534fbfd4e708f4ed6fa 100644 (file)
@@ -40,12 +40,12 @@ import org.sonar.db.component.ComponentDto;
 import org.sonar.db.permission.GlobalPermission;
 import org.sonar.server.almsettings.ws.DevOpsProjectCreator;
 import org.sonar.server.almsettings.ws.DevOpsProjectCreatorFactory;
-import org.sonar.server.project.ws.ProjectCreator;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.management.ManagedInstanceService;
 import org.sonar.server.permission.PermissionTemplateService;
+import org.sonar.server.project.ws.ProjectCreator;
 import org.sonar.server.user.UserSession;
 
 import static java.lang.String.format;
index acff438ab1e1c2ea45dba6319e8c4bf547b83921..73c913a43ed4d9c20cc6353d8609e416f5052acb 100644 (file)
@@ -62,7 +62,7 @@ public class ProjectCreator {
       .userLogin(userSession.getLogin())
       .userUuid(userSession.getUuid())
       .mainBranchName(mainBranchName)
-      .isManaged(managedInstanceService.isInstanceExternallyManaged())
+      .isManaged(managedInstanceService.isInstanceExternallyManaged() && !creationMethod.isLocal())
       .creationMethod(creationMethod)
       .build();
     return componentUpdater.createWithoutCommit(dbSession, componentCreationParameters);