Skip to content

Commit

Permalink
Merge pull request #223 from bitwiseman/okhttp3
Browse files Browse the repository at this point in the history
OkHttp3
  • Loading branch information
bitwiseman authored Jun 4, 2020
2 parents a73a57f + 8378bbe commit b012689
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 45 deletions.
6 changes: 5 additions & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -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" ]
])
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.57</version>
<version>4.2</version>
<relativePath />
</parent>
<artifactId>github-branch-source</artifactId>
Expand All @@ -21,7 +21,7 @@
</licenses>

<properties>
<revision>2.7.3</revision>
<revision>2.8.0</revision>
<changelist>-SNAPSHOT</changelist>
<hpi.compatibleSinceVersion>2.2.0</hpi.compatibleSinceVersion>
<java.level>8</java.level>
Expand All @@ -46,7 +46,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.110</version>
<version>1.111.4</version>
</dependency>
<!-- TODO: after upgrading jenkins.version >= 2.171, migrate dependency -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,10 +76,11 @@
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;
import org.kohsuke.github.extras.OkHttpConnector;
import org.kohsuke.github.extras.okhttp3.OkHttpConnector;

import static java.util.logging.Level.FINE;

Expand All @@ -104,6 +104,8 @@ protected boolean removeEldestEntry(Map.Entry<String,Long> 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");
Expand Down Expand Up @@ -363,44 +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 client = new OkHttpClient().setProxy(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);
client.setCache(cache);
}
}
Cache cache = getCache(jenkins, apiUrl, authHash, username);

gb.withConnector(new OkHttpConnector(new OkUrlFactory(client)));
GitHubBuilder gb = createGitHubBuilder(apiUrl, cache);

if (username != null) {
gb.withPassword(username, password);
Expand All @@ -414,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit b012689

Please sign in to comment.