Skip to content

Commit

Permalink
SONARCLOUD-150 Store if application owner is a user
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju authored and ehartmann committed Oct 25, 2018
1 parent 9ae4d36 commit b9c7590
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 33 deletions.
Expand Up @@ -115,7 +115,7 @@ public void test_real_start() throws IOException {
); );
assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize( assertThat(picoContainer.getParent().getParent().getComponentAdapters()).hasSize(
CONTAINER_ITSELF CONTAINER_ITSELF
+ 18 // MigrationConfigurationModule + 19 // MigrationConfigurationModule
+ 17 // level 2 + 17 // level 2
); );
assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize( assertThat(picoContainer.getParent().getParent().getParent().getComponentAdapters()).hasSize(
Expand Down
Expand Up @@ -868,6 +868,7 @@ CREATE TABLE "ALM_APP_INSTALLS" (
"UUID" VARCHAR(40) NOT NULL, "UUID" VARCHAR(40) NOT NULL,
"ALM_ID" VARCHAR(40) NOT NULL, "ALM_ID" VARCHAR(40) NOT NULL,
"OWNER_ID" VARCHAR(4000) NOT NULL, "OWNER_ID" VARCHAR(4000) NOT NULL,
"IS_OWNER_USER" BOOLEAN,
"INSTALL_ID" VARCHAR(4000) NOT NULL, "INSTALL_ID" VARCHAR(4000) NOT NULL,
"CREATED_AT" BIGINT NOT NULL, "CREATED_AT" BIGINT NOT NULL,
"UPDATED_AT" BIGINT NOT NULL, "UPDATED_AT" BIGINT NOT NULL,
Expand Down
4 changes: 4 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/alm/ALM.java
Expand Up @@ -25,6 +25,10 @@ public enum ALM {
BITBUCKETCLOUD, BITBUCKETCLOUD,
GITHUB; GITHUB;


public static ALM fromId(String almId) {
return ALM.valueOf(almId.toUpperCase(Locale.ENGLISH));
}

public String getId() { public String getId() {
return this.name().toLowerCase(Locale.ENGLISH); return this.name().toLowerCase(Locale.ENGLISH);
} }
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/ */
package org.sonar.db.alm; package org.sonar.db.alm;


import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import javax.annotation.Nullable; import javax.annotation.Nullable;
Expand Down Expand Up @@ -48,25 +49,29 @@ public AlmAppInstallDao(System2 system2, UuidFactory uuidFactory) {
* @param ownerId ALM specific identifier of the owner of the app, like team or user uuid for Bitbucket Cloud or organization id for Github, can't be null * @param ownerId ALM specific identifier of the owner of the app, like team or user uuid for Bitbucket Cloud or organization id for Github, can't be null
* @param installId ALM specific identifier of the app installation, can't be null * @param installId ALM specific identifier of the app installation, can't be null
*/ */
public void insertOrUpdate(DbSession dbSession, ALM alm, String ownerId, String installId) { public void insertOrUpdate(DbSession dbSession, ALM alm, String ownerId, @Nullable Boolean isOwnerUser, String installId) {
checkAlm(alm); checkAlm(alm);
checkOwnerId(ownerId); checkOwnerId(ownerId);
checkArgument(isNotEmpty(installId), "installId can't be null nor empty"); checkArgument(isNotEmpty(installId), "installId can't be null nor empty");


AlmAppInstallMapper mapper = getMapper(dbSession); AlmAppInstallMapper mapper = getMapper(dbSession);
long now = system2.now(); long now = system2.now();


if (mapper.update(alm.getId(), ownerId, installId, now) == 0) { if (mapper.update(alm.getId(), ownerId, isOwnerUser, installId, now) == 0) {
mapper.insert(uuidFactory.create(), alm.getId(), ownerId, installId, now); mapper.insert(uuidFactory.create(), alm.getId(), ownerId, isOwnerUser, installId, now);
} }
} }


public Optional<String> getInstallId(DbSession dbSession, ALM alm, String ownerId) { public List<AlmAppInstallDto> findAllWithNoOwnerType(DbSession dbSession) {
return getMapper(dbSession).selectAllWithNoOwnerType();
}

public Optional<AlmAppInstallDto> selectByOwner(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.selectInstallId(alm.getId(), ownerId)); return Optional.ofNullable(mapper.selectByOwner(alm.getId(), ownerId));
} }


public void delete(DbSession dbSession, ALM alm, String ownerId) { public void delete(DbSession dbSession, ALM alm, String ownerId) {
Expand Down
@@ -0,0 +1,117 @@
/*
* 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;

public class AlmAppInstallDto {

/**
* Technical unique identifier, can't be null
*/
private String uuid;
/**
* alm_id, can't be null
*/
private String almId;
/**
* Owner id, can't be null
*/
private String ownerId;
/**
* Installation id, can't be null
*/
private String installId;
/**
* Is owner a user, can be null
*/
private Boolean isOwnerUser;

private long createdAt;
private long updatedAt;

public AlmAppInstallDto setUuid(String uuid) {
this.uuid = uuid;
return this;
}

public AlmAppInstallDto setAlmId(String almId) {
this.almId = almId;
return this;
}

public AlmAppInstallDto setOwnerId(String ownerId) {
this.ownerId = ownerId;
return this;
}

public AlmAppInstallDto setInstallId(String installId) {
this.installId = installId;
return this;
}

public AlmAppInstallDto setIsOwnerUser(@Nullable Boolean isOwnerUser) {
this.isOwnerUser = isOwnerUser;
return this;
}

AlmAppInstallDto setCreatedAt(long createdAt) {
this.createdAt = createdAt;
return this;
}

AlmAppInstallDto setUpdatedAt(long updatedAt) {
this.updatedAt = updatedAt;
return this;
}

public String getUuid() {
return uuid;
}

public String getAlmId() {
return almId;
}

public ALM getAlm() {
return ALM.fromId(almId);
}

public String getOwnerId() {
return ownerId;
}

public String getInstallId() {
return installId;
}

@Nullable
public Boolean isOwnerUser() {
return isOwnerUser;
}

public long getCreatedAt() {
return createdAt;
}

public long getUpdatedAt() {
return updatedAt;
}
}
Expand Up @@ -19,17 +19,23 @@
*/ */
package org.sonar.db.alm; package org.sonar.db.alm;


import java.util.List;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;


public interface AlmAppInstallMapper { public interface AlmAppInstallMapper {


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


void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("ownerId") String ownerId, @Param("installId") String installId, @Param("now") long now); List<AlmAppInstallDto> selectAllWithNoOwnerType();


int update(@Param("almId") String almId, @Param("ownerId") String ownerId, @Param("installId") String installId, @Param("now") long now); void insert(@Param("uuid") String uuid, @Param("almId") String almId, @Param("ownerId") String ownerId,
@Nullable @Param("isOwnerUser") Boolean isOwnerUser, @Param("installId") String installId, @Param("now") long now);

int update(@Param("almId") String almId, @Param("ownerId") String ownerId,
@Nullable @Param("isOwnerUser") Boolean isOwnerUser, @Param("installId") String installId, @Param("now") long now);


void delete(@Param("almId") String almId, @Param("ownerId") String ownerId); void delete(@Param("almId") String almId, @Param("ownerId") String ownerId);
} }
Expand Up @@ -3,22 +3,40 @@


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


<select id="selectInstallId" parameterType="Map" resultType="String"> <sql id="sqlColumns">
select uuid,
install_id as installId alm_id as almId,
owner_id as ownerId,
is_owner_user as isOwnerUser,
install_id as installId,
created_at as createdAt,
updated_at as updatedAt
</sql>

<select id="selectByOwner" parameterType="Map" resultType="org.sonar.db.alm.AlmAppInstallDto">
select <include refid="sqlColumns" />
from from
alm_app_installs alm_app_installs
where where
alm_id = #{almId, jdbcType=VARCHAR} alm_id = #{almId, jdbcType=VARCHAR}
and owner_id = #{ownerId, jdbcType=VARCHAR} and owner_id = #{ownerId, jdbcType=VARCHAR}
</select> </select>


<select id="selectAllWithNoOwnerType" parameterType="Map" resultType="org.sonar.db.alm.AlmAppInstallDto">
select <include refid="sqlColumns" />
from
alm_app_installs
where
is_owner_user is null
</select>

<insert id="insert" parameterType="Map" useGeneratedKeys="false"> <insert id="insert" parameterType="Map" useGeneratedKeys="false">
INSERT INTO alm_app_installs INSERT INTO alm_app_installs
( (
uuid, uuid,
alm_id, alm_id,
owner_id, owner_id,
is_owner_user,
install_id, install_id,
created_at, created_at,
updated_at updated_at
Expand All @@ -27,6 +45,7 @@
#{uuid, jdbcType=VARCHAR}, #{uuid, jdbcType=VARCHAR},
#{almId, jdbcType=VARCHAR}, #{almId, jdbcType=VARCHAR},
#{ownerId, jdbcType=VARCHAR}, #{ownerId, jdbcType=VARCHAR},
#{isOwnerUser, jdbcType=BOOLEAN},
#{installId, jdbcType=VARCHAR}, #{installId, jdbcType=VARCHAR},
#{now, jdbcType=BIGINT}, #{now, jdbcType=BIGINT},
#{now, jdbcType=BIGINT} #{now, jdbcType=BIGINT}
Expand All @@ -36,6 +55,7 @@
<update id="update" parameterType="map"> <update id="update" parameterType="map">
update alm_app_installs set update alm_app_installs set
install_id = #{installId, jdbcType=VARCHAR}, install_id = #{installId, jdbcType=VARCHAR},
is_owner_user = #{isOwnerUser, jdbcType=BOOLEAN},
updated_at = #{now, jdbcType=BIGINT} updated_at = #{now, jdbcType=BIGINT}
where where
alm_id = #{almId, jdbcType=VARCHAR} alm_id = #{almId, jdbcType=VARCHAR}
Expand Down

0 comments on commit b9c7590

Please sign in to comment.