hub4j / github-api

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

GHProjectCard return null on getContent() #1024

Open WolverMinion opened 3 years ago

WolverMinion commented 3 years ago

Describe the bug I need a issue list from a project column. The implementation is in a jenkins shared library. It's easy to use. Unfortunately I get an error. See this Code. github-api is included as a dependency with version 1.122

To Reproduce

import ....
final GitHub github = GitHub.connectToEnterpriseWithOAuth('https://github.enterprise.com/api/v3', null, 'usertoken123')
final GHProjectColumn projectColumn = github.getProjectColumn(12345)
projectColumn.listCards().each { projectCard ->
   echo """
ProjectCard: ${projectCard}
ContentUrl: ${projectCard.getContentUrl()}
Content: ${projectCard.getContent()}
"""
    GHIssue issue = projectCard.getContent()
   // issue must not be null
}

https://github.com/hub4j/github-api/blob/8e6dbf37724cd76096b339ea7399fd49d06b49cb/src/main/java/org/kohsuke/github/GHProjectCard.java#L120

Expected behavior projectCard.getContent() return the existing issue object or a helpful error message.

Desktop (please complete the following information):

bitwiseman commented 3 years ago

Do you get an error or do you get null?

WolverMinion commented 3 years ago

i get null

I used the code in jenkins shared library and found that the url is assembled incorrectly.

root.createRequest().withUrlPath(getContentUrl().getPath()).fetch(GHIssue.class).wrap(root)

https://github.enterprise.com/api/v3/api/v3/..... => FileNotFoundException

I need a second github connection with github-domain and it's run correctly.

import ....
final GitHub github = GitHub.connectToEnterpriseWithOAuth('https://github.enterprise.com/api/v3', null, 'usertoken123')
final GitHub github4issue = GitHub.connectToEnterpriseWithOAuth('https://github.enterprise.com', null, 'usertoken123')
final GHProjectColumn projectColumn = github.getProjectColumn(12345)
projectColumn.listCards().each { projectCard ->
   echo """
ProjectCard: ${projectCard}
ContentUrl: ${projectCard.getContentUrl()}
Content: ${projectCard.getContent()}
"""
    GHIssue issue = github4issue.createRequest().withUrlPath(projectCard.getContentUrl()).fetch(org.kohsuke.github.GHIssue.class).wrap(github4issue)
   // issue is not null
}
bitwiseman commented 3 years ago

Right, here's the issue:

https://github.com/hub4j/github-api/blob/8e6dbf37724cd76096b339ea7399fd49d06b49cb/src/main/java/org/kohsuke/github/GHProjectCard.java#L114-L121

It probably effects these as well:

https://github.com/hub4j/github-api/blob/8e6dbf37724cd76096b339ea7399fd49d06b49cb/src/main/java/org/kohsuke/github/GHProjectCard.java#L89-L98

https://github.com/hub4j/github-api/blob/8e6dbf37724cd76096b339ea7399fd49d06b49cb/src/main/java/org/kohsuke/github/GHProjectCard.java#L71-L77

Would you be willing to fix these and file a PR with tests to cover these methods?

WolverMinion commented 3 years ago

I like to do that. This can take a while. I have to set everything up for development first.

can yout set me as assignee for this issue?

i get started here: https://github.com/helheim-project/github-api/tree/fix/projectcard-get-object-from-url

km2018 commented 2 years ago

\assign

km2018 commented 2 years ago

Hi @bitwiseman was looking for some guidance on reproducing this issue. I'm mainly having trouble with connectToEnterpriseWithOAuth. I see that most of the connect to Enterprise methods are deprecated except for connectToEnterpriseWithOAuth. I'm mainly unsure what Enterprise URL I should input to the method, and how I can write a test for this if I do not have an Enterprise account. I believe some things have changed with the codebase since this issue was created, so I think writing a test to reproduce will help me see where the current version stands on the issue. Thanks in advance!

bitwiseman commented 2 years ago

@km2018
Strictly speaking you'll need a GitHub Enterprise Server and then you'd configure the URL for it.

But you don't need to use a GHE instance, you can use github.com. The problem is that URL.getPath() returns the path of the URL. This works fine if your URL is https://api.github.com/repos/12345 where the root of all the endpoints is https://api.github.com. It is failing because on GHE the URL is something like https://github.mycompany.com/api/v3/repos/12345 and the root of all endpoints is https://github.mycompany.com/api/v3. Using URL.getPath() and then appending it to the root produces the wrong target.

I would say you can replace URL.getPath() with something the removes the apiRoot from the URL string but not all URLs for a an instance point the the same place. On github.com for example, there is not only api.github.com but also usercontent.github.com and others.

It would also be good to use ArchUnit in ArchTest to prevent usage of URL.getPath() in the library. It is clearly not a reliable way to do this.

Note: I have unmarked this as a "good first issue". I can see now it is a bit more involved than I first thought. Unless you have some personal interest in fixing this issue, you might consider working on an easier issue first and come back to this one when you're familiar with the code.

km2018 commented 2 years ago

Hi @bitwiseman thanks for the response! I think I want to keep working on this issue. Just to clarify, are you saying I can use connectToEnterpriseWithOAuth without a GHE instance (using https://api.github.com), but I won't actually be able to re-produce the issue without a GitHub Enterprise Server? From what I understand, this issue won't occur unless it contains /api/v3 in the URL, which is only the case with Enterprise URLs.

bitwiseman commented 2 years ago

@km2018 You could record data from github.com and then manually adjust it to have "/api/v3" to in the path (by changing the mapping json files). Does that make sense?

km2018 commented 2 years ago

@bitwiseman Thought about this for a bit and I think I'm still a bit confused - could you maybe give an example in code to show this?

km2018 commented 2 years ago

Hi, I was hoping to get access to the test organization, so I could work on testing this issue.

bitwiseman commented 2 years ago

@km2018 Invitation sent.

bitwiseman commented 2 years ago

@km2018 I'll see if I can find time to create an example for you later today.

km2018 commented 2 years ago

@bitwiseman Thanks for sending the invitation - I went ahead and accepted it. However, when trying to take a snapshot, I still received a "Must have admin rights to repository". I tried to follow the instructions in CONTRIBUTING.md, and I've correctly configured GITHUB_OAUTH. Here is the error I'm getting:

Screen Shot 2021-12-08 at 3 25 08 PM

:

bitwiseman commented 2 years ago

@km2018 I think I know what is wrong. The CONTRIBUTING.md is out of date and doesn't mention that you'll need to give you're OAuth token the appropriate permissions, including admin rights to create and delete repositories in this case.