From 1b63a58e631e69a4f4691be3e2e9305c67817c12 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Tue, 25 Feb 2020 17:06:02 -0800 Subject: [PATCH 1/7] Increase coverage --- pom.xml | 2 +- src/test/java/org/kohsuke/github/AppTest.java | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 7aa0a7e56b..1e2c6607ba 100644 --- a/pom.xml +++ b/pom.xml @@ -42,7 +42,7 @@ format sort - 0.556 + 0.60 0.25 false diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java index 6bbb2880a6..cd99c12b34 100755 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.oneOf; /** * Unit test for simple App. @@ -861,10 +862,27 @@ public void notifications() throws Exception { for (GHThread t : gitHub.listNotifications().nonBlocking(true).read(true)) { if (!found) { found = true; + // both thread an unread are included + assertThat(t.getTitle(), is("Create a Jenkinsfile for Librecores CI in mor1kx")); + assertThat(t.getLastReadAt(), notNullValue()); + assertThat(t.isRead(), equalTo(true)); + t.markAsRead(); // test this by calling it once on old notfication } - assertNotNull(t.getTitle()); - assertNotNull(t.getReason()); + assertThat(t.getReason(), oneOf("subscribed", "mention", "review_requested", "comment")); + assertThat(t.getTitle(), notNullValue()); + assertThat(t.getLastCommentUrl(), notNullValue()); + assertThat(t.getRepository(), notNullValue()); + assertThat(t.getUpdatedAt(), notNullValue()); + assertThat(t.getType(), oneOf("Issue", "PullRequest")); + + // both thread an unread are included + // assertThat(t.getLastReadAt(), notNullValue()); + // assertThat(t.isRead(), equalTo(true)); + + // Doesn't exist on threads but is part of GHObject. :( + assertThat(t.getCreatedAt(), nullValue()); + } assertTrue(found); gitHub.listNotifications().markAsRead(); From fe4f45c2b05d503afda64fb717514f02950b9ac6 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Tue, 25 Feb 2020 17:43:21 -0800 Subject: [PATCH 2/7] Remove DeleteToken class the was never in github v3 api --- pom.xml | 1 - .../java/org/kohsuke/github/DeleteToken.java | 34 ------------------- 2 files changed, 35 deletions(-) delete mode 100644 src/main/java/org/kohsuke/github/DeleteToken.java diff --git a/pom.xml b/pom.xml index 1e2c6607ba..737aa5a1cf 100644 --- a/pom.xml +++ b/pom.xml @@ -146,7 +146,6 @@ org.kohsuke.github.example.* - org.kohsuke.github.DeleteToken org.kohsuke.github.Previews diff --git a/src/main/java/org/kohsuke/github/DeleteToken.java b/src/main/java/org/kohsuke/github/DeleteToken.java deleted file mode 100644 index 176acc1d83..0000000000 --- a/src/main/java/org/kohsuke/github/DeleteToken.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * The MIT License - * - * Copyright (c) 2010, Kohsuke Kawaguchi - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.kohsuke.github; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - -/** -*/ -@SuppressFBWarnings(value = "UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD", - justification = "Being constructed by JSON deserialization") -class DeleteToken { - public String delete_token; -} From 0155d5aa39103f6e290aa4f44271be878c44dc61 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Tue, 25 Feb 2020 20:23:40 -0800 Subject: [PATCH 3/7] Make HttpConnector a functional interface --- .../org/kohsuke/github/GitHubBuilder.java | 8 ++-- .../org/kohsuke/github/HttpConnector.java | 13 ++---- .../kohsuke/github/GitHubConnectionTest.java | 9 ++-- ...-2ce6df61-378a-43bd-9d37-d0851f8699bf.json | 34 ++++++++++++++ .../mappings/-1-2ce6df.json | 45 +++++++++++++++++++ 5 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json create mode 100644 src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json diff --git a/src/main/java/org/kohsuke/github/GitHubBuilder.java b/src/main/java/org/kohsuke/github/GitHubBuilder.java index 65822741be..1b0034420b 100644 --- a/src/main/java/org/kohsuke/github/GitHubBuilder.java +++ b/src/main/java/org/kohsuke/github/GitHubBuilder.java @@ -93,6 +93,7 @@ static GitHubBuilder fromCredentials() throws IOException { * @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. */ + @Deprecated public static GitHubBuilder fromEnvironment(String loginVariableName, String passwordVariableName, String oauthVariableName) throws IOException { @@ -122,6 +123,7 @@ private static void loadIfSet(String envName, Properties p, String propName) { * @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. */ + @Deprecated public static GitHubBuilder fromEnvironment(String loginVariableName, String passwordVariableName, String oauthVariableName, @@ -396,11 +398,7 @@ public GitHubBuilder withRateLimitChecker(@Nonnull RateLimitChecker coreRateLimi * @return the git hub builder */ public GitHubBuilder withProxy(final Proxy p) { - return withConnector(new ImpatientHttpConnector(new HttpConnector() { - public HttpURLConnection connect(URL url) throws IOException { - return (HttpURLConnection) url.openConnection(p); - } - })); + return withConnector(new ImpatientHttpConnector(url -> (HttpURLConnection) url.openConnection(p))); } /** diff --git a/src/main/java/org/kohsuke/github/HttpConnector.java b/src/main/java/org/kohsuke/github/HttpConnector.java index 098930fa2d..d5e900f5b3 100644 --- a/src/main/java/org/kohsuke/github/HttpConnector.java +++ b/src/main/java/org/kohsuke/github/HttpConnector.java @@ -12,6 +12,7 @@ *

* For example, you can implement this to st custom timeouts. */ +@FunctionalInterface public interface HttpConnector { /** * Opens a connection to the given URL. @@ -27,18 +28,12 @@ public interface HttpConnector { /** * Default implementation that uses {@link URL#openConnection()}. */ - HttpConnector DEFAULT = new ImpatientHttpConnector(new HttpConnector() { - public HttpURLConnection connect(URL url) throws IOException { - return (HttpURLConnection) url.openConnection(); - } - }); + HttpConnector DEFAULT = new ImpatientHttpConnector(url -> (HttpURLConnection) url.openConnection()); /** * Stub implementation that is always off-line. */ - HttpConnector OFFLINE = new HttpConnector() { - public HttpURLConnection connect(URL url) throws IOException { - throw new IOException("Offline"); - } + HttpConnector OFFLINE = url -> { + throw new IOException("Offline"); }; } diff --git a/src/test/java/org/kohsuke/github/GitHubConnectionTest.java b/src/test/java/org/kohsuke/github/GitHubConnectionTest.java index 63856f6678..882cc3f4b7 100644 --- a/src/test/java/org/kohsuke/github/GitHubConnectionTest.java +++ b/src/test/java/org/kohsuke/github/GitHubConnectionTest.java @@ -1,6 +1,5 @@ package org.kohsuke.github; -import org.junit.Ignore; import org.junit.Test; import java.io.IOException; @@ -106,11 +105,13 @@ public void testGithubBuilderWithAppInstallationToken() throws Exception { assertEquals("", github.getClient().login); } - @Ignore @Test public void testGitHubIsApiUrlValid() throws IOException { - GitHub hub = GitHub.connectAnonymously(); - // GitHub hub = GitHub.connectToEnterpriseAnonymously(mockGitHub.apiServer().baseUrl()); + // NOTE: We cannot test connectAnonymously on a general basis because it can hang if + // rate limit is reached. We connectToEnterpriseAnonymously as the nearest equivalent. + // GitHub hub = GitHub.connectAnonymously(); + + GitHub hub = GitHub.connectToEnterpriseAnonymously(mockGitHub.apiServer().baseUrl()); try { hub.checkApiUrlValidity(); } catch (IOException ioe) { diff --git a/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json new file mode 100644 index 0000000000..d1fb32fb19 --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/__files/-2ce6df61-378a-43bd-9d37-d0851f8699bf.json @@ -0,0 +1,34 @@ +{ + "current_user_url": "https://api.github.com/user", + "current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}", + "authorizations_url": "https://api.github.com/authorizations", + "code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}", + "commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}", + "emails_url": "https://api.github.com/user/emails", + "emojis_url": "https://api.github.com/emojis", + "events_url": "https://api.github.com/events", + "feeds_url": "https://api.github.com/feeds", + "followers_url": "https://api.github.com/user/followers", + "following_url": "https://api.github.com/user/following{/target}", + "gists_url": "https://api.github.com/gists{/gist_id}", + "hub_url": "https://api.github.com/hub", + "issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}", + "issues_url": "https://api.github.com/issues", + "keys_url": "https://api.github.com/user/keys", + "label_search_url": "https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}", + "notifications_url": "https://api.github.com/notifications", + "organization_url": "https://api.github.com/orgs/{org}", + "organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}", + "organization_teams_url": "https://api.github.com/orgs/{org}/teams", + "public_gists_url": "https://api.github.com/gists/public", + "rate_limit_url": "https://api.github.com/rate_limit", + "repository_url": "https://api.github.com/repos/{owner}/{repo}", + "repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}", + "current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}", + "starred_url": "https://api.github.com/user/starred{/owner}{/repo}", + "starred_gists_url": "https://api.github.com/gists/starred", + "user_url": "https://api.github.com/users/{user}", + "user_organizations_url": "https://api.github.com/user/orgs", + "user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}", + "user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,sort,order}" +} \ No newline at end of file diff --git a/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json new file mode 100644 index 0000000000..19a8e7bfcb --- /dev/null +++ b/src/test/resources/org/kohsuke/github/GitHubConnectionTest/wiremock/testGitHubIsApiUrlValid/mappings/-1-2ce6df.json @@ -0,0 +1,45 @@ +{ + "id": "2ce6df61-378a-43bd-9d37-d0851f8699bf", + "name": "", + "request": { + "url": "/", + "method": "GET", + "headers": { + "Accept": { + "equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2" + } + } + }, + "response": { + "status": 200, + "bodyFileName": "-2ce6df61-378a-43bd-9d37-d0851f8699bf.json", + "headers": { + "Date": "Wed, 26 Feb 2020 03:55:33 GMT", + "Content-Type": "application/json; charset=utf-8", + "Server": "GitHub.com", + "Status": "200 OK", + "X-RateLimit-Limit": "60", + "X-RateLimit-Remaining": "58", + "X-RateLimit-Reset": "1582692906", + "Cache-Control": "public, max-age=60, s-maxage=60", + "Vary": [ + "Accept", + "Accept-Encoding, Accept, X-Requested-With" + ], + "ETag": "W/\"323592c0d9feaab7a8e4a4e2a555feec\"", + "X-GitHub-Media-Type": "unknown, github.v3", + "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", + "Access-Control-Allow-Origin": "*", + "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", + "X-Frame-Options": "deny", + "X-Content-Type-Options": "nosniff", + "X-XSS-Protection": "1; mode=block", + "Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin", + "Content-Security-Policy": "default-src 'none'", + "X-GitHub-Request-Id": "EE6F:3AF2:5C10D:6DDAD:5E55EC35" + } + }, + "uuid": "2ce6df61-378a-43bd-9d37-d0851f8699bf", + "persistent": true, + "insertionIndex": 1 +} \ No newline at end of file From 944d92bbb408dc7be91fcbe539a21762c6e0491a Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Tue, 25 Feb 2020 20:24:02 -0800 Subject: [PATCH 4/7] Make more methods as deprecated --- pom.xml | 2 - .../github/GHDeploymentStatusBuilder.java | 1 + src/main/java/org/kohsuke/github/GHIssue.java | 12 +--- .../java/org/kohsuke/github/GHMyself.java | 1 + .../org/kohsuke/github/GHOrganization.java | 2 + .../org/kohsuke/github/GHPullRequest.java | 2 + .../kohsuke/github/GHPullRequestReview.java | 1 + .../github/GHPullRequestReviewComment.java | 1 + .../java/org/kohsuke/github/GHRelease.java | 1 + .../java/org/kohsuke/github/GHRepository.java | 58 +++++++++---------- src/main/java/org/kohsuke/github/GitHub.java | 13 ++++- .../org/kohsuke/github/GHRepositoryTest.java | 2 +- 12 files changed, 51 insertions(+), 45 deletions(-) diff --git a/pom.xml b/pom.xml index 737aa5a1cf..19fb607c56 100644 --- a/pom.xml +++ b/pom.xml @@ -160,7 +160,6 @@ org.kohsuke.github.GHRelease - org.kohsuke.github.GitHubClient.GHApiInfo org.kohsuke.github.GHBranchProtection.RequiredSignatures org.kohsuke.github.GHBranchProtectionBuilder.Restrictions org.kohsuke.github.GHBranchProtection.Restrictions @@ -201,7 +200,6 @@ org.kohsuke.github.GHTeam.Role org.kohsuke.github.GHUserSearchBuilder.Sort org.kohsuke.github.GHVerifiedKey - org.kohsuke.github.GitHubBuilder.1 diff --git a/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java b/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java index 34ff201a33..af419840e4 100644 --- a/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java +++ b/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java @@ -23,6 +23,7 @@ public class GHDeploymentStatusBuilder { * the state * @deprecated Use {@link GHDeployment#createStatus(GHDeploymentState)} */ + @Deprecated public GHDeploymentStatusBuilder(GHRepository repo, int deploymentId, GHDeploymentState state) { this(repo, (long) deploymentId, state); } diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java index 051e367324..14f1818b37 100644 --- a/src/main/java/org/kohsuke/github/GHIssue.java +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -64,7 +64,7 @@ public class GHIssue extends GHObject implements Reactable { @SkipFromToString protected String body; // for backward compatibility with < 1.63, this collection needs to hold instances of Label, not GHLabel - protected List