From d8b2c8a9067db38d9cc141a92ac3208b8c097b10 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Mon, 1 Jun 2020 14:09:59 -0700 Subject: [PATCH 1/3] Switch to using okhttp3 --- pom.xml | 4 ++-- .../plugins/github_branch_source/Connector.java | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index bad8a7da6..9640df239 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ - 2.7.3 + 2.8.0 -SNAPSHOT 2.2.0 8 @@ -46,7 +46,7 @@ org.jenkins-ci.plugins github-api - 1.110 + 1.111.4 diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java index 54ceb481b..e2750df3a 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java @@ -35,9 +35,8 @@ import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; import com.cloudbees.plugins.credentials.domains.DomainRequirement; import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; -import com.squareup.okhttp.Cache; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.OkUrlFactory; +import okhttp3.Cache; +import okhttp3.OkHttpClient; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.AbortException; import hudson.Extension; @@ -80,7 +79,7 @@ import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHubBuilder; import org.kohsuke.github.RateLimitHandler; -import org.kohsuke.github.extras.OkHttpConnector; +import org.kohsuke.github.extras.okhttp3.OkHttpConnector; import static java.util.logging.Level.FINE; @@ -104,6 +103,8 @@ protected boolean removeEldestEntry(Map.Entry eldest) { }; private static final Random ENTROPY = new Random(); private static final String SALT = Long.toHexString(ENTROPY.nextLong()); + private static final OkHttpClient baseClient = new OkHttpClient(); + private Connector() { throw new IllegalAccessError("Utility class"); @@ -374,7 +375,8 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta gb.withEndpoint(apiUrl); gb.withRateLimitHandler(CUSTOMIZED); - OkHttpClient client = new OkHttpClient().setProxy(getProxy(host)); + OkHttpClient.Builder clientBuilder = baseClient.newBuilder(); + clientBuilder.proxy(getProxy(host)); int cacheSize = GitHubSCMSource.getCacheSize(); if (cacheSize > 0) { @@ -396,11 +398,11 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta } if (cacheDir != null) { Cache cache = new Cache(cacheDir, cacheSize * 1024L * 1024L); - client.setCache(cache); + clientBuilder.cache(cache); } } - gb.withConnector(new OkHttpConnector(new OkUrlFactory(client))); + gb.withConnector(new OkHttpConnector(clientBuilder.build())); if (username != null) { gb.withPassword(username, password); From 342952d4f51a6c75a439c62b8a35a0bf08990e71 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 3 Jun 2020 13:59:20 -0700 Subject: [PATCH 2/3] Refactor Connector for better GitHubAppCredentials behavior GitHubAppCredentials could not use the existing Connector.connect() and so created its own GithubBuilder. However, that means that is missing a number of standard settings provided by Connector.connect(), including okhttp, Jenkins proxy settings, and rate limit handling. This change refactors Connector to add an internal createGitHubBuilder() method that returns a GitHubBuilder with those features configured. For simplicity, The returned GitHubBuilder is not cached and also does not cache responses. If there turns out to be a need for it, that behavior can be added later. --- .../github_branch_source/Connector.java | 107 ++++++++++++------ .../GitHubAppCredentials.java | 5 +- 2 files changed, 74 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java index e2750df3a..a024ffd32 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/Connector.java @@ -76,6 +76,7 @@ import org.apache.commons.lang.StringUtils; import org.jenkinsci.plugins.gitclient.GitClient; import org.jenkinsci.plugins.github.config.GitHubServerConfig; +import org.kohsuke.github.GHAppInstallationToken; import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHubBuilder; import org.kohsuke.github.RateLimitHandler; @@ -364,45 +365,10 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta usage.put(hub, count == null ? 1 : Math.max(count + 1, 1)); return hub; } - String host; - try { - host = new URL(apiUrl).getHost(); - } catch (MalformedURLException e) { - throw new IOException("Invalid GitHub API URL: " + apiUrl, e); - } - - GitHubBuilder gb = new GitHubBuilder(); - gb.withEndpoint(apiUrl); - gb.withRateLimitHandler(CUSTOMIZED); - - OkHttpClient.Builder clientBuilder = baseClient.newBuilder(); - clientBuilder.proxy(getProxy(host)); - int cacheSize = GitHubSCMSource.getCacheSize(); - if (cacheSize > 0) { - File cacheBase = new File(jenkins.getRootDir(), - GitHubSCMProbe.class.getName() + ".cache"); - File cacheDir = null; - try { - MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); - sha256.update(apiUrl.getBytes(StandardCharsets.UTF_8)); - sha256.update("::".getBytes(StandardCharsets.UTF_8)); - if (username != null) { - sha256.update(username.getBytes(StandardCharsets.UTF_8)); - } - sha256.update("::".getBytes(StandardCharsets.UTF_8)); - sha256.update(authHash.getBytes(StandardCharsets.UTF_8)); - cacheDir = new File(cacheBase, Base64.encodeBase64URLSafeString(sha256.digest())); - } catch (NoSuchAlgorithmException e) { - // no cache for you mr non-spec compliant JVM - } - if (cacheDir != null) { - Cache cache = new Cache(cacheDir, cacheSize * 1024L * 1024L); - clientBuilder.cache(cache); - } - } + Cache cache = getCache(jenkins, apiUrl, authHash, username); - gb.withConnector(new OkHttpConnector(clientBuilder.build())); + GitHubBuilder gb = createGitHubBuilder(apiUrl, cache); if (username != null) { gb.withPassword(username, password); @@ -416,6 +382,73 @@ public static void checkApiUrlValidity(@Nonnull GitHub gitHub, @CheckForNull Sta } } + /** + * Creates a {@link GitHubBuilder} that can be used to build a {@link GitHub} instance. + * + * This method creates and configures a new {@link GitHubBuilder}. + * This should be used only when {@link #connect(String, StandardCredentials)} cannot be used, + * such as when using {@link GitHubBuilder#withJwtToken(String)} to getting the {@link GHAppInstallationToken}. + * + * This method intentionally does not support caching requests or {@link GitHub} instances. + * + * @param apiUrl the GitHub API URL to be used for the connection + * @return a configured GitHubBuilder instance + * @throws IOException if I/O error occurs + */ + static GitHubBuilder createGitHubBuilder(@Nonnull String apiUrl) throws IOException { + return createGitHubBuilder(apiUrl, null); + } + + @Nonnull + private static GitHubBuilder createGitHubBuilder(@Nonnull String apiUrl, @CheckForNull Cache cache) throws IOException { + String host; + try { + host = new URL(apiUrl).getHost(); + } catch (MalformedURLException e) { + throw new IOException("Invalid GitHub API URL: " + apiUrl, e); + } + + GitHubBuilder gb = new GitHubBuilder(); + gb.withEndpoint(apiUrl); + gb.withRateLimitHandler(CUSTOMIZED); + + OkHttpClient.Builder clientBuilder = baseClient.newBuilder(); + clientBuilder.proxy(getProxy(host)); + if (cache != null) { + clientBuilder.cache(cache); + } + gb.withConnector(new OkHttpConnector(clientBuilder.build())); + return gb; + } + + @CheckForNull + private static Cache getCache(@Nonnull Jenkins jenkins, @Nonnull String apiUrl, @Nonnull String authHash, @CheckForNull String username) { + Cache cache = null; + int cacheSize = GitHubSCMSource.getCacheSize(); + if (cacheSize > 0) { + File cacheBase = new File(jenkins.getRootDir(), + GitHubSCMProbe.class.getName() + ".cache"); + File cacheDir = null; + try { + MessageDigest sha256 = MessageDigest.getInstance("SHA-256"); + sha256.update(apiUrl.getBytes(StandardCharsets.UTF_8)); + sha256.update("::".getBytes(StandardCharsets.UTF_8)); + if (username != null) { + sha256.update(username.getBytes(StandardCharsets.UTF_8)); + } + sha256.update("::".getBytes(StandardCharsets.UTF_8)); + sha256.update(authHash.getBytes(StandardCharsets.UTF_8)); + cacheDir = new File(cacheBase, Base64.encodeBase64URLSafeString(sha256.digest())); + } catch (NoSuchAlgorithmException e) { + // no cache for you mr non-spec compliant JVM + } + if (cacheDir != null) { + cache = new Cache(cacheDir, cacheSize * 1024L * 1024L); + } + } + return cache; + } + public static void release(@CheckForNull GitHub hub) { if (hub == null) { return; diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java index 86da634c1..347a1dcb5 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubAppCredentials.java @@ -107,7 +107,10 @@ public void setOwner(String owner) { static String generateAppInstallationToken(String appId, String appPrivateKey, String apiUrl, String owner) { try { String jwtToken = createJWT(appId, appPrivateKey); - GitHub gitHubApp = new GitHubBuilder().withEndpoint(apiUrl).withJwtToken(jwtToken).build(); + GitHub gitHubApp = Connector + .createGitHubBuilder(apiUrl) + .withJwtToken(jwtToken) + .build(); GHApp app = gitHubApp.getApp(); From 8378bbeba2ee9bd32b94e68b1b02c87e6c6da347 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Wed, 3 Jun 2020 15:18:22 -0700 Subject: [PATCH 3/3] Move to locally controlled build configs --- Jenkinsfile | 6 +++++- pom.xml | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 87a086dad..18cd978f6 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1 +1,5 @@ -buildPlugin(configurations: buildPlugin.recommendedConfigurations()) +buildPlugin(configurations: [ + [ platform: "linux", jdk: "8"], + [ platform: "windows", jdk: "8"], + [ platform: "linux", jdk: "11", jenkins: "2.176.4", javaLevel: "8" ] +]) diff --git a/pom.xml b/pom.xml index 9640df239..5fb766390 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 3.57 + 4.2 github-branch-source