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

Asking for a list of commits from a PR that has been a sourced from a search results in GHFileNotFoundException #1778

Closed
NlightNFotis opened this issue Jan 10, 2024 · 0 comments · Fixed by #1779

Comments

@NlightNFotis
Copy link
Contributor

NlightNFotis commented Jan 10, 2024

Describe the bug

Hello.

I'm writing a test program to evaluate the library:

package org.example;

import org.kohsuke.github.*;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello world from Github API Experiment!");
        String token = "xxx";

        try {
            // Connect
            GitHub github = new GitHubBuilder().withOAuthToken(token).build();
            System.out.println("Built Github API bridge");
            System.out.println("With valid credential: " + github.isCredentialValid());

            // Get ref from environment
            String ref_name = System.getenv("GITHUB_REF_NAME");
            System.out.println("Ref name is " + ref_name);

            GHRepository repo = github.getRepository("NlightNFotis/demo-spring-petclinic");

            // Create branch out of ref
            GHBranch pr_branch = repo.getBranch(ref_name);

            // Find pull request corresponding to ref
            GHPullRequestSearchBuilder builder = repo.searchPullRequests().isOpen();
            var pr_list = builder.list();
            System.out.println("PRs found " + pr_list.getTotalCount());

            // Iterate through PRs
            for (var pr : pr_list) {
                System.out.println("PR URL: " + pr.listCommits().toArray()[0].getUrl());
            }
        } catch (Exception e) {
            System.out.println("Got an exception: " + e.getMessage());
        }
    }
}

This unfortunately results in a 404 from Github's endpoint, with the output being the following:

Hello world from Github API Experiment!
Built Github API bridge
With valid credential: true
Ref name is cover_action
PRs found 1
Got an exception: https://api.github.com/repos/NlightNFotis/demo-spring-petclinic/issues/1/commits {"message":"Not Found","documentation_url":"https://docs.github.com/rest"}

The error that shows up is a GHFileNotFoundException.

I spent some time trying to debug it, and I understand that the issue is because of the /issues/ path in the URL being queried. If I change the URL to point to /pulls/ instead of /issues/ in a debugger, the issue vanishes: https://api.github.com/repos/NlightNFotis/demo-spring-petclinic/pulls/1/commits.

I have managed to identify the culprit as the GHPullRequest.getApiRoute method.

/**
     * Gets the api route.
     *
     * @return the api route
     */
    @Override
    protected String getApiRoute() {
        if (owner == null) {
            // Issues returned from search to do not have an owner. Attempt to use url.
            final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
            return StringUtils.prependIfMissing(url.toString().replace(root().getApiUrl(), ""), "/");
        }
        return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/pulls/" + number;
    }

What I understand about the issue is:

  1. In my code, I retrieve pull requests using a search. This results in objects that have no owner.
  2. Inside of GHPullRequest.getApiRoute, the owner == null path is being followed, trying to compensate for the lack of an owner by trying to get an API URL from previous API responses.
  3. This results in a URL that contains the /issues/ path, e.g. repos/NlightNFotis/demo-sping-petclinic/issues/, which is then appended to by other queries, such as listCommits().
  4. The request is then sent to GitHub, resulting in a 404.

Expected behavior

If I build with a patched version locally, I get the (expected) output of:

Hello world from Github API Experiment!
Built Github API bridge
With valid credential: true
Ref name is cover_action
PRs found 1
PR URL: https://github.com/NlightNFotis/demo-spring-petclinic/commit/c0b514d30b11a03dbaea71cebec4d4f6cad0f45a

Desktop (please complete the following information):

  • OS: [e.g. iOS] MacOS Sonoma 14.0
  • Browser [e.g. chrome, safari] Safari 17.0
  • Version [e.g. 22] 1.319

Additional context

I have a fix and a regression test locally that I will try to push. It's a drive by change, so I don't know if it satisfies the quality standards of the project, though I have taken every care to try to meet them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant