/** * Version that connects to GitHub Enterprise. * * @deprecated * Use with caution. Login with password is not a preferred method. */ public static GitHub connectToEnterprise(String apiUrl, String login, String password) throws IOException { return new GitHubBuilder().withEndpoint(apiUrl).withPassword(login, password).build(); }
/** * Version that connects to GitHub Enterprise. * * @param apiUrl * The URL of GitHub (or GitHub enterprise) API endpoint, such as "https://api.github.com" or * "http://ghe.acme.com/api/v3". Note that GitHub Enterprise has <tt>/api/v3</tt> in the URL. * For historical reasons, this parameter still accepts the bare domain name, but that's considered deprecated. */ public static GitHub connectToEnterpriseWithOAuth(String apiUrl, String login, String oauthAccessToken) throws IOException { return new GitHubBuilder().withEndpoint(apiUrl).withOAuthToken(oauthAccessToken, login).build(); }
/** * 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); } }); }
/** * Connects to GitHub Enterprise anonymously. * * All operations that requires authentication will fail. */ public static GitHub connectToEnterpriseAnonymously(String apiUrl) throws IOException { return new GitHubBuilder().withEndpoint(apiUrl).build(); }
public static GitHubBuilder fromProperties(Properties props) { GitHubBuilder self = new GitHubBuilder(); self.withOAuthToken(props.getProperty("oauth"), props.getProperty("login")); self.withPassword(props.getProperty("login"), props.getProperty("password")); self.withEndpoint(props.getProperty("endpoint", GitHub.GITHUB_URL)); return self; }
public static GitHub connectUsingOAuth(String oauthAccessToken) throws IOException { return new GitHubBuilder().withOAuthToken(oauthAccessToken).build(); }
/** * 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; } }
public GitHub getGithub() throws IOException { if (github == null) { GitHubBuilder ghb = new GitHubBuilder(); String username = getGithubUsername(); String password = getGithubPassword(); String token = getGithubToken(); if (Strings.notEmpty(username) && Strings.notEmpty(password)) { ghb.withPassword(username, password); } else if (Strings.notEmpty(token)) { if (Strings.notEmpty(username)) { ghb.withOAuthToken(token, username); } else { ghb.withOAuthToken(token); } } ghb.withRateLimitHandler(RateLimitHandler.WAIT). withAbuseLimitHandler(AbuseLimitHandler.WAIT); this.github = ghb.build(); } return this.github; }
public static GitHub connectUsingPassword(String login, String password) throws IOException { return new GitHubBuilder().withPassword(login, password).build(); }
/** * Connects to GitHub anonymously. * * All operations that requires authentication will fail. */ public static GitHub connectAnonymously() throws IOException { return new GitHubBuilder().build(); }
public GitHub getGithub() throws IOException { if (this.github == null) { if (!this.travisConfig.getGithubToken().isEmpty()) { try { this.github = GitHubBuilder.fromEnvironment().withOAuthToken(this.travisConfig.getGithubToken()).build(); } catch (IOException e) { String token = (this.travisConfig.getGithubToken().length() > 10) ? "[10 first characters of token] "+this.travisConfig.getGithubToken().substring(0,10) : this.travisConfig.getGithubToken(); LOGGER.error("Error while using credentials ("+token+"): fallback to anonymous connection.", e); this.github = GitHub.connectAnonymously(); } } else { LOGGER.info("No Github token has been given, try to connect using environment information"); try { this.github = GitHubBuilder.fromEnvironment().build(); } catch (IOException e) { LOGGER.warn("Error while using environment credentials: fallback to anonymous connection. In the future try by setting a proper token in GITHUB_OAUTH env variable.", e); this.github = GitHub.connectAnonymously(); } } } GHRateLimit rateLimit = this.github.getRateLimit(); LOGGER.info("GitHub ratelimit: Limit: " + rateLimit.limit + " Remaining: " + rateLimit.remaining + " Reset hour: " + hourSimpleDateFormat.format(rateLimit.reset)); return this.github; }
private void buildConnection(Item context) { GitHubBuilder builder = getBuilder(context, serverAPIUrl, credentialsId); if (builder == null) { LOGGER.log(Level.SEVERE, "Unable to get builder using credentials: {0}", credentialsId); return; } try { gh = builder.build(); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Unable to connect using credentials: " + credentialsId, e); } }
/** * Obtains the credential from "~/.github" or from the System Environment Properties. */ public static GitHub connect() throws IOException { return GitHubBuilder.fromCredentials().build(); }
private GHCompare getCompare(Build build, Build previousBuild) { try { GitHub gh = GitHubBuilder.fromEnvironment().build(); GHRateLimit rateLimit = gh.getRateLimit(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); this.logger.debug("GitHub rate limit: Limit: " + rateLimit.limit + " - Remaining: " + rateLimit.remaining + " - Reset hour: " + dateFormat.format(rateLimit.reset)); if (rateLimit.remaining > 2) { GHRepository ghRepo = gh.getRepository(build.getRepository().getSlug()); GHCommit buildCommit = ghRepo.getCommit(build.getCommit().getSha()); GHCommit previousBuildCommit = ghRepo.getCommit(previousBuild.getCommit().getSha()); GHCompare compare = ghRepo.getCompare(previousBuildCommit, buildCommit); return compare; } else { this.logger.warn("You reached your rate limit for GitHub. You have to wait until " + dateFormat.format(rateLimit.reset) + " to get data. PRInformation will be null for build "+build.getId()+"."); } } catch (IOException e) { this.logger.warn("Error while getting commit from GitHub: " + e); } return null; }
public GitHubBuilder withOAuthToken(String oauthToken) { return withOAuthToken(oauthToken, null); } public GitHubBuilder withOAuthToken(String oauthToken, String user) {
/** * First check if the credentials are configured using the ~/.github properties file. * * If no user is specified it means there is no configuration present so check the environment instead. * * If there is still no user it means there are no credentials defined and throw an IOException. * * @return the configured Builder from credentials defined on the system or in the environment. * * @throws IOException If there are no credentials defined in the ~/.github properties file or the process environment. */ public static GitHubBuilder fromCredentials() throws IOException { Exception cause = null; GitHubBuilder builder; try { builder = fromPropertyFile(); if (builder.oauthToken != null || builder.user != null) return builder; } catch (FileNotFoundException e) { // fall through cause = e; } builder = fromEnvironment(); if (builder.oauthToken != null || builder.user != null) return builder; else throw (IOException)new IOException("Failed to resolve credentials from ~/.github or the environment.").initCause(cause); }
/** * @deprecated * Use {@link #fromEnvironment()} to pick up standard set of environment variables, so that * different clients of this library will all recognize one consistent set of coordinates. */ public static GitHubBuilder fromEnvironment(String loginVariableName, String passwordVariableName, String oauthVariableName) throws IOException { return fromEnvironment(loginVariableName, passwordVariableName, oauthVariableName, ""); }
public static GitHubBuilder fromProperties(Properties props) { GitHubBuilder self = new GitHubBuilder(); self.withOAuthToken(props.getProperty("oauth"), props.getProperty("login")); self.withPassword(props.getProperty("login"), props.getProperty("password")); self.withEndpoint(props.getProperty("endpoint", GitHub.GITHUB_URL)); return self; }
public static GitHub connect(String login, String oauthAccessToken) throws IOException { return new GitHubBuilder().withOAuthToken(oauthAccessToken, login).build(); }
public GitHub getGithub() throws IOException { if (github == null) { GitHubBuilder ghb = new GitHubBuilder(); String username = getGithubUsername(); String password = getGithubPassword(); String token = getGithubToken(); if (Strings.notEmpty(username) && Strings.notEmpty(password)) { ghb.withPassword(username, password); } else if (Strings.notEmpty(token)) { if (Strings.notEmpty(username)) { ghb.withOAuthToken(token, username); } else { ghb.withOAuthToken(token); } } ghb.withRateLimitHandler(RateLimitHandler.WAIT). withAbuseLimitHandler(AbuseLimitHandler.WAIT); this.github = ghb.build(); } return this.github; }