]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11797 fix for exception on project overview page
authorMichal Duda <michal.duda@sonarsource.com>
Wed, 6 Mar 2019 11:16:58 +0000 (12:16 +0100)
committerSonarTech <sonartech@sonarsource.com>
Thu, 7 Mar 2019 19:21:00 +0000 (20:21 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/ce/ws/AnalysisStatusAction.java
server/sonar-server/src/test/java/org/sonar/server/ce/ws/AnalysisStatusActionTest.java

index e7f869148461fe458fc82e70bcd8c2531c76bb50..9646d588b70a343ed4e2f0326fe97b1baa92ab26 100644 (file)
@@ -80,12 +80,8 @@ public class CeActivityDao implements Dao {
     return mapper(dbSession).countLastByStatusAndMainComponentUuid(status, mainComponentUuid);
   }
 
-  public Optional<CeActivityDto> selectLastByComponentUuid(DbSession dbSession, String componentUuid) {
-    return Optional.ofNullable(mapper(dbSession).selectLastByComponentUuid(componentUuid));
-  }
-
-  public Optional<CeActivityDto> selectLastByMainComponentUuid(DbSession dbSession, String mainComponentUuid) {
-    return Optional.ofNullable(mapper(dbSession).selectLastByMainComponentUuid(mainComponentUuid));
+  public Optional<CeActivityDto> selectLastByComponentUuidAndTaskType(DbSession dbSession, String componentUuid, String taskType) {
+    return Optional.ofNullable(mapper(dbSession).selectLastByComponentUuidAndTaskType(componentUuid, taskType));
   }
 
   private static CeActivityMapper mapper(DbSession dbSession) {
index 84879e7a66249ab5a6a25f148d49bde25ee9fa45..3457d343799a2f3f13573092fa2b3dcdf04d8c60 100644 (file)
@@ -45,8 +45,5 @@ public interface CeActivityMapper {
   void deleteByUuids(@Param("uuids") List<String> uuids);
 
   @CheckForNull
-  CeActivityDto selectLastByComponentUuid(@Param("componentUuid") String componentUuid);
-
-  @CheckForNull
-  CeActivityDto selectLastByMainComponentUuid(@Param("mainComponentUuid") String mainComponentUuid);
+  CeActivityDto selectLastByComponentUuidAndTaskType(@Param("componentUuid") String componentUuid, @Param("taskType") String taskType);
 }
index d9a5932360693e7f347d98f37a74dfd3cdc79450..b924394460eccf1347196adf50d5916fde3fe4dd 100644 (file)
       </foreach>
   </delete>
 
-  <select id="selectLastByComponentUuid" parameterType="map" resultType="org.sonar.db.ce.CeActivityDto">
+  <select id="selectLastByComponentUuidAndTaskType" parameterType="map" resultType="org.sonar.db.ce.CeActivityDto">
     select
       <include refid="columns"/>
     from ce_activity ca
       ca.uuid = csc.task_uuid
     where
       ca.component_uuid = #{componentUuid,jdbcType=VARCHAR}
+      and ca.task_type = #{taskType,jdbcType=VARCHAR}
       and ca.is_last = ${_true}
   </select>
 
-  <select id="selectLastByMainComponentUuid" parameterType="map" resultType="org.sonar.db.ce.CeActivityDto">
-    select
-      <include refid="columns"/>
-    from ce_activity ca
-    left outer join ce_scanner_context csc on
-      ca.uuid = csc.task_uuid
-    where
-      ca.main_component_uuid = #{mainComponentUuid,jdbcType=VARCHAR}
-      and ca.main_is_last = ${_true}
-  </select>
-
 </mapper>
index 22fb2aaeaa1c7b24e1d45a59fc0d26532d225a6b..549f3bb20e2961e8506d6873a1dc2a5bac3ee6e6 100644 (file)
@@ -671,6 +671,29 @@ public class CeActivityDaoTest {
     assertThat(underTest.countLastByStatusAndMainComponentUuid(dbSession, SUCCESS, null)).isEqualTo(2);
   }
 
+  @Test
+  public void selectLastByComponentUuidAndTaskType_returns_task_of_given_type() {
+    insert("TASK_1", "VIEW_REFRESH", MAINCOMPONENT_1, MAINCOMPONENT_1, SUCCESS);
+    insert("TASK_2", CeTaskTypes.REPORT, MAINCOMPONENT_1, MAINCOMPONENT_1, SUCCESS);
+    insert("TASK_3", "PROJECT_EXPORT", MAINCOMPONENT_1, MAINCOMPONENT_1, SUCCESS);
+    insert("TASK_4", "PROJECT_IMPORT", MAINCOMPONENT_1, MAINCOMPONENT_1, SUCCESS);
+    db.commit();
+
+    Optional<CeActivityDto> result = underTest.selectLastByComponentUuidAndTaskType(db.getSession(), MAINCOMPONENT_1, "PROJECT_EXPORT");
+
+    assertThat(result).hasValueSatisfying(value -> assertThat(value.getUuid()).isEqualTo("TASK_3"));
+  }
+
+  @Test
+  public void selectLastByComponentUuidAndTaskType_returns_empty_if_task_of_given_type_does_not_exist() {
+    insert("TASK_1", CeTaskTypes.REPORT, MAINCOMPONENT_1, SUCCESS);
+    db.commit();
+
+    Optional<CeActivityDto> result = underTest.selectLastByComponentUuidAndTaskType(db.getSession(), MAINCOMPONENT_1, "PROJECT_EXPORT");
+
+    assertThat(result).isEmpty();
+  }
+
   private CeActivityDto insert(String uuid, String type, @Nullable String mainComponentUuid, CeActivityDto.Status status) {
     return insert(uuid, type, mainComponentUuid, mainComponentUuid, status);
   }
index cc2a0e52b11a735bed49b5f2f9ccf96e5b1f47f2..dcedd00a345ff38c85401f43513fa4aae42403f4 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeTaskMessageDto;
+import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
@@ -101,7 +102,8 @@ public class AnalysisStatusAction implements CeWsAction {
       checkRequest(isProject(component), "Component '%s' must be a project.", componentKey);
 
       AnalysisStatusWsResponse.Builder responseBuilder = AnalysisStatusWsResponse.newBuilder();
-      CeActivityDto lastActivity = dbClient.ceActivityDao().selectLastByComponentUuid(dbSession, component.uuid()).orElse(null);
+      CeActivityDto lastActivity = dbClient.ceActivityDao()
+        .selectLastByComponentUuidAndTaskType(dbSession, component.uuid(), CeTaskTypes.REPORT).orElse(null);
       responseBuilder.setComponent(formatComponent(dbSession, component, lastActivity, branchKey, pullRequestKey));
 
       writeProtobuf(responseBuilder.build(), request, response);
index d583ab6cea3baa56f3660824bfce8843156f3c38..2585a7e872378232c568ac7b41f8200507e15fac 100644 (file)
@@ -35,7 +35,6 @@ import org.sonar.db.DbTester;
 import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeQueueDto;
 import org.sonar.db.ce.CeTaskMessageDto;
-import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
@@ -50,6 +49,7 @@ import org.sonarqube.ws.Ce;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.ce.CeActivityDto.Status.SUCCESS;
+import static org.sonar.db.ce.CeTaskTypes.REPORT;
 import static org.sonar.server.ce.ws.CeWsParameters.PARAM_BRANCH;
 import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT;
 import static org.sonar.server.ce.ws.CeWsParameters.PARAM_PULL_REQUEST;
@@ -164,7 +164,7 @@ public class AnalysisStatusActionTest {
     userSession.addProjectPermission(UserRole.USER, project);
 
     SnapshotDto analysis = db.components().insertSnapshot(project);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
     createTaskMessage(activity, WARNING_IN_MAIN);
 
     Ce.AnalysisStatusWsResponse response = ws.newRequest()
@@ -174,7 +174,8 @@ public class AnalysisStatusActionTest {
     assertThat(response.getComponent().getWarningsList()).containsExactly(WARNING_IN_MAIN);
 
     SnapshotDto analysis2 = db.components().insertSnapshot(project);
-    insertActivity("task-uuid" + counter++, project, SUCCESS, analysis2);
+    insertActivity("task-uuid" + counter++, project, SUCCESS, analysis2, REPORT);
+    insertActivity("task-uuid" + counter++, project, SUCCESS, null, "PROJECT_EXPORT");
 
     Ce.AnalysisStatusWsResponse response2 = ws.newRequest()
       .setParam(PARAM_COMPONENT, project.getKey())
@@ -190,7 +191,7 @@ public class AnalysisStatusActionTest {
 
     ComponentDto 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);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, branch, SUCCESS, analysis, REPORT);
     createTaskMessage(activity, WARNING_IN_BRANCH);
 
     Ce.AnalysisStatusWsResponse response = ws.newRequest()
@@ -201,7 +202,8 @@ public class AnalysisStatusActionTest {
     assertThat(response.getComponent().getWarningsList()).containsExactly(WARNING_IN_BRANCH);
 
     SnapshotDto analysis2 = db.components().insertSnapshot(branch);
-    insertActivity("task-uuid" + counter++, branch, SUCCESS, analysis2);
+    insertActivity("task-uuid" + counter++, branch, SUCCESS, analysis2, REPORT);
+    insertActivity("task-uuid" + counter++, branch, SUCCESS, null, "PROJECT_EXPORT");
 
     Ce.AnalysisStatusWsResponse response2 = ws.newRequest()
       .setParam(PARAM_COMPONENT, project.getKey())
@@ -221,7 +223,7 @@ public class AnalysisStatusActionTest {
       b.setKey(PULL_REQUEST);
     });
     SnapshotDto analysis = db.components().insertSnapshot(pullRequest);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, analysis);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, analysis, REPORT);
     createTaskMessage(activity, WARNING_IN_PR);
 
     Ce.AnalysisStatusWsResponse response = ws.newRequest()
@@ -232,7 +234,8 @@ public class AnalysisStatusActionTest {
     assertThat(response.getComponent().getWarningsList()).containsExactly(WARNING_IN_PR);
 
     SnapshotDto analysis2 = db.components().insertSnapshot(pullRequest);
-    insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, analysis2);
+    insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, analysis2, REPORT);
+    insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, null, "PROJECT_EXPORT");
 
     Ce.AnalysisStatusWsResponse response2 = ws.newRequest()
       .setParam(PARAM_COMPONENT, project.getKey())
@@ -248,24 +251,24 @@ public class AnalysisStatusActionTest {
     userSession.addProjectPermission(UserRole.USER, project);
 
     SnapshotDto analysis = db.components().insertSnapshot(project);
-    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis);
+    CeActivityDto activity = insertActivity("task-uuid" + counter++, project, SUCCESS, analysis, REPORT);
     createTaskMessage(activity, WARNING_IN_MAIN);
 
     ComponentDto 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);
