From 511971650ddb2fe1fa4e55e5ab85dea85993db42 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Wed, 31 May 2023 15:13:59 +0200 Subject: [PATCH] SONAR-19337 Add endpoint to validate GitHub provisioning config. (#8414) --- .../alm/client/github/GithubBinding.java | 92 +++++++++++++------ .../org/sonar/auth/github/GitHubSettings.java | 5 + 2 files changed, 70 insertions(+), 27 deletions(-) diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java index 90b77f763de..bb569f0db86 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubBinding.java @@ -21,13 +21,41 @@ package org.sonar.alm.client.github; import com.google.gson.annotations.SerializedName; import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import static org.sonar.alm.client.github.GithubApplicationClient.Repository; public class GithubBinding { private GithubBinding() { - //nothing to do + // nothing to do + } + + public static class GsonApp { + + @SerializedName("installations_count") + private long installationsCount; + + @SerializedName("permissions") + Permissions permissions; + + public GsonApp(long installationsCount, Permissions permissions) { + this.installationsCount = installationsCount; + this.permissions = permissions; + } + + public GsonApp() { + // http://stackoverflow.com/a/18645370/229031 + } + + public Permissions getPermissions() { + return permissions; + } + + public long getInstallationsCount() { + return installationsCount; + } } public static class GsonInstallations { @@ -82,32 +110,6 @@ public class GithubBinding { return account; } - public static class Permissions { - @SerializedName("checks") - String checks; - @SerializedName("members") - String members; - - public Permissions(String checks, String members) { - this.checks = checks; - this.members = members; - } - - public Permissions() { - // even if empty constructor is not required for Gson, it is strongly - // recommended: - // http://stackoverflow.com/a/18645370/229031 - } - - public String getMembers() { - return members; - } - - public String getChecks() { - return checks; - } - } - public static class GsonAccount { @SerializedName("id") long id; @@ -132,6 +134,42 @@ public class GithubBinding { } } + public static class Permissions { + @SerializedName("checks") + String checks; + @SerializedName("members") + String members; + @SerializedName("emails") + String emails; + + public Permissions(@Nullable String checks, @Nullable String members, @Nullable String emails) { + this.checks = checks; + this.members = members; + this.emails = emails; + } + + public Permissions() { + // even if empty constructor is not required for Gson, it is strongly + // recommended: + // http://stackoverflow.com/a/18645370/229031 + } + + @CheckForNull + public String getMembers() { + return members; + } + + @CheckForNull + public String getChecks() { + return checks; + } + + @CheckForNull + public String getEmails() { + return emails; + } + } + public static class GsonRepositorySearch { @SerializedName("total_count") int totalCount; diff --git a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java index cd1e30d010e..26fa5bc9392 100644 --- a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java +++ b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java @@ -64,6 +64,7 @@ public class GitHubSettings { private static final String CATEGORY = "authentication"; private static final String SUBCATEGORY = "github"; + private final Configuration configuration; private final InternalProperties internalProperties; @@ -113,6 +114,10 @@ public class GitHubSettings { return urlWithEndingSlash(configuration.get(API_URL).orElse("")); } + public String apiURLOrDefault() { + return configuration.get(API_URL).map(GitHubSettings::urlWithEndingSlash).orElse(DEFAULT_API_URL); + } + public Set getOrganizations() { return Set.of(configuration.getStringArray(ORGANIZATIONS)); } -- 2.39.5