Skip to content

gh variable get, gh variable delete, gh secret delete do not resolve Enterprise host based on repository #9274

Description

@yukha-dw

Describe the bug

Most of gh variable/secret command does not working on enterprise due to invalid hostname such as:

  • gh variable get
  • gh variable delete
  • gh secret delete

UPDATED: @andyfeller removed remove aliases from the above list being redundant to delete as well as gh secret get because this command does not exist

Context:

  • I use SSH to clone repository (doesn't matter as I have tried to git clone using HTTPS)
  • git version 2.25.1
  • gh version 2.51.0 (2024-06-13)
  • GitHub Enterprise Server 3.9.13

Steps to reproduce the behavior

  1. chdir to repository

  2. Check FOO_BAR is exist

$ gh variable list
NAME                   VALUE                                           UPDATED             
FOO_BAR                FOO                                             about 58 minutes ago
  1. Get FOO_BAR without --repo arg
$ gh variable get FOO_BAR
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
* Request at 2024-07-01 12:36:05.457290778 +0700 WIB m=+0.061940590
* Request to https://api.github.com/repos/owner-name/repo-name/actions/variables/FOO_BAR
* Request took 752.878786ms
variable FOO_BAR was not found
  1. Get FOO_BAR with --repo arg (https://, http://, without protocol)
$ gh variable get FOO_BAR --repo https://enterprise-github.com/owner-name/repo-name
* Request at 2024-07-01 12:35:21.358327957 +0700 WIB m=+0.047103012
* Request to https://api.github.com/repos/owner-name/repo-name/actions/variables/FOO_BAR
* Request took 638.744861ms
variable FOO_BAR was not found
  1. Get FOO_BAR with REST API
$ gh api -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  --hostname enterprise-github.com
  /repos/owner-name/repo-name/actions/variables/FOO_BAR
* Request at 2024-07-01 12:39:02.137287999 +0700 WIB m=+0.047488350
* Request to https://enterprise-github.com/api/v3/repos/owner-name/repo-name/actions/variables/FOO_BAR
* Request took 747.641379ms
{
  "name": "FOO_BAR",
  "value": "FOO",
  "created_at": "2024-07-01T04:32:35Z",
  "updated_at": "2024-07-01T04:32:35Z"
}

Activity

  1. changed the title [-]gh variable delete does not working on enterprise[/-] [+]gh variable get does not working on enterprise[/+] on Jul 1, 2024
  2. andyfeller commented on Aug 1, 2024

    @andyfeller
    Contributor

    Thank you for your patience, @yukha-dw! This is definitely a bug in gh variable get as it doesn't leverage the host associated with the repo.

    Reproducing

    $ gh auth status
    github.com
      ✓ Logged in to github.com account andyfeller (keyring)
      - Active account: true
      - Git operations protocol: https
      - Token: gho_************************************
      - Token scopes: 'codespace', 'gist', 'project', 'read:org', 'repo', 'workflow'
    
    ghe.io
      ✓ Logged in to ghe.io account andyfeller (keyring)
      - Active account: true
      - Git operations protocol: https
      - Token: gho_************************************
      - Token scopes: 'gist', 'read:org', 'repo', 'workflow'
    
    
    $ GH_HOST=ghe.io gh repo clone services/gh-repo-export-test
    Cloning into 'gh-repo-export-test'...
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Receiving objects: 100% (3/3), done.
    
    
    $ cd gh-repo-export-test 
    $ gh variable list
    no variables found
    
    
    $ gh variable set FOO_BAR --body FOO
    ✓ Created variable FOO_BAR for services/gh-repo-export-test
    
    
    $ gh variable list                  
    NAME     VALUE  UPDATED               
    FOO_BAR  FOO    less than a minute ago
    
    
    $ gh repo view
    services/gh-repo-export-test
    No description provided
    
       gh-repo-export-test                                                                                                
    
    View this repository on GitHub: https://ghe.io/services/gh-repo-export-test
    
    
    $ gh variable get FOO_BAR             
    variable FOO_BAR was not found
    
    
    $ GH_DEBUG=api gh variable get FOO_BAR
    [git remote -v]
    [git config --get-regexp ^remote\..*\.gh-resolved$]
    * Request at 2024-08-01 14:21:44.30068 -0400 EDT m=+0.080073793
    * Request to https://api.github.com/repos/services/gh-repo-export-test/actions/variables/FOO_BAR
    > GET /repos/services/gh-repo-export-test/actions/variables/FOO_BAR HTTP/1.1
    > Host: api.github.com
    > Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
    > Authorization: token ████████████████████
    > Content-Type: application/json; charset=utf-8
    > Time-Zone: America/New_York
    > User-Agent: GitHub CLI 2.52.0
    
    < HTTP/2.0 404 Not Found
    < 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: Thu, 01 Aug 2024 18:21:44 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-Api-Version-Selected: 2022-11-28
    < X-Github-Media-Type: github.v3; param=merge-info-preview.nebula-preview; format=json
    < X-Github-Request-Id: EB3B:3D304A:783B42:E0BCDD:66ABD238
    < X-Oauth-Client-Id: 178c6fc778ccc68e1d6a
    < X-Oauth-Scopes: codespace, gist, project, read:org, repo, workflow
    < X-Ratelimit-Limit: 15000
    < X-Ratelimit-Remaining: 14990
    < X-Ratelimit-Reset: 1722538980
    < X-Ratelimit-Resource: core
    < X-Ratelimit-Used: 10
    < X-Xss-Protection: 0
    
    {
      "message": "Not Found",
      "documentation_url": "https://docs.github.com/rest/actions/variables#get-a-repository-variable",
      "status": "404"
    }
    
    * Request took 301.246958ms
    variable FOO_BAR was not found

    Related source code

    This is the gh variable get code:

    host, _ := cfg.Authentication().DefaultHost()
    var variable shared.Variable
    if err = client.REST(host, "GET", path, nil, &variable); err != nil {
    var httpErr api.HTTPError
    if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound {
    return fmt.Errorf("variable %s was not found", opts.VariableName)
    }
    return fmt.Errorf("failed to get variable %s: %w", opts.VariableName, err)
    }

    Comparing it to gh variable list that shows the value, we see how repository and repository environment based variables use the host associated with the repo:

    func getRepoVariables(client *http.Client, repo ghrepo.Interface) ([]shared.Variable, error) {
    return getVariables(client, repo.RepoHost(), fmt.Sprintf("repos/%s/actions/variables", ghrepo.FullName(repo)))
    }
    func getEnvVariables(client *http.Client, repo ghrepo.Interface, envName string) ([]shared.Variable, error) {
    path := fmt.Sprintf("repos/%s/environments/%s/variables", ghrepo.FullName(repo), envName)
    return getVariables(client, repo.RepoHost(), path)
    }

  3. added
    priority-1Affects a large population and inhibits work
    and removed on Aug 1, 2024
  4. added a commit that references this issue on Aug 2, 2024
    22a5a4c
  5. andyfeller commented on Aug 2, 2024

    @andyfeller
    Contributor

    @yukha-dw : one thing that was a little unclear between the title of this issue and the issue body was the totally affected scope of commands. I've modified the original issue body to remove the redundant aliases and the non-existent gh secret get command mentioned.

    That said, I was able to confirm this impacting gh variable delete and gh secret delete, so I'm expanding the PR to account for these, too.

  6. changed the title [-]gh variable get does not working on enterprise[/-] [+]`gh variable get`, `gh variable delete`, `gh secret delete` do not resolve Enterprise host based on repository[/+] on Aug 2, 2024
  7. yukha-dw commented on Aug 5, 2024

    @yukha-dw
    Author

    @andyfeller great, thank you. I'm not good at communicating issue 😅

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

    bugSomething isn't workinggh-variablerelating to the gh variable commandpriority-1Affects a large population and inhibits work

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions