hub4j / github-api

Java API for GitHub
https://github-api.kohsuke.org/
MIT License
1.14k stars 727 forks source link

Calling getRef().delete() does not respect the base URL set in .withEndpoint() #1974

Open ajmalab opened 4 days ago

ajmalab commented 4 days ago

Describe the bug Calling getRef().delete() does not respect the base URL set in .withEndpoint() when creating the OAuth client. It uses api.github.com by default. While this would work in production, it blocks the ability to test this endpoint using local mock servers.

To Reproduce Steps to reproduce the behavior:

  1. Create an OAuth client as follows
    var githubClient = new GitHubBuilder().withOAuthToken("my-token").withEndpoint("http://localhost:8080").build();
  2. Call the method to delete a ref:
    githubClient.getRepo("owner/repo").getRef("heads/my-branch").delete()
  3. If you inspect the request in a debugger, you will see that the constructed URL has the host https://api.github.com and not http://localhost:8080.

Expected behavior The getRef().delete() method should respect the base URL passed in via withEndpoint() like the other chained methods of getRepository().

Additional context Library version: 1.321

bitwiseman commented 2 days ago

@ajmalab
What is the JSON that is returned by the call to getRepo and getRef()? The library uses the returned urls to calculate the endpoint to call for delete().

It sounds like you're trying to run local test server. To do that you need to modify the response body to replace api.github.com with localhost:8080.

We use GitHubApiResponseTransformer:

https://github.com/hub4j/github-api/blob/6ea075b2e4994ddad644143de3214ff48749781f/src/test/java/org/kohsuke/github/GitHubWireMockRule.java#L567

Which then calls mapToMockGitHub which has a bunch of body string replacements:

https://github.com/hub4j/github-api/blob/6ea075b2e4994ddad644143de3214ff48749781f/src/test/java/org/kohsuke/github/GitHubWireMockRule.java#L516