private void showGitHubRateInformation(GitHub gh, AbstractStep step) throws IOException { GHRateLimit rateLimit = gh.getRateLimit(); SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); this.getLogger().info("Step " + step.getName() + " - GitHub rate limit: Limit: " + rateLimit.limit + " - Remaining: " + rateLimit.remaining + " - Reset hour: " + dateFormat.format(rateLimit.reset)); }
protected void processCauses(GitHubSourceContext context, Stream<? extends GitHubCause<?>> causeStream) throws IOException { GitHub github = context.getGitHub(); TaskListener listener = context.getListener(); listener.getLogger().println("GitHub rate limit before check: " + github.getRateLimit()); // run the process forEachIo(causeStream, context::observe); listener.getLogger().println("GitHub rate limit after check: " + github.getRateLimit()); } }
/** * Gets the current rate limit while trying not to actually make any remote requests unless absolutely necessary. * * @return the current rate limit data. * @throws IOException if we couldn't get the current rate limit data. */ @Nonnull public GHRateLimit rateLimit() throws IOException { synchronized (headerRateLimitLock) { if (headerRateLimit != null) { return headerRateLimit; } } GHRateLimit rateLimit = this.rateLimit; if (rateLimit == null || rateLimit.getResetDate().getTime() < System.currentTimeMillis()) { rateLimit = getRateLimit(); } return rateLimit; }
/** * Gets the current rate limit while trying not to actually make any remote requests unless absolutely necessary. * * @return the current rate limit data. * @throws IOException if we couldn't get the current rate limit data. */ @Nonnull public GHRateLimit rateLimit() throws IOException { synchronized (headerRateLimitLock) { if (headerRateLimit != null) { return headerRateLimit; } } GHRateLimit rateLimit = this.rateLimit; if (rateLimit == null || rateLimit.getResetDate().getTime() < System.currentTimeMillis()) { rateLimit = getRateLimit(); } return rateLimit; }
if (gitHub.getRateLimit().remaining == 0) { LOGGER.log(Level.INFO, "Exceeded rate limit for repository"); return false;
GHRateLimit rateLimitBefore = github.getRateLimit(); listener.debug("GitHub rate limit before check: {}", rateLimitBefore); saveIfSkipFirstRun(); GHRateLimit rateLimitAfter = github.getRateLimit(); int consumed = rateLimitBefore.remaining - rateLimitAfter.remaining; LOG.info("GitHub rate limit after check {}: {}, consumed: {}, checked branches: {}",
long now = System.currentTimeMillis(); if (now < expiration) { GHRateLimit current = github.getRateLimit(); if (current.remaining > rateLimit.remaining || current.getResetDate().getTime() > rateLimit.getResetDate().getTime()) {
public String forkRepository(String repository, AbstractStep step) throws IOException { GitHub gh = RepairnatorConfig.getInstance().getGithub(); showGitHubRateInformation(gh, step); if (gh.getRateLimit().remaining > 10) { GHRepository originalRepo = gh.getRepository(repository); if (originalRepo != null) { return originalRepo.fork().getUrl().toString(); } } return null; }
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 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; }
@RequirePOST @Restricted(DoNotUse.class) // WebOnly @SuppressWarnings("unused") public FormValidation doVerifyCredentials( @QueryParameter String apiUrl, @QueryParameter String credentialsId) throws IOException { Jenkins.getActiveInstance().checkPermission(Jenkins.ADMINISTER); GitHubServerConfig config = new GitHubServerConfig(credentialsId); config.setApiUrl(apiUrl); config.setClientCacheSize(0); GitHub gitHub = new GitHubLoginFunction().apply(config); try { if (gitHub != null && gitHub.isCredentialValid()) { return FormValidation.ok("Credentials verified for user %s, rate limit: %s", gitHub.getMyself().getLogin(), gitHub.getRateLimit().remaining); } else { return FormValidation.error("Failed to validate the account"); } } catch (IOException e) { return FormValidation.error(e, "Failed to validate the account"); } }
GHRateLimit rateLimitBefore = github.getRateLimit(); listener.debug("GitHub rate limit before check: {}", rateLimitBefore); GHRateLimit rateLimitAfter = github.getRateLimit(); int consumed = rateLimitBefore.remaining - rateLimitAfter.remaining; LOG.info("GitHub rate limit after check {}: {}, consumed: {}, checked PRs: {}",