import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import com.google.common.collect.Collections2;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
switch(uniqueQualifier) {
case Qualifiers.VIEW:
case Qualifiers.SUBVIEW:
- List<String> filteredViewUuids = newArrayList();
- for (String viewUuid : componentUuids) {
- if ((Qualifiers.VIEW.equals(uniqueQualifier) && UserSession.get().hasProjectPermissionByUuid(UserRole.USER, viewUuid))
- || (Qualifiers.SUBVIEW.equals(uniqueQualifier) && UserSession.get().hasComponentUuidPermission(UserRole.USER, viewUuid))) {
- filteredViewUuids.add(viewUuid);
- }
- }
- if (filteredViewUuids.isEmpty()) {
- filteredViewUuids.add(UNKNOWN);
- }
- builder.viewUuids(filteredViewUuids);
+ addViewsOrSubViews(builder, componentUuids, uniqueQualifier);
break;
case "DEV":
// XXX No constant for developer !!!
- Collection<String> actualAuthors = authors == null ? dbClient.authorDao().selectScmAccountsByDeveloperUuids(session, componentUuids) : authors;
+ Collection<String> actualAuthors = authorsFromParamsOrFromDeveloper(session, componentUuids, authors);
builder.authors(actualAuthors);
break;
+ case "DEV_PRJ":
+ addDeveloperTechnicalProjects(builder, session, componentUuids, authors);
+ break;
case Qualifiers.PROJECT:
builder.projectUuids(componentUuids);
break;
builder.moduleRootUuids(componentUuids);
break;
case Qualifiers.DIRECTORY:
- Collection<String> directoryModuleUuids = Sets.newHashSet();
- Collection<String> directoryPaths = Sets.newHashSet();
- for (ComponentDto directory : componentService.getByUuids(session, componentUuids)) {
- directoryModuleUuids.add(directory.moduleUuid());
- directoryPaths.add(directory.path());
- }
- builder.moduleUuids(directoryModuleUuids);
- builder.directories(directoryPaths);
+ addDirectories(builder, session, componentUuids);
break;
case Qualifiers.FILE:
case Qualifiers.UNIT_TEST_FILE:
}
}
+ private void addViewsOrSubViews(IssueQuery.Builder builder, Collection<String> componentUuids, String uniqueQualifier) {
+ List<String> filteredViewUuids = newArrayList();
+ for (String viewUuid : componentUuids) {
+ if ((Qualifiers.VIEW.equals(uniqueQualifier) && UserSession.get().hasProjectPermissionByUuid(UserRole.USER, viewUuid))
+ || (Qualifiers.SUBVIEW.equals(uniqueQualifier) && UserSession.get().hasComponentUuidPermission(UserRole.USER, viewUuid))) {
+ filteredViewUuids.add(viewUuid);
+ }
+ }
+ if (filteredViewUuids.isEmpty()) {
+ filteredViewUuids.add(UNKNOWN);
+ }
+ builder.viewUuids(filteredViewUuids);
+ }
+
+ private void addDeveloperTechnicalProjects(IssueQuery.Builder builder, DbSession session, Collection<String> componentUuids, Collection<String> authors) {
+ Collection<ComponentDto> technicalProjects = dbClient.componentDao().getByUuids(session, componentUuids);
+ Collection<String> developerUuids = Collections2.transform(technicalProjects, new Function<ComponentDto, String>() {
+ @Override
+ public String apply(ComponentDto input) {
+ return input.projectUuid();
+ }
+ });
+ Collection<String> authorsFromProjects = authorsFromParamsOrFromDeveloper(session, developerUuids, authors);
+ builder.authors(authorsFromProjects);
+ Collection<Long> projectIds = Collections2.transform(technicalProjects, new Function<ComponentDto, Long>() {
+ @Override
+ public Long apply(ComponentDto input) {
+ return input.getCopyResourceId();
+ }
+ });
+ List<ComponentDto> originalProjects = dbClient.componentDao().getByIds(session, projectIds);
+ Collection<String> projectUuids = Collections2.transform(originalProjects, new Function<ComponentDto, String>() {
+ @Override
+ public String apply(ComponentDto input) {
+ return input.uuid();
+ }
+ });
+ builder.projectUuids(projectUuids);
+ }
+
+ private Collection<String> authorsFromParamsOrFromDeveloper(DbSession session, Collection<String> componentUuids, Collection<String> authors) {
+ return authors == null ? dbClient.authorDao().selectScmAccountsByDeveloperUuids(session, componentUuids) : authors;
+ }
+
+ private void addDirectories(IssueQuery.Builder builder, DbSession session, Collection<String> componentUuids) {
+ Collection<String> directoryModuleUuids = Sets.newHashSet();
+ Collection<String> directoryPaths = Sets.newHashSet();
+ for (ComponentDto directory : componentService.getByUuids(session, componentUuids)) {
+ directoryModuleUuids.add(directory.moduleUuid());
+ directoryPaths.add(directory.path());
+ }
+ builder.moduleUuids(directoryModuleUuids);
+ builder.directories(directoryPaths);
+ }
+
private Collection<String> componentUuids(DbSession session, @Nullable Collection<String> componentKeys) {
Collection<String> componentUuids = Lists.newArrayList();
componentUuids.addAll(componentService.componentUuids(session, componentKeys, true));
.setLanguage(null);
}
+ public static ComponentDto newDevProjectCopy(String uuid, ComponentDto project, ComponentDto developer) {
+ Preconditions.checkNotNull(project.getId(), "The project need to be persisted before creating this technical project.");
+ return newChildComponent(uuid, developer)
+ .setUuid(uuid)
+ .setKey(developer.key() + ":" + project.key())
+ .setName(project.name())
+ .setLongName(project.longName())
+ .setCopyResourceId(project.getId())
+ .setScope(Scopes.PROJECT)
+ .setQualifier("DEV_PRJ")
+ .setPath(null)
+ .setLanguage(null);
+ }
+
private static ComponentDto newChildComponent(String uuid, ComponentDto module) {
return newChildComponent(uuid, module, false);
}
assertThat(result.qualifier()).isEqualTo("FIL");
assertThat(result.scope()).isEqualTo("FIL");
assertThat(result.language()).isEqualTo("java");
+ assertThat(result.getCopyResourceId()).isNull();
+ }
+
+ @Test
+ public void get_by_uuid_on_technical_project_copy() {
+ setupData("shared");
+
+ ComponentDto result = dao.getNullableByUuid(session, "STUV");
+ assertThat(result).isNotNull();
+ assertThat(result.uuid()).isEqualTo("STUV");
+ assertThat(result.moduleUuid()).isEqualTo("OPQR");
+ assertThat(result.moduleUuidPath()).isEqualTo(".OPQR.");
+ assertThat(result.parentProjectId()).isEqualTo(11);
+ assertThat(result.projectUuid()).isEqualTo("OPQR");
+ assertThat(result.key()).isEqualTo("DEV:anakin@skywalker.name:org.struts:struts");
+ assertThat(result.path()).isNull();
+ assertThat(result.name()).isEqualTo("Apache Struts");
+ assertThat(result.longName()).isEqualTo("Apache Struts");
+ assertThat(result.qualifier()).isEqualTo("DEV_PRJ");
+ assertThat(result.scope()).isEqualTo("PRJ");
+ assertThat(result.language()).isNull();
+ assertThat(result.getCopyResourceId()).isEqualTo(1L);
}
@Test
assertThat(query.authors()).containsExactly(login);
}
+ @Test
+ public void should_search_on_developer_technical_project() throws Exception {
+ String projectUuid = "sample1";
+ String devUuid = "DEV:anakin.skywalker";
+ String login1 = "anakin@skywalker.name";
+ String login2 = "darth.vader";
+ String copyProjectUuid = devUuid + ":" + projectUuid;
+
+ long copyResourceId = 42L;
+ ComponentDto technicalProject = new ComponentDto().setProjectUuid(devUuid).setCopyResourceId(copyResourceId);
+ when(componentDao.getByUuids(isA(DbSession.class), anyCollection())).thenReturn(Arrays.asList(technicalProject));
+
+ when(componentService.getDistinctQualifiers(isA(DbSession.class), anyCollection())).thenReturn(Sets.newHashSet("DEV_PRJ"));
+ when(authorDao.selectScmAccountsByDeveloperUuids(isA(DbSession.class), anyCollection())).thenReturn(Lists.newArrayList(login1, login2));
+
+ ComponentDto actualProject = new ComponentDto().setUuid(projectUuid);
+ when(componentDao.getByIds(isA(DbSession.class), anyCollection())).thenReturn(Arrays.asList(actualProject));
+
+ Map<String, Object> map = newHashMap();
+ map.put("componentUuids", newArrayList(copyProjectUuid));
+ IssueQuery query = issueQueryService.createFromMap(map);
+ assertThat(query.authors()).containsExactly(login1, login2);
+ assertThat(query.projectUuids()).containsExactly(projectUuid);
+ }
+
@Test
public void should_search_in_tree_with_module_uuid() throws Exception {
String moduleUuid = "ABCD";
import org.sonar.server.view.index.ViewDoc;
import org.sonar.server.view.index.ViewIndexer;
import org.sonar.server.ws.WsTester;
+import org.sonar.server.ws.WsTester.Result;
import java.util.List;
.assertJson(this.getClass(), "search_by_developer.json", false);
}
+ @Test
+ public void search_by_developer_technical_project() throws Exception {
+ ComponentDto project = insertComponent(ComponentTesting.newProjectDto("ABCD").setKey("MyProject"));
+ setDefaultProjectPermission(project);
+ ComponentDto file = insertComponent(ComponentTesting.newFileDto(project, "BCDE").setKey("MyComponent"));
+
+ ComponentDto otherProject = insertComponent(ComponentTesting.newProjectDto("XXXX").setKey("OtherProject"));
+ setDefaultProjectPermission(otherProject);
+ ComponentDto otherFile = insertComponent(ComponentTesting.newFileDto(otherProject, "YYYY").setKey("OtherComponent"));
+
+ ComponentDto developer = insertComponent(ComponentTesting.newDeveloper("Anakin Skywalker"));
+ ComponentDto technicalProject = insertComponent(ComponentTesting.newDevProjectCopy("CDEF", project, developer));
+ insertComponent(ComponentTesting.newDevProjectCopy("DEFG", otherProject, developer));
+
+ db.authorDao().insertAuthor("vader", developer.getId());
+ db.authorDao().insertAuthor("anakin@skywalker.name", developer.getId());
+ RuleDto newRule = newRule();
+
+ IssueDto issue1 = IssueTesting.newDto(newRule, file, project).setAuthorLogin("vader").setKee("2bd4eac2-b650-4037-80bc-7b112bd4eac2");
+ IssueDto issue2 = IssueTesting.newDto(newRule, file, project).setAuthorLogin("anakin@skywalker.name").setKee("82fd47d4-b650-4037-80bc-7b1182fd47d4");
+ IssueDto issueX = IssueTesting.newDto(newRule, otherFile, otherProject).setAuthorLogin("anakin@skywalker.name").setKee("82fd47d4-b650-4037-7b11-80bc82fd47d4");
+
+ db.issueDao().insert(session, issue1, issue2, issueX);
+ session.commit();
+ tester.get(IssueIndexer.class).indexAll();
+
+ Result result = wsTester.newGetRequest(IssuesWs.API_ENDPOINT, SearchAction.SEARCH_ACTION)
+ .setParam(IssueFilterParameters.COMPONENT_UUIDS, technicalProject.uuid())
+ .execute();
+ System.out.println(result.outputAsString());
+ result
+ .assertJson(this.getClass(), "search_by_developer.json", false);
+ }
+
private RuleDto newRule() {
RuleDto rule = RuleTesting.newXooX1()
.setName("Rule name")
description="the description" long_name="Disabled project"
enabled="[false]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" authorization_updated_at="123456789" />
+ <!-- Developer and technical project copy -->
+ <projects id="11" root_id="[null]" scope="PRJ" qualifier="DEV" kee="DEV:anakin@skywalker.name" name="Anakin Skywalker"
+ uuid="OPQR" project_uuid="OPQR" module_uuid="[null]" module_uuid_path=".OPQR."
+ description="the description" long_name="Anakin Skywalker"
+ enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" authorization_updated_at="123456789" />
+ <projects id="12" project_id="11" root_id="11" scope="PRJ" qualifier="DEV_PRJ" kee="DEV:anakin@skywalker.name:org.struts:struts" name="Apache Struts"
+ uuid="STUV" project_uuid="OPQR" module_uuid="OPQR" module_uuid_path=".OPQR."
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="1" person_id="11" path="[null]" authorization_updated_at="123456789" />
+
</dataset>
description="the description" long_name="Disabled project"
enabled="[false]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" authorization_updated_at="123456789" />
+ <!-- Developer and technical project copy -->
+ <projects id="11" root_id="[null]" scope="PRJ" qualifier="DEV" kee="DEV:anakin@skywalker.name" name="Anakin Skywalker"
+ uuid="OPQR" project_uuid="OPQR" module_uuid="[null]" module_uuid_path=".OPQR."
+ description="the description" long_name="Anakin Skywalker"
+ enabled="[true]" language="[null]" copy_resource_id="[null]" person_id="[null]" path="[null]" authorization_updated_at="123456789" />
+ <projects id="12" project_id="11" root_id="11" scope="PRJ" qualifier="DEV_PRJ" kee="DEV:anakin@skywalker.name:org.struts:struts" name="Apache Struts"
+ uuid="STUV" project_uuid="OPQR" module_uuid="OPQR" module_uuid_path=".OPQR."
+ description="the description" long_name="Apache Struts"
+ enabled="[true]" language="[null]" copy_resource_id="1" person_id="11" path="[null]" authorization_updated_at="123456789" />
+
</dataset>
p.root_id as parentProjectId,
p.path as path,
p.enabled as enabled,
+ p.copy_resource_id as copyResourceId,
p.authorization_updated_at as authorizationUpdatedAt,
p.created_at as createdAt
</sql>