Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent NPE when accessing description for GHLicense #820

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/main/java/org/kohsuke/github/GHLicense.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@

package org.kohsuke.github;

import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* The GitHub Preview API's license information
Expand Down Expand Up @@ -78,14 +78,6 @@ public String getName() {
return name;
}

/**
* @return API URL of this object.
*/
@WithBridgeMethods(value = String.class, adapterMethod = "urlToString")
public URL getUrl() {
return GitHubClient.parseURL(url);
}

/**
* Featured licenses are bold in the new repository drop-down
*
Expand Down Expand Up @@ -199,7 +191,14 @@ protected synchronized void populate() throws IOException {
if (description != null)
return; // already populated

root.createRequest().withUrlPath(url).fetchInto(this);
if (root == null || root.isOffline()) {
return; // cannot populate, will have to live with what we have
}

URL url = getUrl();
if (url != null) {
root.createRequest().setRawUrlPath(url.toString()).fetchInto(this);
}
}

@Override
Expand All @@ -210,12 +209,12 @@ public boolean equals(Object o) {
return false;

GHLicense that = (GHLicense) o;
return this.url.equals(that.url);
return Objects.equals(getUrl(), that.getUrl());
}

@Override
public int hashCode() {
return url.hashCode();
return Objects.hashCode(getUrl());
}

GHLicense wrap(GitHub root) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/org/kohsuke/github/GHLicenseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.io.IOException;
import java.net.URL;

import static org.hamcrest.Matchers.*;

/**
* @author Duncan Dickinson
*/
Expand Down Expand Up @@ -190,4 +192,21 @@ public void checkRepositoryLicenseContent() throws IOException {
fail("Expected the license to be Base64 encoded but instead it was " + content.getEncoding());
}
}

