Skip to content

Commit

Permalink
SONAR-3718 Allow creation of components with branch with provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed Jan 9, 2015
1 parent 6415792 commit dbddd10
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 195 deletions.
Expand Up @@ -21,16 +21,27 @@
package org.sonar.server.component; package org.sonar.server.component;


import org.sonar.api.ServerComponent; import org.sonar.api.ServerComponent;
import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.internal.Uuids;
import org.sonar.api.web.UserRole; import org.sonar.api.web.UserRole;
import org.sonar.core.component.ComponentDto; import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.DbSession;
import org.sonar.core.preview.PreviewCache; import org.sonar.core.preview.PreviewCache;
import org.sonar.core.resource.ResourceIndexerDao;
import org.sonar.core.resource.ResourceKeyUpdaterDao; import org.sonar.core.resource.ResourceKeyUpdaterDao;
import org.sonar.server.db.DbClient; import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.permission.InternalPermissionService;
import org.sonar.server.user.UserSession; import org.sonar.server.user.UserSession;


import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable;


import java.util.Date;
import java.util.Locale;
import java.util.Map; import java.util.Map;


public class ComponentService implements ServerComponent { public class ComponentService implements ServerComponent {
Expand All @@ -39,17 +50,24 @@ public class ComponentService implements ServerComponent {


private final ResourceKeyUpdaterDao resourceKeyUpdaterDao; private final ResourceKeyUpdaterDao resourceKeyUpdaterDao;
private final PreviewCache previewCache; private final PreviewCache previewCache;
private final I18n i18n;
private final ResourceIndexerDao resourceIndexerDao;
private final InternalPermissionService permissionService;


public ComponentService(DbClient dbClient, ResourceKeyUpdaterDao resourceKeyUpdaterDao, PreviewCache previewCache) { public ComponentService(DbClient dbClient, ResourceKeyUpdaterDao resourceKeyUpdaterDao, PreviewCache previewCache, I18n i18n, ResourceIndexerDao resourceIndexerDao,
InternalPermissionService permissionService) {
this.dbClient = dbClient; this.dbClient = dbClient;
this.resourceKeyUpdaterDao = resourceKeyUpdaterDao; this.resourceKeyUpdaterDao = resourceKeyUpdaterDao;
this.previewCache = previewCache; this.previewCache = previewCache;
this.i18n = i18n;
this.resourceIndexerDao = resourceIndexerDao;
this.permissionService = permissionService;
} }


public ComponentDto getByKey(String key) { public ComponentDto getByKey(String key) {
DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
return dbClient.componentDao().getByKey(session, key); return getByKey(session, key);
} finally { } finally {
session.close(); session.close();
} }
Expand All @@ -59,7 +77,7 @@ public ComponentDto getByKey(String key) {
public ComponentDto getNullableByKey(String key) { public ComponentDto getNullableByKey(String key) {
DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
return dbClient.componentDao().getNullableByKey(session, key); return getNullableByKey(session, key);
} finally { } finally {
session.close(); session.close();
} }
Expand Down Expand Up @@ -89,7 +107,7 @@ public void updateKey(String projectOrModuleKey, String newKey) {


DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
ComponentDto projectOrModule = getByKey(projectOrModuleKey); ComponentDto projectOrModule = getByKey(session, projectOrModuleKey);
resourceKeyUpdaterDao.updateKey(projectOrModule.getId(), newKey); resourceKeyUpdaterDao.updateKey(projectOrModule.getId(), newKey);
session.commit(); session.commit();


Expand Down Expand Up @@ -117,7 +135,7 @@ public void bulkUpdateKey(String projectKey, String stringToReplace, String repl


DbSession session = dbClient.openSession(false); DbSession session = dbClient.openSession(false);
try { try {
ComponentDto project = getByKey(projectKey); ComponentDto project = getByKey(session, projectKey);


resourceKeyUpdaterDao.bulkUpdateKey(project.getId(), stringToReplace, replacementString); resourceKeyUpdaterDao.bulkUpdateKey(project.getId(), stringToReplace, replacementString);
session.commit(); session.commit();
Expand All @@ -131,4 +149,67 @@ public void bulkUpdateKey(String projectKey, String stringToReplace, String repl
} }
} }


public String create(NewComponent newComponent) {
UserSession.get().checkGlobalPermission(GlobalPermissions.PROVISIONING);

DbSession session = dbClient.openSession(false);
try {
checkKeyFormat(newComponent.qualifier(), newComponent.key());
checkBranchFormat(newComponent.qualifier(), newComponent.branch());
String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch());

ComponentDto existingComponent = getNullableByKey(keyWithBranch);
if (existingComponent != null) {
throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch));
}

String uuid = Uuids.create();
ComponentDto component = dbClient.componentDao().insert(session,
new ComponentDto()
.setUuid(uuid)
.setProjectUuid(uuid)
.setKey(keyWithBranch)
.setDeprecatedKey(keyWithBranch)
.setName(newComponent.name())
.setLongName(newComponent.name())
.setScope(Scopes.PROJECT)
.setQualifier(newComponent.qualifier())
.setCreatedAt(new Date()));
resourceIndexerDao.indexResource(session, component.getId());
session.commit();

permissionService.applyDefaultPermissionTemplate(component.key());
return component.key();
} finally {
session.close();
}
}

private void checkKeyFormat(String qualifier, String kee) {
if (!ComponentKeys.isValidModuleKey(kee)) {
throw new BadRequestException(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);
}

@CheckForNull
private ComponentDto getNullableByKey(DbSession session, String key) {
return dbClient.componentDao().getNullableByKey(session, key);
}

private ComponentDto getByKey(DbSession session, String key) {
return dbClient.componentDao().getByKey(session, key);
}

} }
Expand Up @@ -24,33 +24,28 @@
import org.sonar.api.component.RubyComponentService; import org.sonar.api.component.RubyComponentService;
import org.sonar.api.i18n.I18n; import org.sonar.api.i18n.I18n;
import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.internal.Uuids;
import org.sonar.core.component.ComponentDto; import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDao;
import org.sonar.core.resource.ResourceDto; import org.sonar.core.resource.ResourceDto;
import org.sonar.core.resource.ResourceIndexerDao;
import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.util.RubyUtils; import org.sonar.server.util.RubyUtils;


import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable;


import java.util.*; import java.util.List;
import java.util.Map;


public class DefaultRubyComponentService implements RubyComponentService { public class DefaultRubyComponentService implements RubyComponentService {


private final ResourceDao resourceDao; private final ResourceDao resourceDao;
private final DefaultComponentFinder finder; private final DefaultComponentFinder finder;
private final ResourceIndexerDao resourceIndexerDao;
private final ComponentService componentService; private final ComponentService componentService;
private final I18n i18n; private final I18n i18n;


public DefaultRubyComponentService(ResourceDao resourceDao, DefaultComponentFinder finder, ResourceIndexerDao resourceIndexerDao, ComponentService componentService, I18n i18n) { public DefaultRubyComponentService(ResourceDao resourceDao, DefaultComponentFinder finder, ComponentService componentService, I18n i18n) {
this.resourceDao = resourceDao; this.resourceDao = resourceDao;
this.finder = finder; this.finder = finder;
this.resourceIndexerDao = resourceIndexerDao;
this.componentService = componentService; this.componentService = componentService;
this.i18n = i18n; this.i18n = i18n;
} }
Expand All @@ -66,48 +61,28 @@ public Component findByUuid(String uuid) {
return componentService.getNullableByUuid(uuid); return componentService.getNullableByUuid(uuid);
} }


/**
* Be careful when updating this method, it's used by the Views plugin
*/
@CheckForNull @CheckForNull
public Long createComponent(String kee, String name, String qualifier) { public Long createComponent(String key, String name, String qualifier) {
return createComponent(key, null, name, qualifier);
}

@CheckForNull
public Long createComponent(String key, @Nullable String branch, String name, @Nullable String qualifier) {
// Sub view should not be created with provisioning. Will be fixed by http://jira.sonarsource.com/browse/VIEWS-296 // Sub view should not be created with provisioning. Will be fixed by http://jira.sonarsource.com/browse/VIEWS-296
if (!Qualifiers.SUBVIEW.equals(qualifier)) { if (!Qualifiers.SUBVIEW.equals(qualifier)) {
ComponentDto component = (ComponentDto) resourceDao.findByKey(kee); String createdKey = componentService.create(NewComponent.create(key, name).setQualifier(qualifier).setBranch(branch));
if (component != null) { ComponentDto component = (ComponentDto) resourceDao.findByKey(createdKey);
throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", qualifier, kee));
}
checkKeyFormat(qualifier, kee);

String uuid = Uuids.create();
resourceDao.insertOrUpdate(
new ResourceDto()
.setUuid(uuid)
.setProjectUuid(uuid)
.setKey(kee)
.setDeprecatedKey(kee)
.setName(name)
.setLongName(name)
.setScope(Scopes.PROJECT)
.setQualifier(qualifier)
.setCreatedAt(new Date()));
component = (ComponentDto) resourceDao.findByKey(kee);
if (component == null) { if (component == null) {
throw new BadRequestException(String.format("Component not created: %s", kee)); throw new BadRequestException(String.format("Component not created: %s", createdKey));
} }
resourceIndexerDao.indexResource(component.getId());
return component.getId(); return component.getId();
} }
return null; return null;
} }


public void updateComponent(Long id, String key, String name) {
ResourceDto resource = resourceDao.getResource(id);
if (resource == null) {
throw new NotFoundException();
}
checkKeyFormat(resource.getQualifier(), key);

resourceDao.insertOrUpdate(resource.setKey(key).setName(name));
}

public DefaultComponentQueryResult find(Map<String, Object> params) { public DefaultComponentQueryResult find(Map<String, Object> params) {
ComponentQuery query = toQuery(params); ComponentQuery query = toQuery(params);
List<Component> components = resourceDao.selectProjectsByQualifiers(query.qualifiers()); List<Component> components = resourceDao.selectProjectsByQualifiers(query.qualifiers());
Expand Down Expand Up @@ -158,14 +133,4 @@ static ComponentQuery toQuery(Map<String, Object> props) {
return builder.build(); return builder.build();
} }


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

private String formatMessage(String message, String qualifier, String key) {
return String.format(message, i18n.message(Locale.getDefault(), "qualifier." + qualifier, "Project"), key);
}
} }
@@ -0,0 +1,73 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube 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.
*
* SonarQube 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 com.google.common.base.Preconditions;
import org.sonar.api.resources.Qualifiers;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class NewComponent {

private String key;
private String branch;
private String qualifier;
private String name;

public NewComponent(String key, String name) {
this.key = key;
this.name = name;
}

public String key() {
return key;
}

public String name() {
return name;
}

@CheckForNull
public String branch() {
return branch;
}

public NewComponent setBranch(@Nullable String branch) {
this.branch = branch;
return this;
}

public String qualifier() {
return qualifier != null ? qualifier : Qualifiers.PROJECT;
}

public NewComponent setQualifier(@Nullable String qualifier) {
this.qualifier = qualifier;
return this;
}

public static NewComponent create(String key, String name) {
Preconditions.checkNotNull(key, "Key can't be null");
Preconditions.checkNotNull(name, "Name can't be null");
return new NewComponent(key, name);
}
}

0 comments on commit dbddd10

Please sign in to comment.