Skip to content

Using 'gh project copy' without proper scope gives confusing errors #8326

Open

Description

@chrishodson

Describe the bug

When logged in, but without the 'project' scope, the behavior leads to confusing error messages, that do not lead to scope/permissions being the culprit.

$ gh --version
gh version 2.38.0 (2023-11-01)
https://github.com/cli/cli/releases/tag/v2.38.0

Steps to reproduce the behavior

$ gh auth status
github.com
  ✓ Logged in to github.com as chrishodson (GITHUB_TOKEN)
  ✓ Git operations for github.com configured to use https protocol.
  ✓ Token: ghu_************************************

If you do a list of projects, it seems to work:

$ gh project list
NUMBER  TITLE  STATE  ID
0              open   

But if you try to use that number (0):

$ gh project copy 0 --drafts --source-owner chrishodson-test-org --target-owner '@me' --title 'Test_Project'
? Which project would you like to use? 
(#0)
GraphQL: Could not resolve to a node with the global id of '' (copyProjectV2)

Note also that it asked which project number to use, even though one was specified.

If you try and use it non-interactively:

$ yes | gh project copy 0 --drafts --source-owner chrishodson-test-org --target-owner '@me' --title 'Test_Project'
project number is required when not running interactively

Expected vs actual behavior

Working as expected after gh auth refresh -s project:

$ gh auth status
github.com
  ✓ Logged in to github.com as chrishodson (/home/codespace/.config/gh/hosts.yml)
  ✓ Git operations for github.com configured to use https protocol.
  ✓ Token: gho_************************************
  ✓ Token scopes: gist, project, read:org, repo, workflow

$ gh project list --owner chrishodson-test-org
NUMBER  TITLE            STATE  ID                  
5       sample template  open   PVT_****
$ gh project copy 5 --drafts --source-owner chrishodson-test-org --target-owner '@me' --title 'Test_Project'
https://github.com/users/chrishodson/projects/3

Both the list and copy command make no mention that there is a scope missing, or even to check that. The behavior isn't wrong, but the error messages could be expanded.

In addition, showing the results of the list command, but with a project number of 0 is confusing.

Finally, the handling of 0 as the project number when trying to do a copy is a bit confusing

Logs

See inline

Activity

  1. added
    bug
    on Nov 13, 2023
  2. added
    needs-triage
    on Nov 13, 2023
  3. andyfeller commented on Nov 23, 2023

    @andyfeller
    Contributor

    @chrishodson : Thanks for opening this issue up! ❤

    Several things that stood out to me

    1. this is through a GitHub user-to-server token (ghu)

      Rather than login through the web browser (resulting in an OAuth access token gho) or using a classic token or using a classic or fine grain PAT, this session appears to be authenticated in a different way.

      I don't know if this is meaningful because I get a different behavior when using the web flow:

      ~ $ gh auth status
      github.com
        ✓ Logged in to github.com as andyfeller (keyring)
        ✓ Git operations for github.com configured to use https protocol.
        ✓ Token: gho_************************************
        ✓ Token scopes: gist, read:org, repo, workflow
       
       
      ~ $ gh project list
      error: your authentication token is missing required scopes [read:project]
      To request it, run:  gh auth refresh -s read:project

      When I refresh my token with read:project scope and attempt to gh project copy, I get an error message as expected that I don't have project scope:

      ~ $ gh auth refresh -s read:project
      ! Using secure storage could break installed extensions
      ! First copy your one-time code: C83A-4E32
      Press Enter to open github.com in your browser... 
      ✓ Authentication complete.
       
      ~ $ gh auth status
      github.com
        ✓ Logged in to github.com as andyfeller (keyring)
        ✓ Git operations for github.com configured to use https protocol.
        ✓ Token: gho_************************************
        ✓ Token scopes: gist, read:org, read:project, repo, workflow
       
      ~ $ gh project copy 1 --drafts --title "Test copy"
      ? Which owner would you like to use? andyfeller
      ? Which owner would you like to use? andyfeller
      error: your authentication token is missing required scopes [project]
      To request it, run:  gh auth refresh -s project
    2. none of the token scopes are listed for ghu tokens

      maybe this is cut off from creating the issue or we simply do not pull that information the same way as other tokens.

    3. project with a number of 0 is weird

      I'm not sure I've ever seen an issue, pull request, discussion, workflow, workflow run, or project with a number of 0 before.

    What is going on under the covers

    Let's figure out how the GitHub CLI is trying to suggest users fix permission scope issues and work backwards.

    1. Handling errors and signaling missing permissions

    func handleError(err error) error {
    var gerr api.GraphQLError
    if errors.As(err, &gerr) {
    missing := set.NewStringSet()
    for _, e := range gerr.Errors {
    if e.Type != "INSUFFICIENT_SCOPES" {
    continue
    }
    missing.AddValues(requiredScopesFromServerMessage(e.Message))
    }
    if missing.Len() > 0 {
    s := missing.ToSlice()
    // TODO: this duplicates parts of generateScopesSuggestion
    return fmt.Errorf(
    "error: your authentication token is missing required scopes %v\n"+
    "To request it, run: gh auth refresh -s %s",
    s,
    strings.Join(s, ","))
    }
    }
    return err
    }

    2. Handling GraphQL mutations for gh projects commands

    // TODO: un-export this since it couples the caller heavily to api.GraphQLClient
    func (c *Client) Mutate(operationName string, query interface{}, variables map[string]interface{}) error {
    err := c.apiClient.Mutate(operationName, query, variables)
    return handleError(err)
    }

    3. Top-level gh project copy command invocation

    err = config.client.Mutate("CopyProjectV2", query, variables)
    if err != nil {
    return err
    }

    Theory

    I think there is something about GitHub user-to-server tokens at play here that differs how we get scope information and handle these errors.

  4. added
    priority-2
    and removed
    needs-triage
    on Nov 23, 2023
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

    bugpriority-2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions