Skip to content

Commit

Permalink
SONAR-7370 WS api/ce/activity returns developers
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed May 10, 2016
1 parent b37fcd7 commit 2d9bae8
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 35 deletions.
Expand Up @@ -74,6 +74,7 @@
public class ActivityAction implements CeWsAction {
private static final int OFFSET = 0;
private static final int MAX_PAGE_SIZE = 1000;
private static final List<String> POSSIBLE_QUALIFIERS = ImmutableList.of(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");

private final UserSession userSession;
private final DbClient dbClient;
Expand Down Expand Up @@ -216,22 +217,28 @@ private CeTaskQuery buildQuery(DbSession dbSession, ActivityWsRequest request) {
query.setStatuses(request.getStatus());
}

loadComponentUuids(dbSession, request, query);
query.setComponentUuids(loadComponentUuids(dbSession, request));
return query;
}

private void loadComponentUuids(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
@CheckForNull
private List<String> loadComponentUuids(DbSession dbSession, ActivityWsRequest request) {
String componentUuid = request.getComponentId();
String componentQuery = request.getQuery();

if (componentUuid != null) {
query.setComponentUuid(componentUuid);
return singletonList(componentUuid);
}
if (componentQuery != null) {
ComponentQuery componentDtoQuery = ComponentQuery.builder().setNameOrKeyQuery(componentQuery).setQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW).build();
ComponentQuery componentDtoQuery = ComponentQuery.builder()
.setNameOrKeyQuery(componentQuery)
.setQualifiers(POSSIBLE_QUALIFIERS.toArray(new String[0]))
.build();
List<ComponentDto> componentDtos = dbClient.componentDao().selectByQuery(dbSession, componentDtoQuery, 0, CeTaskQuery.MAX_COMPONENT_UUIDS);
query.setComponentUuids(Lists.transform(componentDtos, ComponentDtoFunctions.toUuid()));
return Lists.transform(componentDtos, ComponentDtoFunctions.toUuid());
}

return null;
}

private Iterable<WsCe.Task> loadQueuedTasks(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
Expand Down
Expand Up @@ -43,6 +43,7 @@
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
Expand All @@ -61,7 +62,9 @@
import static org.mockito.Mockito.when;
import static org.sonar.api.utils.DateUtils.formatDate;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.db.component.ComponentTesting.newDeveloper;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.ComponentTesting.newView;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY;
import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS;

Expand Down Expand Up @@ -210,11 +213,14 @@ public void project_administrator_can_access_his_project_activity() {

@Test
public void search_activity_by_component_name() throws IOException {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("apache struts").setUuid("P1"));
componentDb.insertProjectAndSnapshot(newProjectDto().setName("apache zookeeper").setUuid("P2"));
componentDb.insertProjectAndSnapshot(newProjectDto().setName("eclipse").setUuid("P3"));
ComponentDto struts = newProjectDto().setName("old apache struts").setUuid("P1");
ComponentDto zookeeper = newProjectDto().setName("new apache zookeeper").setUuid("P2");
ComponentDto eclipse = newProjectDto().setName("eclipse").setUuid("P3");
componentDb.insertProjectAndSnapshot(struts);
componentDb.insertProjectAndSnapshot(zookeeper);
componentDb.insertProjectAndSnapshot(eclipse);
dbTester.commit();
componentDb.indexProjects();
componentDb.indexComponents(struts.getId(), zookeeper.getId(), eclipse.getId());
userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
insertActivity("T1", "P1", CeActivityDto.Status.SUCCESS);
insertActivity("T2", "P2", CeActivityDto.Status.SUCCESS);
Expand All @@ -225,6 +231,22 @@ public void search_activity_by_component_name() throws IOException {
assertThat(activityResponse.getTasksList()).extracting("id").containsOnly("T1", "T2");
}

@Test
public void search_activity_returns_views_and_developers() {
ComponentDto developer = newDeveloper("Apache Developer").setUuid("D1");
ComponentDto apacheView = newView().setName("Apache View").setUuid("V1");
componentDb.insertDeveloperAndSnapshot(developer);
componentDb.insertViewAndSnapshot(apacheView);
componentDb.indexComponents(developer.getId(), apacheView.getId());
userSession.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
insertActivity("T1", "D1", CeActivityDto.Status.SUCCESS);
insertActivity("T2", "V1", CeActivityDto.Status.SUCCESS);

ActivityResponse activityResponse = call(ws.newRequest().setParam(PARAM_COMPONENT_QUERY, "apac"));

assertThat(activityResponse.getTasksList()).extracting("id").containsOnly("T1", "T2");
}

@Test
public void search_task_id_in_queue_ignoring_other_parameters() throws IOException {
insertQueue("T1", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
Expand Down
Expand Up @@ -118,7 +118,7 @@ public void direct_children() throws IOException {
SnapshotDto directorySnapshot = componentDb.insertComponentAndSnapshot(newDirectory(project, "directory-path-1"), moduleSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, 10), directorySnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

TreeWsResponse response = call(ws.newRequest()
.setParam(PARAM_STRATEGY, "children")
Expand Down Expand Up @@ -149,7 +149,7 @@ public void all_children() throws IOException {
SnapshotDto directorySnapshot = componentDb.insertComponentAndSnapshot(newDirectory(project, "directory-path-1"), moduleSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, 1), directorySnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

TreeWsResponse response = call(ws.newRequest()
.setParam(PARAM_STRATEGY, "all")
Expand All @@ -175,7 +175,7 @@ public void leaves_children() throws IOException {
SnapshotDto directorySnapshot = componentDb.insertComponentAndSnapshot(newDirectory(project, "directory-path-1"), moduleSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, 3), directorySnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

TreeWsResponse response = call(ws.newRequest()
.setParam(PARAM_STRATEGY, "leaves")
Expand All @@ -195,7 +195,7 @@ public void all_children_by_file_qualifier() throws IOException {
componentDb.insertComponentAndSnapshot(newFileDto(project, 2), projectSnapshot);
componentDb.insertComponentAndSnapshot(newModuleDto("module-uuid-1", project), projectSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

TreeWsResponse response = call(ws.newRequest()
.setParam(PARAM_STRATEGY, "all")
Expand All @@ -215,7 +215,7 @@ public void all_children_sort_by_qualifier() throws IOException {
componentDb.insertComponentAndSnapshot(module, projectSnapshot);
componentDb.insertComponentAndSnapshot(newDirectory(project, "path/directory/", "directory-uuid-1"), projectSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

TreeWsResponse response = call(ws.newRequest()
.setParam(PARAM_STRATEGY, "all")
Expand All @@ -234,7 +234,7 @@ public void direct_children_of_a_view() {
componentDb.insertComponentAndSnapshot(newProjectCopy("project-uuid-1-copy", project, view), viewSnapshot);
componentDb.insertComponentAndSnapshot(newSubView(view, "sub-view-uuid", "sub-view-key").setName("sub-view-name"), viewSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

TreeWsResponse response = call(ws.newRequest()
.setParam(PARAM_STRATEGY, "children")
Expand Down Expand Up @@ -414,7 +414,7 @@ private ComponentDto initJsonExampleComponents() throws IOException {
projectSnapshot);
}
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();
return project;
}

Expand Down
Expand Up @@ -184,7 +184,7 @@ public void has_projects_ordered_by_name() {
public void search_by_query_on_name() {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-name"));
componentDb.insertProjectAndSnapshot(newProjectDto().setName("another-name"));
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

String result = ws.newRequest()
.setParam(TEXT_QUERY, "project")
Expand All @@ -198,7 +198,7 @@ public void search_by_query_on_name() {
public void search_by_query_on_key_must_match_exactly() {
componentDb.insertProjectAndSnapshot(newProjectDto().setKey("project-key"));
componentDb.insertProjectAndSnapshot(newProjectDto().setKey("another-key"));
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

String result = ws.newRequest()
.setParam(TEXT_QUERY, "project-key")
Expand All @@ -213,7 +213,7 @@ public void handle_more_than_1000_projects() {
for (int i = 1; i <= 1001; i++) {
componentDb.insertProjectAndSnapshot(newProjectDto("project-uuid-" + i));
}
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

String result = ws.newRequest()
.setParam(TEXT_QUERY, "project")
Expand Down
Expand Up @@ -181,7 +181,7 @@ public void apply_template_by_query_on_name_and_key() {
// match must be exact on key
ComponentDto projectUntouched = newProjectDto().setKey("new-sonar").setName("project-name");
componentDb.insertProjectAndSnapshot(projectUntouched);
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

call(ws.newRequest()
.setParam(PARAM_TEMPLATE_ID, template1.getUuid())
Expand Down
Expand Up @@ -267,7 +267,7 @@
select
<include refid="componentColumns"/>
<include refid="sqlSelectByQuery"/>
ORDER BY LOWER(p.name), p.name
ORDER BY LOWER(p.name), p.name, p.id
</select>

<select id="countByQuery" resultType="int">
Expand Down
26 changes: 13 additions & 13 deletions sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java
Expand Up @@ -695,7 +695,7 @@ public void select_by_query_with_paging_query_and_qualifiers() {
for (int i = 9; i >= 1; i--) {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-" + i));
}
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("oJect").setQualifiers(Qualifiers.PROJECT).build();
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 1, 3);
Expand All @@ -708,7 +708,7 @@ public void select_by_query_with_paging_query_and_qualifiers() {
@Test
public void select_by_query_name_with_special_characters() {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-\\_%/-name"));
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("-\\_%/-").setQualifiers(Qualifiers.PROJECT).build();
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
Expand All @@ -720,7 +720,7 @@ public void select_by_query_name_with_special_characters() {
@Test
public void select_by_query_key_with_special_characters() {
componentDb.insertProjectAndSnapshot(newProjectDto().setKey("project-_%-key"));
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("project-_%-key").setQualifiers(Qualifiers.PROJECT).build();
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
Expand Down Expand Up @@ -749,7 +749,7 @@ public void select_direct_children_of_a_project() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot).build();

Expand All @@ -768,7 +768,7 @@ public void select_direct_children_with_name_query() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid").setName("file-name-1"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid").setName("file-name-2"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setNameOrKeyQuery("file-name").build();
Expand All @@ -788,7 +788,7 @@ public void select_direct_children_with_key_query() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid").setKey("file-key-1").setName("File one"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid").setKey("file-key-2").setName("File two"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setNameOrKeyQuery("file-key-1").build();
Expand All @@ -808,7 +808,7 @@ public void select_direct_children_with_pagination() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-" + i), projectSnapshot);
}
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setPage(2)
Expand All @@ -831,7 +831,7 @@ public void select_direct_children_with_order_by_path() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-2").setName("file-name-2").setPath("2"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-3").setName("file-name-3").setPath("1"), projectSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setSortFields(singletonList("path"))
Expand All @@ -851,7 +851,7 @@ public void select_direct_children_of_a_module() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(moduleSnapshot).build();

Expand All @@ -868,7 +868,7 @@ public void select_all_children_of_a_project() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot).build();

Expand All @@ -890,7 +890,7 @@ public void list_direct_children_of_a_view() {
ComponentDto project = newProjectDto("project-uuid").setName("project-name");
componentDb.insertProjectAndSnapshot(project);
componentDb.insertComponentAndSnapshot(newProjectCopy("project-copy-uuid", project, view), viewSnapshot);
componentDb.indexProjects();
componentDb.indexProjectsAndViews();
ComponentTreeQuery dbQuery = newTreeQuery(viewSnapshot).build();

List<ComponentDtoWithSnapshotId> components = underTest.selectDirectChildren(dbSession, dbQuery);
Expand All @@ -909,7 +909,7 @@ public void search_direct_children_of_a_view() {
ComponentDto project = newProjectDto("project-uuid").setName("project name");
componentDb.insertProjectAndSnapshot(project);
componentDb.insertComponentAndSnapshot(newProjectCopy("project-copy-uuid", project, view), viewSnapshot);
componentDb.indexProjects();
componentDb.indexProjectsAndViews();
ComponentTreeQuery dbQuery = newTreeQuery(viewSnapshot).setNameOrKeyQuery("name").build();

List<ComponentDtoWithSnapshotId> components = underTest.selectDirectChildren(dbSession, dbQuery);
Expand All @@ -928,7 +928,7 @@ public void select_all_files_of_a_project_paginated_and_ordered() {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-" + i).setName("file-name-" + i), moduleSnapshot);
}
db.commit();
componentDb.indexProjects();
componentDb.indexProjectsAndViews();

ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setQualifiers(newArrayList(Qualifiers.FILE))
Expand Down
Expand Up @@ -83,8 +83,15 @@ public void insertComponents(ComponentDto... components) {
db.commit();
}

public void indexProjects() {
public void indexProjectsAndViews() {
dbClient.componentIndexDao().indexProjects();
db.commit();
}

public void indexComponents(long... componentIdList) {
for (long componentId : componentIdList) {
dbClient.componentIndexDao().indexResource(dbSession, componentId);
}
db.commit();
}
}

0 comments on commit 2d9bae8

Please sign in to comment.