aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorGuillaume Jambet <guillaume.jambet@sonarsource.com>2017-12-14 12:04:25 +0100
committerStas Vilchik <stas.vilchik@sonarsource.com>2018-01-02 10:38:10 +0100
commit56c1dd13a73468dbbdc543f117876f9d574bc761 (patch)
tree56fbb4392d4183042a7593eafd0d6422bcce93d7 /server
parent7b757f90537d99f1863407dfd2667874561d53aa (diff)
downloadsonarqube-56c1dd13a73468dbbdc543f117876f9d574bc761.tar.gz
sonarqube-56c1dd13a73468dbbdc543f117876f9d574bc761.zip
SONAR-10182 Add api/users/current homepage persistence
Diffstat (limited to 'server')
-rw-r--r--server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl6
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java22
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml16
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java20
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsers.java51
-rw-r--r--server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java1
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest.java61
-rw-r--r--server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java2
-rw-r--r--server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest/initial.sql19
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java53
-rw-r--r--server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java42
12 files changed, 277 insertions, 18 deletions
diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
index 3b335d6ccd6..faf1bc7731d 100644
--- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
+++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
@@ -1,3 +1,5 @@
+-- noinspection SqlNoDataSourceInspectionForFile
+
CREATE TABLE "ORGANIZATIONS" (
"UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
"KEE" VARCHAR(32) NOT NULL,
@@ -457,7 +459,9 @@ CREATE TABLE "USERS" (
"USER_LOCAL" BOOLEAN,
"ONBOARDED" BOOLEAN NOT NULL,
"CREATED_AT" BIGINT,
- "UPDATED_AT" BIGINT
+ "UPDATED_AT" BIGINT,
+ "HOMEPAGE_TYPE" VARCHAR(40),
+ "HOMEPAGE_VALUE" VARCHAR(40)
);
CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN");
CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT");
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java
index 1213e5bc0b7..50e3207c727 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java
@@ -48,6 +48,8 @@ public class UserDto {
private String salt;
private Long createdAt;
private Long updatedAt;
+ private String homepageType;
+ private String homepageValue;
private boolean local = true;
private boolean root = false;
private boolean onboarded = false;
@@ -202,6 +204,26 @@ public class UserDto {
return this;
}
+ @CheckForNull
+ public String getHomepageType() {
+ return homepageType;
+ }
+
+ public UserDto setHomepageType(@Nullable String homepageType) {
+ this.homepageType = homepageType;
+ return this;
+ }
+
+ @CheckForNull
+ public String getHomepageValue() {
+ return homepageValue;
+ }
+
+ public UserDto setHomepageValue(@Nullable String homepageValue) {
+ this.homepageValue = homepageValue;
+ return this;
+ }
+
public boolean isRoot() {
return root;
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
index 9e4c671343e..7e16d4ef582 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
@@ -18,7 +18,9 @@
u.is_root as "root",
u.onboarded as "onboarded",
u.created_at as "createdAt",
- u.updated_at as "updatedAt"
+ u.updated_at as "updatedAt",
+ u.homepage_type as "homepageType",
+ u.homepage_value as "homepageValue"
</sql>
<select id="selectByLogin" parameterType="String" resultType="User">
@@ -159,7 +161,9 @@
is_root,
onboarded,
created_at,
- updated_at
+ updated_at,
+ homepage_type,
+ homepage_value
) values (
#{user.login,jdbcType=VARCHAR},
#{user.name,jdbcType=VARCHAR},
@@ -174,7 +178,9 @@
#{user.root,jdbcType=BOOLEAN},
#{user.onboarded,jdbcType=BOOLEAN},
#{now,jdbcType=BIGINT},
- #{now,jdbcType=BIGINT}
+ #{now,jdbcType=BIGINT},
+ #{user.homepageType,jdbcType=VARCHAR},
+ #{user.homepageValue,jdbcType=VARCHAR}
)
</insert>
@@ -190,7 +196,9 @@
onboarded = #{user.onboarded, jdbcType=BOOLEAN},
salt = #{user.salt, jdbcType=VARCHAR},
crypted_password = #{user.cryptedPassword, jdbcType=BIGINT},
- updated_at = #{now, jdbcType=BIGINT}
+ updated_at = #{now, jdbcType=BIGINT},
+ homepage_type = #{user.homepageType, jdbcType=VARCHAR},
+ homepage_value = #{user.homepageValue, jdbcType=VARCHAR}
where
login = #{user.login, jdbcType=VARCHAR}
</update>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java
index 773c0eb1fa4..448b17a4086 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java
@@ -321,7 +321,9 @@ public class UserDaoTest {
.setExternalIdentityProvider("github")
.setLocal(true)
.setCreatedAt(date)
- .setUpdatedAt(date);
+ .setUpdatedAt(date)
+ .setHomepageType("project")
+ .setHomepageValue("OB1");
underTest.insert(db.getSession(), userDto);
db.getSession().commit();
@@ -340,6 +342,8 @@ public class UserDaoTest {
assertThat(user.getExternalIdentityProvider()).isEqualTo("github");
assertThat(user.isLocal()).isTrue();
assertThat(user.isRoot()).isFalse();
+ assertThat(user.getHomepageType()).isEqualTo("project");
+ assertThat(user.getHomepageValue()).isEqualTo("OB1");
}
@Test
@@ -364,7 +368,9 @@ public class UserDaoTest {
.setCryptedPassword("abcde")
.setExternalIdentity("johngithub")
.setExternalIdentityProvider("github")
- .setLocal(false);
+ .setLocal(false)
+ .setHomepageType("project")
+ .setHomepageValue("OB1");
underTest.update(db.getSession(), userUpdate);
UserDto reloaded = underTest.selectByLogin(db.getSession(), user.getLogin());
@@ -382,6 +388,8 @@ public class UserDaoTest {
assertThat(reloaded.getExternalIdentityProvider()).isEqualTo("github");
assertThat(reloaded.isLocal()).isFalse();
assertThat(reloaded.isRoot()).isFalse();
+ assertThat(reloaded.getHomepageType()).isEqualTo("project");
+ assertThat(reloaded.getHomepageValue()).isEqualTo("OB1");
}
@Test
@@ -403,6 +411,8 @@ public class UserDaoTest {
assertThat(userReloaded.getExternalIdentityProvider()).isNull();
assertThat(userReloaded.isRoot()).isFalse();
assertThat(userReloaded.getUpdatedAt()).isEqualTo(NOW);
+ assertThat(userReloaded.getHomepageType()).isNull();
+ assertThat(userReloaded.getHomepageValue()).isNull();
assertThat(underTest.selectUserById(session, otherUser.getId())).isNotNull();
}
@@ -420,7 +430,9 @@ public class UserDaoTest {
.setActive(true)
.setScmAccounts("\nma\nmarius33\n")
.setSalt("79bd6a8e79fb8c76ac8b121cc7e8e11ad1af8365")
- .setCryptedPassword("650d2261c98361e2f67f90ce5c65a95e7d8ea2fg"));
+ .setCryptedPassword("650d2261c98361e2f67f90ce5c65a95e7d8ea2fg")
+ .setHomepageType("project")
+ .setHomepageValue("OB1"));
UserDto user2 = db.users().insertUser();
underTest.setRoot(session, user2.getLogin(), true);
@@ -436,6 +448,8 @@ public class UserDaoTest {
assertThat(dto.isRoot()).isFalse();
assertThat(dto.getCreatedAt()).isEqualTo(user1.getCreatedAt());
assertThat(dto.getUpdatedAt()).isEqualTo(user1.getUpdatedAt());
+ assertThat(dto.getHomepageType()).isEqualTo("project");
+ assertThat(dto.getHomepageValue()).isEqualTo("OB1");
dto = underTest.selectOrFailByLogin(session, user2.getLogin());
assertThat(dto.isRoot()).isTrue();
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsers.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsers.java
new file mode 100644
index 00000000000..b1c2d83da86
--- /dev/null
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsers.java
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.platform.db.migration.version.v70;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class AddHomepageToUsers extends DdlChange {
+
+ public AddHomepageToUsers(Database db) {
+ super(db);
+ }
+
+ @Override
+ public void execute(Context context) throws SQLException {
+ context.execute(new AddColumnsBuilder(getDialect(), "users")
+ .addColumn(newVarcharColumnDefBuilder()
+ .setColumnName("homepage_type")
+ .setIsNullable(true)
+ .setLimit(40)
+ .build())
+ .addColumn(newVarcharColumnDefBuilder()
+ .setColumnName("homepage_value")
+ .setIsNullable(true)
+ .setLimit(40)
+ .build())
+ .build());
+ }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java
index a389efd9709..9a244e1d4c6 100644
--- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java
+++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70.java
@@ -50,6 +50,7 @@ public class DbVersion70 implements DbVersion {
.add(1920, "Read 'sonar.qualitygate' setting and set the value to default organization", ReadGlobalSonarQualityGateSettingToDefaultOrg.class)
.add(1921, "Delete 'sonar.qualitygate' setting at global level", DeleteGlobalSonarQualityGateSetting.class)
.add(1922, "Make ORGANIZATIONS.DEFAULT_QUALITY_GATE_UUID not nullable", SetDefaultQualityGateUuidAsNotNullableInOrganizations.class)
+ .add(1923, "Add users.homepage_type and users.homepage_value", AddHomepageToUsers.class)
;
}
}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest.java
new file mode 100644
index 00000000000..5114bbd547e
--- /dev/null
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest.java
@@ -0,0 +1,61 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v70;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.BOOLEAN;
+import static java.sql.Types.VARCHAR;
+import static org.junit.rules.ExpectedException.none;
+import static org.sonar.db.CoreDbTester.createForSchema;
+
+public class AddHomepageToUsersTest {
+
+ @Rule
+ public final CoreDbTester dbTester = createForSchema(AddHomepageToUsersTest.class, "initial.sql");
+
+ @Rule
+ public ExpectedException expectedException = none();
+
+ private AddHomepageToUsers underTest = new AddHomepageToUsers(dbTester.database());
+
+ @Test
+ public void column_is_added_to_table() throws SQLException {
+ underTest.execute();
+
+ dbTester.assertColumnDefinition("users", "homepage_type", VARCHAR, 40, true);
+ dbTester.assertColumnDefinition("users", "homepage_value", VARCHAR, 40, true);
+ }
+
+ @Test
+ public void migration_is_not_reentrant() throws SQLException {
+ underTest.execute();
+
+ expectedException.expect(IllegalStateException.class);
+
+ underTest.execute();
+ }
+
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java
index 010a5befb63..bbb8177639d 100644
--- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java
+++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v70/DbVersion70Test.java
@@ -35,7 +35,7 @@ public class DbVersion70Test {
@Test
public void verify_migration_count() {
- verifyMigrationCount(underTest, 23);
+ verifyMigrationCount(underTest, 24);
}
}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest/initial.sql
new file mode 100644
index 00000000000..8d6baaacafa
--- /dev/null
+++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v70/AddHomepageToUsersTest/initial.sql
@@ -0,0 +1,19 @@
+CREATE TABLE "USERS" (
+ "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+ "LOGIN" VARCHAR(255),
+ "NAME" VARCHAR(200),
+ "EMAIL" VARCHAR(100),
+ "CRYPTED_PASSWORD" VARCHAR(40),
+ "SALT" VARCHAR(40),
+ "ACTIVE" BOOLEAN DEFAULT TRUE,
+ "SCM_ACCOUNTS" VARCHAR(4000),
+ "EXTERNAL_IDENTITY" VARCHAR(255),
+ "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100),
+ "IS_ROOT" BOOLEAN NOT NULL,
+ "USER_LOCAL" BOOLEAN,
+ "ONBOARDED" BOOLEAN NOT NULL,
+ "CREATED_AT" BIGINT,
+ "UPDATED_AT" BIGINT
+);
+CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN");
+CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT");
diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java
index 0e931ad7b2a..65e741f1c78 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/CurrentAction.java
@@ -21,26 +21,32 @@ package org.sonar.server.user.ws;
import java.util.Collection;
import java.util.List;
-import java.util.stream.Collectors;
+import javax.annotation.CheckForNull;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService.NewController;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.OrganizationPermission;
import org.sonar.db.user.UserDto;
import org.sonar.server.issue.ws.AvatarResolver;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Users.CurrentWsResponse;
-import org.sonarqube.ws.Users.CurrentWsResponse.Homepage;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.emptyToNull;
+import static java.lang.String.format;
import static java.util.Collections.singletonList;
+import static java.util.stream.Collectors.toList;
+import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.sonar.core.util.Protobuf.setNullable;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.MY_PROJECTS;
+import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.ORGANIZATION;
import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.PROJECT;
import static org.sonarqube.ws.Users.CurrentWsResponse.Permissions;
import static org.sonarqube.ws.Users.CurrentWsResponse.newBuilder;
@@ -52,7 +58,8 @@ public class CurrentAction implements UsersWsAction {
private final DefaultOrganizationProvider defaultOrganizationProvider;
private final AvatarResolver avatarResolver;
- public CurrentAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider, AvatarResolver avatarResolver) {
+ public CurrentAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider,
+ AvatarResolver avatarResolver) {
this.userSession = userSession;
this.dbClient = dbClient;
this.defaultOrganizationProvider = defaultOrganizationProvider;
@@ -98,7 +105,7 @@ public class CurrentAction implements UsersWsAction {
.addAllGroups(groups)
.addAllScmAccounts(user.getScmAccountsAsList())
.setPermissions(Permissions.newBuilder().addAllGlobal(getGlobalPermissions()).build())
- .setHomepage(Homepage.newBuilder().setType(PROJECT).setValue("project-key").build())
+ .setHomepage(findHomepageFor(dbSession, user))
.setShowOnboardingTutorial(!user.isOnboarded());
setNullable(emptyToNull(user.getEmail()), builder::setEmail);
setNullable(emptyToNull(user.getEmail()), u -> builder.setAvatar(avatarResolver.create(user)));
@@ -112,6 +119,42 @@ public class CurrentAction implements UsersWsAction {
return OrganizationPermission.all()
.filter(permission -> userSession.hasPermission(permission, defaultOrganizationUuid))
.map(OrganizationPermission::getKey)
- .collect(Collectors.toList());
+ .collect(toList());
}
+
+ private CurrentWsResponse.Homepage findHomepageFor(DbSession dbSession, UserDto user) {
+ if (user.getHomepageType() == null) {
+ return defaultHomepageOf();
+ }
+ String homepageValue = getHomepageValue(dbSession, user.getHomepageType(), user.getHomepageValue());
+ CurrentWsResponse.Homepage.Builder homepage = CurrentWsResponse.Homepage.newBuilder()
+ .setType(CurrentWsResponse.HomepageType.valueOf(user.getHomepageType()));
+ setNullable(homepageValue, homepage::setValue);
+ return homepage.build();
+ }
+
+ @CheckForNull
+ private String getHomepageValue(DbSession dbSession, String homepageType, String homepageValue) {
+ if (PROJECT.toString().equals(homepageType)) {
+ return dbClient.componentDao().selectByUuid(dbSession, homepageValue)
+ .transform(ComponentDto::getKey)
+ .or(() -> {
+ throw new IllegalStateException(format("Unknown component '%s' for homepageValue", homepageValue));
+ });
+ }
+ if (ORGANIZATION.toString().equals(homepageType)) {
+ return dbClient.organizationDao().selectByUuid(dbSession, homepageValue)
+ .map(OrganizationDto::getKey)
+ .orElseThrow(() -> new IllegalStateException(format("Unknown organization '%s' for homepageValue", homepageValue)));
+ }
+ return null;
+ }
+
+ // Default WIP implementation to be done in SONAR-10185
+ private static CurrentWsResponse.Homepage defaultHomepageOf() {
+ return CurrentWsResponse.Homepage.newBuilder()
+ .setType(MY_PROJECTS)
+ .build();
+ }
+
}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json
index 8b373a028a2..64942a33c81 100644
--- a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json
+++ b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/current-example.json
@@ -15,6 +15,6 @@
},
"homepage": {
"type": "PROJECT",
- "value": "project-key"
+ "value": "death-star-key"
}
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java
index 39c4c96d0ab..f075450f0b8 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CurrentActionTest.java
@@ -26,6 +26,7 @@ import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.issue.ws.AvatarResolverImpl;
import org.sonar.server.organization.DefaultOrganizationProvider;
@@ -42,6 +43,7 @@ import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS;
import static org.sonar.db.permission.OrganizationPermission.SCAN;
import static org.sonar.db.user.GroupTesting.newGroupDto;
import static org.sonar.test.JsonAssert.assertJson;
+import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.MY_PROJECTS;
public class CurrentActionTest {
@Rule
@@ -65,16 +67,23 @@ public class CurrentActionTest {
.setExternalIdentity("obiwan")
.setExternalIdentityProvider("sonarqube")
.setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket"))
- .setOnboarded(false));
+ .setOnboarded(false)
+ .setHomepageType("MY_PROJECTS"));
userSessionRule.logIn("obiwan.kenobi");
CurrentWsResponse response = call();
assertThat(response)
- .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::getEmail, CurrentWsResponse::getAvatar, CurrentWsResponse::getLocal,
+ .extracting(CurrentWsResponse::getIsLoggedIn, CurrentWsResponse::getLogin, CurrentWsResponse::getName, CurrentWsResponse::getEmail, CurrentWsResponse::getAvatar,
+ CurrentWsResponse::getLocal,
CurrentWsResponse::getExternalIdentity, CurrentWsResponse::getExternalProvider, CurrentWsResponse::getScmAccountsList, CurrentWsResponse::getShowOnboardingTutorial)
.containsExactly(true, "obiwan.kenobi", "Obiwan Kenobi", "obiwan.kenobi@starwars.com", "f5aa64437a1821ffe8b563099d506aef", true, "obiwan", "sonarqube",
newArrayList("obiwan:github", "obiwan:bitbucket"), true);
+
+ assertThat(response.getHomepage()).isNotNull();
+ assertThat(response.getHomepage()).extracting(CurrentWsResponse.Homepage::getType)
+ .containsExactly(MY_PROJECTS);
+
}
@Test
@@ -185,10 +194,14 @@ public class CurrentActionTest {
.setExternalIdentity("obiwan.kenobi")
.setExternalIdentityProvider("sonarqube")
.setScmAccounts(newArrayList("obiwan:github", "obiwan:bitbucket"))
- .setOnboarded(true));
+ .setOnboarded(true)
+ .setHomepageType("PROJECT")
+ .setHomepageValue("UUID-of-the-death-star"));
db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Jedi")), obiwan);
db.users().insertMember(db.users().insertGroup(newGroupDto().setName("Rebel")), obiwan);
+ db.components().insertPublicProject(u->u.setUuid("UUID-of-the-death-star"), u->u.setDbKey("death-star-key"));
+
String response = ws.newRequest().execute().getInput();
assertJson(response).isSimilarTo(getClass().getResource("current-example.json"));
@@ -211,4 +224,27 @@ public class CurrentActionTest {
return ws.newRequest().executeProtobuf(CurrentWsResponse.class);
}
+ @Test
+ public void fail_with_ISE_when_user_homepage_project_does_not_exist_in_db() {
+ UserDto user = db.users().insertUser(u -> u.setHomepageType("PROJECT").setHomepageValue("not-existing-project-uuid"));
+ userSessionRule.logIn(user.getLogin());
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Unknown component 'not-existing-project-uuid' for homepageValue");
+
+ call();
+ }
+
+ @Test
+ public void fail_with_ISE_when_user_homepage_organization_does_not_exist_in_db() {
+ UserDto user = db.users().insertUser(u -> u.setHomepageType("ORGANIZATION").setHomepageValue("not-existing-organization-uuid"));
+ userSessionRule.logIn(user.getLogin());
+
+ expectedException.expect(IllegalStateException.class);
+ expectedException.expectMessage("Unknown organization 'not-existing-organization-uuid' for homepageValue");
+
+ call();
+ }
+
+
}