diff options
author | Antoine Vigneau <antoine.vigneau@sonarsource.com> | 2024-06-11 17:44:45 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-06-17 20:02:35 +0000 |
commit | 078306d53ad53ba38d5d4b06e6e8958a0c2c6595 (patch) | |
tree | cd0ac74f560aac0e2a3720b096239d7519f856c0 /sonar-ws/src | |
parent | 0bdfddeed0bf06255f61c6b59dcfc6d132598e14 (diff) | |
download | sonarqube-078306d53ad53ba38d5d4b06e6e8958a0c2c6595.tar.gz sonarqube-078306d53ad53ba38d5d4b06e6e8958a0c2c6595.zip |
SONAR-22365 Fix SSF-571
Diffstat (limited to 'sonar-ws/src')
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java | 16 | ||||
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java | 6 | ||||
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfiguration.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/githubprovisioning/GithubProvisioningService.java) | 36 | ||||
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfigurationService.java | 98 | ||||
-rw-r--r-- | sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/package-info.java (renamed from sonar-ws/src/main/java/org/sonarqube/ws/client/githubprovisioning/package-info.java) | 2 |
5 files changed, 126 insertions, 32 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java index 4ca293f2dd5..4f7ea365c7b 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/DefaultWsClient.java @@ -34,8 +34,8 @@ import org.sonarqube.ws.client.duplications.DuplicationsService; import org.sonarqube.ws.client.editions.EditionsService; import org.sonarqube.ws.client.emails.EmailsService; import org.sonarqube.ws.client.favorites.FavoritesService; +import org.sonarqube.ws.client.github.configuration.GithubConfigurationService; import org.sonarqube.ws.client.github.provisioning.permissions.GithubPermissionsService; -import org.sonarqube.ws.client.githubprovisioning.GithubProvisioningService; import org.sonarqube.ws.client.gitlab.configuration.GitlabConfigurationService; import org.sonarqube.ws.client.gitlab.synchronization.run.GitlabSynchronizationRunService; import org.sonarqube.ws.client.governancereports.GovernanceReportsService; @@ -144,7 +144,7 @@ class DefaultWsClient implements WsClient { private final SecurityReportsService securityReportsService; private final RegulatoryReportsService regulatoryReportsService; private final SonarLintServerPushService sonarLintPushService; - private final GithubProvisioningService githubProvisioningService; + private final GithubConfigurationService githubConfigurationService; private final GithubPermissionsService githubPermissionsService; private final GitlabConfigurationService gitlabConfigurationService; @@ -207,7 +207,7 @@ class DefaultWsClient implements WsClient { this.securityReportsService = new SecurityReportsService(wsConnector); this.sonarLintPushService = new SonarLintServerPushService(wsConnector); this.regulatoryReportsService = new RegulatoryReportsService(wsConnector); - this.githubProvisioningService = new GithubProvisioningService(wsConnector); + this.githubConfigurationService = new GithubConfigurationService(wsConnector); this.githubPermissionsService = new GithubPermissionsService(wsConnector); this.gitlabConfigurationService = new GitlabConfigurationService(wsConnector); this.gitlabSynchronizationRunService = new GitlabSynchronizationRunService(wsConnector); @@ -290,6 +290,11 @@ class DefaultWsClient implements WsClient { } @Override + public GithubConfigurationService githubConfigurationService() { + return githubConfigurationService; + } + + @Override public GithubPermissionsService githubPermissionsService() { return githubPermissionsService; } @@ -445,11 +450,6 @@ class DefaultWsClient implements WsClient { } @Override - public GithubProvisioningService githubProvisioning() { - return githubProvisioningService; - } - - @Override public SourcesService sources() { return sourcesService; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java index aee175fb7a3..53cd28e86ab 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/WsClient.java @@ -34,8 +34,8 @@ import org.sonarqube.ws.client.duplications.DuplicationsService; import org.sonarqube.ws.client.editions.EditionsService; import org.sonarqube.ws.client.emails.EmailsService; import org.sonarqube.ws.client.favorites.FavoritesService; +import org.sonarqube.ws.client.github.configuration.GithubConfigurationService; import org.sonarqube.ws.client.github.provisioning.permissions.GithubPermissionsService; -import org.sonarqube.ws.client.githubprovisioning.GithubProvisioningService; import org.sonarqube.ws.client.gitlab.configuration.GitlabConfigurationService; import org.sonarqube.ws.client.gitlab.synchronization.run.GitlabSynchronizationRunService; import org.sonarqube.ws.client.governancereports.GovernanceReportsService; @@ -128,6 +128,8 @@ public interface WsClient { FavoritesService favorites(); + GithubConfigurationService githubConfigurationService(); + GithubPermissionsService githubPermissionsService(); GitlabConfigurationService gitlabConfigurationService(); @@ -186,8 +188,6 @@ public interface WsClient { SettingsService settings(); - GithubProvisioningService githubProvisioning(); - SourcesService sources(); SupportService support(); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/githubprovisioning/GithubProvisioningService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfiguration.java index 75780fcacd9..9cf0082d4dc 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/githubprovisioning/GithubProvisioningService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfiguration.java @@ -17,26 +17,22 @@ * 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.sonarqube.ws.client.githubprovisioning; +package org.sonarqube.ws.client.github.configuration; -import org.sonarqube.ws.client.BaseService; -import org.sonarqube.ws.client.PostRequest; -import org.sonarqube.ws.client.WsConnector; +import java.util.Set; -public class GithubProvisioningService extends BaseService { - public GithubProvisioningService(WsConnector wsConnector) { - super(wsConnector, "api/github_provisioning"); - } - - public void enable() { - call( - new PostRequest(path("enable")) - ); - } - - public void disable() { - call( - new PostRequest(path("disable")) - ); - } +public record GithubConfiguration( + boolean enabled, + String clientId, + String clientSecret, + String applicationId, + String privateKey, + boolean synchronizeGroups, + String apiUrl, + String webUrl, + Set<String> allowedOrganizations, + String provisioningType, + boolean allowUsersToSignUp, + boolean provisionProjectVisibility, + boolean userConsentRequiredAfterUpgrade) { } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfigurationService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfigurationService.java new file mode 100644 index 00000000000..2e52a03c2f5 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/GithubConfigurationService.java @@ -0,0 +1,98 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.sonarqube.ws.client.github.configuration; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import java.util.stream.Collectors; +import org.sonarqube.ws.client.BaseService; +import org.sonarqube.ws.client.PatchRequest; +import org.sonarqube.ws.client.PostRequest; +import org.sonarqube.ws.client.WsConnector; +import org.sonarqube.ws.client.WsResponse; + +public class GithubConfigurationService extends BaseService { + + + public GithubConfigurationService(WsConnector wsConnector) { + super(wsConnector, "api/v2/dop-translation/github-configurations"); + } + + public String saveGithubConfiguration(GithubConfiguration config) { + String body = String.format(""" + { + "enabled": "%s", + "clientId": "%s", + "clientSecret": "%s", + "applicationId": "%s", + "privateKey": "%s", + "synchronizeGroups": "%s", + "apiUrl": "%s", + "webUrl": "%s", + "allowedOrganizations": ["%s"], + "provisioningType": "%s", + "allowUsersToSignUp": "%s", + "projectVisibility": "%s", + "userConsentRequiredAfterUpgrade": "%s" + } + """, + config.enabled(), + config.clientId(), + config.clientSecret(), + config.applicationId(), + escapeNewLineChars(config.privateKey()), + config.synchronizeGroups(), + config.apiUrl(), + config.webUrl(), + config.allowedOrganizations().stream().map(Object::toString).collect(Collectors.joining("\", \"")), + config.provisioningType(), + config.allowUsersToSignUp(), + config.provisionProjectVisibility(), + config.userConsentRequiredAfterUpgrade()); + + WsResponse response = call( + new PostRequest(path()).setBody(body) + ); + return new Gson().fromJson(response.content(), JsonObject.class).get("id").getAsString(); + } + + private static String escapeNewLineChars(String key) { + return key.replace("\n", "\\n"); + } + + public void enableAutoProvisioning(String configId) { + setProvisioningMode(configId, "AUTO_PROVISIONING"); + } + + public void disableAutoProvisioning(String configId) { + setProvisioningMode(configId, "JIT"); + } + + private void setProvisioningMode(String configId, String provisioningMode) { + String body = String.format(""" + { + "provisioningType": "%s" + } + """, provisioningMode); + call( + new PatchRequest(path(configId)).setBody(body).setContentType(APPLICATION_MERGE_PATCH_JSON) + ); + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/githubprovisioning/package-info.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/package-info.java index 72d3e7fa447..9721ced584a 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/githubprovisioning/package-info.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/github/configuration/package-info.java @@ -18,6 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package org.sonarqube.ws.client.githubprovisioning; +package org.sonarqube.ws.client.github.configuration; import javax.annotation.ParametersAreNonnullByDefault; |