Skip to content

Commit

Permalink
accept https://github.com/owner/name format and owner/name format
Browse files Browse the repository at this point in the history
  • Loading branch information
k1nho committed Sep 10, 2023
1 parent ad8609a commit 8a5bde6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
11 changes: 1 addition & 10 deletions cmd/show/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"sync"

"github.com/charmbracelet/bubbles/table"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand Down
14 changes: 12 additions & 2 deletions cmd/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
},
Expand Down
39 changes: 23 additions & 16 deletions pkg/utils/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 8a5bde6

Please sign in to comment.