aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao
diff options
context:
space:
mode:
authorAntoine Vinot <antoine.vinot@sonarsource.com>2022-09-29 10:11:41 +0200
committersonartech <sonartech@sonarsource.com>2022-09-29 20:03:15 +0000
commit6f85804537da5fb66a091458dfdef4bd12da2572 (patch)
tree19de443b1783f1a9a7a92f7b19c73a40d5435e51 /server/sonar-db-dao
parent29868c3beba7895a71597532b727d213e6320ad0 (diff)
downloadsonarqube-6f85804537da5fb66a091458dfdef4bd12da2572.tar.gz
sonarqube-6f85804537da5fb66a091458dfdef4bd12da2572.zip
SONAR-17389 - Prevent project creation with existing key but different case
Diffstat (limited to 'server/sonar-db-dao')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java4
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java7
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml8
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java21
4 files changed, 38 insertions, 2 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
index afaba3af0e2..b11d2801afd 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
@@ -241,6 +241,10 @@ public class ComponentDao implements Dao {
return Optional.ofNullable(mapper(session).selectByKey(key));
}
+ public Optional<ComponentDto> selectByKeyCaseInsensitive(DbSession session, String key) {
+ return Optional.ofNullable(mapper(session).selectByKeyCaseInsensitive(key));
+ }
+
public Optional<ComponentDto> selectByKeyAndBranch(DbSession session, String key, String branch) {
return Optional.ofNullable(mapper(session).selectBranchByKeyAndBranchKey(key, generateBranchKey(key, branch), branch));
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
index 2d827bee828..e62a69a23b7 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
@@ -31,7 +31,10 @@ import org.apache.ibatis.session.RowBounds;
public interface ComponentMapper {
@CheckForNull
- ComponentDto selectByKey(String key);
+ ComponentDto selectByKey(@Param("key") String key);
+
+ @CheckForNull
+ ComponentDto selectByKeyCaseInsensitive(@Param("key") String key);
@CheckForNull
ComponentDto selectBranchByKeyAndBranchKey(@Param("key") String key, @Param("dbKey") String dbKey, @Param("branch") String branch);
@@ -40,7 +43,7 @@ public interface ComponentMapper {
ComponentDto selectPrByKeyAndBranchKey(@Param("key") String key, @Param("dbKey") String dbKey, @Param("branch") String branch);
@CheckForNull
- ComponentDto selectByUuid(String uuid);
+ ComponentDto selectByUuid(@Param("uuid") String uuid);
/**
* Return sub project of component keys
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
index 05d2e777fe1..aed19c345e9 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
@@ -32,6 +32,14 @@
p.kee=#{key,jdbcType=VARCHAR}
</select>
+ <select id="selectByKeyCaseInsensitive" parameterType="String" resultType="Component">
+ SELECT
+ <include refid="componentColumns"/>
+ FROM components p
+ where
+ lower(p.kee)=lower(#{key,jdbcType=VARCHAR})
+ </select>
+
<select id="selectBranchByKeyAndBranchKey" parameterType="String" resultType="Component">
select
<include refid="componentColumns"/>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
index c7c7d77f643..0996f6a56ff 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
@@ -1979,6 +1979,27 @@ public class ComponentDaoTest {
verifyNoInteractions(auditPersister);
}
+ @Test
+ public void selectByKeyCaseInsensitive_shouldFindProject_whenCaseIsDifferent() {
+ String projectKey = randomAlphabetic(5).toLowerCase();
+ db.components().insertPrivateProject(c -> c.setDbKey(projectKey));
+
+ ComponentDto result = underTest.selectByKeyCaseInsensitive(db.getSession(), projectKey.toUpperCase()).orElse(null);
+
+ assertThat(result).isNotNull();
+ assertThat(result.getKey()).isEqualTo(projectKey);
+ }
+
+ @Test
+ public void selectByKeyCaseInsensitive_shouldNotFindProject_whenKeyIsDifferent() {
+ String projectKey = randomAlphabetic(5).toLowerCase();
+ db.components().insertPrivateProject(c -> c.setDbKey(projectKey));
+
+ Optional<ComponentDto> result = underTest.selectByKeyCaseInsensitive(db.getSession(), projectKey + randomAlphabetic(1));
+
+ assertThat(result).isEmpty();
+ }
+
private boolean privateFlagOfUuid(String uuid) {
return underTest.selectByUuid(db.getSession(), uuid).get().isPrivate();
}