import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.db.component.SnapshotQuery.SORT_FIELD.BY_DATE;
import static org.sonar.db.component.SnapshotQuery.SORT_ORDER.DESC;
+import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.projectanalysis.EventCategory.OTHER;
+import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_BRANCH;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_CATEGORY;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_FROM;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_PROJECT;
.setRequired(true)
.setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001);
+ action.createParam(PARAM_BRANCH)
+ .setDescription("Branch key")
+ .setSince("6.6")
+ .setInternal(true)
+ .setExampleValue(KEY_BRANCH_EXAMPLE_001);
+
action.createParam(PARAM_CATEGORY)
.setDescription("Event category. Filter analyses that have at least one event of the category specified.")
.setPossibleValues(EnumSet.allOf(EventCategory.class))
.setDescription("Filter analyses created before the given date (inclusive). Format: date or datetime ISO formats")
.setExampleValue("2013-05-01T13:00:00+0100")
.setSince("6.5");
+
}
@Override
String category = request.param(PARAM_CATEGORY);
return SearchRequest.builder()
.setProject(request.mandatoryParam(PARAM_PROJECT))
+ .setBranch(request.param(PARAM_BRANCH))
.setCategory(category == null ? null : EventCategory.valueOf(category))
.setPage(request.mandatoryParamAsInt(Param.PAGE))
.setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE))
}
private void addProject(SearchData.Builder data) {
- ComponentDto project = componentFinder.getByKey(data.getDbSession(), data.getRequest().getProject());
+ ComponentDto project = loadComponent(data.getDbSession(), data.getRequest());
checkArgument(Scopes.PROJECT.equals(project.scope()) && ALLOWED_QUALIFIERS.contains(project.qualifier()), "A project or application is required");
data.setProject(project);
}
+ private ComponentDto loadComponent(DbSession dbSession, SearchRequest request) {
+ String project = request.getProject();
+ String branch = request.getBranch();
+ if (branch != null) {
+ return componentFinder.getByKeyAndBranch(dbSession, project, branch);
+ }
+ return componentFinder.getByKey(dbSession, project);
+ }
+
}
import static org.sonarqube.ws.client.projectanalysis.EventCategory.OTHER;
import static org.sonarqube.ws.client.projectanalysis.EventCategory.QUALITY_GATE;
import static org.sonarqube.ws.client.projectanalysis.EventCategory.VERSION;
+import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_BRANCH;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_CATEGORY;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_FROM;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_PROJECT;
@Test
public void return_analyses_ordered_by_analysis_date() {
- ComponentDto project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto(db.organizations().insert()).setDbKey("P1"));
+ ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
db.components().insertSnapshot(newAnalysis(project).setUuid("A1").setCreatedAt(1_000_000L));
db.components().insertSnapshot(newAnalysis(project).setUuid("A2").setCreatedAt(2_000_000L));
db.components().insertSnapshot(newAnalysis(project).setUuid("A3").setCreatedAt(3_000_000L));
- List<Analysis> result = call("P1").getAnalysesList();
+ List<Analysis> result = call(project.getKey()).getAnalysesList();
assertThat(result).hasSize(3);
assertThat(result).extracting(Analysis::getKey, a -> parseDateTime(a.getDate()).getTime()).containsExactly(
assertThat(result.getAnalysesList())
.extracting(Analysis::getKey)
.containsOnly(a2.getUuid(), a3.getUuid())
- .doesNotContain(a1.getUuid(), a4.getUuid());
+ .doesNotContain(a1.getUuid(), a4.getUuid());
}
@Test
}
@Test
- public void empty_response() {
+ public void branch() {
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
+ ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(branch));
+ EventDto event = db.events().insertEvent(newEvent(analysis).setCategory(EventCategory.QUALITY_GATE.getLabel()));
- SearchResponse result = call(project.getDbKey());
+ List<Analysis> result = call(SearchRequest.builder()
+ .setProject(project.getKey())
+ .setBranch("my_branch")
+ .build())
+ .getAnalysesList();
+
+ assertThat(result).extracting(Analysis::getKey).containsExactlyInAnyOrder(analysis.getUuid());
+ assertThat(result.get(0).getEventsList()).extracting(Event::getKey).containsExactlyInAnyOrder(event.getUuid());
- assertThat(result.hasPaging()).isTrue();
- assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 0);
- assertThat(result.getAnalysesCount()).isEqualTo(0);
}
@Test
- public void definition() {
- WebService.Action definition = ws.getDef();
-
- assertThat(definition.key()).isEqualTo("search");
- assertThat(definition.since()).isEqualTo("6.3");
- assertThat(definition.responseExampleAsString()).isNotEmpty();
- assertThat(definition.param("project").isRequired()).isTrue();
- assertThat(definition.param("category")).isNotNull();
+ public void empty_response() {
+ ComponentDto project = db.components().insertPrivateProject();
+ userSession.addProjectPermission(UserRole.USER, project);
- Param from = definition.param("from");
- assertThat(from.since()).isEqualTo("6.5");
+ SearchResponse result = call(project.getDbKey());
- Param to = definition.param("to");
- assertThat(to.since()).isEqualTo("6.5");
+ assertThat(result.hasPaging()).isTrue();
+ assertThat(result.getPaging()).extracting(Paging::getPageIndex, Paging::getPageSize, Paging::getTotal).containsExactly(1, 100, 0);
+ assertThat(result.getAnalysesCount()).isEqualTo(0);
}
@Test
call(view.getDbKey());
}
+ @Test
+ public void fail_if_branch_does_not_exist() {
+ ComponentDto project = db.components().insertPrivateProject();
+ 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", project.getKey(), "another_branch"));
+
+ call(SearchRequest.builder()
+ .setProject(project.getKey())
+ .setBranch("another_branch")
+ .build());
+ }
+
+ @Test
+ public void definition() {
+ WebService.Action definition = ws.getDef();
+
+ assertThat(definition.key()).isEqualTo("search");
+ assertThat(definition.since()).isEqualTo("6.3");
+ assertThat(definition.responseExampleAsString()).isNotEmpty();
+ assertThat(definition.param("project").isRequired()).isTrue();
+ assertThat(definition.param("category")).isNotNull();
+ assertThat(definition.params()).hasSize(7);
+
+ Param from = definition.param("from");
+ assertThat(from.since()).isEqualTo("6.5");
+
+ Param to = definition.param("to");
+ assertThat(to.since()).isEqualTo("6.5");
+
+ Param branch = definition.param("branch");
+ assertThat(branch.since()).isEqualTo("6.6");
+ assertThat(branch.isInternal()).isTrue();
+ assertThat(branch.isRequired()).isFalse();
+ }
+
private static Function<Event, String> wsToDbCategory() {
return e -> e == null ? null : EventCategory.valueOf(e.getCategory()).getLabel();
}
TestRequest request = ws.newRequest()
.setMethod(POST.name());
setNullable(wsRequest.getProject(), project -> request.setParam(PARAM_PROJECT, project));
+ setNullable(wsRequest.getBranch(), branch -> request.setParam(PARAM_BRANCH, branch));
setNullable(wsRequest.getCategory(), category -> request.setParam(PARAM_CATEGORY, category.name()));
setNullable(wsRequest.getPage(), page -> request.setParam(Param.PAGE, String.valueOf(page)));
setNullable(wsRequest.getPageSize(), pageSize -> request.setParam(Param.PAGE_SIZE, String.valueOf(pageSize)));
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonarqube.ws.client.projectanalysis.EventCategory.QUALITY_GATE;
+import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_BRANCH;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_CATEGORY;
import static org.sonarqube.ws.client.projectanalysis.ProjectAnalysesWsParameters.PARAM_PROJECT;
public void search() {
underTest.search(SearchRequest.builder()
.setProject("project")
+ .setBranch("my_branch")
.setCategory(QUALITY_GATE)
.setPage(10)
.setPageSize(50)
assertThat(serviceTester.getGetParser()).isSameAs(ProjectAnalyses.SearchResponse.parser());
serviceTester.assertThat(getRequest)
.hasParam(PARAM_PROJECT, "project")
+ .hasParam(PARAM_BRANCH, "my_branch")
.hasParam(PARAM_CATEGORY, QUALITY_GATE.name())
.hasParam(PAGE, 10)
.hasParam(PAGE_SIZE, 50)
}
@Test
- public void search_without_category() {
+ public void search_with_minimal_fields() {
underTest.search(SearchRequest.builder()
.setProject("project")
- .setPage(10)
- .setPageSize(50)
.build());
GetRequest getRequest = serviceTester.getGetRequest();
assertThat(serviceTester.getGetParser()).isSameAs(ProjectAnalyses.SearchResponse.parser());
serviceTester.assertThat(getRequest)
.hasParam(PARAM_PROJECT, "project")
- .hasParam(PAGE, 10)
- .hasParam(PAGE_SIZE, 50)
+ .hasParam(PAGE, 1)
+ .hasParam(PAGE_SIZE, 100)
.andNoOtherParam();
}