Skip to content

gh repo view --json should allow fetching v2 projects #8960

Description

@major0

Describe the bug

The repo view command fails to return a list of projects.

Steps to reproduce the behavior

  1. gh repo view --json projects
  2. View the output '....'

Expected vs actual behavior

Expected a list of projects

Logs

$ gh repo view --json projects
{
  "projects": []
}

Activity

  1. babakks commented on Apr 14, 2024

    @babakks
    Member

    I can confirm this with one of my repos that has a project:

    $ gh repo view --json 'projects' ████████
    {
      "projects": []
    }
    

    This is the debug output:

    $ GH_DEBUG=api gh repo view --json 'projects' ████████
    * Request at 2024-04-14 21:41:31.467797815 +0100 BST m=+0.031329531
    * Request to https://api.github.com/graphql
    > POST /graphql HTTP/1.1
    > Host: api.github.com
    > Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
    > Authorization: token ████████████████████
    > Content-Length: 255
    > Content-Type: application/json; charset=utf-8
    > Graphql-Features: merge_queue
    > Time-Zone: Europe/London
    > User-Agent: GitHub CLI 2.42.0
    
    GraphQL query:
    query RepositoryInfo($owner: String!, $name: String!) {
        repository(owner: $owner, name: $name) {projects(first:100,states:OPEN){nodes{id,name,number,body,resourcePath}}}
      }
    GraphQL variables: {"name":"████████","owner":"████████"}
    
    < HTTP/2.0 200 OK
    < Access-Control-Allow-Origin: *
    < Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
    < Content-Security-Policy: default-src 'none'
    < Content-Type: application/json; charset=utf-8
    < Date: Sun, 14 Apr 2024 20:41:31 GMT
    < Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
    < Server: GitHub.com
    < Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
    < Vary: Accept-Encoding, Accept, X-Requested-With
    < X-Accepted-Oauth-Scopes: repo
    < X-Content-Type-Options: nosniff
    < X-Frame-Options: deny
    < X-Github-Media-Type: github.merge-info-preview; param=nebula-preview; format=json
    < X-Github-Request-Id: DBA2:126079:45F4387:478230B:661C3F7B
    < X-Oauth-Client-Id: 178c6fc778ccc68e1d6a
    < X-Oauth-Scopes: admin:public_key, delete_repo, gist, read:org, read:project, repo
    < X-Ratelimit-Limit: 5000
    < X-Ratelimit-Remaining: 4901
    < X-Ratelimit-Reset: 1713129978
    < X-Ratelimit-Resource: graphql
    < X-Ratelimit-Used: 99
    < X-Xss-Protection: 0
    
    {
      "data": {
        "repository": {
          "projects": {
            "nodes": []
          }
        }
      }
    }
    
    * Request took 315.205145ms
    

    Further investigations

    As of my investigations, the CLI uses the projects (which is of type ProjectConnection defined here) field of the Repository type to retrieve project information:

    cli/api/query_builder.go

    Lines 488 to 489 in a42450e

    case "projects":
    q = append(q, "projects(first:100,states:OPEN){nodes{id,name,number,body,resourcePath}}")

    There's also a projectsV2 (of type ProjectV2Connection) in the docs, so I tried to change the GraphQL query to use that instead (code below), but that didn't work, either. Actually, using the V2 type, I got null (instead of an empty array, []):

    case "projects":
    	q = append(q, "projectsV2(first:100){nodes{id,title,number,readme,url}}")

    I'm not sure if this is a problem with the CLI, because the query seems to be in the right format. Maybe something has changed on the GH API service.

  2. major0 commented on Apr 17, 2024

    @major0
    Author

    I did manage to get a projectsV2 query to return useful results:

    gh api graphql -F owner='{owner}' -F repo='{repo}' -f query='
    query ($owner: String!, $repo: String!){
      repository(owner: $owner, name: $repo) {
        projectsV2(first: 10) {
          nodes {
            id
            databaseId
            title
            number
          }
        }
      }
    }'
  3. babakks commented on Apr 21, 2024

    @babakks
    Member

    @major0 Thanks for the hint. I can confirm this works. I pushed this branch onto my fork that let's you run this:

    gh repo view --json 'projectsV2' owner/repo

    Which returns this:

    {
      "projectsV2": {
        "Nodes": [
          {
            "id": "...",
            "title": "...",
            "number": 1,
            "resourcePath": "",
            "closed": false,
            "url": "..."
          }
        ]
      }
    }

    Since this issue is not yet labeled as help-wanted, I wouldn't push the PR. But as soon as maintainers approve this, I'll go ahead and make the PR.

    @williammartin @andyfeller I mentioned you guys to hear your opinion.

  4. changed the title [-]repo view --json projects returns empty list[/-] [+]`gh repo view --json` should allow fetching v2 projects[/+] on Apr 22, 2024
  5. added and removed
    bugSomething isn't working
    on Apr 22, 2024
  6. williammartin commented on Apr 22, 2024

    @williammartin
    Member

    Hey folks, great investigation into this, thanks.

    As I think you've discovered, this not a bug but a missing feature. The projects field contains classic projects, and you are looking for v2 projects which are not currently fetchable in this way. I've updated the title and the labels to reflect this.

    @babakks I welcome a PR for this. Let's also make sure it works for gh repo list --json ... (I think it should by default but I'm not 100% sure).

  7. babakks commented on Apr 26, 2024

    @babakks
    Member

    @williammartin I just submitted a PR (#9007) for this. This also works with gh repo list command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-reporelating to the gh repo commandhelp wantedContributions welcomeneeds-triageneeds to be reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions