Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback clone from user organization #9044

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
andyfeller marked this conversation as resolved.
Show resolved Hide resolved
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