Skip to content

Commit

Permalink
Make sure we refresh the PRs with the PR API (and not the issues API) (
Browse files Browse the repository at this point in the history
…#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]>
  • Loading branch information
gsmet and bitwiseman authored Mar 11, 2024
1 parent 352106f commit 0fac2a9
Show file tree
Hide file tree
Showing 14 changed files with 1,950 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/main/java/org/kohsuke/github/GHPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,14 @@ public void refresh() throws IOException {
return; // cannot populate, will have to live with what we have
}

URL url = getUrl();
if (url != null) {
root().createRequest().withPreview(SHADOW_CAT).setRawUrlPath(url.toString()).fetchInto(this).wrapUp(owner);
}
// we do not want to use getUrl() here as it points to the issues API
// and not the pull request one
URL absoluteUrl = GitHubRequest.getApiURL(root().getApiUrl(), getApiRoute());
root().createRequest()
.withPreview(SHADOW_CAT)
.setRawUrlPath(absoluteUrl.toString())
.fetchInto(this)
.wrapUp(owner);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/kohsuke/github/GHPullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,44 @@ public void reactions() throws Exception {
assertThat(p.listReactions().toList(), hasSize(0));
}

/**
* Test refreshing a PR coming from the search results.
*
* @throws Exception
* the exception
*/
@Test
public void refreshFromSearchResults() throws Exception {
// To re-record, uncomment the Thread.sleep() calls below
snapshotNotAllowed();

String prName = "refreshFromSearchResults";
GHRepository repository = getRepository();

repository.createPullRequest(prName, "test/stable", "main", "## test");

// we need to wait a bit for the pull request to be indexed by GitHub
// Thread.sleep(2000);

GHPullRequest pullRequestFromSearchResults = repository.searchPullRequests()
.isOpen()
.titleLike(prName)
.list()
.toList()
.get(0);

pullRequestFromSearchResults.getMergeableState();

// wait a bit for the mergeable state to get populated
// Thread.sleep(5000);

assertThat("Pull request is supposed to have been refreshed and have a mergeable state",
pullRequestFromSearchResults.getMergeableState(),
equalTo("clean"));

pullRequestFromSearchResults.close();
}

/**
* Gets the repository.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"description": "Hub4j Test Org Description (this could be null or blank too)",
"name": "Hub4j Test Org Name (this could be null or blank too)",
"company": null,
"blog": "https://hub4j.url.io/could/be/null",
"location": "Hub4j Test Org Location (this could be null or blank too)",
"email": "[email protected]",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 26,
"public_gists": 0,
"followers": 2,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2020-06-04T05:56:10Z",
"archived_at": null,
"type": "Organization",
"total_private_repos": 6,
"owned_private_repos": 6,
"private_gists": 0,
"disk_usage": 12014,
"collaborators": 1,
"billing_email": "[email protected]",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"members_allowed_repository_creation_type": "none",
"members_can_create_public_repositories": false,
"members_can_create_private_repositories": false,
"members_can_create_internal_repositories": false,
"members_can_create_pages": true,
"members_can_fork_private_repositories": false,
"web_commit_signoff_required": false,
"members_can_create_public_pages": true,
"members_can_create_private_pages": true,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 50,
"seats": 3
},
"advanced_security_enabled_for_new_repositories": false,
"dependabot_alerts_enabled_for_new_repositories": false,
"dependabot_security_updates_enabled_for_new_repositories": false,
"dependency_graph_enabled_for_new_repositories": false,
"secret_scanning_enabled_for_new_repositories": false,
"secret_scanning_push_protection_enabled_for_new_repositories": false,
"secret_scanning_push_protection_custom_link_enabled": false,
"secret_scanning_push_protection_custom_link": null,
"secret_scanning_validity_checks_enabled": false
}
Loading

0 comments on commit 0fac2a9

Please sign in to comment.