Skip to content

Commit 796c644

Browse files
committed
Change credential check to use rate limit
Fixes #582
1 parent bfd9023 commit 796c644

File tree

6 files changed

+98
-19
lines changed

6 files changed

+98
-19
lines changed

src/main/java/org/kohsuke/github/GitHubClient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,15 @@ private <T> T fetch(Class<T> type, String urlPath) throws IOException {
148148
}
149149

150150
/**
151-
* Ensures that the credential is valid.
151+
* Ensures that the credential for this client is valid.
152152
*
153153
* @return the boolean
154154
*/
155155
public boolean isCredentialValid() {
156156
try {
157-
fetch(GHUser.class, "/user");
157+
// If 404, ratelimit returns a default value.
158+
// This works as credential test because invalid credentials returns 401, not 404
159+
getRateLimit();
158160
return true;
159161
} catch (IOException e) {
160162
if (LOGGER.isLoggable(FINE))

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ private void cleanupUserRepository(final String name) throws IOException {
7777
@Test
7878
public void testCredentialValid() throws IOException {
7979
assertTrue(gitHub.isCredentialValid());
80+
81+
// TODO: Doh, this doesn't go through the proxy. Needs rewriting.
8082
GitHub connect = GitHub.connect("totally", "bogus");
8183
assertFalse(connect.isCredentialValid());
84+
85+
// TODO: Add GHE test - where RateLimit returns 404.
8286
}
8387

8488
@Test
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"resources": {
3+
"core": {
4+
"limit": 5000,
5+
"remaining": 4998,
6+
"reset": 1585671841
7+
},
8+
"search": {
9+
"limit": 30,
10+
"remaining": 30,
11+
"reset": 1585668302
12+
},
13+
"graphql": {
14+
"limit": 5000,
15+
"remaining": 5000,
16+
"reset": 1585671842
17+
},
18+
"integration_manifest": {
19+
"limit": 5000,
20+
"remaining": 5000,
21+
"reset": 1585671842
22+
},
23+
"source_import": {
24+
"limit": 100,
25+
"remaining": 100,
26+
"reset": 1585668302
27+
}
28+
},
29+
"rate": {
30+
"limit": 5000,
31+
"remaining": 4998,
32+
"reset": 1585671841
33+
}
34+
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testCredentialValid/__files/user-1.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
"email": "[email protected]",
2525
"hireable": null,
2626
"bio": "https://twitter.com/bitwiseman",
27-
"public_repos": 167,
28-
"public_gists": 4,
29-
"followers": 136,
27+
"public_repos": 181,
28+
"public_gists": 7,
29+
"followers": 151,
3030
"following": 9,
3131
"created_at": "2012-07-11T20:38:33Z",
32-
"updated_at": "2019-09-24T19:32:29Z",
33-
"private_gists": 7,
34-
"total_private_repos": 9,
32+
"updated_at": "2020-03-27T19:14:56Z",
33+
"private_gists": 8,
34+
"total_private_repos": 10,
3535
"owned_private_repos": 0,
3636
"disk_usage": 33697,
3737
"collaborators": 0,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"id": "776ae4cc-bd8c-4c7d-b299-4ba1b2c11ad0",
3+
"name": "rate_limit",
4+
"request": {
5+
"url": "/rate_limit",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "rate_limit-2.json",
16+
"headers": {
17+
"Date": "Tue, 31 Mar 2020 15:24:02 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4998",
23+
"X-RateLimit-Reset": "1585671841",
24+
"Cache-Control": "no-cache",
25+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
26+
"X-Accepted-OAuth-Scopes": "",
27+
"X-GitHub-Media-Type": "unknown, github.v3",
28+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
29+
"X-Frame-Options": "deny",
30+
"X-Content-Type-Options": "nosniff",
31+
"X-XSS-Protection": "1; mode=block",
32+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
33+
"Content-Security-Policy": "default-src 'none'",
34+
"Vary": "Accept-Encoding, Accept, X-Requested-With",
35+
"X-GitHub-Request-Id": "FEBE:52B1:FCCEE:123132:5E836091"
36+
}
37+
},
38+
"uuid": "776ae4cc-bd8c-4c7d-b299-4ba1b2c11ad0",
39+
"persistent": true,
40+
"insertionIndex": 2
41+
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "eba96b24-3354-4d21-bd2a-388a13ba9832",
2+
"id": "218a25c5-b10d-4371-a446-bd4b220c2402",
33
"name": "user",
44
"request": {
55
"url": "/user",
@@ -14,35 +14,33 @@
1414
"status": 200,
1515
"bodyFileName": "user-1.json",
1616
"headers": {
17-
"Date": "Wed, 02 Oct 2019 21:39:57 GMT",
17+
"Date": "Tue, 31 Mar 2020 15:24:01 GMT",
1818
"Content-Type": "application/json; charset=utf-8",
1919
"Server": "GitHub.com",
2020
"Status": "200 OK",
2121
"X-RateLimit-Limit": "5000",
22-
"X-RateLimit-Remaining": "4964",
23-
"X-RateLimit-Reset": "1570055937",
22+
"X-RateLimit-Remaining": "4999",
23+
"X-RateLimit-Reset": "1585671841",
2424
"Cache-Control": "private, max-age=60, s-maxage=60",
2525
"Vary": [
2626
"Accept, Authorization, Cookie, X-GitHub-OTP",
27-
"Accept-Encoding"
27+
"Accept-Encoding, Accept, X-Requested-With"
2828
],
29-
"ETag": "W/\"cf6199fecf47b59c42190e1e11147ee2\"",
30-
"Last-Modified": "Tue, 24 Sep 2019 19:32:29 GMT",
29+
"ETag": "W/\"740bb7db37d5437d08ea1aa5e652cf37\"",
30+
"Last-Modified": "Fri, 27 Mar 2020 19:14:56 GMT",
3131
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
3232
"X-Accepted-OAuth-Scopes": "",
3333
"X-GitHub-Media-Type": "unknown, github.v3",
34-
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
35-
"Access-Control-Allow-Origin": "*",
3634
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
3735
"X-Frame-Options": "deny",
3836
"X-Content-Type-Options": "nosniff",
3937
"X-XSS-Protection": "1; mode=block",
4038
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
4139
"Content-Security-Policy": "default-src 'none'",
42-
"X-GitHub-Request-Id": "C2BA:67D6:CA2979:EFEEDC:5D95192D"
40+
"X-GitHub-Request-Id": "FEBE:52B1:FCCA5:12311D:5E836091"
4341
}
4442
},
45-
"uuid": "eba96b24-3354-4d21-bd2a-388a13ba9832",
43+
"uuid": "218a25c5-b10d-4371-a446-bd4b220c2402",
4644
"persistent": true,
4745
"insertionIndex": 1
4846
}

0 commit comments

Comments
 (0)