Skip to content

Commit

Permalink
NoThrottle will now redirect to ThrottleOnOver if GitHub.com api dete…
Browse files Browse the repository at this point in the history
…cted
  • Loading branch information
Marc Salles committed Jul 20, 2020
1 parent b88408f commit 7b39521
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.kohsuke.github.GHRateLimit;
import org.kohsuke.github.GitHub;

Expand Down Expand Up @@ -115,6 +116,13 @@ public void checkApiRateLimit(@NonNull TaskListener listener, GitHub github) thr

@Override
public void checkApiRateLimit(@NonNull TaskListener listener, GitHub github) throws IOException, InterruptedException {
if (GitHubServerConfig.GITHUB_URL.equals(github.getApiUrl())) {

// If the GitHub public API is being used, this will fallback to ThrottleOnOver
listener.getLogger().println(GitHubConsoleNote.create(System.currentTimeMillis(),
"GitHub throttling is disabled, which is not allowed for public GitHub usage, so ThrottleOnOver will be used instead. To configure a different rate limiting strategy, go to \"GitHub API usage\" under \"Configure System\" in the Jenkins settings."));
ThrottleOnOver.checkApiRateLimit(listener, github);
}
// Nothing needed
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
import com.github.tomakehurst.wiremock.stubbing.Scenario;
import hudson.util.LogTaskListener;
import hudson.util.RingBufferLogHandler;
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
import org.junit.Test;
import org.junit.Before;
import org.kohsuke.github.GitHub;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -187,11 +190,34 @@ public void NoThrottleTestShouldNotThrottle() throws Exception {
setupStubs(scenarios);
ApiRateLimitChecker.NoThrottle.checkApiRateLimit(listener, github);
// there should be no output
assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping")));
assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*")));
// github rate_limit endpoint should not be contacted
assertEquals(0, getRequestCount(githubApi));
}

/**
* Verify that "NoThrottle" falls back to "ThrottleOnOver" if using GitHub.com
*
* @author Marc Salles Navarro
*/
@Test
public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
GitHub spy = Mockito.spy(github);
Mockito.when(spy.getApiUrl()).thenReturn(GitHubServerConfig.GITHUB_URL).thenReturn(github.getApiUrl());
// set up scenarios
List<RateLimit> scenarios = new ArrayList<>();
int limit = 5000;
int buffer = ApiRateLimitChecker.calculateBuffer(limit);
scenarios.add(new RateLimit(limit, buffer -1, soon));
scenarios.add(new RateLimit(limit, limit, new Date(soon.getTime() + 2000)));
setupStubs(scenarios);
ApiRateLimitChecker.NoThrottle.checkApiRateLimit(listener, spy);

assertEquals(1, countOfOutputLines(m -> m.matches(".*[sS]leeping.*")));
// github rate_limit endpoint should be contacted by ThrottleOnOver
assertEquals(3, getRequestCount(githubApi));
}

/**
* Verify exactly when the throttle is occurring in "OnOver"
*
Expand Down Expand Up @@ -222,7 +248,7 @@ public void ThrottleOnOverTest() throws Exception {
}

//should be no output
assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping")));
assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*")));

assertEquals(11, getRequestCount(githubApi));

Expand Down Expand Up @@ -515,7 +541,7 @@ public void NormalizeExpectedIdealOverTime() throws Exception {
// Making sure the budgets are correct
assertEquals(12, countOfOutputLinesContaining("0 under budget"));
// no occurrences of sleeping
assertEquals(0, countOfOutputLines(m -> m.matches("[sS]leeping")));
assertEquals(0, countOfOutputLines(m -> m.matches(".*[sS]leeping.*")));
}

/**
Expand Down

0 comments on commit 7b39521

Please sign in to comment.