]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7478 WS api/users/identity_providers return external identity providers
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 29 Mar 2016 14:34:18 +0000 (16:34 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 30 Mar 2016 10:18:13 +0000 (12:18 +0200)
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java [new file with mode: 0644]
sonar-ws/src/main/protobuf/ws-users.proto [new file with mode: 0644]

index f5d88c657fc269ad7aa1bd276e52df955e394950..3bebe3a0f7923589e57efcc0c6e26267e683d47f 100644 (file)
@@ -294,6 +294,7 @@ import org.sonar.server.user.index.UserIndexDefinition;
 import org.sonar.server.user.index.UserIndexer;
 import org.sonar.server.user.ws.CurrentAction;
 import org.sonar.server.user.ws.FavoritesWs;
+import org.sonar.server.user.ws.IdentityProvidersAction;
 import org.sonar.server.user.ws.UserJsonWriter;
 import org.sonar.server.user.ws.UserPropertiesWs;
 import org.sonar.server.user.ws.UsersWs;
@@ -524,6 +525,7 @@ public class PlatformLevel4 extends PlatformLevel {
       CurrentAction.class,
       org.sonar.server.user.ws.SearchAction.class,
       org.sonar.server.user.ws.GroupsAction.class,
+      IdentityProvidersAction.class,
       FavoritesWs.class,
       UserPropertiesWs.class,
       UserIndexDefinition.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java
new file mode 100644 (file)
index 0000000..48edb23
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.user.ws;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import javax.annotation.Nonnull;
+import org.sonar.api.server.authentication.Display;
+import org.sonar.api.server.authentication.IdentityProvider;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.authentication.IdentityProviderRepository;
+import org.sonarqube.ws.WsUsers;
+import org.sonarqube.ws.WsUsers.IdentityProvidersWsResponse;
+
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class IdentityProvidersAction implements UsersWsAction {
+  private final IdentityProviderRepository identityProviderRepository;
+
+  public IdentityProvidersAction(IdentityProviderRepository identityProviderRepository) {
+    this.identityProviderRepository = identityProviderRepository;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    context.createAction("identity_providers")
+      .setDescription("List the external identity providers")
+      .setResponseExample(getClass().getResource("identity_providers-example.json"))
+      .setSince("5.5")
+      .setInternal(true)
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    writeProtobuf(buildResponse(), request, response);
+  }
+
+  private IdentityProvidersWsResponse buildResponse() {
+    IdentityProvidersWsResponse.Builder response = IdentityProvidersWsResponse.newBuilder();
+    response.addAllIdentityProviders(Lists.transform(identityProviderRepository.getAllEnabledAndSorted(), IdentityProviderToWsResponse.INSTANCE));
+    return response.build();
+  }
+
+  private enum IdentityProviderToWsResponse implements Function<IdentityProvider, WsUsers.IdentityProvider> {
+    INSTANCE;
+
+    @Override
+    public WsUsers.IdentityProvider apply(@Nonnull IdentityProvider input) {
+      WsUsers.IdentityProvider.Builder builder = WsUsers.IdentityProvider.newBuilder()
+        .setKey(input.getKey())
+        .setName(input.getName());
+      Display display = input.getDisplay();
+      builder
+        .setIconPath(display.getIconPath())
+        .setBackgroundColor(display.getBackgroundColor());
+
+      return builder.build();
+    }
+  }
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json
new file mode 100644 (file)
index 0000000..06cb431
--- /dev/null
@@ -0,0 +1,16 @@
+{
+  "identityProviders": [
+    {
+      "key": "bitbucket",
+      "name": "Bitbucket",
+      "iconPath": "/static/authbitbucket/bitbucket.svg",
+      "backgroundColor": "#205081"
+    },
+    {
+      "key": "github",
+      "name": "Github",
+      "iconPath": "/static/authgithub/github.svg",
+      "backgroundColor": "#444444"
+    }
+  ]
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java
new file mode 100644 (file)
index 0000000..54b8d1e
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact 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.user.ws;
+
+import java.io.IOException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.authentication.Display;
+import org.sonar.api.server.authentication.IdentityProvider;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.authentication.IdentityProviderRepositoryRule;
+import org.sonar.server.authentication.TestIdentityProvider;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class IdentityProvidersActionTest {
+  static IdentityProvider GITHUB = new TestIdentityProvider()
+    .setKey("github")
+    .setName("Github")
+    .setDisplay(Display.builder()
+      .setIconPath("/static/authgithub/github.svg")
+      .setBackgroundColor("#444444")
+      .build())
+    .setEnabled(true);
+
+  static IdentityProvider BITBUCKET = new TestIdentityProvider()
+    .setKey("bitbucket")
+    .setName("Bitbucket")
+    .setDisplay(Display.builder()
+      .setIconPath("/static/authbitbucket/bitbucket.svg")
+      .setBackgroundColor("#205081")
+      .build())
+    .setEnabled(true);
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule()
+    .addIdentityProvider(GITHUB)
+    .addIdentityProvider(BITBUCKET);
+
+  WsActionTester ws = new WsActionTester(new IdentityProvidersAction(identityProviderRepository));
+
+  @Test
+  public void json_example() throws IOException {
+    String response = ws.newRequest().execute().getInput();
+
+    assertJson(response).isSimilarTo(getClass().getResource("identity_providers-example.json"));
+  }
+
+  @Test
+  public void ws_properties() {
+    WebService.Action webService = ws.getDef();
+
+    assertThat(webService.key()).isEqualTo("identity_providers");
+    assertThat(webService.responseExampleAsString()).isNotEmpty();
+    assertThat(webService.since()).isEqualTo("5.5");
+    assertThat(webService.isInternal()).isTrue();
+
+  }
+}
diff --git a/sonar-ws/src/main/protobuf/ws-users.proto b/sonar-ws/src/main/protobuf/ws-users.proto
new file mode 100644 (file)
index 0000000..20b6fff
--- /dev/null
@@ -0,0 +1,37 @@
+// SonarQube, open source software quality management tool.
+// Copyright (C) 2008-2015 SonarSource
+// mailto:contact AT sonarsource DOT com
+//
+// SonarQube 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.
+//
+// SonarQube 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.
+
+syntax = "proto2";
+
+package sonarqube.ws.users;
+
+option java_package = "org.sonarqube.ws";
+option java_outer_classname = "WsUsers";
+option optimize_for = SPEED;
+
+// WS api/users/identity_providers
+message IdentityProvidersWsResponse {
+  repeated IdentityProvider identityProviders = 1;
+}
+
+message IdentityProvider {
+  optional string key = 1;
+  optional string name = 2;
+  optional string iconPath = 3;
+  optional string backgroundColor = 4;
+}