]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20630 Extract parameters to create a project in a dedicated class
authorAurelien Poscia <aurelien.poscia@sonarsource.com>
Mon, 2 Oct 2023 13:33:39 +0000 (15:33 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 5 Oct 2023 20:02:47 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/queue/BranchReportSubmitterIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ComponentUpdaterIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/azure/ImportAzureProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/github/ImportGithubProjectAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/gitlab/ImportGitLabProjectAction.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/component/ComponentUpdater.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ProjectCreationData.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java

index 7a485268bac7bdc4feb9bb973a009c4b9aae70fc..4f5b073e79e5924d96968ba3d2710a6a8a7d6d65 100644 (file)
@@ -50,6 +50,7 @@ import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.favorite.FavoriteUpdater;
 import org.sonar.server.permission.PermissionTemplateService;
@@ -191,7 +192,7 @@ public class BranchReportSubmitterIT {
     ComponentCreationData componentCreationData = mock(ComponentCreationData.class);
     when(componentCreationData.mainBranchComponent())
       .thenAnswer((Answer<ComponentDto>) invocation -> db.components().insertPrivateProject(PROJECT_UUID, nonExistingBranch).getMainBranchComponent());
-    when(componentUpdater.createWithoutCommit(any(), any(), eq(user.getUuid()), eq(user.getLogin())))
+    when(componentUpdater.createWithoutCommit(any(), new ProjectCreationData(any(), eq(user.getUuid()), eq(user.getLogin()))))
       .thenReturn(componentCreationData);
     when(branchSupportDelegate.createBranchComponent(any(DbSession.class), same(componentKey), any(), any())).thenReturn(createdBranch);
     when(permissionTemplateService.wouldUserHaveScanPermissionWithDefaultTemplate(any(DbSession.class), any(), eq(nonExistingBranch.getKey()))).thenReturn(true);
index 53a3a70a9aecdd2d4afe1af5f1cb24dfdca26462..f6a99fed46d274926985080cfe073cf78b8457cb 100644 (file)
@@ -417,7 +417,7 @@ public class ComponentUpdaterIT {
       .setKey(DEFAULT_PROJECT_KEY)
       .setName(DEFAULT_PROJECT_NAME)
       .build();
-    underTest.createWithoutCommit(db.getSession(), project, userDto.getUuid(), userDto.getLogin(), null, true);
+    underTest.createWithoutCommit(db.getSession(), new ProjectCreationData(project, userDto.getUuid(), userDto.getLogin(), null, true));
 
     verify(permissionTemplateService, never()).applyDefaultToNewComponent(any(), any(), any());
   }
@@ -432,7 +432,7 @@ public class ComponentUpdaterIT {
       .build();
 
     DbSession session = db.getSession();
-    ComponentCreationData componentCreationData = underTest.createWithoutCommit(session, newComponent, userDto.getUuid(), userDto.getLogin(), null, true);
+    ComponentCreationData componentCreationData = underTest.createWithoutCommit(session, new ProjectCreationData(newComponent, userDto.getUuid(), userDto.getLogin(), null, true));
 
     List<String> permissions = db.getDbClient().userPermissionDao().selectEntityPermissionsOfUser(session, userDto.getUuid(), componentCreationData.projectDto().getUuid());
     assertThat(permissions)
index ea1f98ef13d41746eb12a9b6ff3d199a63414ac4..eb1bca8b93e46a4008aca84a2d68146ff44137a7 100644 (file)
@@ -39,6 +39,8 @@ import org.sonar.server.almintegration.ws.ImportHelper;
 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
+import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
 import org.sonar.server.project.DefaultBranchNameResolver;
 import org.sonar.server.project.ProjectDefaultVisibility;
@@ -92,8 +94,8 @@ public class ImportAzureProjectAction implements AlmIntegrationsWsAction {
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction("import_azure_project")
       .setDescription("Create a SonarQube project with the information from the provided Azure DevOps project.<br/>" +
-        "Autoconfigure pull request decoration mechanism.<br/>" +
-        "Requires the 'Create Projects' permission")
+                      "Autoconfigure pull request decoration mechanism.<br/>" +
+                      "Requires the 'Create Projects' permission")
       .setPost(true)
       .setSince("8.6")
       .setHandler(this)
@@ -176,15 +178,14 @@ public class ImportAzureProjectAction implements AlmIntegrationsWsAction {
   private ComponentCreationData createProject(DbSession dbSession, GsonAzureRepo repo) {
     boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
     String uniqueProjectKey = projectKeyGenerator.generateUniqueProjectKey(repo.getProject().getName(), repo.getName());
-    return componentUpdater.createWithoutCommit(dbSession, newComponentBuilder()
+    NewComponent newProject = newComponentBuilder()
       .setKey(uniqueProjectKey)
       .setName(repo.getName())
       .setPrivate(visibility)
       .setQualifier(PROJECT)
-      .build(),
-      userSession.isLoggedIn() ? userSession.getUuid() : null,
-      userSession.isLoggedIn() ? userSession.getLogin() : null,
-      repo.getDefaultBranchName());
+      .build();
+    ProjectCreationData projectCreationData = new ProjectCreationData(newProject, userSession.getUuid(), userSession.getLogin(), repo.getDefaultBranchName());
+    return componentUpdater.createWithoutCommit(dbSession, projectCreationData);
   }
 
   private void populatePRSetting(DbSession dbSession, GsonAzureRepo repo, ProjectDto projectDto, AlmSettingDto almSettingDto) {
index 656f4dd07e5e3b932e3e46e6bf20876136d8ffc2..0fb852c97da192f39f6e0a7b6d30fbc60b66aede 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
 import org.sonar.server.project.DefaultBranchNameResolver;
 import org.sonar.server.project.ProjectDefaultVisibility;
@@ -93,8 +94,8 @@ public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction {
   public void define(WebService.NewController context) {
     WebService.NewAction action = context.createAction("import_bitbucketcloud_repo")
       .setDescription("Create a SonarQube project with the information from the provided Bitbucket Cloud repository.<br/>" +
-        "Autoconfigure pull request decoration mechanism.<br/>" +
-        "Requires the 'Create Projects' permission")
+                      "Autoconfigure pull request decoration mechanism.<br/>" +
+                      "Requires the 'Create Projects' permission")
       .setPost(true)
       .setSince("9.0")
       .setHandler(this)
@@ -175,16 +176,14 @@ public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction {
   private ComponentCreationData createProject(DbSession dbSession, String workspace, Repository repo, @Nullable String defaultBranchName) {
     boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
     String uniqueProjectKey = projectKeyGenerator.generateUniqueProjectKey(workspace, repo.getSlug());
-    NewComponent mainBranch = newComponentBuilder()
+    NewComponent newProject = newComponentBuilder()
       .setKey(uniqueProjectKey)
       .setName(repo.getName())
       .setPrivate(visibility)
       .setQualifier(PROJECT)
       .build();
-    String userUuid = userSession.isLoggedIn() ? userSession.getUuid() : null;
-    String userLogin = userSession.isLoggedIn() ? userSession.getLogin() : null;
-
-    return componentUpdater.createWithoutCommit(dbSession, mainBranch, userUuid, userLogin, defaultBranchName);
+    ProjectCreationData projectCreationData = new ProjectCreationData(newProject, userSession.getUuid(), userSession.getLogin(), defaultBranchName);
+    return componentUpdater.createWithoutCommit(dbSession, projectCreationData);
   }
 
   private void populatePRSetting(DbSession dbSession, Repository repo, ProjectDto projectDto, AlmSettingDto almSettingDto) {
index e6001d8697b0c4c8e9481ed810de2ee0ce58ee6f..06ef54e0674568b255b80fe2107e693e089d7d23 100644 (file)
@@ -43,6 +43,7 @@ import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
 import org.sonar.server.project.DefaultBranchNameResolver;
 import org.sonar.server.project.ProjectDefaultVisibility;
@@ -199,10 +200,8 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
       .setPrivate(visibility)
       .setQualifier(PROJECT)
       .build();
-    String userUuid = userSession.isLoggedIn() ? userSession.getUuid() : null;
-    String userLogin = userSession.isLoggedIn() ? userSession.getLogin() : null;
-
-    return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userLogin, defaultBranchName);
+    ProjectCreationData projectCreationData = new ProjectCreationData(newProject, userSession.getUuid(), userSession.getLogin(), defaultBranchName);
+    return componentUpdater.createWithoutCommit(dbSession, projectCreationData);
   }
 
   private void populatePRSetting(DbSession dbSession, Repository repo, ProjectDto componentDto, AlmSettingDto almSettingDto) {
index c621b028f6e7a0bcae35cfd62d68529187579865..8d6ffeda08f375fa373516d7308667f3957361a6 100644 (file)
@@ -45,6 +45,7 @@ import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.management.ManagedProjectService;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
@@ -203,13 +204,11 @@ public class ImportGithubProjectAction implements AlmIntegrationsWsAction {
       .setPrivate(visibility)
       .setQualifier(PROJECT)
       .build();
+    ProjectCreationData projectCreationData = new ProjectCreationData(projectComponent, userSession.getUuid(), userSession.getLogin(), mainBranchName,
+      gitHubSettings.isProvisioningEnabled());
     return componentUpdater.createWithoutCommit(
       dbSession,
-      projectComponent,
-      userSession.getUuid(),
-      userSession.getLogin(),
-      mainBranchName,
-      gitHubSettings.isProvisioningEnabled());
+      projectCreationData);
   }
 
   private void populatePRSetting(DbSession dbSession, Repository repo, ProjectDto projectDto, AlmSettingDto almSettingDto) {
index 850c6bf3c058cb1754008520458d71534ce14e8b..f459fcb7a64b27b370779a9c790911e19ee06b3a 100644 (file)
@@ -40,6 +40,8 @@ import org.sonar.server.almintegration.ws.ImportHelper;
 import org.sonar.server.almintegration.ws.ProjectKeyGenerator;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
+import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
 import org.sonar.server.project.DefaultBranchNameResolver;
 import org.sonar.server.project.ProjectDefaultVisibility;
@@ -178,14 +180,14 @@ public class ImportGitLabProjectAction implements AlmIntegrationsWsAction {
   private ComponentCreationData createProject(DbSession dbSession, Project gitlabProject, @Nullable String mainBranchName) {
     boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
     String uniqueProjectKey = projectKeyGenerator.generateUniqueProjectKey(gitlabProject.getPathWithNamespace());
-
-    return componentUpdater.createWithoutCommit(dbSession, newComponentBuilder()
-        .setKey(uniqueProjectKey)
-        .setName(gitlabProject.getName())
-        .setPrivate(visibility)
-        .setQualifier(PROJECT)
-        .build(),
-      userSession.getUuid(), userSession.getLogin(), mainBranchName);
+    NewComponent newProject = newComponentBuilder()
+      .setKey(uniqueProjectKey)
+      .setName(gitlabProject.getName())
+      .setPrivate(visibility)
+      .setQualifier(PROJECT)
+      .build();
+    ProjectCreationData projectCreationData = new ProjectCreationData(newProject, userSession.getUuid(), userSession.getLogin(), mainBranchName);
+    return componentUpdater.createWithoutCommit(dbSession, projectCreationData);
   }
 
 }
index 9a332ccbd7d2607213853c296c9773ef4af2703c..8f750acb417b1786d4562b7b2007eab89dd4afbf 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.db.permission.GlobalPermission;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.project.ProjectDefaultVisibility;
@@ -168,7 +169,7 @@ public class ReportSubmitter {
       .setQualifier(Qualifiers.PROJECT)
       .setPrivate(getDefaultVisibility(dbSession).isPrivate())
       .build();
-    return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userName);
+    return componentUpdater.createWithoutCommit(dbSession, new ProjectCreationData(newProject, userUuid, userName));
   }
 
   private Visibility getDefaultVisibility(DbSession dbSession) {
index 1eeb768ad5aa1dfb41f6fa0b4d8d0a6db16757c2..0ce85806adc17c040b49b9a3538f6410b3b36deb 100644 (file)
@@ -96,7 +96,7 @@ public class ComponentUpdater {
    * - Index component in es indexes
    */
   public ComponentCreationData create(DbSession dbSession, NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin) {
-    ComponentCreationData componentCreationData = createWithoutCommit(dbSession, newComponent, userUuid, userLogin, null);
+    ComponentCreationData componentCreationData = createWithoutCommit(dbSession, new ProjectCreationData(newComponent, userUuid, userLogin));
     commitAndIndex(dbSession, componentCreationData);
     return componentCreationData;
   }
@@ -113,28 +113,13 @@ public class ComponentUpdater {
    * Create component without committing.
    * Don't forget to call commitAndIndex(...) when ready to commit.
    */
-  public ComponentCreationData createWithoutCommit(DbSession dbSession, NewComponent newComponent,
-    @Nullable String userUuid, @Nullable String userLogin) {
-    return createWithoutCommit(dbSession, newComponent, userUuid, userLogin, null, false);
-  }
-
-  public ComponentCreationData createWithoutCommit(DbSession dbSession, NewComponent newComponent,
-    @Nullable String userUuid, @Nullable String userLogin, @Nullable String mainBranchName) {
-    return createWithoutCommit(dbSession, newComponent, userUuid, userLogin, mainBranchName, false);
-  }
-
-  /**
-   * Create component without committing.
-   * Don't forget to call commitAndIndex(...) when ready to commit.
-   */
-  public ComponentCreationData createWithoutCommit(DbSession dbSession, NewComponent newComponent,
-    @Nullable String userUuid, @Nullable String userLogin, @Nullable String mainBranchName, boolean isManaged) {
-    checkKeyFormat(newComponent.qualifier(), newComponent.key());
-    checkKeyAlreadyExists(dbSession, newComponent);
+  public ComponentCreationData createWithoutCommit(DbSession dbSession, ProjectCreationData projectCreationData) {
+    checkKeyFormat(projectCreationData.newComponent().qualifier(), projectCreationData.newComponent().key());
+    checkKeyAlreadyExists(dbSession, projectCreationData.newComponent());
 
     long now = system2.now();
 
-    ComponentDto componentDto = createRootComponent(dbSession, newComponent, now);
+    ComponentDto componentDto = createRootComponent(dbSession, projectCreationData.newComponent(), now);
 
     BranchDto mainBranch = null;
     ProjectDto projectDto = null;
@@ -143,17 +128,17 @@ public class ComponentUpdater {
     if (isProjectOrApp(componentDto)) {
       projectDto = toProjectDto(componentDto, now);
       dbClient.projectDao().insert(dbSession, projectDto);
-      addToFavourites(dbSession, projectDto, userUuid, userLogin);
-      mainBranch = createMainBranch(dbSession, componentDto.uuid(), projectDto.getUuid(), mainBranchName);
-      if (isManaged) {
-        applyPublicPermissionsForCreator(dbSession, projectDto, userUuid);
+      addToFavourites(dbSession, projectDto, projectCreationData.userUuid(), projectCreationData.userLogin());
+      mainBranch = createMainBranch(dbSession, componentDto.uuid(), projectDto.getUuid(), projectCreationData.mainBranchName());
+      if (projectCreationData.isManaged()) {
+        applyPublicPermissionsForCreator(dbSession, projectDto, projectCreationData.userUuid());
       } else {
-        permissionTemplateService.applyDefaultToNewComponent(dbSession, projectDto, userUuid);
+        permissionTemplateService.applyDefaultToNewComponent(dbSession, projectDto, projectCreationData.userUuid());
       }
     } else if (isPortfolio(componentDto)) {
       portfolioDto = toPortfolioDto(componentDto, now);
       dbClient.portfolioDao().insert(dbSession, portfolioDto);
-      permissionTemplateService.applyDefaultToNewComponent(dbSession, portfolioDto, userUuid);
+      permissionTemplateService.applyDefaultToNewComponent(dbSession, portfolioDto, projectCreationData.userUuid());
     } else {
       throw new IllegalArgumentException("Component " + componentDto + " is not a top level entity");
     }
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ProjectCreationData.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ProjectCreationData.java
new file mode 100644 (file)
index 0000000..df6abd4
--- /dev/null
@@ -0,0 +1,13 @@
+package org.sonar.server.component;
+
+import javax.annotation.Nullable;
+
+public record ProjectCreationData(NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin, @Nullable String mainBranchName, boolean isManaged) {
+  public ProjectCreationData(NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin, @Nullable String mainBranchName) {
+    this(newComponent, userUuid, userLogin, mainBranchName, false);
+  }
+
+  public ProjectCreationData(NewComponent newComponent, @Nullable String userUuid, @Nullable String userLogin) {
+    this(newComponent, userUuid, userLogin, null, false);
+  }
+}
index 6b3409d0e41925479690f0dfb600bdc394633e40..5d49c46699dac396843d69e18349994c135891a5 100644 (file)
@@ -33,6 +33,8 @@ import org.sonar.db.entity.EntityDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
+import org.sonar.server.component.NewComponent;
+import org.sonar.server.component.ProjectCreationData;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
 import org.sonar.server.project.DefaultBranchNameResolver;
 import org.sonar.server.project.ProjectDefaultVisibility;
@@ -155,15 +157,14 @@ public class CreateAction implements ProjectsWsAction {
     String visibility = request.getVisibility();
     boolean changeToPrivate = visibility == null ? projectDefaultVisibility.get(dbSession).isPrivate() : "private".equals(visibility);
 
-    return componentUpdater.createWithoutCommit(dbSession, newComponentBuilder()
-        .setKey(request.getProjectKey())
-        .setName(request.getName())
-        .setPrivate(changeToPrivate)
-        .setQualifier(PROJECT)
-        .build(),
-      userSession.isLoggedIn() ? userSession.getUuid() : null,
-      userSession.isLoggedIn() ? userSession.getLogin() : null,
-      request.getMainBranchKey());
+    NewComponent newProject = newComponentBuilder()
+      .setKey(request.getProjectKey())
+      .setName(request.getName())
+      .setPrivate(changeToPrivate)
+      .setQualifier(PROJECT)
+      .build();
+    ProjectCreationData projectCreationData = new ProjectCreationData(newProject, userSession.getUuid(), userSession.getLogin(), request.getMainBranchKey());
+    return componentUpdater.createWithoutCommit(dbSession, projectCreationData);
 
   }