aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-05-08 19:31:41 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-05-08 19:31:41 +0200
commitd83ff5e2042f848a1640e84ecb30a867311cfae6 (patch)
tree1e614e9b16806824141f86d2f2d5364ed44d8c74 /server/sonar-db-dao
parentf16ba7b4228e0908d7dd7495be1d3905b14b577d (diff)
downloadsonarqube-d83ff5e2042f848a1640e84ecb30a867311cfae6.tar.gz
sonarqube-d83ff5e2042f848a1640e84ecb30a867311cfae6.zip
SONAR-9182 Add visibility to WS api/projects/search
Diffstat (limited to 'server/sonar-db-dao')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentQuery.java14
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml8
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java14
3 files changed, 36 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentQuery.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentQuery.java
index f5e6df3a26a..72ed52dd99c 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentQuery.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentQuery.java
@@ -32,6 +32,7 @@ public class ComponentQuery {
private final String nameOrKeyQuery;
private final String[] qualifiers;
private final String language;
+ private final Boolean isPrivate;
private final Set<Long> componentIds;
/**
@@ -46,6 +47,7 @@ public class ComponentQuery {
this.qualifiers = Builder.validateQualifiers(qualifiers);
this.language = null;
this.componentIds = null;
+ this.isPrivate = null;
}
private ComponentQuery(Builder builder) {
@@ -53,6 +55,7 @@ public class ComponentQuery {
this.qualifiers = builder.qualifiers;
this.language = builder.language;
this.componentIds = builder.componentIds;
+ this.isPrivate = builder.isPrivate;
}
public String[] getQualifiers() {
@@ -82,6 +85,11 @@ public class ComponentQuery {
return componentIds;
}
+ @CheckForNull
+ public Boolean getPrivate() {
+ return isPrivate;
+ }
+
public static Builder builder() {
return new Builder();
}
@@ -90,6 +98,7 @@ public class ComponentQuery {
private String nameOrKeyQuery;
private String[] qualifiers;
private String language;
+ private Boolean isPrivate;
private Set<Long> componentIds;
public Builder setNameOrKeyQuery(@Nullable String nameOrKeyQuery) {
@@ -112,6 +121,11 @@ public class ComponentQuery {
return this;
}
+ public Builder setPrivate(@Nullable Boolean isPrivate) {
+ this.isPrivate = isPrivate;
+ return this;
+ }
+
protected static String[] validateQualifiers(@Nullable String... qualifiers) {
checkArgument(qualifiers != null && qualifiers.length > 0, "At least one qualifier must be provided");
return qualifiers;
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
index 1c4920b51e3..fa61bb41a3b 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
@@ -298,6 +298,14 @@
upper(p.name) like #{query.nameOrKeyUpperLikeQuery,jdbcType=VARCHAR} escape '/'
)
</if>
+ <if test="query.private!=null">
+ <if test="query.private.equals(true)">
+ and p.private=${_true}
+ </if>
+ <if test="query.private.equals(false)">
+ and p.private=${_false}
+ </if>
+ </if>
</sql>
<select id="selectDescendants" resultType="Component">
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
index 72e33569986..7e3e3208d01 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
@@ -961,6 +961,20 @@ public class ComponentDaoTest {
}
@Test
+ public void selectByQuery_filter_on_visibility() {
+ db.components().insertComponent(ComponentTesting.newPrivateProjectDto(db.getDefaultOrganization()).setKey("private-key"));
+ db.components().insertComponent(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()).setKey("public-key"));
+
+ ComponentQuery privateProjectsQuery = ComponentQuery.builder().setPrivate(true).setQualifiers(Qualifiers.PROJECT).build();
+ ComponentQuery publicProjectsQuery = ComponentQuery.builder().setPrivate(false).setQualifiers(Qualifiers.PROJECT).build();
+ ComponentQuery allProjectsQuery = ComponentQuery.builder().setPrivate(null).setQualifiers(Qualifiers.PROJECT).build();
+
+ assertThat(underTest.selectByQuery(dbSession, privateProjectsQuery, 0, 10)).extracting(ComponentDto::getKey).containsExactly("private-key");
+ assertThat(underTest.selectByQuery(dbSession, publicProjectsQuery, 0, 10)).extracting(ComponentDto::getKey).containsExactly("public-key");
+ assertThat(underTest.selectByQuery(dbSession, allProjectsQuery, 0, 10)).extracting(ComponentDto::getKey).containsOnly("public-key", "private-key");
+ }
+
+ @Test
public void selectByQuery_on_empty_list_of_component_id() {
ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(Qualifiers.PROJECT).setComponentIds(emptySet()).build();
List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10);