+    CeActivityDto branchActivity = insertActivity("task-uuid" + counter++, branchWithWarning, SUCCESS, branchAnalysis, REPORT);
     createTaskMessage(branchActivity, WARNING_IN_BRANCH);
 
     ComponentDto branchWithoutWarning = db.components().insertProjectBranch(project, b -> b.setKey(BRANCH_WITHOUT_WARNING));
     SnapshotDto branchWithoutWarningAnalysis = db.components().insertSnapshot(branchWithoutWarning);
-    insertActivity("task-uuid" + counter++, branchWithoutWarning, SUCCESS, branchWithoutWarningAnalysis);
+    insertActivity("task-uuid" + counter++, branchWithoutWarning, SUCCESS, branchWithoutWarningAnalysis, REPORT);
 
     ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> {
       b.setBranchType(BranchType.PULL_REQUEST);
       b.setKey(PULL_REQUEST);
     });
     SnapshotDto prAnalysis = db.components().insertSnapshot(pullRequest);
-    CeActivityDto prActivity = insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, prAnalysis);
+    CeActivityDto prActivity = insertActivity("task-uuid" + counter++, pullRequest, SUCCESS, prAnalysis, REPORT);
     createTaskMessage(prActivity, WARNING_IN_PR);
 
     Ce.AnalysisStatusWsResponse responseForMain = ws.newRequest()
@@ -341,9 +344,10 @@ public class AnalysisStatusActionTest {
     db.commit();
   }
 
-  private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status, @Nullable SnapshotDto analysis) {
+  private CeActivityDto insertActivity(String taskUuid, ComponentDto component, CeActivityDto.Status status,
+    @Nullable SnapshotDto analysis, String taskType) {
     CeQueueDto queueDto = new CeQueueDto();
-    queueDto.setTaskType(CeTaskTypes.REPORT);
+    queueDto.setTaskType(taskType);
     queueDto.setComponent(component);
     queueDto.setUuid(taskUuid);
     CeActivityDto activityDto = new CeActivityDto(queueDto);
@@ -351,6 +355,8 @@ public class AnalysisStatusActionTest {
     activityDto.setExecutionTimeMs(500L);
     activityDto.setAnalysisUuid(analysis == null ? null : analysis.getUuid());
     activityDto.setExecutedAt((long) counter++);
+    activityDto.setTaskType(taskType);
+    activityDto.setComponentUuid(component.uuid());
     db.getDbClient().ceActivityDao().insert(db.getSession(), activityDto);
     db.getSession().commit();
     return activityDto;