Skip to content

Commit a63306d

Browse files
authored
Merge pull request #1318 from sahansera/deleted-user-scenarios
Fix NullPointerException when a reviewer is deleted (#771)
2 parents 7df326e + 614ffcc commit a63306d

32 files changed

+2351
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ public String getBody() {
8080
* the io exception
8181
*/
8282
public GHUser getUser() throws IOException {
83-
return owner.root().getUser(user.getLogin());
83+
if (user != null) {
84+
return owner.root().getUser(user.getLogin());
85+
}
86+
return null;
8487
}
8588

8689
/**

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Collection;
1010
import java.util.Collections;
1111
import java.util.List;
12+
import java.util.Optional;
1213

1314
import static org.hamcrest.Matchers.*;
1415

@@ -543,6 +544,38 @@ public void getUserTest() throws IOException {
543544
}
544545
}
545546

547+
@Test
548+
public void checkNonExistentReviewer() throws IOException {
549+
// PR id is based on https://github.com/sahansera/TestRepo/pull/1
550+
final GHPullRequest pullRequest = getRepository().getPullRequest(1);
551+
final Optional<GHPullRequestReview> review = pullRequest.listReviews().toList().stream().findFirst();
552+
final GHUser reviewer = review.get().getUser();
553+
554+
assertThat(pullRequest.getRequestedReviewers(), is(empty()));
555+
assertThat(review, notNullValue());
556+
assertThat(reviewer, is(nullValue()));
557+
}
558+
559+
@Test
560+
public void checkNonExistentAuthor() throws IOException {
561+
// PR id is based on https://github.com/sahansera/TestRepo/pull/2
562+
final GHPullRequest pullRequest = getRepository().getPullRequest(2);
563+
564+
assertThat(pullRequest.getUser(), is(notNullValue()));
565+
assertThat(pullRequest.getUser().login, is("ghost"));
566+
}
567+
568+
@Test
569+
public void checkPullRequestReviewer() throws IOException {
570+
// PR id is based on https://github.com/sahansera/TestRepo/pull/6
571+
final GHPullRequest pullRequest = getRepository().getPullRequest(6);
572+
final Optional<GHPullRequestReview> review = pullRequest.listReviews().toList().stream().findFirst();
573+
final GHUser reviewer = review.get().getUser();
574+
575+
assertThat(review, notNullValue());
576+
assertThat(reviewer, notNullValue());
577+
}
578+
546579
protected GHRepository getRepository() throws IOException {
547580
return getRepository(gitHub);
548581
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 2032296,
4+
"node_id": "MDQ6VXNlcjIwMzIyOTY=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/2032296?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/hub4j-test-org",
8+
"html_url": "https://github.com/hub4j-test-org",
9+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
10+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
14+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
15+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
16+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
18+
"type": "User"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"id": 423321524,
3+
"node_id": "R_kgDOGTtftA",
4+
"name": "github-api",
5+
"full_name": "hub4j-test-org/github-api",
6+
"private": false,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 2032296,
10+
"node_id": "MDQ6VXNlcjIwMzIyOTY=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/2032296?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "User",
25+
"site_admin": true
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/github-api",
28+
"description": "Recreating an issue",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/github-api",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/github-api/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/github-api/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/github-api/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/github-api/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/github-api/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/github-api/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/github-api/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/github-api/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/github-api/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/github-api/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/github-api/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/github-api/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/github-api/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/github-api/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/github-api/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/github-api/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/github-api/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/github-api/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/github-api/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/github-api/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/github-api/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/github-api/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/github-api/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/github-api/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/github-api/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/github-api/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/github-api/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/github-api/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/github-api/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/github-api/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/github-api/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/github-api/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/github-api/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/github-api/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/github-api/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments",
67+
"created_at": "2021-11-01T03:08:27Z",
68+
"updated_at": "2021-11-01T03:09:57Z",
69+
"pushed_at": "2021-11-08T13:29:40Z",
70+
"git_url": "git://github.com/hub4j-test-org/github-api.git",
71+
"ssh_url": "[email protected]:hub4j-test-org/github-api.git",
72+
"clone_url": "https://github.com/hub4j-test-org/github-api.git",
73+
"svn_url": "https://github.com/hub4j-test-org/github-api",
74+
"homepage": null,
75+
"size": 2,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"allow_forking": true,
91+
"is_template": false,
92+
"topics": [],
93+
"visibility": "public",
94+
"forks": 0,
95+
"open_issues": 0,
96+
"watchers": 0,
97+
"default_branch": "main",
98+
"permissions": {
99+
"admin": true,
100+
"maintain": true,
101+
"push": true,
102+
"triage": true,
103+
"pull": true
104+
},
105+
"temp_clone_token": "",
106+
"allow_squash_merge": true,
107+
"allow_merge_commit": true,
108+
"allow_rebase_merge": true,
109+
"allow_auto_merge": false,
110+
"delete_branch_on_merge": false,
111+
"allow_update_branch": false,
112+
"network_count": 0,
113+
"subscribers_count": 1
114+
}

0 commit comments

Comments
 (0)