From 8a5bde6b1b8c32add4aeee6a7d8653d92cc582f4 Mon Sep 17 00:00:00 2001 From: k1nho Date: Sun, 10 Sep 2023 12:56:44 -0400 Subject: [PATCH] accept https://github.com/owner/name format and owner/name format --- cmd/show/dashboard.go | 11 +---------- cmd/show/show.go | 14 ++++++++++++-- pkg/utils/github.go | 39 +++++++++++++++++++++++---------------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/cmd/show/dashboard.go b/cmd/show/dashboard.go index 4310d45..cda0ea5 100644 --- a/cmd/show/dashboard.go +++ b/cmd/show/dashboard.go @@ -5,7 +5,6 @@ import ( "errors" "fmt" "strconv" - "strings" "sync" "github.com/charmbracelet/bubbles/table" @@ -44,8 +43,7 @@ func InitDashboard(opts *Options) (tea.Model, error) { return model, err } - ownerRepo := strings.Split(opts.RepoName, "/") - resp, r, err := opts.APIClient.RepositoryServiceAPI.FindOneByOwnerAndRepo(opts.ServerContext, ownerRepo[0], ownerRepo[1]).Execute() + resp, r, err := opts.APIClient.RepositoryServiceAPI.FindOneByOwnerAndRepo(opts.ServerContext, opts.Owner, opts.RepoName).Execute() if err != nil { return model, err } @@ -206,13 +204,6 @@ func (m *DashboardModel) drawDashboardView() string { // validateShowQuery: validates fields set to query the contributor tables func validateShowQuery(opts *Options) error { - if opts.RepoName == "" { - return errors.New("no repository was provided") - } - - if len(strings.Split(opts.RepoName, "/")) != 2 { - return errors.New("wrong repository name format. Must be in the form owner/name, i.e(open-sauced/pizza)") - } if opts.Limit < 1 { return errors.New("--limit flag must be a positive integer value") } diff --git a/cmd/show/show.go b/cmd/show/show.go index 82ab4dc..662b268 100644 --- a/cmd/show/show.go +++ b/cmd/show/show.go @@ -8,13 +8,17 @@ import ( client "github.com/open-sauced/go-api/client" "github.com/open-sauced/pizza-cli/pkg/api" + "github.com/open-sauced/pizza-cli/pkg/utils" "github.com/spf13/cobra" ) // Options are the options for the pizza show command including user // defined configurations type Options struct { - // RepoName is the git repo name to get statistics + // Owner: the owner of the repository + Owner string + + // RepoName: the name of the repository RepoName string // Page is the page to be requested @@ -49,7 +53,13 @@ func NewShowCommand() *cobra.Command { if len(args) != 1 { return errors.New("must specify the URL of a git repository to analyze") } - opts.RepoName = args[0] + + owner, name, err := utils.GetOwnerAndRepoFromURL(args[0]) + if err != nil { + return err + } + opts.Owner = owner + opts.RepoName = name return nil }, diff --git a/pkg/utils/github.go b/pkg/utils/github.go index 83a8ac1..68be868 100644 --- a/pkg/utils/github.go +++ b/pkg/utils/github.go @@ -2,27 +2,34 @@ package utils import ( "fmt" + "net/url" "strings" ) -func GetOwnerAndRepoFromURL(url string) (owner, repo string, err error) { - if !strings.HasPrefix(url, "https://github.com/") { - return "", "", fmt.Errorf("invalid URL: %s", url) - } - - // Remove the "https://github.com/" prefix from the URL - url = strings.TrimPrefix(url, "https://github.com/") +// GetOwnerAndRepoFromURL: extracts the owner and repository name +func GetOwnerAndRepoFromURL(input string) (owner, repo string, err error) { + var repoOwner, repoName string - // Split the remaining URL path into segments - segments := strings.Split(url, "/") + // check (https://github.com/owner/repo) format + u, err := url.Parse(input) + if err == nil && u.Host == "github.com" { + path := strings.Trim(u.Path, "/") + parts := strings.Split(path, "/") + if len(parts) != 2 { + return "", "", fmt.Errorf("Invalid URL: %s", input) + } + repoOwner = parts[0] + repoName = parts[1] + return repoOwner, repoName, nil + } - // The first segment is the owner, and the second segment is the repository name - if len(segments) >= 2 { - owner = segments[0] - repo = segments[1] - } else { - return "", "", fmt.Errorf("invalid URL: %s", url) + // check (owner/repo) format + parts := strings.Split(input, "/") + if len(parts) != 2 { + return "", "", fmt.Errorf("Invalid URL: %s", input) } + repoOwner = parts[0] + repoName = parts[1] - return owner, repo, nil + return repoOwner, repoName, nil }