import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.Users.CurrentWsResponse.Permissions;
import static org.sonarqube.ws.Users.CurrentWsResponse.newBuilder;
+import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.APPLICATION;
import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.ORGANIZATION;
+import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.PORTFOLIO;
import static org.sonarqube.ws.Users.CurrentWsResponse.HomepageType.PROJECT;
import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_CURRENT;
private final DefaultOrganizationProvider defaultOrganizationProvider;
private final AvatarResolver avatarResolver;
private final HomepageTypes homepageTypes;
+ private static final String HOMEPAGE_PARAMETER_SHOULD_NOT_BE_NULL = "Homepage parameter should not be null";
public CurrentAction(UserSession userSession, DbClient dbClient, DefaultOrganizationProvider defaultOrganizationProvider,
AvatarResolver avatarResolver, HomepageTypes homepageTypes) {
private void setHomepageParameter(DbSession dbSession, String homepageType, @Nullable String homepageParameter, CurrentWsResponse.Homepage.Builder homepage) {
if (PROJECT.toString().equals(homepageType)) {
- checkState(homepageParameter != null, "Homepage parameter should not be null");
+ checkState(homepageParameter != null, HOMEPAGE_PARAMETER_SHOULD_NOT_BE_NULL);
ComponentDto component = dbClient.componentDao().selectByUuid(dbSession, homepageParameter)
.or(() -> {
throw new IllegalStateException(format("Unknown component '%s' for homepageParameter", homepageParameter));
setNullable(component.getBranch(), homepage::setBranch);
return;
}
+ if (APPLICATION.toString().equals(homepageType) || PORTFOLIO.toString().equals(homepageType)) {
+ checkState(homepageParameter != null, HOMEPAGE_PARAMETER_SHOULD_NOT_BE_NULL);
+ ComponentDto component = dbClient.componentDao().selectByUuid(dbSession, homepageParameter)
+ .or(() -> {
+ throw new IllegalStateException(format("Unknown component '%s' for homepageParameter", homepageParameter));
+ });
+ homepage.setComponent(component.getKey());
+ return;
+ }
if (ORGANIZATION.toString().equals(homepageType)) {
- checkState(homepageParameter != null, "Homepage parameter should not be null");
+ checkState(homepageParameter != null, HOMEPAGE_PARAMETER_SHOULD_NOT_BE_NULL);
OrganizationDto organization = dbClient.organizationDao().selectByUuid(dbSession, homepageParameter)
.orElseThrow(() -> new IllegalStateException(format("Unknown organization '%s' for homepageParameter", homepageParameter)));
homepage.setOrganization(organization.getKey());
/**
* These types in only available on SonarQube
*/
- PROJECTS, ISSUES,
+ PROJECTS, ISSUES, PORTFOLIOS, PORTFOLIO, APPLICATION,
/**
* These types are only available on SonarCloud
*/
private static final String ACTION = "set_homepage";
public static final String PARAM_TYPE = "type";
- private static final String PARAM_ORGANIZATION = "organization";
- private static final String PARAM_COMPONENT = "component";
- private static final String PARAM_BRANCH = "branch";
+ public static final String PARAM_ORGANIZATION = "organization";
+ public static final String PARAM_COMPONENT = "component";
+ public static final String PARAM_BRANCH = "branch";
+ private static final String PARAMETER_REQUIRED = "Type %s requires a parameter '%s'";
private final UserSession userSession;
private final DbClient dbClient;
@Nullable String organizationParameter) {
switch (type) {
case PROJECT:
- checkArgument(isNotBlank(componentParameter), "Type %s requires a parameter '%s'", type.name(), PARAM_COMPONENT);
+ checkArgument(isNotBlank(componentParameter), PARAMETER_REQUIRED, type.name(), PARAM_COMPONENT);
return componentFinder.getByKeyAndOptionalBranch(dbSession, componentParameter, branchParameter).uuid();
+ case PORTFOLIO:
+ case APPLICATION:
+ checkArgument(isNotBlank(componentParameter), PARAMETER_REQUIRED, type.name(), PARAM_COMPONENT);
+ return componentFinder.getByKey(dbSession, componentParameter).uuid();
case ORGANIZATION:
- checkArgument(isNotBlank(organizationParameter), "Type %s requires a parameter '%s'", type.name(), PARAM_ORGANIZATION);
+ checkArgument(isNotBlank(organizationParameter), PARAMETER_REQUIRED, type.name(), PARAM_ORGANIZATION);
return dbClient.organizationDao().selectByKey(dbSession, organizationParameter)
.orElseThrow(() -> new NotFoundException(format("No organizationDto with key '%s'", organizationParameter)))
.getUuid();
+ case PORTFOLIOS:
case PROJECTS:
case ISSUES:
case MY_PROJECTS:
.containsExactly(CurrentWsResponse.HomepageType.MY_PROJECTS);
}
+ @Test
+ public void return_homepage_when_set_to_portfolios() {
+ UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIOS"));
+ userSessionRule.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getHomepage())
+ .extracting(CurrentWsResponse.Homepage::getType)
+ .containsExactly(CurrentWsResponse.HomepageType.PORTFOLIOS);
+ }
+
+ @Test
+ public void return_homepage_when_set_to_a_portfolio() {
+ ComponentDto portfolio = db.components().insertPrivatePortfolio(db.getDefaultOrganization());
+ UserDto user = db.users().insertUser(u -> u.setHomepageType("PORTFOLIO").setHomepageParameter(portfolio.uuid()));
+ userSessionRule.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getHomepage())
+ .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent)
+ .containsExactly(CurrentWsResponse.HomepageType.PORTFOLIO, portfolio.getKey());
+ }
+
+ @Test
+ public void return_homepage_when_set_to_an_application() {
+ ComponentDto application = db.components().insertPrivateApplication(db.getDefaultOrganization());
+ UserDto user = db.users().insertUser(u -> u.setHomepageType("APPLICATION").setHomepageParameter(application.uuid()));
+ userSessionRule.logIn(user);
+
+ CurrentWsResponse response = call();
+
+ assertThat(response.getHomepage())
+ .extracting(CurrentWsResponse.Homepage::getType, CurrentWsResponse.Homepage::getComponent)
+ .containsExactly(CurrentWsResponse.HomepageType.APPLICATION, application.getKey());
+ }
+
@Test
public void return_homepage_when_set_to_a_project() {
ComponentDto project = db.components().insertPrivateProject();
import static org.sonar.server.user.ws.HomepageTypes.Type.ORGANIZATION;
import static org.sonar.server.user.ws.HomepageTypes.Type.PROJECT;
import static org.sonar.server.user.ws.HomepageTypes.Type.PROJECTS;
+import static org.sonar.server.user.ws.SetHomepageAction.PARAM_COMPONENT;
import static org.sonar.server.user.ws.SetHomepageAction.PARAM_TYPE;
public class SetHomepageActionTest {
@Test
public void set_sonarqube_projects_homepage() {
- when(homepageTypes.getTypes()).thenReturn(asList(PROJECT, ORGANIZATION, ISSUES, PROJECTS));
- ws = new WsActionTester(new SetHomepageAction(userSession, dbClient, TestComponentFinder.from(db)));
-
UserDto user = db.users().insertUser();
userSession.logIn(user);
assertThat(actual.getHomepageParameter()).isNullOrEmpty();
}
+ @Test
+ public void set_portfolios_homepage() {
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+
+ ws.newRequest()
+ .setMethod("POST")
+ .setParam(PARAM_TYPE, "PORTFOLIOS")
+ .execute();
+
+ UserDto actual = db.getDbClient().userDao().selectByLogin(db.getSession(), user.getLogin());
+ assertThat(actual).isNotNull();
+ assertThat(actual.getHomepageType()).isEqualTo("PORTFOLIOS");
+ assertThat(actual.getHomepageParameter()).isNullOrEmpty();
+ }
+
+ @Test
+ public void set_portfolio_homepage() {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto portfolio = db.components().insertPrivatePortfolio(organization);
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+
+ ws.newRequest()
+ .setMethod("POST")
+ .setParam(PARAM_TYPE, "PORTFOLIO")
+ .setParam(PARAM_COMPONENT, portfolio.getKey())
+ .execute();
+
+ UserDto actual = db.getDbClient().userDao().selectByLogin(db.getSession(), user.getLogin());
+ assertThat(actual).isNotNull();
+ assertThat(actual.getHomepageType()).isEqualTo("PORTFOLIO");
+ assertThat(actual.getHomepageParameter()).isEqualTo(portfolio.uuid());
+ }
+
+ @Test
+ public void set_application_homepage() {
+ OrganizationDto organization = db.organizations().insert();
+ ComponentDto application = db.components().insertPrivateApplication(organization);
+ UserDto user = db.users().insertUser();
+ userSession.logIn(user);
+
+ ws.newRequest()
+ .setMethod("POST")
+ .setParam(PARAM_TYPE, "APPLICATION")
+ .setParam(PARAM_COMPONENT, application.getKey())
+ .execute();
+
+ UserDto actual = db.getDbClient().userDao().selectByLogin(db.getSession(), user.getLogin());
+ assertThat(actual).isNotNull();
+ assertThat(actual.getHomepageType()).isEqualTo("APPLICATION");
+ assertThat(actual.getHomepageParameter()).isEqualTo(application.uuid());
+ }
+
@Test
public void response_has_no_content() {
UserDto user = db.users().insertUser();
MY_ISSUES = 4;
PROJECTS = 5;
ISSUES = 6;
+ PORTFOLIO = 7;
+ PORTFOLIOS = 8;
+ APPLICATION = 9;
}
message Homepage {