]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18856 Fix ce/Analysis/status endpoint
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Thu, 11 May 2023 09:08:56 +0000 (11:08 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 11 May 2023 20:03:13 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/ce/ws/AnalysisStatusActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java

index 5c8575ef09a3e6c029dd800fa176e6278e0d69de..dc2fbb6776f0347692d041944a8e159867a9e094 100644 (file)
@@ -30,9 +30,11 @@ import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeQueueDto;
 import org.sonar.db.ce.CeTaskMessageDto;
 import org.sonar.db.ce.CeTaskMessageType;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.BranchType;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.component.SnapshotDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -66,14 +68,14 @@ public class AnalysisStatusActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
+  public DbTester db = DbTester.create(System2.INSTANCE, true);
 
   DbClient dbClient = db.getDbClient();
   WsActionTester ws = new WsActionTester(new AnalysisStatusAction(userSession, dbClient, TestComponentFinder.from(db)));
 
   @Test
   public void no_errors_no_warnings() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
 
     Ce.AnalysisStatusWsResponse response = ws.newRequest()
@@ -85,10 +87,11 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void allows_unauthenticated_access() {
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    userSession.registerComponents(project);
+    ProjectData projectData = db.components().insertPublicProject();
+    ProjectDto project = projectData.getProjectDto();
+    userSession.registerProjects(project);
     SnapshotDto analysis = db.components().insertSnapshot(project);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, projectData.getMainBranchDto(), SUCCESS, analysis, REPORT);
     createTaskMessage(activity, WARNING_IN_MAIN);
     createTaskMessage(activity, "Dismissible warning", CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
 
@@ -101,11 +104,12 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void return_warnings_for_last_analysis_of_main() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ProjectDto project = projectData.getProjectDto();
     userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
 
     SnapshotDto analysis = db.components().insertSnapshot(project);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, projectData.getMainBranchDto(), SUCCESS, analysis, REPORT);
     CeTaskMessageDto taskMessage = createTaskMessage(activity, WARNING_IN_MAIN);
     CeTaskMessageDto taskMessageDismissible = createTaskMessage(activity, "Dismissible warning", CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE);
 
@@ -120,8 +124,8 @@ public class AnalysisStatusActionIT {
         tuple(taskMessageDismissible.getUuid(), taskMessageDismissible.getMessage(), true));
 
     SnapshotDto analysis2 = db.components().insertSnapshot(project);
-    insertActivity("task-uuid" + counter++, project, SUCCESS, analysis2, REPORT);
-    insertActivity("task-uuid" + counter++, project, SUCCESS, null, "PROJECT_EXPORT");
+    insertActivity("task-uuid" + counter++, projectData.getMainBranchDto(), SUCCESS, analysis2, REPORT);
+    insertActivity("task-uuid" + counter++, projectData.getMainBranchDto(), SUCCESS, null, "PROJECT_EXPORT");
 
     Ce.AnalysisStatusWsResponse response2 = ws.newRequest()
       .setParam(PARAM_COMPONENT, project.getKey())
@@ -132,10 +136,10 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void return_warnings_for_last_analysis_of_branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
 
-    ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITH_WARNING));
+    BranchDto branch = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITH_WARNING));
     SnapshotDto analysis = db.components().insertSnapshot(branch);
     CeActivityDto activity = insertActivity("task-uuid" + counter++, branch, SUCCESS, analysis, REPORT);
     CeTaskMessageDto taskMessage = createTaskMessage(activity, WARNING_IN_BRANCH);
@@ -163,10 +167,10 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void return_warnings_for_last_analysis_of_pull_request() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
 
-    ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> {
+    BranchDto pullRequest = db.components().insertProjectBranch(project, b -> {
       b.setBranchType(BranchType.PULL_REQUEST);
       b.setKey(PULL_REQUEST);
     });
@@ -197,23 +201,24 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void return_warnings_per_branch() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject();
+    ProjectDto project = projectData.getProjectDto();
     userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
 
     SnapshotDto analysis = db.components().insertSnapshot(project);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, projectData.getMainBranchDto(), SUCCESS, analysis, REPORT);
     CeTaskMessageDto warningInMainMessage = createTaskMessage(activity, WARNING_IN_MAIN);
 
