From 961be4c047ef16ad9c3c7a42d83176c50dd35692 Mon Sep 17 00:00:00 2001 From: Serhat Yenican <104850907+serhat-yenican-sonarsource@users.noreply.github.com> Date: Thu, 7 Nov 2024 09:38:00 +0100 Subject: [PATCH] CODEFIX-187 Add `isAiCodeFixEnabled` property to the Component Response (#12212) --- .../org/sonar/db/project/ProjectDaoIT.java | 19 ++++++++++++++++++- .../java/org/sonar/db/project/ProjectDto.java | 10 ++++++++++ .../org/sonar/db/project/ProjectMapper.xml | 3 +++ .../server/component/ws/ShowActionIT.java | 18 +++++++++++++++++- .../ws/ComponentDtoToWsComponent.java | 3 ++- .../src/main/protobuf/ws-components.proto | 1 + 6 files changed, 51 insertions(+), 3 deletions(-) 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 2ebe14fa96c..0157ec89288 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 @@ -35,7 +35,6 @@ import org.assertj.core.groups.Tuple; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.sonar.api.impl.utils.AlwaysIncreasingSystem2; -import org.sonar.db.component.ComponentQualifiers; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.Pagination; @@ -44,6 +43,7 @@ import org.sonar.db.audit.NoOpAuditPersister; import org.sonar.db.component.BranchDto; import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentQualifiers; import org.sonar.db.entity.EntityDto; import static java.util.Collections.emptySet; @@ -149,6 +149,23 @@ class ProjectDaoIT { .containsEntry("projectName_p2", false); } + @Test + void selectProjects_returnsAiCodeFixEnabled() { + var dbSession = db.getSession(); + + var dto1 = createProject("o1", "p1").setAiCodeFixEnabled(true); + var dto2 = createProject("o1", "p2"); + + projectDao.insert(dbSession, dto1); + projectDao.insert(dbSession, dto2); + + assertThat(projectDao.selectProjects(dbSession)) + .extracting(EntityDto::getName, ProjectDto::getAiCodeFixEnabled) + .containsExactlyInAnyOrder( + tuple("projectName_p1", true), + tuple("projectName_p2", false)); + } + @Test void select_all() { ProjectDto dto1 = createProject("o1", "p1"); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java index e7abfb11ece..06414302257 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDto.java @@ -33,6 +33,7 @@ public class ProjectDto extends EntityDto { private String tags; private CreationMethod creationMethod; private boolean aiCodeAssurance; + private boolean aiCodeFixEnabled = false; private long createdAt; private long updatedAt; @@ -133,4 +134,13 @@ public class ProjectDto extends EntityDto { this.aiCodeAssurance = aiCodeAssurance; return this; } + + public boolean getAiCodeFixEnabled() { + return aiCodeFixEnabled; + } + + public ProjectDto setAiCodeFixEnabled(boolean aiCodeFixEnabled) { + this.aiCodeFixEnabled = aiCodeFixEnabled; + return this; + } } 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 c54aa32323f..9f332354d87 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 @@ -12,6 +12,7 @@ p.private as isPrivate, p.creation_method as creationMethod, p.ai_code_assurance as aiCodeAssurance, + p.ai_code_fix_enabled as aiCodeFixEnabled, p.created_at as createdAt, p.updated_at as updatedAt @@ -138,6 +139,7 @@ tags, creation_method, ai_code_assurance, + ai_code_fix_enabled, created_at, updated_at ) @@ -151,6 +153,7 @@ #{tagsString, jdbcType=VARCHAR}, #{creationMethod, jdbcType=VARCHAR}, #{aiCodeAssurance, jdbcType=BOOLEAN}, + #{aiCodeFixEnabled, jdbcType=BOOLEAN}, #{createdAt,jdbcType=BIGINT}, #{updatedAt,jdbcType=BIGINT} ) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/ShowActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/ShowActionIT.java index 13291397296..83d62b8fdc1 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/ShowActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/component/ws/ShowActionIT.java @@ -24,13 +24,16 @@ import java.util.Optional; import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; -import org.sonar.db.component.ComponentQualifiers; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.db.DbTester; +import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentQualifiers; import org.sonar.db.component.ProjectData; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.ForbiddenException; @@ -463,6 +466,19 @@ public class ShowActionIT { .hasMessage(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch")); } + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void return_isAiCodeFixEnabled(boolean isAiCodeFixEnabled) { + var projectData = db.components().insertPrivateProject(ComponentDbTester.defaults(), + p -> p.setAiCodeFixEnabled(isAiCodeFixEnabled)); + userSession.addProjectPermission(USER, projectData.getProjectDto()) + .registerBranches(projectData.getMainBranchDto()); + + var response = newRequest(projectData.projectKey()); + + assertThat(response.getComponent().getIsAiCodeFixEnabled()).isEqualTo(isAiCodeFixEnabled); + } + private ShowWsResponse newRequest(@Nullable String key) { TestRequest request = ws.newRequest(); if (key != null) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java index 3daa11d4f54..e2ef0c75f0a 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java @@ -48,7 +48,8 @@ class ComponentDtoToWsComponent { Components.Component.Builder wsComponent = Components.Component.newBuilder() .setKey(project.getKey()) .setName(project.getName()) - .setQualifier(project.getQualifier()); + .setQualifier(project.getQualifier()) + .setIsAiCodeFixEnabled(project.getAiCodeFixEnabled()); ofNullable(emptyToNull(project.getDescription())).ifPresent(wsComponent::setDescription); ofNullable(lastAnalysis).ifPresent( diff --git a/sonar-ws/src/main/protobuf/ws-components.proto b/sonar-ws/src/main/protobuf/ws-components.proto index b0ec074464a..d3704c795f1 100644 --- a/sonar-ws/src/main/protobuf/ws-components.proto +++ b/sonar-ws/src/main/protobuf/ws-components.proto @@ -115,6 +115,7 @@ message Component { optional string pullRequest = 20; optional bool needIssueSync = 21; optional bool isAiCodeAssured = 22; + optional bool isAiCodeFixEnabled = 23; message Tags { repeated string tags = 1; -- 2.39.5