diff options
author | Daniel Schwarz <daniel.schwarz@sonarsource.com> | 2017-04-24 14:52:35 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-04-27 14:25:54 +0200 |
commit | 6cb11d0e16a33960b6c699f9122d486655f30f84 (patch) | |
tree | 5b8fb8e06275e26a1b59929ae71785cbd53055b0 | |
parent | 24dd8757c9528b5692eb54b4e4aceff37d8d3f8a (diff) | |
download | sonarqube-6cb11d0e16a33960b6c699f9122d486655f30f84.tar.gz sonarqube-6cb11d0e16a33960b6c699f9122d486655f30f84.zip |
SONAR-9156 apply org’s default visibility on first analysis of project
7 files changed, 64 insertions, 4 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java index 0fc79499f1c..a6513bd16b1 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationDao.java @@ -113,6 +113,10 @@ public class OrganizationDao implements Dao { getMapper(dbSession).updateDefaultGroupId(uuid, requireNonNull(defaultGroupId, "Default group id cannot be null"), system2.now()); } + public boolean getNewProjectPrivate(DbSession dbSession, OrganizationDto organization) { + return getMapper(dbSession).selectNewProjectPrivateByUuid(organization.getUuid()); + } + public int update(DbSession dbSession, OrganizationDto organization) { checkDto(organization); organization.setUpdatedAt(system2.now()); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java index 1a8d78a5744..fb024e3c934 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMapper.java @@ -53,6 +53,8 @@ public interface OrganizationMapper { Integer selectDefaultGroupIdByUuid(@Param("uuid") String uuid); + boolean selectNewProjectPrivateByUuid(@Param("uuid") String uuid); + /** * Update the organization with UUID specified by {@link OrganizationDto#getUuid()}. * <p> diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml index d8b676988d2..c9394628103 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMapper.xml @@ -187,6 +187,13 @@ org.uuid = #{uuid, jdbcType=VARCHAR} </select> + <select id="selectNewProjectPrivateByUuid" resultType="Boolean"> + select org.new_project_private as "newProjectPrivate" + from organizations org + where + org.uuid = #{uuid, jdbcType=VARCHAR} + </select> + <insert id="insert" parameterType="map" useGeneratedKeys="false"> insert into organizations ( diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java index 6ec525f0986..632c646a888 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentUpdater.java @@ -99,6 +99,7 @@ public class ComponentUpdater { .setLongName(newComponent.name()) .setScope(Scopes.PROJECT) .setQualifier(newComponent.qualifier()) + .setPrivate(newComponent.isPrivate()) .setCreatedAt(new Date(system2.now())); dbClient.componentDao().insert(session, component); return component; diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/NewComponent.java b/server/sonar-server/src/main/java/org/sonar/server/component/NewComponent.java index aa7c8ce6f44..01b359c686a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/NewComponent.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/NewComponent.java @@ -36,6 +36,7 @@ public class NewComponent { private final String branch; private final String qualifier; private final String name; + private final boolean isPrivate; private NewComponent(NewComponent.Builder builder) { this.organizationUuid = builder.organizationUuid; @@ -43,6 +44,7 @@ public class NewComponent { this.branch = builder.branch; this.qualifier = builder.qualifier; this.name = builder.name; + this.isPrivate = builder.isPrivate; } public static Builder newComponentBuilder() { @@ -70,12 +72,17 @@ public class NewComponent { return qualifier; } + public boolean isPrivate() { + return isPrivate; + } + public static class Builder { private String organizationUuid; private String key; private String branch; private String qualifier = PROJECT; private String name; + private boolean isPrivate = false; private Builder() { // use static factory method newComponentBuilder() @@ -106,6 +113,11 @@ public class NewComponent { return this; } + public Builder setPrivate(boolean isPrivate) { + this.isPrivate = isPrivate; + return this; + } + public NewComponent build() { requireNonNull(organizationUuid, "organization uuid can't be null"); checkComponentKey(key); diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java index 7ba6746ee6d..6a187736aa5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/queue/ReportSubmitter.java @@ -116,12 +116,15 @@ public class ReportSubmitter { throw insufficientPrivilegesException(); } + boolean newProjectPrivate = dbClient.organizationDao().getNewProjectPrivate(dbSession, organization); + NewComponent newProject = newComponentBuilder() .setOrganizationUuid(organization.getUuid()) .setKey(projectKey) .setName(StringUtils.defaultIfBlank(projectName, projectKey)) .setBranch(projectBranch) .setQualifier(Qualifiers.PROJECT) + .setPrivate(newProjectPrivate) .build(); return componentUpdater.create(dbSession, newProject, userId); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java index ff0c96c5efc..afacf907f68 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java @@ -27,6 +27,7 @@ import org.sonar.api.resources.Scopes; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; +import org.sonar.db.organization.OrganizationDto; import org.sonar.db.user.UserDto; import org.sonar.server.es.ProjectIndexer; import org.sonar.server.exceptions.BadRequestException; @@ -67,10 +68,11 @@ public class ComponentUpdaterTest { @Test public void should_persist_and_index_when_creating_project() throws Exception { NewComponent project = NewComponent.newComponentBuilder() - .setKey(DEFAULT_PROJECT_KEY) - .setName(DEFAULT_PROJECT_NAME) - .setOrganizationUuid(db.getDefaultOrganization().getUuid()) - .build(); + .setKey(DEFAULT_PROJECT_KEY) + .setName(DEFAULT_PROJECT_NAME) + .setOrganizationUuid(db.getDefaultOrganization().getUuid()) + .setPrivate(true) + .build(); ComponentDto returned = underTest.create(db.getSession(), project, null); ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); @@ -85,6 +87,7 @@ public class ComponentUpdaterTest { assertThat(loaded.projectUuid()).isEqualTo(loaded.uuid()); assertThat(loaded.moduleUuid()).isNull(); assertThat(loaded.moduleUuidPath()).isEqualTo("." + loaded.uuid() + "."); + assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); assertThat(loaded.getCreatedAt()).isNotNull(); assertThat(db.getDbClient().componentDao().selectOrFailByKey(db.getSession(), DEFAULT_PROJECT_KEY)).isNotNull(); @@ -92,6 +95,34 @@ public class ComponentUpdaterTest { } @Test + public void should_persist_private_flag_true_when_creating_project() throws Exception { + OrganizationDto organization = db.organizations().insert(); + NewComponent project = NewComponent.newComponentBuilder() + .setKey(DEFAULT_PROJECT_KEY) + .setName(DEFAULT_PROJECT_NAME) + .setOrganizationUuid(organization.getUuid()) + .setPrivate(true) + .build(); + ComponentDto returned = underTest.create(db.getSession(), project, null); + ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); + assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); + } + + @Test + public void should_persist_private_flag_false_when_creating_project() throws Exception { + OrganizationDto organization = db.organizations().insert(); + NewComponent project = NewComponent.newComponentBuilder() + .setKey(DEFAULT_PROJECT_KEY) + .setName(DEFAULT_PROJECT_NAME) + .setOrganizationUuid(organization.getUuid()) + .setPrivate(false) + .build(); + ComponentDto returned = underTest.create(db.getSession(), project, null); + ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid()); + assertThat(loaded.isPrivate()).isEqualTo(project.isPrivate()); + } + + @Test public void create_project_with_branch() throws Exception { ComponentDto project = underTest.create(db.getSession(), NewComponent.newComponentBuilder() |