Skip to content

Commit 0fac2a9

Browse files
gsmetbitwiseman
andauthored
Make sure we refresh the PRs with the PR API (and not the issues API) (#1810)
* Make sure we refresh the PRs with the PR API (and not the issues API) When a PR was loaded from a search, the refresh() method was reloading information from the issues API, which would lead to some information not being refreshed properly, typically the mergeable state. Related to #1779 (comment) * Update GHPullRequestTest.java * Update GHPullRequestTest.java * Update GHPullRequestTest.java * Update GHPullRequestTest.java --------- Co-authored-by: Liam Newman <[email protected]>
1 parent 352106f commit 0fac2a9

File tree

14 files changed

+1950
-4
lines changed

14 files changed

+1950
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,14 @@ public void refresh() throws IOException {
415415
return; // cannot populate, will have to live with what we have
416416
}
417417

418-
URL url = getUrl();
419-
if (url != null) {
420-
root().createRequest().withPreview(SHADOW_CAT).setRawUrlPath(url.toString()).fetchInto(this).wrapUp(owner);
421-
}
418+
// we do not want to use getUrl() here as it points to the issues API
419+
// and not the pull request one
420+
URL absoluteUrl = GitHubRequest.getApiURL(root().getApiUrl(), getApiRoute());
421+
root().createRequest()
422+
.withPreview(SHADOW_CAT)
423+
.setRawUrlPath(absoluteUrl.toString())
424+
.fetchInto(this)
425+
.wrapUp(owner);
422426
}
423427

424428
/**

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,44 @@ public void reactions() throws Exception {
907907
assertThat(p.listReactions().toList(), hasSize(0));
908908
}
909909

910+
/**
911+
* Test refreshing a PR coming from the search results.
912+
*
913+
* @throws Exception
914+
* the exception
915+
*/
916+
@Test
917+
public void refreshFromSearchResults() throws Exception {
918+
// To re-record, uncomment the Thread.sleep() calls below
919+
snapshotNotAllowed();
920+
921+
String prName = "refreshFromSearchResults";
922+
GHRepository repository = getRepository();
923+
924+
repository.createPullRequest(prName, "test/stable", "main", "## test");
925+
926+
// we need to wait a bit for the pull request to be indexed by GitHub
927+
// Thread.sleep(2000);
928+
929+
GHPullRequest pullRequestFromSearchResults = repository.searchPullRequests()
930+
.isOpen()
931+
.titleLike(prName)
932+
.list()
933+
.toList()
934+
.get(0);
935+
936+
pullRequestFromSearchResults.getMergeableState();
937+
938+
// wait a bit for the mergeable state to get populated
939+
// Thread.sleep(5000);
940+
941+
assertThat("Pull request is supposed to have been refreshed and have a mergeable state",
942+
pullRequestFromSearchResults.getMergeableState(),
943+
equalTo("clean"));
944+
945+
pullRequestFromSearchResults.close();
946+
}
947+
910948
/**
911949
* Gets the repository.
912950
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "[email protected]",
19+
"twitter_username": null,
20+
"is_verified": false,
21+
"has_organization_projects": true,
22+
"has_repository_projects": true,
23+
"public_repos": 26,
24+
"public_gists": 0,
25+
"followers": 2,
26+
"following": 0,
27+
"html_url": "https://github.com/hub4j-test-org",
28+
"created_at": "2014-05-10T19:39:11Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
30+
"archived_at": null,
31+
"type": "Organization",
32+
"total_private_repos": 6,
33+
"owned_private_repos": 6,
34+
"private_gists": 0,
35+
"disk_usage": 12014,
36+
"collaborators": 1,
37+
"billing_email": "[email protected]",
38+
"default_repository_permission": "none",
39+
"members_can_create_repositories": false,
40+
"two_factor_requirement_enabled": false,
41+
"members_allowed_repository_creation_type": "none",
42+
"members_can_create_public_repositories": false,
43+
"members_can_create_private_repositories": false,
44+
"members_can_create_internal_repositories": false,
45+
"members_can_create_pages": true,
46+
"members_can_fork_private_repositories": false,
47+
"web_commit_signoff_required": false,
48+
"members_can_create_public_pages": true,
49+
"members_can_create_private_pages": true,
50+
"plan": {
51+
"name": "free",
52+
"space": 976562499,
53+
"private_repos": 10000,
54+
"filled_seats": 50,
55+
"seats": 3
56+
},
57+
"advanced_security_enabled_for_new_repositories": false,
58+
"dependabot_alerts_enabled_for_new_repositories": false,
59+
"dependabot_security_updates_enabled_for_new_repositories": false,
60+
"dependency_graph_enabled_for_new_repositories": false,
61+
"secret_scanning_enabled_for_new_repositories": false,
62+
"secret_scanning_push_protection_enabled_for_new_repositories": false,
63+
"secret_scanning_push_protection_custom_link_enabled": false,
64+
"secret_scanning_push_protection_custom_link": null,
65+
"secret_scanning_validity_checks_enabled": false
66+
}

0 commit comments

Comments
 (0)