Skip to content

Commit

Permalink
SONAR-7299 Move code related to project creation to ComponentUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jan 24, 2017
1 parent 8996815 commit 4fc7e70
Show file tree
Hide file tree
Showing 14 changed files with 563 additions and 456 deletions.
Expand Up @@ -23,24 +23,16 @@
import com.google.common.collect.Collections2; import com.google.common.collect.Collections2;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.sonar.api.ce.ComputeEngineSide; import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.Scopes;
import org.sonar.api.server.ServerSide; import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.System2;
import org.sonar.api.web.UserRole; import org.sonar.api.web.UserRole;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.index.ComponentIndexer; import org.sonar.server.component.index.ComponentIndexer;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.measure.index.ProjectMeasuresIndexer; import org.sonar.server.measure.index.ProjectMeasuresIndexer;
import org.sonar.server.user.UserSession; import org.sonar.server.user.UserSession;
Expand All @@ -55,22 +47,19 @@
@ComputeEngineSide @ComputeEngineSide
public class ComponentService { public class ComponentService {
private final DbClient dbClient; private final DbClient dbClient;
private final I18n i18n;
private final UserSession userSession; private final UserSession userSession;
private final System2 system2;
private final ProjectMeasuresIndexer projectMeasuresIndexer; private final ProjectMeasuresIndexer projectMeasuresIndexer;
private final ComponentIndexer componentIndexer; private final ComponentIndexer componentIndexer;


public ComponentService(DbClient dbClient, I18n i18n, UserSession userSession, System2 system2, ProjectMeasuresIndexer projectMeasuresIndexer, public ComponentService(DbClient dbClient, UserSession userSession, ProjectMeasuresIndexer projectMeasuresIndexer,
ComponentIndexer componentIndexer) { ComponentIndexer componentIndexer) {
this.dbClient = dbClient; this.dbClient = dbClient;
this.i18n = i18n;
this.userSession = userSession; this.userSession = userSession;
this.system2 = system2;
this.projectMeasuresIndexer = projectMeasuresIndexer; this.projectMeasuresIndexer = projectMeasuresIndexer;
this.componentIndexer = componentIndexer; this.componentIndexer = componentIndexer;
} }


// TODO should be moved to ComponentUpdater
public void updateKey(DbSession dbSession, ComponentDto component, String newKey) { public void updateKey(DbSession dbSession, ComponentDto component, String newKey) {
userSession.checkComponentUuidPermission(UserRole.ADMIN, component.projectUuid()); userSession.checkComponentUuidPermission(UserRole.ADMIN, component.projectUuid());
checkIsProjectOrModule(component); checkIsProjectOrModule(component);
Expand All @@ -80,76 +69,25 @@ public void updateKey(DbSession dbSession, ComponentDto component, String newKey
index(component.uuid()); index(component.uuid());
} }


// TODO should be moved to ComponentUpdater
public void bulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) { public void bulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) {
dbClient.componentKeyUpdaterDao().bulkUpdateKey(dbSession, projectUuid, stringToReplace, replacementString); dbClient.componentKeyUpdaterDao().bulkUpdateKey(dbSession, projectUuid, stringToReplace, replacementString);
dbSession.commit(); dbSession.commit();
index(projectUuid); index(projectUuid);
} }


// Used by SQ and Governance
public ComponentDto create(DbSession session, NewComponent newComponent) {
checkKeyFormat(newComponent.qualifier(), newComponent.key());
ComponentDto rootComponent = createRootComponent(session, newComponent);
removeDuplicatedProjects(session, rootComponent.getKey());
index(rootComponent.uuid());
return rootComponent;
}

private void index(String projectUuid) { private void index(String projectUuid) {
projectMeasuresIndexer.index(projectUuid); projectMeasuresIndexer.index(projectUuid);
componentIndexer.indexByProjectUuid(projectUuid); componentIndexer.indexByProjectUuid(projectUuid);
} }


/** public Collection<String> componentUuids(@Nullable Collection<String> componentKeys) {
* No permission check must be done when inserting a new developer as it's done on Compute Engine side. DbSession session = dbClient.openSession(false);
* No check must be done on the key try {
* No need to remove duplicated components as it's not possible to create the same developer twice in the same time. return componentUuids(session, componentKeys, false);
*/ } finally {
public ComponentDto createDeveloper(DbSession session, NewComponent newComponent) { dbClient.closeSession(session);
return createRootComponent(session, newComponent);
}

private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) {
checkBranchFormat(newComponent.qualifier(), newComponent.branch());
String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch());
if (dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent()) {
throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch));
}

String uuid = Uuids.create();
ComponentDto component = new ComponentDto()
.setOrganizationUuid(newComponent.getOrganizationUuid())
.setUuid(uuid)
.setUuidPath(ComponentDto.UUID_PATH_OF_ROOT)
.setRootUuid(uuid)
.setModuleUuid(null)
.setModuleUuidPath(ComponentDto.UUID_PATH_SEPARATOR + uuid + ComponentDto.UUID_PATH_SEPARATOR)
.setProjectUuid(uuid)
.setKey(keyWithBranch)
.setDeprecatedKey(keyWithBranch)
.setName(newComponent.name())
.setLongName(newComponent.name())
.setScope(Scopes.PROJECT)
.setQualifier(newComponent.qualifier())
.setCreatedAt(new Date(system2.now()));
dbClient.componentDao().insert(session, component);
dbClient.componentIndexDao().indexResource(session, component.uuid());
session.commit();
return component;
}

