BITBUCKETCLOUD,
GITHUB;
- String getId() {
+ public String getId() {
return this.name().toLowerCase(Locale.ENGLISH);
}
}
*/
package org.sonar.db.alm;
+import java.util.Arrays;
+import java.util.Optional;
+
+/**
+ * DTO is used only for select, hence no setters (MyBatis populates field by reflection).
+ */
public class ProjectAlmBindingDto {
private String uuid;
- private String almId;
+ private String rawAlmId;
private String repoId;
private String projectUuid;
private String githubSlug;
private String url;
- public String getAlmId() {
- return almId;
- }
+ public Optional<ALM> getAlm() {
+ if (rawAlmId == null) {
+ return Optional.empty();
+ }
- public ProjectAlmBindingDto setAlmId(String almId) {
- this.almId = almId;
- return this;
+ return Arrays.stream(ALM.values())
+ .filter(a -> a.getId().equals(rawAlmId))
+ .findAny();
}
public String getRepoId() {
return repoId;
}
- public ProjectAlmBindingDto setRepoId(String repoId) {
- this.repoId = repoId;
- return this;
- }
-
public String getProjectUuid() {
return projectUuid;
}
- public ProjectAlmBindingDto setProjectUuid(String projectUuid) {
- this.projectUuid = projectUuid;
- return this;
- }
-
public String getGithubSlug() {
return githubSlug;
}
- public ProjectAlmBindingDto setGithubSlug(String githubSlug) {
- this.githubSlug = githubSlug;
- return this;
- }
-
public String getUrl() {
return url;
}
- public ProjectAlmBindingDto setUrl(String url) {
- this.url = url;
- return this;
- }
-
public String getUuid() {
return uuid;
}
- public ProjectAlmBindingDto setUuid(String uuid) {
- this.uuid = uuid;
- return this;
- }
}
<mapper namespace="org.sonar.db.alm.ProjectAlmBindingsMapper">
+ <sql id="columns">
+ uuid,
+ alm_id as rawAlmId,
+ repo_id as repoId,
+ project_uuid as projectUuid,
+ github_slug as githubSlug,
+ url
+ </sql>
+
<select id="bindingCount" parameterType="Map" resultType="int">
select
count(*) as count
</select>
<insert id="insert" parameterType="Map" useGeneratedKeys="false">
- INSERT INTO project_alm_bindings
+ insert into project_alm_bindings
(
uuid,
alm_id,
created_at,
updated_at
)
- VALUES (
+ values (
#{uuid, jdbcType=VARCHAR},
#{almId, jdbcType=VARCHAR},
#{repoId, jdbcType=VARCHAR},
</insert>
<update id="update" parameterType="map">
- update project_alm_bindings set
+ update project_alm_bindings
+ set
project_uuid = #{projectUuid, jdbcType=VARCHAR},
github_slug = #{githubSlug, jdbcType=VARCHAR},
url = #{url, jdbcType=VARCHAR},
<select id="selectByRepoIds" parameterType="map" resultType="ProjectAlmBinding">
select
- uuid,
- alm_id as almId,
- repo_id as repoId,
- project_uuid as projectUuid,
- github_slug as githubSlug,
- url
- from project_alm_bindings
+ <include refid="columns"/>
+ from
+ project_alm_bindings
where
alm_id = #{almId, jdbcType=VARCHAR}
and repo_id in
<select id="selectByRepoId" parameterType="map" resultType="ProjectAlmBinding">
select
- uuid,
- alm_id as almId,
- repo_id as repoId,
- project_uuid as projectUuid,
- github_slug as githubSlug,
- url
- from project_alm_bindings
+ <include refid="columns"/>
+ from
+ project_alm_bindings
where
- alm_id = #{almId, jdbcType=VARCHAR}
- and repo_id = #{repoId, jdbcType=VARCHAR}
+ alm_id = #{almId, jdbcType=VARCHAR}
+ and repo_id = #{repoId, jdbcType=VARCHAR}
</select>
<select id="selectByProjectUuid" parameterType="map" resultType="ProjectAlmBinding">
select
- uuid,
- alm_id as almId,
- repo_id as repoId,
- project_uuid as projectUuid,
- github_slug as githubSlug,
- url
- from project_alm_bindings
+ <include refid="columns"/>
+ from
+ project_alm_bindings
where
- project_uuid = #{projectUuid, jdbcType=VARCHAR}
+ project_uuid = #{projectUuid, jdbcType=VARCHAR}
</select>
<select id="selectProjectKey" parameterType="Map" resultType="String">
select
p.kee as projectKey
from
- project_alm_bindings b join projects p
+ project_alm_bindings b
+ inner join projects p
on b.project_uuid = p.project_uuid
where
alm_id = #{almId, jdbcType=VARCHAR}
Optional<ProjectAlmBindingDto> dto = underTest.selectByRepoId(dbSession, GITHUB, A_REPO);
assertThat(dto).isPresent();
assertThat(dto.get().getUuid()).isEqualTo("uuid1");
- assertThat(dto.get().getAlmId()).isEqualTo(GITHUB.getId());
+ assertThat(dto.get().getAlm()).contains(GITHUB);
assertThat(dto.get().getRepoId()).isEqualTo(A_REPO);
assertThat(dto.get().getProjectUuid()).isEqualTo(A_UUID);
assertThat(dto.get().getUrl()).isEqualTo(A_URL);
.thenReturn("uuid1")
.thenReturn("uuid2")
.thenReturn("uuid3");
- underTest.insertOrUpdate(dbSession, GITHUB, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL);
- underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, ANOTHER_UUID, null, ANOTHER_URL);
- underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, "foo", null, "http://foo");
+ underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, A_REPO, A_UUID, A_GITHUB_SLUG, A_URL);
+ underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, ANOTHER_UUID, null, ANOTHER_URL);
+ underTest.insertOrUpdate(dbSession, GITHUB, ANOTHER_REPO, "foo", null, "http://foo");
assertThat(underTest.selectByProjectUuid(dbSession, "missing")).isNotPresent();
Optional<ProjectAlmBindingDto> dto = underTest.selectByProjectUuid(dbSession, A_UUID);
assertThat(dto).isPresent();
assertThat(dto.get().getUuid()).isEqualTo("uuid1");
- assertThat(dto.get().getAlmId()).isEqualTo(GITHUB.getId());
+ assertThat(dto.get().getAlm()).contains(BITBUCKETCLOUD);
assertThat(dto.get().getRepoId()).isEqualTo(A_REPO);
assertThat(dto.get().getProjectUuid()).isEqualTo(A_UUID);
assertThat(dto.get().getUrl()).isEqualTo(A_URL);
underTest.insertOrUpdate(dbSession, BITBUCKETCLOUD, ANOTHER_REPO, "foo", null, "http://foo");
assertThat(underTest.selectByRepoIds(dbSession, GITHUB, Arrays.asList(A_REPO, ANOTHER_REPO, "foo")))
- .extracting(ProjectAlmBindingDto::getUuid, ProjectAlmBindingDto::getAlmId, ProjectAlmBindingDto::getRepoId, ProjectAlmBindingDto::getProjectUuid,
+ .extracting(ProjectAlmBindingDto::getUuid, t -> t.getAlm().get(), ProjectAlmBindingDto::getRepoId, ProjectAlmBindingDto::getProjectUuid,
ProjectAlmBindingDto::getUrl, ProjectAlmBindingDto::getGithubSlug)
.containsExactlyInAnyOrder(
- tuple("uuid1", GITHUB.getId(), A_REPO, A_UUID, A_URL, A_GITHUB_SLUG),
- tuple("uuid2", GITHUB.getId(), ANOTHER_REPO, ANOTHER_UUID, ANOTHER_URL, null));
+ tuple("uuid1", GITHUB, A_REPO, A_UUID, A_URL, A_GITHUB_SLUG),
+ tuple("uuid2", GITHUB, ANOTHER_REPO, ANOTHER_UUID, ANOTHER_URL, null));
}
@Test
import org.sonar.api.web.page.Page;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.alm.ALM;
import org.sonar.db.alm.ProjectAlmBindingDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
private void writeAlmDetails(JsonWriter json, DbSession session, ComponentDto component) {
Optional<ProjectAlmBindingDto> bindingOpt = dbClient.projectAlmBindingsDao().selectByProjectUuid(session, component.uuid());
- bindingOpt.ifPresent(b -> json.prop("almId", b.getAlmId())
- .prop("almRepoUrl", b.getUrl()));
+ bindingOpt.ifPresent(b -> {
+ String almId = b.getAlm()
+ .map(ALM::getId)
+ .map(String::valueOf)
+ .orElseThrow(() -> new IllegalStateException("Alm binding id DB has no ALM id"));
+ json.prop("almId", almId)
+ .prop("almRepoUrl", b.getUrl());
+ });
}
private static Consumer<QualityProfile> writeToJson(JsonWriter json) {