Skip to content

Commit

Permalink
move generateRepoURL to ghrepo
Browse files Browse the repository at this point in the history
  • Loading branch information
vilmibm committed Jul 23, 2020
1 parent 895af99 commit dc02366
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
14 changes: 3 additions & 11 deletions command/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func issueList(cmd *cobra.Command, args []string) error {
}

if web {
issueListURL := generateRepoURL(baseRepo, "issues")
issueListURL := ghrepo.GenerateRepoURL(baseRepo, "issues")
openURL, err := listURLWithQuery(issueListURL, filterOptions{
entity: "issue",
state: state,
Expand Down Expand Up @@ -504,7 +504,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
}

if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb {
openURL := generateRepoURL(baseRepo, "issues/new")
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
if title != "" || body != "" {
milestone := ""
if len(milestoneTitles) > 0 {
Expand Down Expand Up @@ -582,7 +582,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
}

if action == PreviewAction {
openURL := generateRepoURL(baseRepo, "issues/new")
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
milestone := ""
if len(milestoneTitles) > 0 {
milestone = milestoneTitles[0]
Expand Down Expand Up @@ -618,14 +618,6 @@ func issueCreate(cmd *cobra.Command, args []string) error {
return nil
}

func generateRepoURL(repo ghrepo.Interface, p string, args ...interface{}) string {
baseURL := fmt.Sprintf("https://%s/%s/%s", repo.RepoHost(), repo.RepoOwner(), repo.RepoName())
if p != "" {
return baseURL + "/" + fmt.Sprintf(p, args...)
}
return baseURL
}

func addMetadataToIssueParams(client *api.Client, baseRepo ghrepo.Interface, params map[string]interface{}, tb *issueMetadataState) error {
if !tb.HasMetadata() {
return nil
Expand Down
2 changes: 1 addition & 1 deletion command/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func prList(cmd *cobra.Command, args []string) error {
}

if web {
prListURL := generateRepoURL(baseRepo, "pulls")
prListURL := ghrepo.GenerateRepoURL(baseRepo, "pulls")
openURL, err := listURLWithQuery(prListURL, filterOptions{
entity: "pr",
state: state,
Expand Down
2 changes: 1 addition & 1 deletion command/pr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func withPrAndIssueQueryParams(baseURL, title, body string, assignees, labels, p
}

func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assignees, labels, projects []string, milestone string) (string, error) {
u := generateRepoURL(r, "compare/%s...%s?expand=1", base, head)
u := ghrepo.GenerateRepoURL(r, "compare/%s...%s?expand=1", base, head)
url, err := withPrAndIssueQueryParams(u, title, body, assignees, labels, projects, milestone)
if err != nil {
return "", err
Expand Down
8 changes: 8 additions & 0 deletions internal/ghrepo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ func IsSame(a, b Interface) bool {
normalizeHostname(a.RepoHost()) == normalizeHostname(b.RepoHost())
}

func GenerateRepoURL(repo Interface, p string, args ...interface{}) string {
baseURL := fmt.Sprintf("https://%s/%s/%s", repo.RepoHost(), repo.RepoOwner(), repo.RepoName())
if p != "" {
return baseURL + "/" + fmt.Sprintf(p, args...)
}
return baseURL
}

type ghRepo struct {
owner string
name string
Expand Down
10 changes: 1 addition & 9 deletions pkg/cmd/repo/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func viewRun(opts *ViewOptions) error {
return err
}

openURL := generateRepoURL(toView, "")
openURL := ghrepo.GenerateRepoURL(toView, "")
if opts.Web {
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.ErrOut, "Opening %s in your browser.\n", displayURL(openURL))
Expand Down Expand Up @@ -192,14 +192,6 @@ func isURL(arg string) bool {
return strings.HasPrefix(arg, "http:/") || strings.HasPrefix(arg, "https:/")
}

func generateRepoURL(repo ghrepo.Interface, p string, args ...interface{}) string {
baseURL := fmt.Sprintf("https://%s/%s/%s", repo.RepoHost(), repo.RepoOwner(), repo.RepoName())
if p != "" {
return baseURL + "/" + fmt.Sprintf(p, args...)
}
return baseURL
}

func displayURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
Expand Down

0 comments on commit dc02366

Please sign in to comment.