-    ComponentDto branchWithWarning = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITH_WARNING));
+    BranchDto branchWithWarning = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITH_WARNING));
     SnapshotDto branchAnalysis = db.components().insertSnapshot(branchWithWarning);
     CeActivityDto branchActivity = insertActivity("task-uuid" + counter++, branchWithWarning, SUCCESS, branchAnalysis, REPORT);
     CeTaskMessageDto warningInBranchMessage = createTaskMessage(branchActivity, WARNING_IN_BRANCH);
 
-    ComponentDto branchWithoutWarning = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITHOUT_WARNING));
+    BranchDto branchWithoutWarning = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITHOUT_WARNING));
     SnapshotDto branchWithoutWarningAnalysis = db.components().insertSnapshot(branchWithoutWarning);
     insertActivity("task-uuid" + counter++, branchWithoutWarning, SUCCESS, branchWithoutWarningAnalysis, REPORT);
 
-    ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> {
+    BranchDto pullRequest = db.components().insertProjectBranch(project, b -> {
       b.setBranchType(BranchType.PULL_REQUEST);
       b.setKey(PULL_REQUEST);
     });
@@ -257,7 +262,7 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void response_contains_branch_or_pullRequest_for_branch_or_pullRequest_only() {
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     userSession.logIn().setSystemAdministrator().addProjectPermission(UserRole.USER, project);
 
     db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITHOUT_WARNING));
@@ -293,10 +298,11 @@ public class AnalysisStatusActionIT {
 
   @Test
   public void json_example() {
-    ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("com.github.kevinsawicki:http-request-parent")
-      .setName("HttpRequest")).getMainBranchComponent();
+    ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey("com.github.kevinsawicki:http-request-parent")
+      .setName("HttpRequest"));
+    ProjectDto project = projectData.getProjectDto();
     SnapshotDto analysis = db.components().insertSnapshot(project);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, projectData.getMainBranchDto(), SUCCESS, analysis, REPORT);
     CeTaskMessageDto ceTaskMessage = new CeTaskMessageDto()
       .setUuid("AU-Tpxb--iU5OvuD2FLy")
       .setTaskUuid(activity.getUuid())
@@ -357,11 +363,11 @@ public class AnalysisStatusActionIT {
     return ceTaskMessageDto;
   }
 
-  private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status, @Nullable SnapshotDto analysis, String taskType) {
+  private CeActivityDto insertActivity(String taskUuid, BranchDto branch, CeActivityDto.Status status, @Nullable SnapshotDto analysis, String taskType) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setTaskType(taskType);
-    queueDto.setComponentUuid(component.uuid());
-    queueDto.setMainComponentUuid(component.uuid());
+    queueDto.setComponentUuid(branch.getUuid());
+    queueDto.setMainComponentUuid(branch.getUuid());
     queueDto.setUuid(taskUuid);
     CeActivityDto activityDto = new CeActivityDto(queueDto);
     activityDto.setStatus(status);
@@ -369,7 +375,7 @@ public class AnalysisStatusActionIT {
     activityDto.setAnalysisUuid(analysis == null ? null : analysis.getUuid());
     activityDto.setExecutedAt((long) counter++);
     activityDto.setTaskType(taskType);
-    activityDto.setComponentUuid(component.uuid());
+    activityDto.setComponentUuid(branch.getUuid());
     db.getDbClient().ceActivityDao().insert(db.getSession(), activityDto);
     db.getSession().commit();
     return activityDto;
index ef9394061c1612a28b701ad1ff8baa3d1804bd89..8a5ebdbcd0faa92b06006320d8e9ef9773d4f24e 100644 (file)
@@ -145,7 +145,7 @@ public class ComponentFinder {
       return dbClient.branchDao().selectByPullRequestKey(dbSession, projectUuid, pullRequestKey)
         .orElseThrow(() -> new NotFoundException(String.format("Pull request '%s' in project '%s' not found", pullRequestKey, projectKey)));
     }
-    return dbClient.branchDao().selectByUuid(dbSession, projectUuid)
+    return dbClient.branchDao().selectMainBranchByProjectUuid(dbSession, projectUuid)
       .orElseThrow(() -> new NotFoundException(String.format("Main branch in project '%s' not found", projectKey)));
   }