/** * Configures {@linkplain #withConnector(HttpConnector) connector} * that uses HTTP library in JRE but use a specific proxy, instead of * the system default one. */ public GitHubBuilder withProxy(final Proxy p) { return withConnector(new HttpConnector() { public HttpURLConnection connect(URL url) throws IOException { return (HttpURLConnection) url.openConnection(p); } }); }
/** * Configures {@linkplain #withConnector(HttpConnector) connector} * that uses HTTP library in JRE but use a specific proxy, instead of * the system default one. */ public GitHubBuilder withProxy(final Proxy p) { return withConnector(new ImpatientHttpConnector(new HttpConnector() { public HttpURLConnection connect(URL url) throws IOException { return (HttpURLConnection) url.openConnection(p); } })); }
/** * Configures {@linkplain #withConnector(HttpConnector) connector} * that uses HTTP library in JRE but use a specific proxy, instead of * the system default one. */ public GitHubBuilder withProxy(final Proxy p) { return withConnector(new ImpatientHttpConnector(new HttpConnector() { public HttpURLConnection connect(URL url) throws IOException { return (HttpURLConnection) url.openConnection(p); } })); }
/** * An offline-only {@link GitHub} useful for parsing event notification from an unknown source. * * All operations that require a connection will fail. * * @return An offline-only {@link GitHub}. */ public static GitHub offline() { try { return new GitHubBuilder() .withEndpoint("https://api.github.invalid") .withConnector(HttpConnector.OFFLINE) .build(); } catch (IOException e) { throw new IllegalStateException("The offline implementation constructor should not connect", e); } }
private void retrieveRepositoryNames() throws IOException { System.err.println("Retrieving GitHub repository names..."); Cache cache = new Cache(GITHUB_API_CACHE, 20L*1024*1024); // 20 MB cache github = new GitHubBuilder().withConnector(new OkHttp3Connector(new OkUrlFactory(new OkHttpClient.Builder().cache(cache).build()))).withPassword(GITHUB_API_USERNAME, GITHUB_API_PASSWORD).build(); List<String> ret = new ArrayList<>(); for (GHRepository repo : github.getOrganization("jenkinsci").listRepositories().withPageSize(100)) { ret.add(repo.getHtmlUrl().toString()); } Files.write(GITHUB_REPO_LIST.toPath(), ret); }
public Future<Snapshot> takeSnapshot(String organizationName, Date snapshotDate) throws IOException { GitHubBuilder builder = new GitHubBuilder(); if (StringUtils.isNotEmpty(token)) { builder.withOAuthToken(token); } else if (StringUtils.isNotEmpty(login) && StringUtils.isNotEmpty(password)) { builder.withPassword(login, password); } else { logger.error("GitHub credentials not found, proceeding unauthenticated. That will enforce 60 requests per hour limit."); } GitHub gitHub = builder.withConnector(new OkHttpConnector(new OkUrlFactory(httpClient))).build(); return pool.submit(new TakeSnapshotTask(gitHub, organizationName, scorer, snapshotDate)); }
/** * An offline-only {@link GitHub} useful for parsing event notification from an unknown source. * * All operations that require a connection will fail. * * @return An offline-only {@link GitHub}. */ public static GitHub offline() { try { return new GitHubBuilder() .withEndpoint("https://api.github.invalid") .withConnector(HttpConnector.OFFLINE) .build(); } catch (IOException e) { throw new IllegalStateException("The offline implementation constructor should not connect", e); } }
.withConnector(new HttpConnectorWithJenkinsProxy());
private static GitHubBuilder getBuilder(Item context, String serverAPIUrl, String credentialsId) { GitHubBuilder builder = new GitHubBuilder() .withEndpoint(serverAPIUrl) .withConnector(new HttpConnectorWithJenkinsProxy()); String contextName = context == null ? "(Jenkins.instance)" : context.getFullDisplayName(); if (StringUtils.isEmpty(credentialsId)) { LOGGER.log(Level.WARNING, "credentialsId not set for context {0}, using anonymous connection", contextName); return builder; } StandardCredentials credentials = Ghprb.lookupCredentials(context, credentialsId, serverAPIUrl); if (credentials == null) { LOGGER.log(Level.SEVERE, "Failed to look up credentials for context {0} using id: {1}", new Object[] {contextName, credentialsId}); } else if (credentials instanceof StandardUsernamePasswordCredentials) { LOGGER.log(Level.FINEST, "Using username/password for context {0}", contextName); StandardUsernamePasswordCredentials upCredentials = (StandardUsernamePasswordCredentials) credentials; builder.withPassword(upCredentials.getUsername(), upCredentials.getPassword().getPlainText()); } else if (credentials instanceof StringCredentials) { LOGGER.log(Level.FINEST, "Using OAuth token for context {0}", contextName); StringCredentials tokenCredentials = (StringCredentials) credentials; builder.withOAuthToken(tokenCredentials.getSecret().getPlainText()); } else { LOGGER.log(Level.SEVERE, "Unknown credential type for context {0} using id: {1}: {2}", new Object[] {contextName, credentialsId, credentials.getClass().getName()}); return null; } return builder; }
gb.withConnector(new OkHttpConnector(new OkUrlFactory(client)));
/** * Called by {@link #apply(Object)} * Logins to GH and returns client instance * * @param github config where token saved * * @return authorized client or null on login error */ @Override @CheckForNull protected GitHub applyNullSafe(@Nonnull GitHubServerConfig github) { String accessToken = tokenFor(github.getCredentialsId()); GitHubBuilder builder = new GitHubBuilder() .withOAuthToken(accessToken) .withConnector(connector(github)) .withRateLimitHandler(RateLimitHandler.FAIL); try { if (isNotBlank(github.getApiUrl())) { builder.withEndpoint(github.getApiUrl()); } LOGGER.debug("Create new GH client with creds id {}", github.getCredentialsId()); return builder.build(); } catch (IOException e) { LOGGER.warn("Failed to login with creds {}", github.getCredentialsId(), e); return null; } }