Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin' into ghe-api
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Jul 27, 2020
2 parents 8909a3e + 7512034 commit 0cbcf8a
Show file tree
Hide file tree
Showing 19 changed files with 1,251 additions and 742 deletions.
32 changes: 0 additions & 32 deletions api/queries_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -407,37 +406,6 @@ func RepoCreate(client *Client, input RepoCreateInput) (*Repository, error) {
return initRepoHostname(&response.CreateRepository.Repository, "github.com"), nil
}

type RepoReadme struct {
Filename string
Content string
}

func RepositoryReadme(client *Client, repo ghrepo.Interface) (*RepoReadme, error) {
var response struct {
Name string
Content string
}

err := client.REST(repo.RepoHost(), "GET", fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)), nil, &response)
if err != nil {
var httpError HTTPError
if errors.As(err, &httpError) && httpError.StatusCode == 404 {
return nil, &NotFoundError{err}
}
return nil, err
}

decoded, err := base64.StdEncoding.DecodeString(response.Content)
if err != nil {
return nil, fmt.Errorf("failed to decode readme: %w", err)
}

return &RepoReadme{
Filename: response.Name,
Content: string(decoded),
}, nil
}

type RepoMetadataResult struct {
AssignableUsers []RepoAssignee
Labels []RepoLabel
Expand Down
28 changes: 6 additions & 22 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 All @@ -240,7 +240,7 @@ func issueList(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", utils.DisplayURL(openURL))
return utils.OpenInBrowser(openURL)
}

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 All @@ -518,7 +518,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
openURL += "/choose"
}
if connectedToTerminal(cmd) {
cmd.Printf("Opening %s in your browser.\n", displayURL(openURL))
cmd.Printf("Opening %s in your browser.\n", utils.DisplayURL(openURL))
}
return utils.OpenInBrowser(openURL)
}
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 All @@ -592,7 +592,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
return err
}
// TODO could exceed max url length for explorer
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", utils.DisplayURL(openURL))
return utils.OpenInBrowser(openURL)
} else if action == SubmitAction {
params := map[string]interface{}{
Expand All @@ -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 Expand Up @@ -844,11 +836,3 @@ func issueReopen(cmd *cobra.Command, args []string) error {

return nil
}

func displayURL(urlStr string) string {
u, err := url.Parse(urlStr)
if err != nil {
return urlStr
}
return u.Hostname() + u.Path
}
4 changes: 2 additions & 2 deletions 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 All @@ -246,7 +246,7 @@ func prList(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", utils.DisplayURL(openURL))
return utils.OpenInBrowser(openURL)
}

Expand Down
4 changes: 2 additions & 2 deletions command/pr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func prCreate(cmd *cobra.Command, _ []string) error {
}
if connectedToTerminal(cmd) {
// TODO could exceed max url length for explorer
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", utils.DisplayURL(openURL))
}
return utils.OpenInBrowser(openURL)
} else {
Expand Down 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
Loading

0 comments on commit 0cbcf8a

Please sign in to comment.