Skip to content

Commit 7b39521

Browse files
author
Marc Salles
committed
NoThrottle will now redirect to ThrottleOnOver if GitHub.com api detected
1 parent b88408f commit 7b39521

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

src/main/java/org/jenkinsci/plugins/github_branch_source/ApiRateLimitChecker.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import edu.umd.cs.findbugs.annotations.NonNull;
44
import hudson.Util;
55
import hudson.model.TaskListener;
6+
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
67
import org.kohsuke.github.GHRateLimit;
78
import org.kohsuke.github.GitHub;
89

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

116117
@Override
117118
public void checkApiRateLimit(@NonNull TaskListener listener, GitHub github) throws IOException, InterruptedException {
119+
if (GitHubServerConfig.GITHUB_URL.equals(github.getApiUrl())) {
120+
121+
// If the GitHub public API is being used, this will fallback to ThrottleOnOver
122+
listener.getLogger().println(GitHubConsoleNote.create(System.currentTimeMillis(),
123+
"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."));
124+
ThrottleOnOver.checkApiRateLimit(listener, github);
125+
}
118126
// Nothing needed
119127
}
120128
}

src/test/java/org/jenkinsci/plugins/github_branch_source/ApiRateLimitCheckerTest.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import com.github.tomakehurst.wiremock.stubbing.Scenario;
88
import hudson.util.LogTaskListener;
99
import hudson.util.RingBufferLogHandler;
10+
import org.jenkinsci.plugins.github.config.GitHubServerConfig;
1011
import org.junit.Test;
1112
import org.junit.Before;
1213
import org.kohsuke.github.GitHub;
14+
import org.mockito.Mock;
15+
import org.mockito.Mockito;
1316

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

198+
/**
199+
* Verify that "NoThrottle" falls back to "ThrottleOnOver" if using GitHub.com
200+
*
201+
* @author Marc Salles Navarro
202+
*/
203+
@Test
204+
public void NoThrottleTestShouldFallbackToThrottleOnOverForGitHubDotCom() throws Exception {
205+
GitHub spy = Mockito.spy(github);
206+
Mockito.when(spy.getApiUrl()).thenReturn(GitHubServerConfig.GITHUB_URL).thenReturn(github.getApiUrl());
207+
// set up scenarios
208+
List<RateLimit> scenarios = new ArrayList<>();
209+
int limit = 5000;
210+
int buffer = ApiRateLimitChecker.calculateBuffer(limit);
211+
scenarios.add(new RateLimit(limit, buffer -1, soon));
212+
scenarios.add(new RateLimit(limit, limit, new Date(soon.getTime() + 2000)));
213+
setupStubs(scenarios);
214+
ApiRateLimitChecker.NoThrottle.checkApiRateLimit(listener, spy);
215+
216+
assertEquals(1, countOfOutputLines(m -> m.matches(".*[sS]leeping.*")));
217+
// github rate_limit endpoint should be contacted by ThrottleOnOver
218+
assertEquals(3, getRequestCount(githubApi));
219+
}
220+
195221
/**
196222
* Verify exactly when the throttle is occurring in "OnOver"
197223
*
@@ -222,7 +248,7 @@ public void ThrottleOnOverTest() throws Exception {
222248
}
223249

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

227253
assertEquals(11, getRequestCount(githubApi));
228254

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

521547
/**

0 commit comments

Comments
 (0)