diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
index ff8e310bc7f9..bc7a95804b55 100644
--- a/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
+++ b/server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
@@ -121,7 +121,7 @@ public void test_real_start() throws IOException {
assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
COMPONENTS_IN_LEVEL_1_AT_CONSTRUCTION
+ 27 // level 1
- + 55 // content of DaoModule
+ + 56 // content of DaoModule
+ 3 // content of EsModule
+ 54 // content of CorePropertyDefinitions
+ 1 // StopFlagContainer
diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java
index f33d7f5f5758..9effb7706063 100644
--- a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java
+++ b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java
@@ -53,6 +53,7 @@ public final class SqTables {
"active_rules",
"active_rule_parameters",
"alm_app_installs",
+ "alm_project_mappings",
"analysis_properties",
"ce_activity",
"ce_queue",
diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
index e62fc5698f81..1a36753f7d7a 100644
--- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
+++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
@@ -853,6 +853,20 @@ CREATE TABLE "ALM_APP_INSTALLS" (
CREATE UNIQUE INDEX "ALM_APP_INSTALLS_OWNER" ON "ALM_APP_INSTALLS" ("ALM_ID", "OWNER_ID");
CREATE UNIQUE INDEX "ALM_APP_INSTALLS_INSTALL" ON "ALM_APP_INSTALLS" ("ALM_ID", "INSTALL_ID");
+CREATE TABLE "ALM_PROJECT_MAPPINGS" (
+ "UUID" VARCHAR(40) NOT NULL,
+ "ALM_ID" VARCHAR(40) NOT NULL,
+ "REPO_ID" VARCHAR(256) NOT NULL,
+ "PROJECT_UUID" VARCHAR(40) NOT NULL,
+ "GITHUB_SLUG" VARCHAR(256) NULL,
+ "URL" VARCHAR(2000) NOT NULL,
+ "CREATED_AT" BIGINT NOT NULL,
+ "UPDATED_AT" BIGINT NOT NULL,
+ CONSTRAINT "PK_ALM_PROJECT_MAPPINGS" PRIMARY KEY ("UUID")
+);
+CREATE UNIQUE INDEX "ALM_PROJECT_MAPPINGS_ALM_REPO" ON "ALM_PROJECT_MAPPINGS" ("ALM_ID", "REPO_ID");
+CREATE UNIQUE INDEX "ALM_PROJECT_MAPPINGS_PROJECT" ON "ALM_PROJECT_MAPPINGS" ("PROJECT_UUID");
+
CREATE TABLE "PROJECT_MAPPINGS" (
"UUID" VARCHAR(40) NOT NULL,
"KEY_TYPE" VARCHAR(200) NOT NULL,
@@ -863,4 +877,3 @@ CREATE TABLE "PROJECT_MAPPINGS" (
);
CREATE UNIQUE INDEX "KEY_TYPE_KEE" ON "PROJECT_MAPPINGS" ("KEY_TYPE", "KEE");
CREATE INDEX "PROJECT_UUID" ON "PROJECT_MAPPINGS" ("PROJECT_UUID");
-
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
index ff37086215ac..b6908ba0cb00 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
@@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.List;
import org.sonar.core.platform.Module;
+import org.sonar.db.alm.AlmProjectMappingsDao;
import org.sonar.db.ce.CeActivityDao;
import org.sonar.db.ce.CeQueueDao;
import org.sonar.db.ce.CeScannerContextDao;
@@ -105,6 +106,7 @@ public class DaoModule extends Module {
GroupMembershipDao.class,
GroupPermissionDao.class,
AlmAppInstallDao.class,
+ AlmProjectMappingsDao.class,
InternalPropertiesDao.class,
IssueChangeDao.class,
IssueDao.class,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
index 31d0465dcd48..2965acf71db9 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
@@ -21,6 +21,7 @@
import java.util.IdentityHashMap;
import java.util.Map;
+import org.sonar.db.alm.AlmProjectMappingsDao;
import org.sonar.db.ce.CeActivityDao;
import org.sonar.db.ce.CeQueueDao;
import org.sonar.db.ce.CeScannerContextDao;
@@ -90,6 +91,7 @@ public class DbClient {
private final QualityProfileDao qualityProfileDao;
private final PropertiesDao propertiesDao;
private final AlmAppInstallDao almAppInstallDao;
+ private final AlmProjectMappingsDao almProjectMappingsDao;
private final InternalPropertiesDao internalPropertiesDao;
private final SnapshotDao snapshotDao;
private final ComponentDao componentDao;
@@ -149,6 +151,7 @@ public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao..
map.put(dao.getClass(), dao);
}
almAppInstallDao = getDao(map, AlmAppInstallDao.class);
+ almProjectMappingsDao = getDao(map, AlmProjectMappingsDao.class);
schemaMigrationDao = getDao(map, SchemaMigrationDao.class);
authorizationDao = getDao(map, AuthorizationDao.class);
organizationDao = getDao(map, OrganizationDao.class);
@@ -217,6 +220,10 @@ public AlmAppInstallDao almAppInstallDao() {
return almAppInstallDao;
}
+ public AlmProjectMappingsDao almProjectMappingsDao() {
+ return almProjectMappingsDao;
+ }
+
public SchemaMigrationDao schemaMigrationDao() {
return schemaMigrationDao;
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
index f26fffb5ef8f..e6faa27aff4c 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
@@ -31,6 +31,7 @@
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.sonar.api.Startable;
import org.sonar.db.alm.AlmAppInstallMapper;
+import org.sonar.db.alm.AlmProjectMappingsMapper;
import org.sonar.db.ce.CeActivityMapper;
import org.sonar.db.ce.CeQueueMapper;
import org.sonar.db.ce.CeScannerContextMapper;
@@ -201,6 +202,7 @@ public void start() {
ActiveRuleMapper.class,
AnalysisPropertiesMapper.class,
AlmAppInstallMapper.class,
+ AlmProjectMappingsMapper.class,
AuthorizationMapper.class,
BranchMapper.class,
CeActivityMapper.class,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java
new file mode 100644
index 000000000000..0e5dd4ee2be5
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java
@@ -0,0 +1,31 @@
+/*
+ * 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.Locale;
+
+public enum ALM {
+ BITBUCKETCLOUD,
+ GITHUB;
+
+ String getId() {
+ return this.name().toLowerCase(Locale.ENGLISH);
+ }
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java
index 6d360f4f7f59..6deb486a5f43 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmAppInstallDao.java
@@ -19,7 +19,6 @@
*/
package org.sonar.db.alm;
-import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
@@ -36,15 +35,6 @@
*/
public class AlmAppInstallDao implements Dao {
- public enum ALM {
- BITBUCKETCLOUD,
- GITHUB;
-
- String getId() {
- return this.name().toLowerCase(Locale.ENGLISH);
- }
- }
-
private final System2 system2;
private final UuidFactory uuidFactory;
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsDao.java
new file mode 100644
index 000000000000..992e8f1f2bfe
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsDao.java
@@ -0,0 +1,74 @@
+/*
+ * 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.Objects;
+import javax.annotation.Nullable;
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.db.Dao;
+import org.sonar.db.DbSession;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.commons.lang.StringUtils.isNotEmpty;
+
+public class AlmProjectMappingsDao implements Dao {
+
+ private final System2 system2;
+ private final UuidFactory uuidFactory;
+
+ public AlmProjectMappingsDao(System2 system2, UuidFactory uuidFactory) {
+ this.system2 = system2;
+ this.uuidFactory = uuidFactory;
+ }
+
+ public void insertOrUpdate(DbSession dbSession, ALM alm, String repoId, String projectUuid, @Nullable String githubSlug, String url) {
+ checkAlm(alm);
+ checkRepoId(repoId);
+ checkArgument(isNotEmpty(projectUuid), "projectUuid can't be null nor empty");
+ checkArgument(isNotEmpty(url), "url can't be null nor empty");
+
+ AlmProjectMappingsMapper mapper = getMapper(dbSession);
+ long now = system2.now();
+
+ if (mapper.update(alm.getId(), repoId, projectUuid, githubSlug, url, now) == 0) {
+ mapper.insert(uuidFactory.create(), alm.getId(), repoId, projectUuid, githubSlug, url, now);
+ }
+ }
+
+ public boolean mappingExists(DbSession dbSession, ALM alm, String repoId) {
+ checkAlm(alm);
+ checkRepoId(repoId);
+
+ return getMapper(dbSession).mappingCount(alm.getId(), repoId) == 1;
+ }
+
+ private static void checkAlm(@Nullable ALM alm) {
+ Objects.requireNonNull(alm, "alm can't be null");
+ }
+
+ private static void checkRepoId(@Nullable String repoId) {
+ checkArgument(isNotEmpty(repoId), "repoId can't be null nor empty");
+ }
+
+ private static AlmProjectMappingsMapper getMapper(DbSession dbSession) {
+ return dbSession.getMapper(AlmProjectMappingsMapper.class);
+ }
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsMapper.java
new file mode 100644
index 000000000000..250f588ddae7
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/AlmProjectMappingsMapper.java
@@ -0,0 +1,34 @@
+/*
+ * 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 javax.annotation.Nullable;
+import org.apache.ibatis.annotations.Param;
+
+public interface AlmProjectMappingsMapper {
+
+ int mappingCount(@Param("almId") String almId, @Param("repoId") String repoId);
+
+ void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("repoId") String repoId, @Param("projectUuid") String projectUuid,
+ @Nullable @Param("githubSlug") String githubSlug, @Param("url") String url, @Param("now") long now);
+
+ int update(@Param("almId") String almId, @Param("repoId") String repoId, @Param("projectUuid") String projectUuid,
+ @Nullable @Param("githubSlug") String githubSlug, @Param("url") String url, @Param("now") long now);
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
index bab365e9f53b..ec3fe0d866fa 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
@@ -298,6 +298,13 @@ void deleteProjectMappings(String rootUuid) {
profiler.stop();
}
+ void deleteAlmProjectMappings(String rootUuid) {
+ profiler.start("deleteAlmProjectMappings (alm_project_mappings)");
+ purgeMapper.deleteAlmProjectMappingsByProjectUuid(rootUuid);
+ session.commit();
+ profiler.stop();
+ }
+
void deleteBranch(String rootUuid) {
profiler.start("deleteBranch (project_branches)");
purgeMapper.deleteBranchByUuid(rootUuid);
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
index 0c8316a92e80..17060408739b 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
@@ -203,6 +203,7 @@ private static void deleteRootComponent(String rootUuid, PurgeMapper mapper, Pur
commands.deleteCeQueue(rootUuid);
commands.deleteWebhookDeliveries(rootUuid);
commands.deleteProjectMappings(rootUuid);
+ commands.deleteAlmProjectMappings(rootUuid);
commands.deleteBranch(rootUuid);
commands.deleteLiveMeasures(rootUuid);
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
index 510377605fac..6d5d5e0d7d6e 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeMapper.java
@@ -110,6 +110,8 @@ public interface PurgeMapper {
void deleteProjectMappingsByProjectUuid(@Param("projectUuid") String projectUuid);
+ void deleteAlmProjectMappingsByProjectUuid(@Param("projectUuid") String projectUuid);
+
void deleteBranchByUuid(@Param("uuid") String uuid);
void deleteLiveMeasuresByProjectUuid(@Param("projectUuid") String projectUuid);
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmProjectMappingsMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmProjectMappingsMapper.xml
new file mode 100644
index 000000000000..0f6be2489e69
--- /dev/null
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/AlmProjectMappingsMapper.xml
@@ -0,0 +1,51 @@
+
+
+
+
+
+
+
+
+ INSERT INTO alm_project_mappings
+ (
+ uuid,
+ alm_id,
+ repo_id,
+ project_uuid,
+ github_slug,
+ url,
+ created_at,
+ updated_at
+ )
+ VALUES (
+ #{uuid, jdbcType=VARCHAR},
+ #{almId, jdbcType=VARCHAR},
+ #{repoId, jdbcType=VARCHAR},
+ #{projectUuid, jdbcType=VARCHAR},
+ #{githubSlug, jdbcType=VARCHAR},
+ #{url, jdbcType=VARCHAR},
+ #{now, jdbcType=BIGINT},
+ #{now, jdbcType=BIGINT}
+ )
+
+
+
+ update alm_project_mappings set
+ project_uuid = #{projectUuid, jdbcType=VARCHAR},
+ github_slug = #{githubSlug, jdbcType=VARCHAR},
+ url = #{url, jdbcType=VARCHAR},
+ updated_at = #{now, jdbcType=BIGINT}
+ where
+ alm_id = #{almId, jdbcType=VARCHAR}
+ and repo_id = #{repoId, jdbcType=VARCHAR}
+
+
+
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
index 3216e49cf645..a248eba70243 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/purge/PurgeMapper.xml
@@ -364,6 +364,10 @@
delete from project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR}
+
+ delete from alm_project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR}
+
+
delete from project_branches where uuid=#{uuid,jdbcType=VARCHAR}
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java
index 60d417966bd0..aa65e1e51833 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/DaoModuleTest.java
@@ -30,6 +30,6 @@ public class DaoModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new DaoModule().configure(container);
- assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 55);
+ assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 56);
}
}
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java
index a4a24259186f..aab2669fb067 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/AlmAppInstallDaoTest.java
@@ -36,7 +36,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.sonar.db.alm.AlmAppInstallDao.ALM.GITHUB;
+import static org.sonar.db.alm.ALM.GITHUB;
public class AlmAppInstallDaoTest {
@@ -214,17 +214,17 @@ private void expectInstallIdNullOrEmptyIAE() {
expectedException.expectMessage("installId can't be null nor empty");
}
- private AlmAppInstallAssert assertThatAlmAppInstall(AlmAppInstallDao.ALM alm, String ownerId) {
+ private AlmAppInstallAssert assertThatAlmAppInstall(ALM alm, String ownerId) {
return new AlmAppInstallAssert(dbTester, dbSession, alm, ownerId);
}
private static class AlmAppInstallAssert extends AbstractAssert {
- private AlmAppInstallAssert(DbTester dbTester, DbSession dbSession, AlmAppInstallDao.ALM alm, String ownerId) {
+ private AlmAppInstallAssert(DbTester dbTester, DbSession dbSession, ALM alm, String ownerId) {
super(asAlmAppInstall(dbTester, dbSession, alm, ownerId), AlmAppInstallAssert.class);
}
- private static AlmAppInstall asAlmAppInstall(DbTester dbTester, DbSession dbSession, AlmAppInstallDao.ALM alm, String ownerId) {
+ private static AlmAppInstall asAlmAppInstall(DbTester dbTester, DbSession dbSession, ALM alm, String ownerId) {
List