Skip to content

{owner} placeholder in gh api calls do not respect origin set via gh set-default #7595

Description

@whi-tw

Describe the bug

When using the {owner} placeholder in gh api, the chosen remote is whichever comes first when sort.Sort() is called on the remotes array.

❯ gh --version
gh version 2.30.0 (2023-05-30)
https://github.com/cli/cli/releases/tag/v2.30.0

Steps to reproduce the behavior

❯ cat .git/config
...
[remote "origin"]
	url = [email protected]:whi-tw/dnscontrol.git
	fetch = +refs/heads/*:refs/remotes/origin/*
	gh-resolved = base
[remote "upstream"]
	url = [email protected]:StackExchange/dnscontrol.git
	fetch = +refs/heads/*:refs/remotes/upstream/*
  1. gh api "repos/{owner}/{repo}" -q '.full_name'

Expected vs actual behavior

Expected

'whi-tw/dnscontrol'

Actual

'StackExchange/dnscontrol'

Logs

❯ GH_DEBUG=api gh api "repos/{owner}/{repo}" -q '.full_name'
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
* Request at 2023-06-19 14:25:57.345605 +0100 BST m=+0.088005710
* Request to https://api.github.com/repos/StackExchange/dnscontrol

Activity

  1. samcoe commented on Jun 20, 2023

    @samcoe
    Contributor

    @whi-tw I don't think I would consider this a bug but rather a feature request that the api command respects repo set-default. I think it makes sense to add to the api command 👍

  2. added
    enhancementa request to improve CLI
    and removed
    bugSomething isn't working
    on Jun 20, 2023
  3. whi-tw commented on Jun 20, 2023

    @whi-tw
    ContributorAuthor

    @samcoe Ah, for sure - it seemed like a bug, because it was behaviour that seemed odd and doesn't seem to match up with the usage text:

    ... will get replaced with values from the repository of the current directory.

    I can't easily get around this in my use case (a sync-fork alias), as I want to make the API call against 'my' remote (origin). I think the issue is likely that my username is alphabetically sorted after StackExchange.

    aliases:
      sync-fork: |-
        !
        set -euo pipefail
        gh api "repos/{owner}/{repo}/merge-upstream" -F branch='{branch}'
        git pull

    There's no way to specify which origin is selected by BaseRepoFunc() as far as I can tell.

    Perhaps another feature could be a NamedRepoFunc() with the ability to specify a remote by name via an envar $GH_REMOTE, failing over to BaseRepoFunc() if the envar is not set.

    I have done an implementation in #7594.

  4. samcoe commented on Jun 21, 2023

    @samcoe
    Contributor

    @whi-tw We have discussed adding some functionality around remotes in the past but have generally dismissed them as being a bit too low level.

    Out of curiosity is there a reason why your sync-fork alias does not use the repo sync command? That command does take into account repo set-default and seems like it would fix your use case.

  5. whi-tw commented on Jun 21, 2023

    @whi-tw
    ContributorAuthor

    That makes sense.

    Yeah, I did see repo sync, just when I was playing with it, it didn't seem to actually do what I wanted. Well, I couldn't be sure that it would actually doing what I wanted from the usage text.

    I presume that if I set the default origin to my fork, it'll sync it against the fork's origin?

    I think maybe it's a language thing - is the source of my fork the 'parent'?

    I guess when writing the alias, I was hoping to avoid trying to understand how repo sync actually worked, and just to use the API endpoint that I know will do what I want. Unfortunately that didn't turn out to be such a quick process as I've ended up down this placeholder yak shave!

  6. samcoe commented on Jun 23, 2023

    @samcoe
    Contributor

    @whi-tw repo sync does call the /merge-upstream API endpoint underneath the covers. You are correct, the source of a fork repo is its parent repo. Unfortunately now that I think about the command behavior a bit more, it does require the user to specify any remote repo explicitly. It won't assume a default repo according to what was selected with repo set-default, as the default behavior is to sync the local repo from its remote parent.

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 CLIhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions