public ComponentDto create(NewComponent newComponent) {
userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING);
- ComponentDto project = createProject(newComponent);
- removeDuplicatedProjects(project.getKey());
- return project;
- }
- private ComponentDto createProject(NewComponent newComponent) {
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 = new ComponentDto()
- .setUuid(uuid)
- .setModuleUuid(null)
- .setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + uuid + ComponentDto.MODULE_UUID_PATH_SEP)
- .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.getId());
- session.commit();
- return component;
+ return create(session, newComponent);
} finally {
dbClient.closeSession(session);
}
}
+ public ComponentDto create(DbSession session, NewComponent newComponent) {
+ userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING);
+ ComponentDto project = createProject(session, newComponent);
+ removeDuplicatedProjects(session, project.getKey());
+ return project;
+ }
+
+ private ComponentDto createProject(DbSession session, NewComponent newComponent) {
+ 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 = new ComponentDto()
+ .setUuid(uuid)
+ .setModuleUuid(null)
+ .setModuleUuidPath(ComponentDto.MODULE_UUID_PATH_SEP + uuid + ComponentDto.MODULE_UUID_PATH_SEP)
+ .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.getId());
+ 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(String projectKey) {
- DbSession session = dbClient.openSession(false);
- try {
- 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();
- } finally {
- dbClient.closeSession(session);
+ 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(@Nullable Collection<String> componentKeys) {
public void applyDefaultPermissionTemplate(String componentKey) {
DbSession session = dbClient.openSession(false);
try {
- ComponentDto component = componentFinder.getByKey(session, componentKey);
- ResourceDto provisioned = dbClient.resourceDao().selectProvisionedProject(session, componentKey);
- if (provisioned == null) {
- checkProjectAdminPermission(componentKey);
- } else {
- userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING);
- }
- permissionRepository.grantDefaultRoles(session, component.getId(), component.qualifier());
- session.commit();
+ applyDefaultPermissionTemplate(session, componentKey);
} finally {
session.close();
}
indexProjectPermissions();
}
+ public void applyDefaultPermissionTemplate(DbSession session, String componentKey) {
+ ComponentDto component = componentFinder.getByKey(session, componentKey);
+ ResourceDto provisioned = dbClient.resourceDao().selectProvisionedProject(session, componentKey);
+ if (provisioned == null) {
+ checkProjectAdminPermission(componentKey);
+ } else {
+ userSession.checkGlobalPermission(GlobalPermissions.PROVISIONING);
+ }
+ permissionRepository.grantDefaultRoles(session, component.getId(), component.qualifier());
+ session.commit();
+ indexProjectPermissions();
+ }
+
/**
* Important - this method checks caller permissions
*/