/**
* On MySQL, as PROJECTS.KEE is not unique, if the same project is provisioned multiple times, then it will be duplicated in the database.
* So, after creating a project, we commit, and we search in the db if their are some duplications and we remove them.
*
* SONAR-6332
*/
private void removeDuplicatedProjects(DbSession session, String projectKey) {
List<ComponentDto> duplicated = dbClient.componentDao().selectComponentsHavingSameKeyOrderedById(session, projectKey);
for (int i = 1; i < duplicated.size(); i++) {
dbClient.componentDao().delete(session, duplicated.get(i).getId());
} }
session.commit();
} }


public Collection<String> componentUuids(DbSession session, @Nullable Collection<String> componentKeys, boolean ignoreMissingComponents) { public Collection<String> componentUuids(DbSession session, @Nullable Collection<String> componentKeys, boolean ignoreMissingComponents) {
Expand Down Expand Up @@ -196,24 +134,8 @@ public Collection<ComponentDto> getByUuids(DbSession session, @Nullable Collecti
return directoryPaths; return directoryPaths;
} }


private void checkKeyFormat(String qualifier, String kee) {
checkRequest(isValidModuleKey(kee), formatMessage("Malformed key for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
qualifier, kee));
}

private static void checkProjectOrModuleKeyFormat(String key) { private static void checkProjectOrModuleKeyFormat(String key) {
checkRequest(isValidModuleKey(key), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", key); checkRequest(isValidModuleKey(key), "Malformed key for '%s'. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.", key);
} }


private void checkBranchFormat(String qualifier, @Nullable String branch) {
if (branch != null && !ComponentKeys.isValidBranch(branch)) {
throw new BadRequestException(formatMessage("Malformed branch for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.",
qualifier, branch));
}
}

private String formatMessage(String message, String qualifier, String key) {
return String.format(message, i18n.message(Locale.getDefault(), "qualifier." + qualifier, "Project"), key);
}

} }
@@ -0,0 +1,151 @@
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.component;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.System2;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.component.index.ComponentIndexer;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.measure.index.ProjectMeasuresIndexer;
import org.sonar.server.permission.PermissionTemplateService;

import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.core.component.ComponentKeys.isValidModuleKey;
import static org.sonar.server.ws.WsUtils.checkRequest;

public class ComponentUpdater {

private final DbClient dbClient;
private final I18n i18n;
private final System2 system2;
private final PermissionTemplateService permissionTemplateService;
private final FavoriteUpdater favoriteUpdater;
private final ProjectMeasuresIndexer projectMeasuresIndexer;
private final ComponentIndexer componentIndexer;

public ComponentUpdater(DbClient dbClient, I18n i18n, System2 system2,
PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater,
ProjectMeasuresIndexer projectMeasuresIndexer, ComponentIndexer componentIndexer) {
this.dbClient = dbClient;
this.i18n = i18n;
this.system2 = system2;
this.projectMeasuresIndexer = projectMeasuresIndexer;
this.componentIndexer = componentIndexer;
this.permissionTemplateService = permissionTemplateService;
this.favoriteUpdater = favoriteUpdater;
}

/**
* - Create component
* - Apply default permission template
* - Add component to favorite if the component has the 'Project Creators' permission
* - Index component if es indexes
*/
public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable Long userId) {
checkKeyFormat(newComponent.qualifier(), newComponent.key());
ComponentDto componentDto = createRootComponent(dbSession, newComponent);
removeDuplicatedProjects(dbSession, componentDto.getKey());
handlePermissionTemplate(dbSession, componentDto, newComponent.getOrganizationUuid(), userId);
dbSession.commit();
index(componentDto.uuid());
return componentDto;
}

private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) {
checkBranchFormat(newComponent.qualifier(), newComponent.branch());
String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch());
if (dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent()) {
throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch));
}

String uuid = Uuids.create();
ComponentDto component = new ComponentDto()
.setOrganizationUuid(newComponent.getOrganizationUuid())
.setUuid(uuid)
.setUuidPath(ComponentDto.UUID_PATH_OF_ROOT)
.setRootUuid(uuid)
.setModuleUuid(null)
.setModuleUuidPath(ComponentDto.UUID_PATH_SEPARATOR + uuid + ComponentDto.UUID_PATH_SEPARATOR)
.setProjectUuid(uuid)
.setKey(keyWithBranch)
.setDeprecatedKey(keyWithBranch)
.setName(newComponent.name())
.setLongName(newComponent.name())
.setScope(Scopes.PROJECT)
.setQualifier(newComponent.qualifier())
.setCreatedAt(new Date(system2.now()));
dbClient.componentDao().insert(session, component);
dbClient.componentIndexDao().indexResource(session, component.uuid());
return component;
}

