From 966250379cd22b0c1ff462194bdea6fb4fa85e49 Mon Sep 17 00:00:00 2001 From: Vojtech Suchy Date: Tue, 12 Nov 2024 14:06:36 +0100 Subject: [PATCH] CODEFIX-181 Introduce a new endpoint for AI CodeFix project level enablement --- .../org/sonar/db/project/ProjectDaoIT.java | 50 +++++++++++++++++++ .../java/org/sonar/db/project/ProjectDao.java | 4 ++ .../org/sonar/db/project/ProjectMapper.java | 2 + .../org/sonar/db/project/ProjectMapper.xml | 7 +++ 4 files changed, 63 insertions(+) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java index 0157ec89288..6de21b9cf7d 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/project/ProjectDaoIT.java @@ -34,6 +34,8 @@ import org.assertj.core.api.Assertions; import org.assertj.core.groups.Tuple; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; @@ -289,6 +291,50 @@ class ProjectDaoIT { assertProject(projectsByUuids.get(1), "projectName_p2", "projectKee_o1_p2", "uuid_o1_p2", "desc_p2", "tag1,tag2", false, true); } + @Test + void update_aiCodeFixEnabledPerProject() { + ProjectDto dto1 = createProject("o1", "p1").setAiCodeFixEnabled(true); + ProjectDto dto2 = createProject("o1", "p2"); + + projectDao.insert(db.getSession(), dto1); + projectDao.insert(db.getSession(), dto2); + + List projectsByUuids = projectDao.selectByUuids(db.getSession(), new HashSet<>(Arrays.asList("uuid_o1_p1", "uuid_o1_p2"))); + assertThat(projectsByUuids).hasSize(2); + assertProjectAiCodeFixEnablement(projectsByUuids.get(0), "uuid_o1_p1", true); + assertProjectAiCodeFixEnablement(projectsByUuids.get(1), "uuid_o1_p2", false); + + projectDao.update(db.getSession(), projectsByUuids.get(0).setAiCodeFixEnabled(false)); + projectDao.update(db.getSession(), projectsByUuids.get(1).setAiCodeFixEnabled(true)); + + projectsByUuids = projectDao.selectByUuids(db.getSession(), new HashSet<>(Arrays.asList("uuid_o1_p1", "uuid_o1_p2"))); + assertThat(projectsByUuids).hasSize(2); + assertProjectAiCodeFixEnablement(projectsByUuids.get(0), "uuid_o1_p1", false); + assertProjectAiCodeFixEnablement(projectsByUuids.get(1), "uuid_o1_p2", true); + } + + @ParameterizedTest + @ValueSource(booleans = { true, false }) + void update_aiCodeFixEnabledForAllProjects(boolean aiCodeFixEnablement) { + ProjectDto dto1 = createProject("o1", "p1").setAiCodeFixEnabled(true); + ProjectDto dto2 = createProject("o1", "p2"); + + projectDao.insert(db.getSession(), dto1); + projectDao.insert(db.getSession(), dto2); + + List projectsByUuids = projectDao.selectByUuids(db.getSession(), new HashSet<>(Arrays.asList("uuid_o1_p1", "uuid_o1_p2"))); + assertThat(projectsByUuids).hasSize(2); + assertProjectAiCodeFixEnablement(projectsByUuids.get(0), "uuid_o1_p1", true); + assertProjectAiCodeFixEnablement(projectsByUuids.get(1), "uuid_o1_p2", false); + + projectDao.updateAiCodeFixEnablementForAllProjects(db.getSession(), aiCodeFixEnablement); + + projectsByUuids = projectDao.selectByUuids(db.getSession(), new HashSet<>(Arrays.asList("uuid_o1_p1", "uuid_o1_p2"))); + assertThat(projectsByUuids).hasSize(2); + assertProjectAiCodeFixEnablement(projectsByUuids.get(0), "uuid_o1_p1", aiCodeFixEnablement); + assertProjectAiCodeFixEnablement(projectsByUuids.get(1), "uuid_o1_p2", aiCodeFixEnablement); + } + @Test void select_by_uuids() { ProjectDto dto1 = createProject("o1", "p1"); @@ -447,6 +493,10 @@ class ProjectDaoIT { .containsExactly(name, kee, kee, uuid, desc, tags, isPrivate, isAiCodeAssurance); } + private void assertProjectAiCodeFixEnablement(ProjectDto dto, String uuid, boolean isAiCodeFixEnabled) { + assertThat(dto).extracting("uuid", "aiCodeFixEnabled").containsExactly(uuid, isAiCodeFixEnabled); + } + private ProjectDto createProject(String org, String name) { return new ProjectDto() .setName("projectName_" + name) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java index 74849bf4f04..876c8a0ff28 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java @@ -123,6 +123,10 @@ public class ProjectDao implements Dao { mapper(session).updateAiCodeAssurance(uuid, aiCodeAssurance, system2.now()); } + public void updateAiCodeFixEnablementForAllProjects(DbSession dbSession, boolean featureEnablement) { + mapper(dbSession).updateAiCodeFixEnablementForAllProjects(featureEnablement, system2.now()); + } + public void updateTags(DbSession session, ProjectDto project) { mapper(session).updateTags(project); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java index 4717d3e55b1..4b899aab902 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java @@ -60,6 +60,8 @@ public interface ProjectMapper { void updateAiCodeAssurance(@Param("uuid") String uuid, @Param("aiCodeAssurance") boolean aiCodeAssurance, @Param("updatedAt") long updatedAt); + void updateAiCodeFixEnablementForAllProjects(@Param("aiCodeFixEnabled") boolean aiCodeFixEnabled, @Param("updatedAt") long updatedAt); + List selectAllApplications(); List selectApplicationsByKeys(@Param("kees") Collection kees); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml index 9f332354d87..aa87a5f8900 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml @@ -171,6 +171,7 @@ update projects set name = #{name,jdbcType=VARCHAR}, description = #{description,jdbcType=VARCHAR}, + ai_code_fix_enabled = #{aiCodeFixEnabled, jdbcType=BOOLEAN}, updated_at = #{updatedAt,jdbcType=BIGINT} where uuid = #{uuid,jdbcType=VARCHAR} @@ -192,6 +193,12 @@ uuid = #{uuid,jdbcType=VARCHAR} + + update projects set + ai_code_fix_enabled = #{aiCodeFixEnabled,jdbcType=BOOLEAN}, + updated_at = #{updatedAt,jdbcType=BIGINT} + + update projects set ncloc = #{ncloc,jdbcType=BIGINT} -- 2.39.5