cli/cli

`gh repo view --json` should allow fetching v2 projects

major0 opened this issue · 5 comments

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": []
}

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.

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
      }
    }
  }
}'

@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.

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).

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