/**
* On MySQL, as PROJECTS.KEE is not unique, if the same project is provisioned multiple times, then it will be duplicated in the database.
* So, after creating a project, we commit, and we search in the db if their are some duplications and we remove them.
*
* SONAR-6332
*/
private void removeDuplicatedProjects(DbSession session, String projectKey) {
List<ComponentDto> duplicated = dbClient.componentDao().selectComponentsHavingSameKeyOrderedById(session, projectKey);
for (int i = 1; i < duplicated.size(); i++) {
dbClient.componentDao().delete(session, duplicated.get(i).getId());
}
}

private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, String organizationUuid, @Nullable Long userId) {
permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userId);
if (componentDto.qualifier().equals(PROJECT) && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, componentDto)) {
favoriteUpdater.add(dbSession, componentDto, userId);
}
}

private void checkKeyFormat(String qualifier, String kee) {
checkRequest(isValidModuleKey(kee), formatMessage("Malformed key for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.",
qualifier, kee));
}

private void checkBranchFormat(String qualifier, @Nullable String branch) {
if (branch != null && !ComponentKeys.isValidBranch(branch)) {
throw new BadRequestException(formatMessage("Malformed branch for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.",
qualifier, branch));
}
}

private String formatMessage(String message, String qualifier, String key) {
return String.format(message, i18n.message(Locale.getDefault(), "qualifier." + qualifier, "Project"), key);
}

private void index(String projectUuid) {
projectMeasuresIndexer.index(projectUuid);
componentIndexer.indexByProjectUuid(projectUuid);
}
}
Expand Up @@ -19,15 +19,10 @@
*/ */
package org.sonar.server.component; package org.sonar.server.component;


import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.resources.Qualifiers;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.permission.PermissionTemplateService; import org.sonar.server.user.UserSession;


import static org.sonar.server.component.NewComponent.newComponentBuilder; import static org.sonar.server.component.NewComponent.newComponentBuilder;


Expand All @@ -36,50 +31,36 @@
*/ */
public class DefaultRubyComponentService { public class DefaultRubyComponentService {


private final UserSession userSession;
private final DbClient dbClient; private final DbClient dbClient;
private final ComponentService componentService; private final ComponentUpdater componentUpdater;
private final PermissionTemplateService permissionTemplateService;
private final FavoriteUpdater favoriteUpdater;
private final DefaultOrganizationProvider defaultOrganizationProvider; private final DefaultOrganizationProvider defaultOrganizationProvider;


public DefaultRubyComponentService(DbClient dbClient, ComponentService componentService, public DefaultRubyComponentService(UserSession userSession, DbClient dbClient, ComponentUpdater componentUpdater,
PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater,
DefaultOrganizationProvider defaultOrganizationProvider) { DefaultOrganizationProvider defaultOrganizationProvider) {
this.userSession = userSession;
this.dbClient = dbClient; this.dbClient = dbClient;
this.componentService = componentService; this.componentUpdater = componentUpdater;
this.permissionTemplateService = permissionTemplateService;
this.favoriteUpdater = favoriteUpdater;
this.defaultOrganizationProvider = defaultOrganizationProvider; this.defaultOrganizationProvider = defaultOrganizationProvider;
} }


// Used in GOV // Used in GOV
@CheckForNull /**
* @deprecated Use {@link ComponentUpdater#create(DbSession, NewComponent, Long)} instead
*/
@Deprecated
public Long createComponent(String key, String name, String qualifier) { public Long createComponent(String key, String name, String qualifier) {
try (DbSession dbSession = dbClient.openSession(false)) { try (DbSession dbSession = dbClient.openSession(false)) {
return createComponent(dbSession, key, null, name, qualifier); return componentUpdater.create(
dbSession,
newComponentBuilder()
.setOrganizationUuid(defaultOrganizationProvider.get().getUuid())
.setKey(key)
.setName(name)
.setQualifier(qualifier)
.build(),
userSession.isLoggedIn() ? userSession.getUserId().longValue() : null).getId();
} }
} }


public long createComponent(DbSession dbSession, String key, @Nullable String branch, String name, @Nullable String qualifier) {
ComponentDto provisionedComponent = componentService.create(
dbSession,
newComponentBuilder()
.setOrganizationUuid(defaultOrganizationProvider.get().getUuid())
.setKey(key)
.setName(name)
.setQualifier(qualifier)
.setBranch(branch)
.build());
String organizationUuid = defaultOrganizationProvider.get().getUuid();
permissionTemplateService.applyDefaultPermissionTemplate(dbSession, organizationUuid, provisionedComponent.getKey());
if (Qualifiers.PROJECT.equals(provisionedComponent.qualifier())
&& permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, provisionedComponent)) {
// TODO Set userId as null as this method will be removed in a next commit
favoriteUpdater.add(dbSession, provisionedComponent, null);
dbSession.commit();
}

return provisionedComponent.getId();
}

} }

0 comments on commit 4fc7e70

Please sign in to comment.