Skip to content

Commit

Permalink
feat(clone): fallback to clone from user organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed May 3, 2024
1 parent 6f35082 commit 9bf322b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/org/list/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Organization struct {
Login string
}

func listOrgs(httpClient *http.Client, hostname string, limit int) (*OrganizationList, error) {
func ListOrgs(httpClient *http.Client, hostname string, limit int) (*OrganizationList, error) {
type response struct {
User struct {
Login string
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/org/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func listRun(opts *ListOptions) error {

host, _ := cfg.Authentication().DefaultHost()

listResult, err := listOrgs(httpClient, host, opts.Limit)
listResult, err := ListOrgs(httpClient, host, opts.Limit)
if err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/cmd/repo/clone/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
org "github.com/cli/cli/v2/pkg/cmd/org/list"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -165,6 +166,12 @@ func cloneRun(opts *CloneOptions) error {
// Load the repo from the API to get the username/repo name in its
// canonical capitalization
canonicalRepo, err := api.GitHubRepo(apiClient, repo)
if err != nil && repositoryIsFullName {
return err
}
// Try to find the repo in the user's organizations, since the user did not
// specify the owner.
canonicalRepo, err = repoFromUserOrgs(apiClient, repo)
if err != nil {
return err
}
Expand Down Expand Up @@ -223,6 +230,25 @@ func cloneRun(opts *CloneOptions) error {
return nil
}

const orgsQueryLimit = 5

func repoFromUserOrgs(apiClient *api.Client, repo ghrepo.Interface) (*api.Repository, error) {
orgs, err := org.ListOrgs(apiClient.HTTP(), repo.RepoHost(), orgsQueryLimit)
if err != nil || orgs.TotalCount == 0 {
// Return the original error in the event that the fallback query fails.
return nil, err
}
var canonicalRepo *api.Repository
for _, org := range orgs.Organizations {
repo = ghrepo.NewWithHost(org.Login, repo.RepoName(), repo.RepoHost())
canonicalRepo, err = api.GitHubRepo(apiClient, repo)
if err == nil {
return canonicalRepo, nil
}
}
return nil, fmt.Errorf("'%s' repository not found", repo.RepoName())
}

// simplifyURL strips given URL of extra parts like extra path segments (i.e.,
// anything beyond `/owner/repo`), query strings, or fragments. This function
// never returns an error.
Expand Down

0 comments on commit 9bf322b

Please sign in to comment.