Skip to content

Commit

Permalink
SONAR-11321 Bind organization to alm installation when creating organ…
Browse files Browse the repository at this point in the history
…ization

* Return AlmAppInstallDto in select methods of AlmAppInstallDao, this is required in order to be able to more easily link an OrganizationDto to a AlmAppInstallDto in next commit
* Create ORGANIZATION_ALM_BINDINGS table
* Bind organization with installation when creating organization
* Delete alm binding when removing organization
* Delete alm binding when uninstalling ALM application
* Return ALM info in api/organizations/search
* Ensure user is admin to return Bitbucket team/user details
  • Loading branch information
julienlancelot authored and SonarTech committed Nov 16, 2018
1 parent f61d654 commit bcddd96
Show file tree
Hide file tree
Showing 44 changed files with 1,101 additions and 130 deletions.
Expand Up @@ -108,7 +108,7 @@ public void test_real_start() throws IOException {
+ 3 // CeCleaningModule + its content + 3 // CeCleaningModule + its content
+ 4 // WebhookModule + 4 // WebhookModule
+ 1 // CeDistributedInformation + 1 // CeDistributedInformation
); );
assertThat(picoContainer.getParent().getComponentAdapters()).hasSize( assertThat(picoContainer.getParent().getComponentAdapters()).hasSize(
CONTAINER_ITSELF CONTAINER_ITSELF
+ 8 // level 3 + 8 // level 3
Expand All @@ -121,7 +121,7 @@ public void test_real_start() throws IOException {
assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize( assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
+ 26 // level 1 + 26 // level 1
+ 57 // content of DaoModule + 58 // content of DaoModule
+ 3 // content of EsModule + 3 // content of EsModule
+ 54 // content of CorePropertyDefinitions + 54 // content of CorePropertyDefinitions
+ 1 // StopFlagContainer + 1 // StopFlagContainer
Expand Down
Expand Up @@ -77,6 +77,7 @@ public final class SqTables {
"metrics", "metrics",
"notifications", "notifications",
"organizations", "organizations",
"organization_alm_bindings",
"organization_members", "organization_members",
"org_qprofiles", "org_qprofiles",
"org_quality_gates", "org_quality_gates",
Expand Down
Expand Up @@ -902,3 +902,16 @@ CREATE TABLE "PROJECT_MAPPINGS" (
); );
CREATE UNIQUE INDEX "KEY_TYPE_KEE" ON "PROJECT_MAPPINGS" ("KEY_TYPE", "KEE"); CREATE UNIQUE INDEX "KEY_TYPE_KEE" ON "PROJECT_MAPPINGS" ("KEY_TYPE", "KEE");
CREATE INDEX "PROJECT_UUID" ON "PROJECT_MAPPINGS" ("PROJECT_UUID"); CREATE INDEX "PROJECT_UUID" ON "PROJECT_MAPPINGS" ("PROJECT_UUID");

CREATE TABLE "ORGANIZATION_ALM_BINDINGS" (
"UUID" VARCHAR(40) NOT NULL,
"ORGANIZATION_UUID" VARCHAR(40) NOT NULL,
"ALM_APP_INSTALL_UUID" VARCHAR(40) NOT NULL,
"ALM_ID" VARCHAR(40) NOT NULL,
"URL" VARCHAR(2000) NOT NULL,
"USER_UUID" VARCHAR(255) NOT NULL,
"CREATED_AT" BIGINT NOT NULL,
CONSTRAINT "PK_ORGANIZATION_ALM_BINDINGS" PRIMARY KEY ("UUID")
);
CREATE UNIQUE INDEX "ORG_ALM_BINDINGS_ORG" ON "ORGANIZATION_ALM_BINDINGS" ("ORGANIZATION_UUID");
CREATE UNIQUE INDEX "ORG_ALM_BINDINGS_INSTALL" ON "ORGANIZATION_ALM_BINDINGS" ("ALM_APP_INSTALL_UUID");
8 changes: 5 additions & 3 deletions server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
Expand Up @@ -23,7 +23,9 @@
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.sonar.core.platform.Module; import org.sonar.core.platform.Module;
import org.sonar.db.alm.ProjectAlmBindingsDao; import org.sonar.db.alm.AlmAppInstallDao;
import org.sonar.db.alm.OrganizationAlmBindingDao;
import org.sonar.db.alm.ProjectAlmBindingDao;
import org.sonar.db.ce.CeActivityDao; import org.sonar.db.ce.CeActivityDao;
import org.sonar.db.ce.CeQueueDao; import org.sonar.db.ce.CeQueueDao;
import org.sonar.db.ce.CeScannerContextDao; import org.sonar.db.ce.CeScannerContextDao;
Expand All @@ -41,7 +43,6 @@
import org.sonar.db.event.EventDao; import org.sonar.db.event.EventDao;
import org.sonar.db.issue.IssueChangeDao; import org.sonar.db.issue.IssueChangeDao;
import org.sonar.db.issue.IssueDao; import org.sonar.db.issue.IssueDao;
import org.sonar.db.alm.AlmAppInstallDao;
import org.sonar.db.mapping.ProjectMappingsDao; import org.sonar.db.mapping.ProjectMappingsDao;
import org.sonar.db.measure.LiveMeasureDao; import org.sonar.db.measure.LiveMeasureDao;
import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.MeasureDao;
Expand Down Expand Up @@ -108,14 +109,15 @@ public class DaoModule extends Module {
GroupMembershipDao.class, GroupMembershipDao.class,
GroupPermissionDao.class, GroupPermissionDao.class,
AlmAppInstallDao.class, AlmAppInstallDao.class,
ProjectAlmBindingsDao.class, ProjectAlmBindingDao.class,
InternalPropertiesDao.class, InternalPropertiesDao.class,
IssueChangeDao.class, IssueChangeDao.class,
IssueDao.class, IssueDao.class,
LiveMeasureDao.class, LiveMeasureDao.class,
MeasureDao.class, MeasureDao.class,
MetricDao.class, MetricDao.class,
NotificationQueueDao.class, NotificationQueueDao.class,
OrganizationAlmBindingDao.class,
OrganizationDao.class, OrganizationDao.class,
OrganizationMemberDao.class, OrganizationMemberDao.class,
PermissionTemplateCharacteristicDao.class, PermissionTemplateCharacteristicDao.class,
Expand Down
19 changes: 13 additions & 6 deletions server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
Expand Up @@ -21,7 +21,9 @@


import java.util.IdentityHashMap; import java.util.IdentityHashMap;
import java.util.Map; import java.util.Map;
import org.sonar.db.alm.ProjectAlmBindingsDao; import org.sonar.db.alm.AlmAppInstallDao;
import org.sonar.db.alm.OrganizationAlmBindingDao;
import org.sonar.db.alm.ProjectAlmBindingDao;
import org.sonar.db.ce.CeActivityDao; import org.sonar.db.ce.CeActivityDao;
import org.sonar.db.ce.CeQueueDao; import org.sonar.db.ce.CeQueueDao;
import org.sonar.db.ce.CeScannerContextDao; import org.sonar.db.ce.CeScannerContextDao;
Expand All @@ -39,7 +41,6 @@
import org.sonar.db.event.EventDao; import org.sonar.db.event.EventDao;
import org.sonar.db.issue.IssueChangeDao; import org.sonar.db.issue.IssueChangeDao;
import org.sonar.db.issue.IssueDao; import org.sonar.db.issue.IssueDao;
import org.sonar.db.alm.AlmAppInstallDao;
import org.sonar.db.mapping.ProjectMappingsDao; import org.sonar.db.mapping.ProjectMappingsDao;
import org.sonar.db.measure.LiveMeasureDao; import org.sonar.db.measure.LiveMeasureDao;
import org.sonar.db.measure.MeasureDao; import org.sonar.db.measure.MeasureDao;
Expand Down Expand Up @@ -92,7 +93,7 @@ public class DbClient {
private final QualityProfileDao qualityProfileDao; private final QualityProfileDao qualityProfileDao;
private final PropertiesDao propertiesDao; private final PropertiesDao propertiesDao;
private final AlmAppInstallDao almAppInstallDao; private final AlmAppInstallDao almAppInstallDao;
private final ProjectAlmBindingsDao projectAlmBindingsDao; private final ProjectAlmBindingDao projectAlmBindingDao;
private final InternalPropertiesDao internalPropertiesDao; private final InternalPropertiesDao internalPropertiesDao;
private final SnapshotDao snapshotDao; private final SnapshotDao snapshotDao;
private final ComponentDao componentDao; private final ComponentDao componentDao;
Expand Down Expand Up @@ -142,6 +143,7 @@ public class DbClient {
private final WebhookDao webhookDao; private final WebhookDao webhookDao;
private final WebhookDeliveryDao webhookDeliveryDao; private final WebhookDeliveryDao webhookDeliveryDao;
private final ProjectMappingsDao projectMappingsDao; private final ProjectMappingsDao projectMappingsDao;
private final OrganizationAlmBindingDao organizationAlmBindingDao;


public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) {
this.database = database; this.database = database;
Expand All @@ -153,7 +155,7 @@ public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao..
map.put(dao.getClass(), dao); map.put(dao.getClass(), dao);
} }
almAppInstallDao = getDao(map, AlmAppInstallDao.class); almAppInstallDao = getDao(map, AlmAppInstallDao.class);
projectAlmBindingsDao = getDao(map, ProjectAlmBindingsDao.class); projectAlmBindingDao = getDao(map, ProjectAlmBindingDao.class);
schemaMigrationDao = getDao(map, SchemaMigrationDao.class); schemaMigrationDao = getDao(map, SchemaMigrationDao.class);
authorizationDao = getDao(map, AuthorizationDao.class); authorizationDao = getDao(map, AuthorizationDao.class);
organizationDao = getDao(map, OrganizationDao.class); organizationDao = getDao(map, OrganizationDao.class);
Expand Down Expand Up @@ -209,6 +211,7 @@ public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao..
webhookDao = getDao(map, WebhookDao.class); webhookDao = getDao(map, WebhookDao.class);
webhookDeliveryDao = getDao(map, WebhookDeliveryDao.class); webhookDeliveryDao = getDao(map, WebhookDeliveryDao.class);
projectMappingsDao = getDao(map, ProjectMappingsDao.class); projectMappingsDao = getDao(map, ProjectMappingsDao.class);
organizationAlmBindingDao = getDao(map, OrganizationAlmBindingDao.class);
} }


public DbSession openSession(boolean batch) { public DbSession openSession(boolean batch) {
Expand All @@ -223,8 +226,8 @@ public AlmAppInstallDao almAppInstallDao() {
return almAppInstallDao; return almAppInstallDao;
} }


public ProjectAlmBindingsDao projectAlmBindingsDao() { public ProjectAlmBindingDao projectAlmBindingsDao() {
return projectAlmBindingsDao; return projectAlmBindingDao;
} }


public SchemaMigrationDao schemaMigrationDao() { public SchemaMigrationDao schemaMigrationDao() {
Expand Down Expand Up @@ -456,4 +459,8 @@ public WebhookDeliveryDao webhookDeliveryDao() {
public ProjectMappingsDao projectMappingsDao() { public ProjectMappingsDao projectMappingsDao() {
return projectMappingsDao; return projectMappingsDao;
} }

public OrganizationAlmBindingDao organizationAlmBindingDao() {
return organizationAlmBindingDao;
}
} }
6 changes: 4 additions & 2 deletions server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
Expand Up @@ -31,8 +31,9 @@
import org.apache.ibatis.session.TransactionIsolationLevel; import org.apache.ibatis.session.TransactionIsolationLevel;
import org.sonar.api.Startable; import org.sonar.api.Startable;
import org.sonar.db.alm.AlmAppInstallMapper; import org.sonar.db.alm.AlmAppInstallMapper;
import org.sonar.db.alm.OrganizationAlmBindingMapper;
import org.sonar.db.alm.ProjectAlmBindingDto; import org.sonar.db.alm.ProjectAlmBindingDto;
import org.sonar.db.alm.ProjectAlmBindingsMapper; import org.sonar.db.alm.ProjectAlmBindingMapper;
import org.sonar.db.ce.CeActivityMapper; import org.sonar.db.ce.CeActivityMapper;
import org.sonar.db.ce.CeQueueMapper; import org.sonar.db.ce.CeQueueMapper;
import org.sonar.db.ce.CeScannerContextMapper; import org.sonar.db.ce.CeScannerContextMapper;
Expand Down Expand Up @@ -232,12 +233,13 @@ public void start() {
MeasureMapper.class, MeasureMapper.class,
MetricMapper.class, MetricMapper.class,
NotificationQueueMapper.class, NotificationQueueMapper.class,
OrganizationAlmBindingMapper.class,
OrganizationMapper.class, OrganizationMapper.class,
OrganizationMemberMapper.class, OrganizationMemberMapper.class,
PermissionTemplateCharacteristicMapper.class, PermissionTemplateCharacteristicMapper.class,
PermissionTemplateMapper.class, PermissionTemplateMapper.class,
PluginMapper.class, PluginMapper.class,
ProjectAlmBindingsMapper.class, ProjectAlmBindingMapper.class,
ProjectLinkMapper.class, ProjectLinkMapper.class,
ProjectMappingsMapper.class, ProjectMappingsMapper.class,
ProjectQgateAssociationMapper.class, ProjectQgateAssociationMapper.class,
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.sonar.db.DbSession; import org.sonar.db.DbSession;


import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isNotEmpty; import static org.apache.commons.lang.StringUtils.isNotEmpty;


/** /**
Expand All @@ -44,17 +45,17 @@ public AlmAppInstallDao(System2 system2, UuidFactory uuidFactory) {
this.uuidFactory = uuidFactory; this.uuidFactory = uuidFactory;
} }


public Optional<AlmAppInstallDto> selectByOwner(DbSession dbSession, ALM alm, String ownerId) { public Optional<AlmAppInstallDto> selectByOwnerId(DbSession dbSession, ALM alm, String ownerId) {
checkAlm(alm); checkAlm(alm);
checkOwnerId(ownerId); checkOwnerId(ownerId);


AlmAppInstallMapper mapper = getMapper(dbSession); AlmAppInstallMapper mapper = getMapper(dbSession);
return Optional.ofNullable(mapper.selectByOwner(alm.getId(), ownerId)); return Optional.ofNullable(mapper.selectByOwnerId(alm.getId(), ownerId));
} }


public Optional<String> getOwerId(DbSession dbSession, ALM alm, String installationId) { public Optional<AlmAppInstallDto> selectByInstallationId(DbSession dbSession, ALM alm, String installationId) {
AlmAppInstallMapper mapper = getMapper(dbSession); AlmAppInstallMapper mapper = getMapper(dbSession);
return Optional.ofNullable(mapper.selectOwnerId(alm.getId(), installationId)); return Optional.ofNullable(mapper.selectByInstallationId(alm.getId(), installationId));
} }


public List<AlmAppInstallDto> findAllWithNoOwnerType(DbSession dbSession) { public List<AlmAppInstallDto> findAllWithNoOwnerType(DbSession dbSession) {
Expand Down Expand Up @@ -88,7 +89,7 @@ public void delete(DbSession dbSession, ALM alm, String ownerId) {
} }


private static void checkAlm(@Nullable ALM alm) { private static void checkAlm(@Nullable ALM alm) {
Objects.requireNonNull(alm, "alm can't be null"); requireNonNull(alm, "alm can't be null");
} }


private static void checkOwnerId(@Nullable String ownerId) { private static void checkOwnerId(@Nullable String ownerId) {
Expand Down
Expand Up @@ -27,10 +27,10 @@
public interface AlmAppInstallMapper { public interface AlmAppInstallMapper {


@CheckForNull @CheckForNull
AlmAppInstallDto selectByOwner(@Param("almId") String almId, @Param("ownerId") String ownerId); AlmAppInstallDto selectByOwnerId(@Param("almId") String almId, @Param("ownerId") String ownerId);


@CheckForNull @CheckForNull
String selectOwnerId(@Param("almId") String almId, @Param("installId") String installId); AlmAppInstallDto selectByInstallationId(@Param("almId") String almId, @Param("installId") String installId);


List<AlmAppInstallDto> selectAllWithNoOwnerType(); List<AlmAppInstallDto> selectAllWithNoOwnerType();


Expand Down
29 changes: 29 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmTesting.java
@@ -0,0 +1,29 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info 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.db.alm;

public class AlmTesting {

private AlmTesting() {
// only statics
}


}
@@ -0,0 +1,76 @@
/*
* SonarQube
* Copyright (C) 2009-2018 SonarSource SA
* mailto:info 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.db.alm;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;

import static org.sonar.db.DatabaseUtils.executeLargeInputs;

public class OrganizationAlmBindingDao implements Dao {

private final System2 system2;
private final UuidFactory uuidFactory;

public OrganizationAlmBindingDao(System2 system2, UuidFactory uuidFactory) {
this.system2 = system2;
this.uuidFactory = uuidFactory;
}

public Optional<OrganizationAlmBindingDto> selectByOrganization(DbSession dbSession, OrganizationDto organization) {
return Optional.ofNullable(getMapper(dbSession).selectByOrganizationUuid(organization.getUuid()));
}

public List<OrganizationAlmBindingDto> selectByOrganizations(DbSession dbSession, Collection<OrganizationDto> organizations) {
return executeLargeInputs(organizations.stream().map(OrganizationDto::getUuid).collect(MoreCollectors.toSet()),
organizationUuids -> getMapper(dbSession).selectByOrganizationUuids(organizationUuids));
}

public void insert(DbSession dbSession, OrganizationDto organization, AlmAppInstallDto almAppInstall, String url, String userUuid) {
long now = system2.now();
getMapper(dbSession).insert(new OrganizationAlmBindingDto()
.setUuid(uuidFactory.create())
.setOrganizationUuid(organization.getUuid())
.setAlmAppInstallUuid(almAppInstall.getUuid())
.setAlmId(almAppInstall.getAlm())
.setUrl(url)
.setUserUuid(userUuid)
.setCreatedAt(now));
}

public void deleteByOrganization(DbSession dbSession, OrganizationDto organization) {
getMapper(dbSession).deleteByOrganizationUuid(organization.getUuid());
}

public void deleteByAlmAppInstall(DbSession dbSession, AlmAppInstallDto almAppInstall) {
getMapper(dbSession).deleteByAlmAppInstallUuid(almAppInstall.getUuid());
}

private static OrganizationAlmBindingMapper getMapper(DbSession dbSession) {
return dbSession.getMapper(OrganizationAlmBindingMapper.class);
}
}

0 comments on commit bcddd96

Please sign in to comment.