/**
* Accesses the 'bndtools/bnd' repo using {@link GitHub#getRepository(String)} and then calls
* {@link GHRepository#getLicense()}. The description is null due to multiple licences
*
* @throws IOException
* if test fails
*/
@Test
public void checkRepositoryLicenseForIndeterminate() throws IOException {
GHRepository repo = gitHub.getRepository("bndtools/bnd");
GHLicense license = repo.getLicense();
assertNotNull("The license is populated", license);
assertThat(license.getKey(), equalTo("other"));
assertThat(license.getDescription(), is(nullValue()));
assertThat(license.getUrl(), is(nullValue()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
{
"id": 606956,
"node_id": "MDEwOlJlcG9zaXRvcnk2MDY5NTY=",
"name": "bnd",
"full_name": "bndtools/bnd",
"private": false,
"owner": {
"login": "bndtools",
"id": 1130828,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjExMzA4Mjg=",
"avatar_url": "https://avatars1.githubusercontent.com/u/1130828?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bndtools",
"html_url": "https://github.com/bndtools",
"followers_url": "https://api.github.com/users/bndtools/followers",
"following_url": "https://api.github.com/users/bndtools/following{/other_user}",
"gists_url": "https://api.github.com/users/bndtools/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bndtools/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bndtools/subscriptions",
"organizations_url": "https://api.github.com/users/bndtools/orgs",
"repos_url": "https://api.github.com/users/bndtools/repos",
"events_url": "https://api.github.com/users/bndtools/events{/privacy}",
"received_events_url": "https://api.github.com/users/bndtools/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/bndtools/bnd",
"description": "Bnd/Bndtools. Tooling to build OSGi bundles including Eclipse, Maven, and Gradle plugins.",
"fork": false,
"url": "https://api.github.com/repos/bndtools/bnd",
"forks_url": "https://api.github.com/repos/bndtools/bnd/forks",
"keys_url": "https://api.github.com/repos/bndtools/bnd/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/bndtools/bnd/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/bndtools/bnd/teams",
"hooks_url": "https://api.github.com/repos/bndtools/bnd/hooks",
"issue_events_url": "https://api.github.com/repos/bndtools/bnd/issues/events{/number}",
"events_url": "https://api.github.com/repos/bndtools/bnd/events",
"assignees_url": "https://api.github.com/repos/bndtools/bnd/assignees{/user}",
"branches_url": "https://api.github.com/repos/bndtools/bnd/branches{/branch}",
"tags_url": "https://api.github.com/repos/bndtools/bnd/tags",
"blobs_url": "https://api.github.com/repos/bndtools/bnd/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/bndtools/bnd/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/bndtools/bnd/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/bndtools/bnd/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/bndtools/bnd/statuses/{sha}",
"languages_url": "https://api.github.com/repos/bndtools/bnd/languages",
"stargazers_url": "https://api.github.com/repos/bndtools/bnd/stargazers",
"contributors_url": "https://api.github.com/repos/bndtools/bnd/contributors",
"subscribers_url": "https://api.github.com/repos/bndtools/bnd/subscribers",
"subscription_url": "https://api.github.com/repos/bndtools/bnd/subscription",
"commits_url": "https://api.github.com/repos/bndtools/bnd/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/bndtools/bnd/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/bndtools/bnd/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/bndtools/bnd/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/bndtools/bnd/contents/{+path}",
"compare_url": "https://api.github.com/repos/bndtools/bnd/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/bndtools/bnd/merges",
"archive_url": "https://api.github.com/repos/bndtools/bnd/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/bndtools/bnd/downloads",
"issues_url": "https://api.github.com/repos/bndtools/bnd/issues{/number}",
"pulls_url": "https://api.github.com/repos/bndtools/bnd/pulls{/number}",
"milestones_url": "https://api.github.com/repos/bndtools/bnd/milestones{/number}",
"notifications_url": "https://api.github.com/repos/bndtools/bnd/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/bndtools/bnd/labels{/name}",
"releases_url": "https://api.github.com/repos/bndtools/bnd/releases{/id}",
"deployments_url": "https://api.github.com/repos/bndtools/bnd/deployments",
"created_at": "2010-04-12T17:30:15Z",
"updated_at": "2020-05-19T13:56:51Z",
"pushed_at": "2020-05-19T23:45:09Z",
"git_url": "git://github.com/bndtools/bnd.git",
"ssh_url": "git@github.com:bndtools/bnd.git",
"clone_url": "https://github.com/bndtools/bnd.git",
"svn_url": "https://github.com/bndtools/bnd",
"homepage": "https://bndtools.org",
"size": 705802,
"stargazers_count": 415,
"watchers_count": 415,
"language": "Java",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 260,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 155,
"license": {
"key": "other",
"name": "Other",
"spdx_id": "NOASSERTION",
"url": null,
"node_id": "MDc6TGljZW5zZTA="
},
"forks": 260,
"open_issues": 155,
"watchers": 415,
"default_branch": "master",
"permissions": {
"admin": false,
"push": false,
"pull": true
},
"temp_clone_token": "",
"organization": {
"login": "bndtools",
"id": 1130828,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjExMzA4Mjg=",
"avatar_url": "https://avatars1.githubusercontent.com/u/1130828?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bndtools",
"html_url": "https://github.com/bndtools",
"followers_url": "https://api.github.com/users/bndtools/followers",
"following_url": "https://api.github.com/users/bndtools/following{/other_user}",
"gists_url": "https://api.github.com/users/bndtools/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bndtools/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bndtools/subscriptions",
"organizations_url": "https://api.github.com/users/bndtools/orgs",
"repos_url": "https://api.github.com/users/bndtools/repos",
"events_url": "https://api.github.com/users/bndtools/events{/privacy}",
"received_events_url": "https://api.github.com/users/bndtools/received_events",
"type": "Organization",
"site_admin": false
},
"network_count": 260,
"subscribers_count": 46
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false,
"name": "Liam Newman",
"company": "Cloudbees, Inc.",
"blog": "",
"location": "Seattle, WA, USA",
"email": "bitwiseman@gmail.com",
"hireable": null,
"bio": "https://twitter.com/bitwiseman",
"public_repos": 183,
"public_gists": 7,
"followers": 159,
"following": 9,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2020-05-19T17:22:31Z",
"private_gists": 19,
"total_private_repos": 12,
"owned_private_repos": 0,
"disk_usage": 33697,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": "61db4e6e-f767-4ac6-ad47-4008ba850d1a",
"name": "repos_bndtools_bnd",
"request": {
"url": "/repos/bndtools/bnd",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
}
},
"response": {
"status": 200,
"bodyFileName": "repos_bndtools_bnd-2.json",
"headers": {
"Date": "Tue, 19 May 2020 23:58:07 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4983",
"X-RateLimit-Reset": "1589935075",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With",
"Accept-Encoding"
],
"ETag": "W/\"1a59fa793bef38ba268640007d48b566\"",
"Last-Modified": "Tue, 19 May 2020 13:56:51 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "repo",
"X-GitHub-Media-Type": "unknown, github.v3",
"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": "FE57:05CD:20923:28641:5EC4728E"
}
},
"uuid": "61db4e6e-f767-4ac6-ad47-4008ba850d1a",
"persistent": true,
"insertionIndex": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": "147795dc-b685-4f3e-8001-8a57588c1073",
"name": "repos_bndtools_bnd_license",
"request": {
"url": "/repos/bndtools/bnd/license",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
}
}
},
"response": {
"status": 200,
"bodyFileName": "repos_bndtools_bnd_license-3.json",
"headers": {
"Date": "Tue, 19 May 2020 23:58:07 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4982",
"X-RateLimit-Reset": "1589935075",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With",
"Accept-Encoding"
],
"ETag": "W/\"717d473a2991de5d2c018d3113dbabda\"",
"Last-Modified": "Tue, 19 May 2020 13:56:47 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "",
"X-GitHub-Media-Type": "unknown, github.v3",
"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": "FE57:05CD:20936:2866D:5EC4728F"
}
},
"uuid": "147795dc-b685-4f3e-8001-8a57588c1073",
"persistent": true,
"insertionIndex": 3
}
Loading