From: Julien Lancelot Date: Fri, 18 Aug 2017 09:00:08 +0000 (+0200) Subject: SONAR-9616 Fix ce ws on branches X-Git-Tag: 6.6-RC1~380^2~81 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=41d132ec6b3841989816e36779cc5e946ba1a2b6;p=sonarqube.git SONAR-9616 Fix ce ws on branches --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java index cf395d10e34..aaf1ea5fa71 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java @@ -39,8 +39,8 @@ import org.sonarqube.ws.client.ce.ActivityStatusWsRequest; import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_KEY; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.ce.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; public class ActivityStatusAction implements CeWsAction { private final UserSession userSession; @@ -67,7 +67,7 @@ public class ActivityStatusAction implements CeWsAction { action.createParam(PARAM_COMPONENT_ID) .setDescription("Id of the component (project) to filter on") .setExampleValue(Uuids.UUID_EXAMPLE_03); - action.createParam(PARAM_COMPONENT_KEY) + action.createParam(DEPRECATED_PARAM_COMPONENT_KEY) .setDescription("Key of the component (project) to filter on") .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001); @@ -120,7 +120,7 @@ public class ActivityStatusAction implements CeWsAction { private static ActivityStatusWsRequest toWsRequest(Request request) { return ActivityStatusWsRequest.newBuilder() .setComponentId(request.param(PARAM_COMPONENT_ID)) - .setComponentKey(request.param(PARAM_COMPONENT_KEY)) + .setComponentKey(request.param(DEPRECATED_PARAM_COMPONENT_KEY)) .build(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java index a842073aaba..71301984c9b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java @@ -36,15 +36,13 @@ import org.sonar.server.component.ComponentFinder; import org.sonar.server.user.UserSession; import org.sonar.server.ws.KeyExamples; -import static com.google.common.base.Preconditions.checkArgument; import static org.sonar.db.Pagination.forPage; -import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_KEY; -import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; +import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_COMPONENT; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.WsCe.ProjectResponse; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_BRANCH; +import static org.sonarqube.ws.client.ce.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; public class ComponentAction implements CeWsAction { @@ -66,7 +64,7 @@ public class ComponentAction implements CeWsAction { .setDescription("Get the pending tasks, in-progress tasks and the last executed task of a given component (usually a project).
" + "Requires the following permission: 'Browse' on the specified component.
" + "Either '%s' or '%s' must be provided, not both.", - PARAM_COMPONENT_ID, PARAM_COMPONENT_KEY) + PARAM_COMPONENT_ID, DEPRECATED_PARAM_COMPONENT_KEY) .setSince("5.2") .setResponseExample(getClass().getResource("component-example.json")) .setChangelog( @@ -77,16 +75,13 @@ public class ComponentAction implements CeWsAction { action.createParam(PARAM_COMPONENT_ID) .setRequired(false) - .setExampleValue(Uuids.UUID_EXAMPLE_01); + .setExampleValue(Uuids.UUID_EXAMPLE_01) + .setDeprecatedSince("6.6"); - action.createParam(PARAM_COMPONENT_KEY) + action.createParam(PARAM_COMPONENT) .setRequired(false) - .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001); - - action.createParam(PARAM_BRANCH) - .setDescription("Branch key") - .setInternal(true) - .setExampleValue(KEY_BRANCH_EXAMPLE_001); + .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001) + .setDeprecatedKey("componentKey", "6.6"); } @Override @@ -110,13 +105,8 @@ public class ComponentAction implements CeWsAction { } private ComponentDto loadComponent(DbSession dbSession, Request wsRequest) { - String componentKey = wsRequest.param(PARAM_COMPONENT_KEY); + String componentKey = wsRequest.param(PARAM_COMPONENT); String componentId = wsRequest.param(PARAM_COMPONENT_ID); - String branch = wsRequest.param(PARAM_BRANCH); - checkArgument(componentId == null || branch == null, "'%s' and '%s' parameters cannot be used at the same time", PARAM_COMPONENT_ID, - PARAM_BRANCH); - return branch == null - ? componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, COMPONENT_ID_AND_KEY) - : componentFinder.getByKeyAndBranch(dbSession, componentKey, branch); + return componentFinder.getByUuidOrKey(dbSession, componentId, componentKey, COMPONENT_ID_AND_COMPONENT); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java index 6e218184800..606c361218e 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/TaskFormatter.java @@ -19,10 +19,8 @@ */ package org.sonar.server.ce.ws; -import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; import java.util.Collection; -import java.util.Collections; import java.util.Date; import java.util.List; import java.util.Map; @@ -40,9 +38,7 @@ import org.sonar.db.DbSession; import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeTaskCharacteristicDto; -import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.SnapshotDto; import org.sonar.db.organization.OrganizationDto; import org.sonarqube.ws.Common; import org.sonarqube.ws.WsCe; @@ -83,7 +79,7 @@ public class TaskFormatter { setNullable(organizationKey, builder::setOrganization); if (dto.getComponentUuid() != null) { builder.setComponentId(dto.getComponentUuid()); - buildComponent(builder, dto.getComponentUuid(), componentDtoCache); + setComponent(builder, dto.getComponentUuid(), componentDtoCache); } builder.setId(dto.getUuid()); builder.setStatus(WsCe.TaskStatus.valueOf(dto.getStatus().name())); @@ -93,13 +89,8 @@ public class TaskFormatter { builder.setSubmittedAt(formatDateTime(new Date(dto.getCreatedAt()))); setNullable(dto.getStartedAt(), builder::setStartedAt, DateUtils::formatDateTime); setNullable(computeExecutionTimeMs(dto), builder::setExecutionTimeMs); - builder.setIncremental(componentDtoCache.hasIncrementalCharacteristic(dto.getUuid())); - componentDtoCache.getBranchName(dto.getUuid()).ifPresent( - b -> { - builder.setBranch(b); - builder.setBranchType(componentDtoCache.getBranchType(dto.getUuid()) - .orElseThrow(() -> new IllegalStateException(format("Could not find branch type of '%s'", dto.getUuid())))); - }); + setIncremental(builder, dto.getUuid(), componentDtoCache); + setBranch(builder, dto.getUuid(), componentDtoCache); return builder.build(); } @@ -123,13 +114,13 @@ public class TaskFormatter { builder.setStatus(WsCe.TaskStatus.valueOf(dto.getStatus().name())); builder.setType(dto.getTaskType()); builder.setLogs(false); - setNullable(dto.getComponentUuid(), uuid -> buildComponent(builder, uuid, componentDtoCache).setComponentId(uuid)); + setNullable(dto.getComponentUuid(), uuid -> setComponent(builder, uuid, componentDtoCache).setComponentId(uuid)); String analysisUuid = dto.getAnalysisUuid(); if (analysisUuid != null) { builder.setAnalysisId(analysisUuid); } - SnapshotDto analysis = analysisUuid == null ? null : componentDtoCache.getAnalysis(analysisUuid); - builder.setIncremental(analysis != null && analysis.getIncremental()); + setIncremental(builder, dto.getUuid(), componentDtoCache); + setBranch(builder, dto.getUuid(), componentDtoCache); setNullable(analysisUuid, builder::setAnalysisId); setNullable(dto.getSubmitterLogin(), builder::setSubmitterLogin); builder.setSubmittedAt(formatDateTime(new Date(dto.getSubmittedAt()))); @@ -143,7 +134,7 @@ public class TaskFormatter { return builder.build(); } - private static WsCe.Task.Builder buildComponent(WsCe.Task.Builder builder, @Nullable String componentUuid, DtoCache componentDtoCache) { + private static WsCe.Task.Builder setComponent(WsCe.Task.Builder builder, @Nullable String componentUuid, DtoCache componentDtoCache) { ComponentDto componentDto = componentDtoCache.getComponent(componentUuid); if (componentDto == null) { return builder; @@ -151,29 +142,34 @@ public class TaskFormatter { builder.setComponentKey(componentDto.getKey()); builder.setComponentName(componentDto.name()); builder.setComponentQualifier(componentDto.qualifier()); - String branch = componentDto.getBranch(); - if (branch == null) { - return builder; - } - builder.setBranch(branch); - builder.setBranchType(Common.BranchType.valueOf(componentDtoCache.getBranch(componentUuid).getBranchType().name())); + return builder; + } + + private static WsCe.Task.Builder setIncremental(WsCe.Task.Builder builder, String taskUuid, DtoCache componentDtoCache) { + builder.setIncremental(componentDtoCache.hasIncrementalCharacteristic(taskUuid)); + return builder; + } + + private static WsCe.Task.Builder setBranch(WsCe.Task.Builder builder, String taskUuid, DtoCache componentDtoCache) { + componentDtoCache.getBranchName(taskUuid).ifPresent( + b -> { + builder.setBranch(b); + builder.setBranchType(componentDtoCache.getBranchType(taskUuid) + .orElseThrow(() -> new IllegalStateException(format("Could not find branch type of task '%s'", taskUuid)))); + }); return builder; } private static class DtoCache { private final Map componentsByUuid; private final Map organizationsByUuid; - private final Map analysisByUuid; - private final Map branchesByUuid; private final Multimap characteristicsByTaskUuid; - private DtoCache(Map componentsByUuid, Map organizationsByUuid, Map analysisByUuid, - Multimap characteristicsByTaskUuid, Map branchesByUuid) { + private DtoCache(Map componentsByUuid, Map organizationsByUuid, + Multimap characteristicsByTaskUuid) { this.componentsByUuid = componentsByUuid; this.organizationsByUuid = organizationsByUuid; - this.analysisByUuid = analysisByUuid; this.characteristicsByTaskUuid = characteristicsByTaskUuid; - this.branchesByUuid = branchesByUuid; } static DtoCache forQueueDtos(DbClient dbClient, DbSession dbSession, Collection ceQueueDtos) { @@ -183,10 +179,7 @@ public class TaskFormatter { Multimap characteristicsByTaskUuid = dbClient.ceTaskCharacteristicsDao() .selectByTaskUuids(dbSession, ceQueueDtos.stream().map(CeQueueDto::getUuid).collect(Collectors.toList())) .stream().collect(MoreCollectors.index(CeTaskCharacteristicDto::getTaskUuid)); - Map branchesByUuid = dbClient.branchDao().selectByUuids(dbSession, componentsByUuid.keySet()).stream() - .collect(MoreCollectors.uniqueIndex(BranchDto::getUuid)); - return new DtoCache(componentsByUuid, buildOrganizationsByUuid(dbClient, dbSession, componentsByUuid), Collections.emptyMap(), characteristicsByTaskUuid, - branchesByUuid); + return new DtoCache(componentsByUuid, buildOrganizationsByUuid(dbClient, dbSession, componentsByUuid), characteristicsByTaskUuid); } private static Set uuidOfCeQueueDtos(Collection ceQueueDtos) { @@ -203,12 +196,10 @@ public class TaskFormatter { uuidOfCeActivityDtos(ceActivityDtos)) .stream() .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid)); - Set analysisUuids = ceActivityDtos.stream().map(CeActivityDto::getAnalysisUuid).filter(Objects::nonNull).collect(MoreCollectors.toSet()); - Map analysisByUuid = dbClient.snapshotDao().selectByUuids(dbSession, analysisUuids).stream().collect(MoreCollectors.uniqueIndex(SnapshotDto::getUuid)); - Map branchesByUuid = dbClient.branchDao().selectByUuids(dbSession, componentsByUuid.keySet()).stream() - .collect(MoreCollectors.uniqueIndex(BranchDto::getUuid)); - return new DtoCache(componentsByUuid, buildOrganizationsByUuid(dbClient, dbSession, componentsByUuid), analysisByUuid, - ImmutableMultimap.builder().build(), branchesByUuid); + Multimap characteristicsByTaskUuid = dbClient.ceTaskCharacteristicsDao() + .selectByTaskUuids(dbSession, ceActivityDtos.stream().map(CeActivityDto::getUuid).collect(Collectors.toList())) + .stream().collect(MoreCollectors.index(CeTaskCharacteristicDto::getTaskUuid)); + return new DtoCache(componentsByUuid, buildOrganizationsByUuid(dbClient, dbSession, componentsByUuid), characteristicsByTaskUuid); } private static Set uuidOfCeActivityDtos(Collection ceActivityDtos) { @@ -252,16 +243,6 @@ public class TaskFormatter { return organizationDto.getKey(); } - @CheckForNull - SnapshotDto getAnalysis(String analysisUuid) { - return analysisByUuid.get(analysisUuid); - } - - @CheckForNull - BranchDto getBranch(String componentUuid) { - return branchesByUuid.get(componentUuid); - } - boolean hasIncrementalCharacteristic(String taskUuid) { return characteristicsByTaskUuid.get(taskUuid).stream() .filter(c -> c.getKey().equals(CeTaskCharacteristicDto.INCREMENTAL_KEY)) diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java index 57e4354b7e4..85297de65b2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java @@ -347,7 +347,8 @@ public class ActivityActionTest { public void incremental_analysis_on_single_project() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true)); - insertActivity("T1", project, SUCCESS, incrementalAnalysis); + CeActivityDto activity = insertActivity("T1", project, SUCCESS, incrementalAnalysis); + insertCharacteristic(activity, INCREMENTAL_KEY, "true"); userSession.logIn().addProjectPermission(UserRole.ADMIN, project); ActivityResponse activityResponse = call(ws.newRequest() @@ -363,7 +364,8 @@ public class ActivityActionTest { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true)); SnapshotDto standardAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(false)); - insertActivity("T1", project, SUCCESS, incrementalAnalysis); + CeActivityDto incrementalTask = insertActivity("T1", project, SUCCESS, incrementalAnalysis); + insertCharacteristic(incrementalTask, INCREMENTAL_KEY, "true"); insertActivity("T2", project, SUCCESS, standardAnalysis); logInAsSystemAdministrator(); @@ -381,7 +383,8 @@ public class ActivityActionTest { public void incremental_analysis_on_search_uuid() { ComponentDto project = db.components().insertPrivateProject(); SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true)); - insertActivity("T1", project, SUCCESS, incrementalAnalysis); + CeActivityDto activity = insertActivity("T1", project, SUCCESS, incrementalAnalysis); + insertCharacteristic(activity, INCREMENTAL_KEY, "true"); logInAsSystemAdministrator(); ActivityResponse activityResponse = call(ws.newRequest() @@ -419,7 +422,9 @@ public class ActivityActionTest { userSession.addProjectPermission(UserRole.USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); SnapshotDto analysis = db.components().insertSnapshot(longLivingBranch); - insertActivity("T1", longLivingBranch, SUCCESS, analysis); + CeActivityDto activity = insertActivity("T1", project, SUCCESS, analysis); + insertCharacteristic(activity, BRANCH_KEY, longLivingBranch.getBranch()); + insertCharacteristic(activity, BRANCH_TYPE_KEY, LONG.name()); ActivityResponse response = ws.newRequest().executeProtobuf(ActivityResponse.class); @@ -566,9 +571,17 @@ public class ActivityActionTest { } private CeTaskCharacteristicDto insertCharacteristic(CeQueueDto queueDto, String key, String value) { + return insertCharacteristic(queueDto.getUuid(), key, value); + } + + private CeTaskCharacteristicDto insertCharacteristic(CeActivityDto activityDto, String key, String value) { + return insertCharacteristic(activityDto.getUuid(), key, value); + } + + private CeTaskCharacteristicDto insertCharacteristic(String taskUuid, String key, String value) { CeTaskCharacteristicDto dto = new CeTaskCharacteristicDto() .setUuid(Uuids.createFast()) - .setTaskUuid(queueDto.getUuid()) + .setTaskUuid(taskUuid) .setKey(key) .setValue(value); db.getDbClient().ceTaskCharacteristicsDao().insert(db.getSession(), Collections.singletonList(dto)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java index 01b223801ab..02dc23d6a80 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityStatusActionTest.java @@ -46,8 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.db.ce.CeQueueTesting.newCeQueueDto; import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto; import static org.sonar.test.JsonAssert.assertJson; +import static org.sonarqube.ws.client.ce.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; public class ActivityStatusActionTest { @@ -192,7 +192,7 @@ public class ActivityStatusActionTest { request.setParam(PARAM_COMPONENT_ID, componentUuid); } if (componentKey != null) { - request.setParam(PARAM_COMPONENT_KEY, componentKey); + request.setParam(DEPRECATED_PARAM_COMPONENT_KEY, componentKey); } return request.executeProtobuf(WsCe.ActivityStatusWsResponse.class); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java index 9672520e1eb..aabe69a6f3b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java @@ -53,10 +53,9 @@ import static org.sonar.db.ce.CeTaskCharacteristicDto.BRANCH_KEY; import static org.sonar.db.ce.CeTaskCharacteristicDto.BRANCH_TYPE_KEY; import static org.sonar.db.ce.CeTaskCharacteristicDto.INCREMENTAL_KEY; import static org.sonar.db.component.BranchType.LONG; -import static org.sonar.db.component.ComponentTesting.newFileDto; +import static org.sonar.db.component.BranchType.SHORT; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; -import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_BRANCH; public class ComponentActionTest { @@ -77,7 +76,7 @@ public class ComponentActionTest { userSession.addProjectPermission(UserRole.USER, project); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentId", project.uuid()) + .setParam(PARAM_COMPONENT, project.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getQueueCount()).isEqualTo(0); @@ -98,7 +97,7 @@ public class ComponentActionTest { insertQueue("T5", project1, PENDING); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentId", project1.uuid()) + .setParam(PARAM_COMPONENT, project1.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getQueueCount()).isEqualTo(2); assertThat(response.getQueue(0).getId()).isEqualTo("T4"); @@ -122,7 +121,22 @@ public class ComponentActionTest { insertActivity("T1", project, CeActivityDto.Status.SUCCESS, analysis); WsCe.ProjectResponse response = ws.newRequest() - .setParam(PARAM_COMPONENT_KEY, project.getDbKey()) + .setParam(PARAM_COMPONENT, project.getDbKey()) + .executeProtobuf(WsCe.ProjectResponse.class); + assertThat(response.hasCurrent()).isTrue(); + assertThat(response.getCurrent().getId()).isEqualTo("T1"); + assertThat(response.getCurrent().getAnalysisId()).isEqualTo(analysis.getUuid()); + } + + @Test + public void search_tasks_by_component_id() { + ComponentDto project = db.components().insertPrivateProject(); + logInWithBrowsePermission(project); + SnapshotDto analysis = db.components().insertSnapshot(project); + insertActivity("T1", project, CeActivityDto.Status.SUCCESS, analysis); + + WsCe.ProjectResponse response = ws.newRequest() + .setParam(PARAM_COMPONENT_ID, project.uuid()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.hasCurrent()).isTrue(); assertThat(response.getCurrent().getId()).isEqualTo("T1"); @@ -140,7 +154,7 @@ public class ComponentActionTest { insertActivity("T5", project, CeActivityDto.Status.CANCELED); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentId", project.uuid()) + .setParam(PARAM_COMPONENT, project.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getQueueCount()).isEqualTo(0); // T3 is the latest task executed on PROJECT_1 ignoring Canceled ones @@ -148,31 +162,16 @@ public class ComponentActionTest { assertThat(response.getCurrent().getId()).isEqualTo("T3"); } - @Test - public void incremental_analysis_by_component_id() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true)); - insertActivity("T1", project, SUCCESS, incrementalAnalysis); - - WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentId", project.uuid()) - .executeProtobuf(WsCe.ProjectResponse.class); - - assertThat(response.getCurrent()) - .extracting(WsCe.Task::getId, WsCe.Task::getIncremental) - .containsExactlyInAnyOrder("T1", true); - } - @Test public void incremental_analysis_by_component_key() { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn().addProjectPermission(UserRole.USER, project); SnapshotDto incrementalAnalysis = db.components().insertSnapshot(project, s -> s.setIncremental(true)); - insertActivity("T1", project, SUCCESS, incrementalAnalysis); + CeActivityDto activity = insertActivity("T1", project, SUCCESS, incrementalAnalysis); + insertCharacteristic(activity, INCREMENTAL_KEY, "true"); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentKey", project.getKey()) + .setParam(PARAM_COMPONENT, project.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getCurrent()) @@ -191,7 +190,7 @@ public class ComponentActionTest { insertCharacteristic(queue2, INCREMENTAL_KEY, "true"); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentId", project.uuid()) + .setParam(PARAM_COMPONENT, project.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getQueueList()) @@ -207,17 +206,18 @@ public class ComponentActionTest { userSession.addProjectPermission(UserRole.USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); SnapshotDto analysis = db.components().insertSnapshot(longLivingBranch); - insertActivity("T1", longLivingBranch, SUCCESS, analysis); + CeActivityDto activity = insertActivity("T1", project, SUCCESS, analysis); + insertCharacteristic(activity, BRANCH_KEY, longLivingBranch.getBranch()); + insertCharacteristic(activity, BRANCH_TYPE_KEY, LONG.name()); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentKey", longLivingBranch.getKey()) - .setParam("branch", longLivingBranch.getBranch()) + .setParam(PARAM_COMPONENT, project.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getCurrent()) .extracting(WsCe.Task::getId, WsCe.Task::getBranch, WsCe.Task::getBranchType, WsCe.Task::getStatus, WsCe.Task::getComponentKey) .containsOnly( - "T1", longLivingBranch.getBranch(), Common.BranchType.LONG, WsCe.TaskStatus.SUCCESS, longLivingBranch.getKey()); + "T1", longLivingBranch.getBranch(), Common.BranchType.LONG, WsCe.TaskStatus.SUCCESS, project.getKey()); } @Test @@ -225,30 +225,73 @@ public class ComponentActionTest { ComponentDto project = db.components().insertMainBranch(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); - CeQueueDto queue1 = insertQueue("T1", longLivingBranch, IN_PROGRESS); + CeQueueDto queue1 = insertQueue("T1", project, IN_PROGRESS); insertCharacteristic(queue1, BRANCH_KEY, longLivingBranch.getBranch()); insertCharacteristic(queue1, BRANCH_TYPE_KEY, LONG.name()); - CeQueueDto queue2 = insertQueue("T2", longLivingBranch, PENDING); + CeQueueDto queue2 = insertQueue("T2", project, PENDING); insertCharacteristic(queue2, BRANCH_KEY, longLivingBranch.getBranch()); insertCharacteristic(queue2, BRANCH_TYPE_KEY, LONG.name()); WsCe.ProjectResponse response = ws.newRequest() - .setParam("componentKey", longLivingBranch.getKey()) - .setParam("branch", longLivingBranch.getBranch()) + .setParam(PARAM_COMPONENT, longLivingBranch.getKey()) .executeProtobuf(WsCe.ProjectResponse.class); assertThat(response.getQueueList()) .extracting(WsCe.Task::getId, WsCe.Task::getBranch, WsCe.Task::getBranchType, WsCe.Task::getStatus, WsCe.Task::getComponentKey) .containsOnly( - tuple("T1", longLivingBranch.getBranch(), Common.BranchType.LONG, WsCe.TaskStatus.IN_PROGRESS, longLivingBranch.getKey()), - tuple("T2", longLivingBranch.getBranch(), Common.BranchType.LONG, WsCe.TaskStatus.PENDING, longLivingBranch.getKey())); + tuple("T1", longLivingBranch.getBranch(), Common.BranchType.LONG, WsCe.TaskStatus.IN_PROGRESS, project.getKey()), + tuple("T2", longLivingBranch.getBranch(), Common.BranchType.LONG, WsCe.TaskStatus.PENDING, project.getKey())); + } + + @Test + public void return_many_tasks_from_same_project() { + ComponentDto project = db.components().insertMainBranch(); + userSession.addProjectPermission(UserRole.USER, project); + insertQueue("Main", project, IN_PROGRESS); + ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG).setKey("long-branch")); + CeQueueDto longLivingBranchQueue = insertQueue("Long", project, IN_PROGRESS); + insertCharacteristic(longLivingBranchQueue, BRANCH_KEY, longLivingBranch.getBranch()); + insertCharacteristic(longLivingBranchQueue, BRANCH_TYPE_KEY, LONG.name()); + ComponentDto shortLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(SHORT).setKey("short-branch")); + CeQueueDto shortLivingBranchQueue = insertQueue("Short", project, PENDING); + insertCharacteristic(shortLivingBranchQueue, BRANCH_KEY, shortLivingBranch.getBranch()); + insertCharacteristic(shortLivingBranchQueue, BRANCH_TYPE_KEY, SHORT.name()); + CeQueueDto incrementalQueue = insertQueue("Incremental", project, PENDING); + insertCharacteristic(incrementalQueue, INCREMENTAL_KEY, "true"); + + WsCe.ProjectResponse response = ws.newRequest() + .setParam(PARAM_COMPONENT, longLivingBranch.getKey()) + .executeProtobuf(WsCe.ProjectResponse.class); + + assertThat(response.getQueueList()) + .extracting(WsCe.Task::getId, WsCe.Task::getComponentKey, WsCe.Task::getBranch, WsCe.Task::getBranchType, WsCe.Task::getIncremental) + .containsOnly( + tuple("Main", project.getKey(), "", Common.BranchType.UNKNOWN_BRANCH_TYPE, false), + tuple("Long", longLivingBranch.getKey(), longLivingBranch.getBranch(), Common.BranchType.LONG, false), + tuple("Short", shortLivingBranch.getKey(), shortLivingBranch.getBranch(), Common.BranchType.SHORT, false), + tuple("Incremental", project.getKey(), "", Common.BranchType.UNKNOWN_BRANCH_TYPE, true)); + } + + @Test + public void deprecated_component_key() { + ComponentDto project = db.components().insertPrivateProject(); + logInWithBrowsePermission(project); + SnapshotDto analysis = db.components().insertSnapshot(project); + insertActivity("T1", project, CeActivityDto.Status.SUCCESS, analysis); + + WsCe.ProjectResponse response = ws.newRequest() + .setParam("componentKey", project.getKey()) + .executeProtobuf(WsCe.ProjectResponse.class); + assertThat(response.hasCurrent()).isTrue(); + assertThat(response.getCurrent().getId()).isEqualTo("T1"); + assertThat(response.getCurrent().getAnalysisId()).isEqualTo(analysis.getUuid()); } @Test public void fail_with_404_when_component_does_not_exist() throws Exception { expectedException.expect(NotFoundException.class); ws.newRequest() - .setParam("componentId", "UNKNOWN") + .setParam(PARAM_COMPONENT, "UNKNOWN") .setMediaType(MediaTypes.PROTOBUF) .execute(); } @@ -262,7 +305,7 @@ public class ComponentActionTest { expectedException.expectMessage("Insufficient privileges"); ws.newRequest() - .setParam(PARAM_COMPONENT_ID, project.uuid()) + .setParam(PARAM_COMPONENT, project.getKey()) .execute(); } @@ -274,38 +317,6 @@ public class ComponentActionTest { ws.newRequest().execute(); } - @Test - public void fail_if_branch_does_not_exist() { - ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project)); - userSession.addProjectPermission(UserRole.USER, project); - db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); - - expectedException.expect(NotFoundException.class); - expectedException.expectMessage(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch")); - - ws.newRequest() - .setParam(PARAM_COMPONENT_KEY, file.getKey()) - .setParam(PARAM_BRANCH, "another_branch") - .execute(); - } - - @Test - public void fail_when_componentId_and_branch_params_are_used_together() { - ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(newFileDto(project)); - userSession.addProjectPermission(UserRole.USER, project); - db.components().insertProjectBranch(project, b -> b.setKey("my_branch")); - - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("'componentId' and 'branch' parameters cannot be used at the same time"); - - ws.newRequest() - .setParam(PARAM_COMPONENT_ID, file.uuid()) - .setParam(PARAM_BRANCH, "my_branch") - .execute(); - } - private void logInWithBrowsePermission(ComponentDto project) { userSession.logIn().addProjectPermission(UserRole.USER, project); } @@ -340,9 +351,17 @@ public class ComponentActionTest { } private CeTaskCharacteristicDto insertCharacteristic(CeQueueDto queueDto, String key, String value) { + return insertCharacteristic(queueDto.getUuid(), key, value); + } + + private CeTaskCharacteristicDto insertCharacteristic(CeActivityDto activityDto, String key, String value) { + return insertCharacteristic(activityDto.getUuid(), key, value); + } + + private CeTaskCharacteristicDto insertCharacteristic(String taskUuid, String key, String value) { CeTaskCharacteristicDto dto = new CeTaskCharacteristicDto() .setUuid(Uuids.createFast()) - .setTaskUuid(queueDto.getUuid()) + .setTaskUuid(taskUuid) .setKey(key) .setValue(value); db.getDbClient().ceTaskCharacteristicsDao().insert(db.getSession(), Collections.singletonList(dto)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java index efee0ee2c4d..b2f66b0eff1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/TaskActionTest.java @@ -36,7 +36,6 @@ import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeTaskCharacteristicDto; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.SnapshotDto; import org.sonar.db.organization.OrganizationDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; @@ -146,16 +145,9 @@ public class TaskActionTest { logInAsRoot(); ComponentDto project = db.components().insertPrivateProject(); - SnapshotDto analysis = db.components().insertSnapshot(project, s -> s.setIncremental(true)); - CeQueueDto queueDto = new CeQueueDto() - .setTaskType(CeTaskTypes.REPORT) - .setUuid(SOME_TASK_UUID) - .setComponentUuid(project.uuid()); - CeActivityDto activityDto = new CeActivityDto(queueDto) - .setStatus(CeActivityDto.Status.FAILED) - .setExecutionTimeMs(500L) - .setAnalysisUuid(analysis.getUuid()); - persist(activityDto); + db.components().insertSnapshot(project, s -> s.setIncremental(true)); + CeActivityDto activity = createAndPersistArchivedTask(project); + insertCharacteristic(activity, "incremental", "true"); WsCe.TaskResponse taskResponse = ws.newRequest() .setParam("id", SOME_TASK_UUID) @@ -170,16 +162,10 @@ public class TaskActionTest { ComponentDto project = db.components().insertMainBranch(); userSession.addProjectPermission(UserRole.USER, project); ComponentDto longLivingBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(LONG)); - SnapshotDto analysis = db.components().insertSnapshot(longLivingBranch); - CeQueueDto queueDto = new CeQueueDto() - .setTaskType(CeTaskTypes.REPORT) - .setUuid(SOME_TASK_UUID) - .setComponentUuid(longLivingBranch.uuid()); - CeActivityDto activityDto = new CeActivityDto(queueDto) - .setStatus(CeActivityDto.Status.FAILED) - .setExecutionTimeMs(500L) - .setAnalysisUuid(analysis.getUuid()); - persist(activityDto); + db.components().insertSnapshot(longLivingBranch); + CeActivityDto activity = createAndPersistArchivedTask(project); + insertCharacteristic(activity, BRANCH_KEY, longLivingBranch.getBranch()); + insertCharacteristic(activity, BRANCH_TYPE_KEY, LONG.name()); WsCe.TaskResponse taskResponse = ws.newRequest() .setParam("id", SOME_TASK_UUID) @@ -436,9 +422,17 @@ public class TaskActionTest { } private CeTaskCharacteristicDto insertCharacteristic(CeQueueDto queueDto, String key, String value) { + return insertCharacteristic(queueDto.getUuid(), key, value); + } + + private CeTaskCharacteristicDto insertCharacteristic(CeActivityDto activityDto, String key, String value) { + return insertCharacteristic(activityDto.getUuid(), key, value); + } + + private CeTaskCharacteristicDto insertCharacteristic(String taskUuid, String key, String value) { CeTaskCharacteristicDto dto = new CeTaskCharacteristicDto() .setUuid(Uuids.createFast()) - .setTaskUuid(queueDto.getUuid()) + .setTaskUuid(taskUuid) .setKey(key) .setValue(value); db.getDbClient().ceTaskCharacteristicsDao().insert(db.getSession(), Collections.singletonList(dto)); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java index 272d6670f47..243af50e8b7 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java @@ -29,9 +29,9 @@ import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.WsConnector; import static org.sonarqube.ws.client.ce.CeWsParameters.ACTION_WORKER_COUNT; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_BRANCH; +import static org.sonarqube.ws.client.ce.CeWsParameters.DEPRECATED_PARAM_COMPONENT_KEY; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS; @@ -89,7 +89,7 @@ public class CeService extends BaseService { return call( new GetRequest(path("activity_status")) .setParam(PARAM_COMPONENT_ID, request.getComponentId()) - .setParam(PARAM_COMPONENT_KEY, request.getComponentKey()), + .setParam(DEPRECATED_PARAM_COMPONENT_KEY, request.getComponentKey()), WsCe.ActivityStatusWsResponse.parser()); } @@ -97,11 +97,10 @@ public class CeService extends BaseService { return call(new GetRequest(path(ACTION_WORKER_COUNT)), WorkerCountResponse.parser()); } - public ProjectResponse component(String componentKey, String branch) { + public ProjectResponse component(String componentKey) { return call( new GetRequest(path("component")) - .setParam(PARAM_COMPONENT_KEY, componentKey) - .setParam(PARAM_BRANCH, branch), + .setParam(PARAM_COMPONENT, componentKey), ProjectResponse.parser()); } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java index 680051f2dd2..9449526952d 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeWsParameters.java @@ -24,14 +24,14 @@ public class CeWsParameters { public static final String ACTION_WORKER_COUNT = "worker_count"; public static final String PARAM_COMPONENT_ID = "componentId"; - public static final String PARAM_COMPONENT_KEY = "componentKey"; + public static final String DEPRECATED_PARAM_COMPONENT_KEY = "componentKey"; + public static final String PARAM_COMPONENT = "component"; public static final String PARAM_COMPONENT_QUERY = "componentQuery"; public static final String PARAM_TYPE = "type"; public static final String PARAM_STATUS = "status"; public static final String PARAM_ONLY_CURRENTS = "onlyCurrents"; public static final String PARAM_MIN_SUBMITTED_AT = "minSubmittedAt"; public static final String PARAM_MAX_EXECUTED_AT = "maxExecutedAt"; - public static final String PARAM_BRANCH = "branch"; private CeWsParameters() { // prevent instantiation diff --git a/sonar-ws/src/main/protobuf/ws-commons.proto b/sonar-ws/src/main/protobuf/ws-commons.proto index 9690b295d21..a9b566583e3 100644 --- a/sonar-ws/src/main/protobuf/ws-commons.proto +++ b/sonar-ws/src/main/protobuf/ws-commons.proto @@ -115,7 +115,7 @@ enum RuleType { } enum BranchType { - // Zero is required in order to not get MAINTAINABILITY as default value + // Zero is required in order to not get LONG as default value // See http://androiddevblog.com/protocol-buffers-pitfall-adding-enum-values/ UNKNOWN_BRANCH_TYPE = 0; diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java index 39d42729c24..b75aec5e9cc 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java @@ -31,9 +31,8 @@ import org.sonarqube.ws.client.WsConnector; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.data.MapEntry.entry; import static org.mockito.Mockito.mock; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_BRANCH; +import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID; -import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_KEY; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT; import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS; @@ -108,9 +107,9 @@ public class CeServiceTest { @Test public void task_with_stacktrace_and_scanner_context() { underTest.task(TaskWsRequest.newBuilder("task_id") - .withErrorStacktrace() - .withScannerContext() - .build()); + .withErrorStacktrace() + .withScannerContext() + .build()); assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/task"); assertThat(serviceTester.getGetRequest().getParams()).containsOnly(entry("id", "task_id"), entry("additionalFields", "stacktrace,scannerContext")); @@ -119,8 +118,8 @@ public class CeServiceTest { @Test public void task_with_scanner_context_only() { underTest.task(TaskWsRequest.newBuilder("task_id") - .withScannerContext() - .build()); + .withScannerContext() + .build()); assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/task"); assertThat(serviceTester.getGetRequest().getParams()).containsOnly(entry("id", "task_id"), entry("additionalFields", "scannerContext")); @@ -129,8 +128,8 @@ public class CeServiceTest { @Test public void task_with_stacktrace_only() { underTest.task(TaskWsRequest.newBuilder("task_id") - .withErrorStacktrace() - .build()); + .withErrorStacktrace() + .build()); assertThat(serviceTester.getGetRequest().getPath()).isEqualTo("api/ce/task"); assertThat(serviceTester.getGetRequest().getParams()).containsOnly(entry("id", "task_id"), entry("additionalFields", "stacktrace")); @@ -146,14 +145,13 @@ public class CeServiceTest { @Test public void component() { - underTest.component("my_component", "my_branch"); + underTest.component("my_component"); GetRequest result = serviceTester.getGetRequest(); assertThat(serviceTester.getGetParser()).isSameAs(WsCe.ProjectResponse.parser()); serviceTester.assertThat(result) .hasPath("component") - .hasParam(PARAM_COMPONENT_KEY, "my_component") - .hasParam(PARAM_BRANCH, "my_branch") + .hasParam(PARAM_COMPONENT, "my_component") .andNoOtherParam(); } }