Skip to content

Commit

Permalink
SONAR-11038 Add table: alm_project_mappings (SonarSource#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Gyerik authored and SonarTech committed Aug 10, 2018
1 parent b08814f commit c243453
Show file tree
Hide file tree
Showing 23 changed files with 772 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");

2 changes: 2 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,6 +106,7 @@ public class DaoModule extends Module {
GroupMembershipDao.class,
GroupPermissionDao.class,
AlmAppInstallDao.class,
AlmProjectMappingsDao.class,
InternalPropertiesDao.class,
IssueChangeDao.class,
IssueDao.class,
Expand Down
7 changes: 7 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -217,6 +220,10 @@ public AlmAppInstallDao almAppInstallDao() {
return almAppInstallDao;
}

public AlmProjectMappingsDao almProjectMappingsDao() {
return almProjectMappingsDao;
}

public SchemaMigrationDao schemaMigrationDao() {
return schemaMigrationDao;
}
Expand Down
2 changes: 2 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,6 +202,7 @@ public void start() {
ActiveRuleMapper.class,
AnalysisPropertiesMapper.class,
AlmAppInstallMapper.class,
AlmProjectMappingsMapper.class,
AuthorizationMapper.class,
BranchMapper.class,
CeActivityMapper.class,
Expand Down
31 changes: 31 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">

<mapper namespace="org.sonar.db.alm.AlmProjectMappingsMapper">

<select id="mappingCount" parameterType="Map" resultType="int">
select
count(*) as count
from
alm_project_mappings
where
alm_id = #{almId, jdbcType=VARCHAR}
and repo_id = #{repoId, jdbcType=VARCHAR}
</select>

<insert id="insert" parameterType="Map" useGeneratedKeys="false">
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}
)
</insert>

<update id="update" parameterType="map">
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}
</update>

</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@
delete from project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR}
</delete>

<delete id="deleteAlmProjectMappingsByProjectUuid">
delete from alm_project_mappings where project_uuid=#{projectUuid,jdbcType=VARCHAR}
</delete>

<delete id="deleteBranchByUuid">
delete from project_branches where uuid=#{uuid,jdbcType=VARCHAR}
</delete>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit c243453

Please sign in to comment.