Merge remote-tracking branch 'origin/master' into detect-existing-fork
This commit is contained in:
commit
265db26b78
12 changed files with 437 additions and 269 deletions
|
|
@ -22,7 +22,9 @@ func TestPullRequest_ChecksStatus(t *testing.T) {
|
|||
{ "status": "COMPLETED",
|
||||
"conclusion": "FAILURE" },
|
||||
{ "status": "COMPLETED",
|
||||
"conclusion": "ACTION_REQUIRED" }
|
||||
"conclusion": "ACTION_REQUIRED" },
|
||||
{ "status": "COMPLETED",
|
||||
"conclusion": "STALE" }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -32,8 +34,8 @@ func TestPullRequest_ChecksStatus(t *testing.T) {
|
|||
eq(t, err, nil)
|
||||
|
||||
checks := pr.ChecksStatus()
|
||||
eq(t, checks.Total, 7)
|
||||
eq(t, checks.Pending, 2)
|
||||
eq(t, checks.Total, 8)
|
||||
eq(t, checks.Pending, 3)
|
||||
eq(t, checks.Failing, 3)
|
||||
eq(t, checks.Passing, 2)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) {
|
|||
summary.Passing++
|
||||
case "ERROR", "FAILURE", "CANCELLED", "TIMED_OUT", "ACTION_REQUIRED":
|
||||
summary.Failing++
|
||||
case "EXPECTED", "REQUESTED", "QUEUED", "PENDING", "IN_PROGRESS":
|
||||
case "EXPECTED", "REQUESTED", "QUEUED", "PENDING", "IN_PROGRESS", "STALE":
|
||||
summary.Pending++
|
||||
default:
|
||||
panic(fmt.Errorf("unsupported status: %q", state))
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
|
@ -9,16 +10,18 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/cli/cli/internal/ghrepo"
|
||||
"github.com/cli/cli/utils"
|
||||
)
|
||||
|
||||
// Repository contains information about a GitHub repo
|
||||
type Repository struct {
|
||||
ID string
|
||||
Name string
|
||||
URL string
|
||||
CloneURL string
|
||||
CreatedAt time.Time
|
||||
Owner RepositoryOwner
|
||||
ID string
|
||||
Name string
|
||||
Description string
|
||||
URL string
|
||||
CloneURL string
|
||||
CreatedAt time.Time
|
||||
Owner RepositoryOwner
|
||||
|
||||
IsPrivate bool
|
||||
HasIssuesEnabled bool
|
||||
|
|
@ -69,6 +72,7 @@ func GitHubRepo(client *Client, repo ghrepo.Interface) (*Repository, error) {
|
|||
repository(owner: $owner, name: $name) {
|
||||
id
|
||||
hasIssuesEnabled
|
||||
description
|
||||
}
|
||||
}`
|
||||
variables := map[string]interface{}{
|
||||
|
|
@ -279,3 +283,43 @@ func RepoCreate(client *Client, input RepoCreateInput) (*Repository, error) {
|
|||
|
||||
return &response.CreateRepository.Repository, nil
|
||||
}
|
||||
|
||||
func RepositoryReadme(client *Client, fullName string) (string, error) {
|
||||
type readmeResponse struct {
|
||||
Name string
|
||||
Content string
|
||||
}
|
||||
|
||||
var readme readmeResponse
|
||||
|
||||
err := client.REST("GET", fmt.Sprintf("repos/%s/readme", fullName), nil, &readme)
|
||||
if err != nil && !strings.HasSuffix(err.Error(), "'Not Found'") {
|
||||
return "", fmt.Errorf("could not get readme for repo: %w", err)
|
||||
}
|
||||
|
||||
decoded, err := base64.StdEncoding.DecodeString(readme.Content)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to decode readme: %w", err)
|
||||
}
|
||||
|
||||
readmeContent := string(decoded)
|
||||
|
||||
if isMarkdownFile(readme.Name) {
|
||||
readmeContent, err = utils.RenderMarkdown(readmeContent)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to render readme as markdown: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return readmeContent, nil
|
||||
|
||||
}
|
||||
|
||||
func isMarkdownFile(filename string) bool {
|
||||
// kind of gross, but i'm assuming that 90% of the time the suffix will just be .md. it didn't
|
||||
// seem worth executing a regex for this given that assumption.
|
||||
return strings.HasSuffix(filename, ".md") ||
|
||||
strings.HasSuffix(filename, ".markdown") ||
|
||||
strings.HasSuffix(filename, ".mdown") ||
|
||||
strings.HasSuffix(filename, ".mkdown")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ func init() {
|
|||
issueListCmd.Flags().StringP("author", "A", "", "Filter by author")
|
||||
|
||||
issueCmd.AddCommand(issueViewCmd)
|
||||
issueViewCmd.Flags().BoolP("preview", "p", false, "Display preview of issue content")
|
||||
issueViewCmd.Flags().BoolP("web", "w", false, "Open issue in browser")
|
||||
}
|
||||
|
||||
var issueCmd = &cobra.Command{
|
||||
|
|
@ -73,7 +73,7 @@ var issueViewCmd = &cobra.Command{
|
|||
}
|
||||
return nil
|
||||
},
|
||||
Short: "View an issue in the browser",
|
||||
Short: "View an issue",
|
||||
RunE: issueView,
|
||||
}
|
||||
|
||||
|
|
@ -213,17 +213,17 @@ func issueView(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
openURL := issue.URL
|
||||
|
||||
preview, err := cmd.Flags().GetBool("preview")
|
||||
web, err := cmd.Flags().GetBool("web")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if preview {
|
||||
out := colorableOut(cmd)
|
||||
return printIssuePreview(out, issue)
|
||||
} else {
|
||||
if web {
|
||||
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
|
||||
return utils.OpenInBrowser(openURL)
|
||||
} else {
|
||||
out := colorableOut(cmd)
|
||||
return printIssuePreview(out, issue)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -216,79 +216,79 @@ func TestIssueList_disabledIssues(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIssueView_web(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
|
||||
"number": 123,
|
||||
"url": "https://github.com/OWNER/REPO/issues/123"
|
||||
} } } }
|
||||
`))
|
||||
|
||||
var seenCmd *exec.Cmd
|
||||
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
|
||||
seenCmd = cmd
|
||||
return &test.OutputStub{}
|
||||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view -w 123")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.String(), "")
|
||||
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n")
|
||||
|
||||
if seenCmd == nil {
|
||||
t.Fatal("expected a command to run")
|
||||
}
|
||||
url := seenCmd.Args[len(seenCmd.Args)-1]
|
||||
eq(t, url, "https://github.com/OWNER/REPO/issues/123")
|
||||
}
|
||||
|
||||
func TestIssueView_web_numberArgWithHash(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
|
||||
"number": 123,
|
||||
"url": "https://github.com/OWNER/REPO/issues/123"
|
||||
} } } }
|
||||
`))
|
||||
|
||||
var seenCmd *exec.Cmd
|
||||
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
|
||||
seenCmd = cmd
|
||||
return &test.OutputStub{}
|
||||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view -w \"#123\"")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.String(), "")
|
||||
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n")
|
||||
|
||||
if seenCmd == nil {
|
||||
t.Fatal("expected a command to run")
|
||||
}
|
||||
url := seenCmd.Args[len(seenCmd.Args)-1]
|
||||
eq(t, url, "https://github.com/OWNER/REPO/issues/123")
|
||||
}
|
||||
|
||||
func TestIssueView(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
|
||||
"number": 123,
|
||||
"url": "https://github.com/OWNER/REPO/issues/123"
|
||||
} } } }
|
||||
`))
|
||||
|
||||
var seenCmd *exec.Cmd
|
||||
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
|
||||
seenCmd = cmd
|
||||
return &test.OutputStub{}
|
||||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view 123")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.String(), "")
|
||||
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n")
|
||||
|
||||
if seenCmd == nil {
|
||||
t.Fatal("expected a command to run")
|
||||
}
|
||||
url := seenCmd.Args[len(seenCmd.Args)-1]
|
||||
eq(t, url, "https://github.com/OWNER/REPO/issues/123")
|
||||
}
|
||||
|
||||
func TestIssueView_numberArgWithHash(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
|
||||
"number": 123,
|
||||
"url": "https://github.com/OWNER/REPO/issues/123"
|
||||
} } } }
|
||||
`))
|
||||
|
||||
var seenCmd *exec.Cmd
|
||||
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
|
||||
seenCmd = cmd
|
||||
return &test.OutputStub{}
|
||||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view \"#123\"")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.String(), "")
|
||||
eq(t, output.Stderr(), "Opening https://github.com/OWNER/REPO/issues/123 in your browser.\n")
|
||||
|
||||
if seenCmd == nil {
|
||||
t.Fatal("expected a command to run")
|
||||
}
|
||||
url := seenCmd.Args[len(seenCmd.Args)-1]
|
||||
eq(t, url, "https://github.com/OWNER/REPO/issues/123")
|
||||
}
|
||||
|
||||
func TestIssueView_preview(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
|
||||
"number": 123,
|
||||
|
|
@ -309,28 +309,21 @@ func TestIssueView_preview(t *testing.T) {
|
|||
} } } }
|
||||
`))
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view -p 123")
|
||||
output, err := RunCommand(issueViewCmd, "issue view 123")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`ix of coins`),
|
||||
regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`),
|
||||
regexp.MustCompile(`bold story`),
|
||||
regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"ix of coins",
|
||||
`opened by marseilles. 9 comments. \(tarot\)`,
|
||||
"bold story",
|
||||
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
|
||||
}
|
||||
|
||||
func TestIssueView_previewWithEmptyBody(t *testing.T) {
|
||||
func TestIssueView_WithEmptyBody(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -355,27 +348,20 @@ func TestIssueView_previewWithEmptyBody(t *testing.T) {
|
|||
} } } }
|
||||
`))
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view -p 123")
|
||||
output, err := RunCommand(issueViewCmd, "issue view 123")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`ix of coins`),
|
||||
regexp.MustCompile(`opened by marseilles. 9 comments. \(tarot\)`),
|
||||
regexp.MustCompile(`View this issue on GitHub: https://github.com/OWNER/REPO/issues/123`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"ix of coins",
|
||||
`opened by marseilles. 9 comments. \(tarot\)`,
|
||||
"View this issue on GitHub: https://github.com/OWNER/REPO/issues/123")
|
||||
}
|
||||
|
||||
func TestIssueView_notFound(t *testing.T) {
|
||||
func TestIssueView_web_notFound(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
|
||||
|
|
@ -392,7 +378,7 @@ func TestIssueView_notFound(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
_, err := RunCommand(issueViewCmd, "issue view 9999")
|
||||
_, err := RunCommand(issueViewCmd, "issue view -w 9999")
|
||||
if err == nil || err.Error() != "graphql error: 'Could not resolve to an Issue with the number of 9999.'" {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
|
@ -420,7 +406,7 @@ func TestIssueView_disabledIssues(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIssueView_urlArg(t *testing.T) {
|
||||
func TestIssueView_web_urlArg(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -439,7 +425,7 @@ func TestIssueView_urlArg(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(issueViewCmd, "issue view https://github.com/OWNER/REPO/issues/123")
|
||||
output, err := RunCommand(issueViewCmd, "issue view -w https://github.com/OWNER/REPO/issues/123")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `issue view`: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func init() {
|
|||
prListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label")
|
||||
prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
|
||||
|
||||
prViewCmd.Flags().BoolP("preview", "p", false, "Display preview of pull request content")
|
||||
prViewCmd.Flags().BoolP("web", "w", false, "Open pull request in browser")
|
||||
}
|
||||
|
||||
var prCmd = &cobra.Command{
|
||||
|
|
@ -57,10 +57,10 @@ var prStatusCmd = &cobra.Command{
|
|||
var prViewCmd = &cobra.Command{
|
||||
Use: "view [{<number> | <url> | <branch>}]",
|
||||
Short: "View a pull request in the browser",
|
||||
Long: `View a pull request specified by the argument in the browser.
|
||||
Long: `View a pull request specified by the argument.
|
||||
|
||||
Without an argument, the pull request that belongs to the current
|
||||
branch is opened.`,
|
||||
branch is displayed.`,
|
||||
RunE: prView,
|
||||
}
|
||||
|
||||
|
|
@ -272,7 +272,7 @@ func prView(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
preview, err := cmd.Flags().GetBool("preview")
|
||||
web, err := cmd.Flags().GetBool("web")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -293,7 +293,7 @@ func prView(cmd *cobra.Command, args []string) error {
|
|||
|
||||
if prNumber > 0 {
|
||||
openURL = fmt.Sprintf("https://github.com/%s/pull/%d", ghrepo.FullName(baseRepo), prNumber)
|
||||
if preview {
|
||||
if !web {
|
||||
pr, err = api.PullRequestByNumber(apiClient, baseRepo, prNumber)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -309,12 +309,12 @@ func prView(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
}
|
||||
|
||||
if preview {
|
||||
out := colorableOut(cmd)
|
||||
return printPrPreview(out, pr)
|
||||
} else {
|
||||
if web {
|
||||
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", openURL)
|
||||
return utils.OpenInBrowser(openURL)
|
||||
} else {
|
||||
out := colorableOut(cmd)
|
||||
return printPrPreview(out, pr)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -418,7 +418,9 @@ func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
|
|||
reviews := pr.ReviewStatus()
|
||||
|
||||
if pr.State == "OPEN" {
|
||||
if checks.Total > 0 || reviews.ChangesRequested || reviews.Approved {
|
||||
reviewStatus := reviews.ChangesRequested || reviews.Approved || reviews.ReviewRequired
|
||||
if checks.Total > 0 || reviewStatus {
|
||||
// show checks & reviews on their own line
|
||||
fmt.Fprintf(w, "\n ")
|
||||
}
|
||||
|
||||
|
|
@ -426,24 +428,29 @@ func printPrs(w io.Writer, totalCount int, prs ...api.PullRequest) {
|
|||
var summary string
|
||||
if checks.Failing > 0 {
|
||||
if checks.Failing == checks.Total {
|
||||
summary = utils.Red("All checks failing")
|
||||
summary = utils.Red("× All checks failing")
|
||||
} else {
|
||||
summary = utils.Red(fmt.Sprintf("%d/%d checks failing", checks.Failing, checks.Total))
|
||||
summary = utils.Red(fmt.Sprintf("× %d/%d checks failing", checks.Failing, checks.Total))
|
||||
}
|
||||
} else if checks.Pending > 0 {
|
||||
summary = utils.Yellow("Checks pending")
|
||||
summary = utils.Yellow("- Checks pending")
|
||||
} else if checks.Passing == checks.Total {
|
||||
summary = utils.Green("Checks passing")
|
||||
summary = utils.Green("✓ Checks passing")
|
||||
}
|
||||
fmt.Fprintf(w, " - %s", summary)
|
||||
fmt.Fprint(w, summary)
|
||||
}
|
||||
|
||||
if checks.Total > 0 && reviewStatus {
|
||||
// add padding between checks & reviews
|
||||
fmt.Fprint(w, " ")
|
||||
}
|
||||
|
||||
if reviews.ChangesRequested {
|
||||
fmt.Fprintf(w, " - %s", utils.Red("Changes requested"))
|
||||
fmt.Fprint(w, utils.Red("+ Changes requested"))
|
||||
} else if reviews.ReviewRequired {
|
||||
fmt.Fprintf(w, " - %s", utils.Yellow("Review required"))
|
||||
fmt.Fprint(w, utils.Yellow("- Review required"))
|
||||
} else if reviews.Approved {
|
||||
fmt.Fprintf(w, " - %s", utils.Green("Approved"))
|
||||
fmt.Fprint(w, utils.Green("✓ Approved"))
|
||||
}
|
||||
} else {
|
||||
s := strings.Title(strings.ToLower(pr.State))
|
||||
|
|
|
|||
|
|
@ -144,9 +144,9 @@ func TestPRStatus_reviewsAndChecks(t *testing.T) {
|
|||
}
|
||||
|
||||
expected := []string{
|
||||
"- Checks passing - Changes requested",
|
||||
"- Checks pending - Approved",
|
||||
"- 1/3 checks failing - Review required",
|
||||
"✓ Checks passing + Changes requested",
|
||||
"- Checks pending ✓ Approved",
|
||||
"× 1/3 checks failing - Review required",
|
||||
}
|
||||
|
||||
for _, line := range expected {
|
||||
|
|
@ -409,25 +409,18 @@ func TestPRView_preview(t *testing.T) {
|
|||
defer jsonFile.Close()
|
||||
http.StubResponse(200, jsonFile)
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view -p 12")
|
||||
output, err := RunCommand(prViewCmd, "pr view 12")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Blueberries are from a fork`),
|
||||
regexp.MustCompile(`nobody wants to merge 12 commits into master from blueberries`),
|
||||
regexp.MustCompile(`blueberries taste good`),
|
||||
regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Blueberries are from a fork",
|
||||
"nobody wants to merge 12 commits into master from blueberries",
|
||||
"blueberries taste good",
|
||||
"View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12")
|
||||
}
|
||||
|
||||
func TestPRView_previewCurrentBranch(t *testing.T) {
|
||||
|
|
@ -444,25 +437,18 @@ func TestPRView_previewCurrentBranch(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view -p")
|
||||
output, err := RunCommand(prViewCmd, "pr view")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Blueberries are a good fruit`),
|
||||
regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`),
|
||||
regexp.MustCompile(`blueberries taste good`),
|
||||
regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Blueberries are a good fruit",
|
||||
"nobody wants to merge 8 commits into master from blueberries",
|
||||
"blueberries taste good",
|
||||
"View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10")
|
||||
}
|
||||
|
||||
func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) {
|
||||
|
|
@ -479,27 +465,20 @@ func TestPRView_previewCurrentBranchWithEmptyBody(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view -p")
|
||||
output, err := RunCommand(prViewCmd, "pr view")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Blueberries are a good fruit`),
|
||||
regexp.MustCompile(`nobody wants to merge 8 commits into master from blueberries`),
|
||||
regexp.MustCompile(`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Blueberries are a good fruit",
|
||||
"nobody wants to merge 8 commits into master from blueberries",
|
||||
"View this pull request on GitHub: https://github.com/OWNER/REPO/pull/10")
|
||||
}
|
||||
|
||||
func TestPRView_currentBranch(t *testing.T) {
|
||||
func TestPRView_web_currentBranch(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "blueberries")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -520,7 +499,7 @@ func TestPRView_currentBranch(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view")
|
||||
output, err := RunCommand(prViewCmd, "pr view -w")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
@ -537,7 +516,7 @@ func TestPRView_currentBranch(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPRView_noResultsForBranch(t *testing.T) {
|
||||
func TestPRView_web_noResultsForBranch(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "blueberries")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -558,7 +537,7 @@ func TestPRView_noResultsForBranch(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
_, err := RunCommand(prViewCmd, "pr view")
|
||||
_, err := RunCommand(prViewCmd, "pr view -w")
|
||||
if err == nil || err.Error() != `no open pull requests found for branch "blueberries"` {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
@ -568,7 +547,7 @@ func TestPRView_noResultsForBranch(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPRView_numberArg(t *testing.T) {
|
||||
func TestPRView_web_numberArg(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -586,7 +565,7 @@ func TestPRView_numberArg(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view 23")
|
||||
output, err := RunCommand(prViewCmd, "pr view -w 23")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
@ -600,7 +579,7 @@ func TestPRView_numberArg(t *testing.T) {
|
|||
eq(t, url, "https://github.com/OWNER/REPO/pull/23")
|
||||
}
|
||||
|
||||
func TestPRView_numberArgWithHash(t *testing.T) {
|
||||
func TestPRView_web_numberArgWithHash(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -618,7 +597,7 @@ func TestPRView_numberArgWithHash(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view \"#23\"")
|
||||
output, err := RunCommand(prViewCmd, "pr view -w \"#23\"")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
@ -632,7 +611,7 @@ func TestPRView_numberArgWithHash(t *testing.T) {
|
|||
eq(t, url, "https://github.com/OWNER/REPO/pull/23")
|
||||
}
|
||||
|
||||
func TestPRView_urlArg(t *testing.T) {
|
||||
func TestPRView_web_urlArg(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
|
||||
|
|
@ -649,7 +628,7 @@ func TestPRView_urlArg(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view https://github.com/OWNER/REPO/pull/23/files")
|
||||
output, err := RunCommand(prViewCmd, "pr view -w https://github.com/OWNER/REPO/pull/23/files")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
@ -663,7 +642,7 @@ func TestPRView_urlArg(t *testing.T) {
|
|||
eq(t, url, "https://github.com/OWNER/REPO/pull/23")
|
||||
}
|
||||
|
||||
func TestPRView_branchArg(t *testing.T) {
|
||||
func TestPRView_web_branchArg(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -683,7 +662,7 @@ func TestPRView_branchArg(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view blueberries")
|
||||
output, err := RunCommand(prViewCmd, "pr view -w blueberries")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
@ -697,7 +676,7 @@ func TestPRView_branchArg(t *testing.T) {
|
|||
eq(t, url, "https://github.com/OWNER/REPO/pull/23")
|
||||
}
|
||||
|
||||
func TestPRView_branchWithOwnerArg(t *testing.T) {
|
||||
func TestPRView_web_branchWithOwnerArg(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -718,7 +697,7 @@ func TestPRView_branchWithOwnerArg(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(prViewCmd, "pr view hubot:blueberries")
|
||||
output, err := RunCommand(prViewCmd, "pr view -w hubot:blueberries")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `pr view`: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
|
|
@ -35,6 +36,7 @@ func init() {
|
|||
repoForkCmd.Flags().Lookup("remote").NoOptDefVal = "true"
|
||||
|
||||
repoCmd.AddCommand(repoViewCmd)
|
||||
repoViewCmd.Flags().BoolP("web", "w", false, "Open repository in browser")
|
||||
}
|
||||
|
||||
var repoCmd = &cobra.Command{
|
||||
|
|
@ -78,9 +80,9 @@ With no argument, creates a fork of the current repository. Otherwise, forks the
|
|||
var repoViewCmd = &cobra.Command{
|
||||
Use: "view [<repository>]",
|
||||
Short: "View a repository in the browser",
|
||||
Long: `View a GitHub repository in the browser.
|
||||
Long: `View a GitHub repository.
|
||||
|
||||
With no argument, the repository for the current directory is opened.`,
|
||||
With no argument, the repository for the current directory is displayed.`,
|
||||
RunE: repoView,
|
||||
}
|
||||
|
||||
|
|
@ -387,6 +389,7 @@ var Confirm = func(prompt string, result *bool) error {
|
|||
|
||||
func repoView(cmd *cobra.Command, args []string) error {
|
||||
ctx := contextForCommand(cmd)
|
||||
|
||||
var toView ghrepo.Interface
|
||||
if len(args) == 0 {
|
||||
var err error
|
||||
|
|
@ -415,12 +418,67 @@ func repoView(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = api.GitHubRepo(apiClient, toView)
|
||||
repo, err := api.GitHubRepo(apiClient, toView)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
openURL := fmt.Sprintf("https://github.com/%s", ghrepo.FullName(toView))
|
||||
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
|
||||
return utils.OpenInBrowser(openURL)
|
||||
web, err := cmd.Flags().GetBool("web")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fullName := ghrepo.FullName(toView)
|
||||
|
||||
openURL := fmt.Sprintf("https://github.com/%s", fullName)
|
||||
if web {
|
||||
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", displayURL(openURL))
|
||||
return utils.OpenInBrowser(openURL)
|
||||
}
|
||||
|
||||
repoTmpl := `
|
||||
{{.FullName}}
|
||||
{{.Description}}
|
||||
|
||||
{{.Readme}}
|
||||
|
||||
{{.View}}
|
||||
`
|
||||
|
||||
tmpl, err := template.New("repo").Parse(repoTmpl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
readmeContent, _ := api.RepositoryReadme(apiClient, fullName)
|
||||
|
||||
if readmeContent == "" {
|
||||
readmeContent = utils.Gray("No README provided")
|
||||
}
|
||||
|
||||
description := repo.Description
|
||||
if description == "" {
|
||||
description = utils.Gray("No description provided")
|
||||
}
|
||||
|
||||
repoData := struct {
|
||||
FullName string
|
||||
Description string
|
||||
Readme string
|
||||
View string
|
||||
}{
|
||||
FullName: utils.Bold(fullName),
|
||||
Description: description,
|
||||
Readme: readmeContent,
|
||||
View: utils.Gray(fmt.Sprintf("View this repository on GitHub: %s", openURL)),
|
||||
}
|
||||
|
||||
out := colorableOut(cmd)
|
||||
|
||||
err = tmpl.Execute(out, repoData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -161,16 +161,9 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
|
|||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Created fork someone/REPO`),
|
||||
regexp.MustCompile(`Remote added at fork`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Created fork someone/REPO",
|
||||
"Remote added at fork")
|
||||
}
|
||||
|
||||
func TestRepoFork_outside_yes(t *testing.T) {
|
||||
|
|
@ -193,16 +186,9 @@ func TestRepoFork_outside_yes(t *testing.T) {
|
|||
|
||||
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Created fork someone/REPO`),
|
||||
regexp.MustCompile(`Cloned fork`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Created fork someone/REPO",
|
||||
"Cloned fork")
|
||||
}
|
||||
|
||||
func TestRepoFork_outside_survey_yes(t *testing.T) {
|
||||
|
|
@ -232,16 +218,9 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
|
|||
|
||||
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Created fork someone/REPO`),
|
||||
regexp.MustCompile(`Cloned fork`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Created fork someone/REPO",
|
||||
"Cloned fork")
|
||||
}
|
||||
|
||||
func TestRepoFork_outside_survey_no(t *testing.T) {
|
||||
|
|
@ -314,16 +293,9 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
|
|||
|
||||
eq(t, output.Stderr(), "")
|
||||
|
||||
expectedLines := []*regexp.Regexp{
|
||||
regexp.MustCompile(`Created fork someone/REPO`),
|
||||
regexp.MustCompile(`Remote added at fork`),
|
||||
}
|
||||
for _, r := range expectedLines {
|
||||
if !r.MatchString(output.String()) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
test.ExpectLines(t, output.String(),
|
||||
"Created fork someone/REPO",
|
||||
"Remote added at fork")
|
||||
}
|
||||
|
||||
func TestRepoFork_in_parent_survey_no(t *testing.T) {
|
||||
|
|
@ -604,7 +576,7 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRepoView(t *testing.T) {
|
||||
func TestRepoView_web(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
|
|
@ -619,7 +591,7 @@ func TestRepoView(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(repoViewCmd, "repo view")
|
||||
output, err := RunCommand(repoViewCmd, "repo view -w")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo view`: %v", err)
|
||||
}
|
||||
|
|
@ -634,7 +606,7 @@ func TestRepoView(t *testing.T) {
|
|||
eq(t, url, "https://github.com/OWNER/REPO")
|
||||
}
|
||||
|
||||
func TestRepoView_ownerRepo(t *testing.T) {
|
||||
func TestRepoView_web_ownerRepo(t *testing.T) {
|
||||
ctx := context.NewBlank()
|
||||
ctx.SetBranch("master")
|
||||
initContext = func() context.Context {
|
||||
|
|
@ -652,7 +624,7 @@ func TestRepoView_ownerRepo(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(repoViewCmd, "repo view cli/cli")
|
||||
output, err := RunCommand(repoViewCmd, "repo view -w cli/cli")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo view`: %v", err)
|
||||
}
|
||||
|
|
@ -667,7 +639,7 @@ func TestRepoView_ownerRepo(t *testing.T) {
|
|||
eq(t, url, "https://github.com/cli/cli")
|
||||
}
|
||||
|
||||
func TestRepoView_fullURL(t *testing.T) {
|
||||
func TestRepoView_web_fullURL(t *testing.T) {
|
||||
ctx := context.NewBlank()
|
||||
ctx.SetBranch("master")
|
||||
initContext = func() context.Context {
|
||||
|
|
@ -684,7 +656,7 @@ func TestRepoView_fullURL(t *testing.T) {
|
|||
})
|
||||
defer restoreCmd()
|
||||
|
||||
output, err := RunCommand(repoViewCmd, "repo view https://github.com/cli/cli")
|
||||
output, err := RunCommand(repoViewCmd, "repo view -w https://github.com/cli/cli")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo view`: %v", err)
|
||||
}
|
||||
|
|
@ -698,3 +670,77 @@ func TestRepoView_fullURL(t *testing.T) {
|
|||
url := seenCmd.Args[len(seenCmd.Args)-1]
|
||||
eq(t, url, "https://github.com/cli/cli")
|
||||
}
|
||||
|
||||
func TestRepoView(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": {
|
||||
"repository": {
|
||||
"description": "social distancing"
|
||||
}}}
|
||||
`))
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "name": "readme.md",
|
||||
"content": "IyB0cnVseSBjb29sIHJlYWRtZSBjaGVjayBpdCBvdXQ="}
|
||||
`))
|
||||
|
||||
output, err := RunCommand(repoViewCmd, "repo view")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo view`: %v", err)
|
||||
}
|
||||
|
||||
test.ExpectLines(t, output.String(),
|
||||
"OWNER/REPO",
|
||||
"social distancing",
|
||||
"truly cool readme",
|
||||
"View this repository on GitHub: https://github.com/OWNER/REPO")
|
||||
|
||||
}
|
||||
|
||||
func TestRepoView_nonmarkdown_readme(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "data": {
|
||||
"repository": {
|
||||
"description": "social distancing"
|
||||
}}}
|
||||
`))
|
||||
http.StubResponse(200, bytes.NewBufferString(`
|
||||
{ "name": "readme.org",
|
||||
"content": "IyB0cnVseSBjb29sIHJlYWRtZSBjaGVjayBpdCBvdXQ="}
|
||||
`))
|
||||
|
||||
output, err := RunCommand(repoViewCmd, "repo view")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo view`: %v", err)
|
||||
}
|
||||
|
||||
test.ExpectLines(t, output.String(),
|
||||
"OWNER/REPO",
|
||||
"social distancing",
|
||||
"# truly cool readme",
|
||||
"View this repository on GitHub: https://github.com/OWNER/REPO")
|
||||
}
|
||||
|
||||
func TestRepoView_blanks(t *testing.T) {
|
||||
initBlankContext("OWNER/REPO", "master")
|
||||
http := initFakeHTTP()
|
||||
http.StubRepoResponse("OWNER", "REPO")
|
||||
http.StubResponse(200, bytes.NewBufferString("{}"))
|
||||
http.StubResponse(200, bytes.NewBufferString("{}"))
|
||||
|
||||
output, err := RunCommand(repoViewCmd, "repo view")
|
||||
if err != nil {
|
||||
t.Errorf("error running command `repo view`: %v", err)
|
||||
}
|
||||
|
||||
test.ExpectLines(t, output.String(),
|
||||
"OWNER/REPO",
|
||||
"No description provided",
|
||||
"No README provided",
|
||||
"View this repository on GitHub: https://github.com/OWNER/REPO")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,21 +8,26 @@ import (
|
|||
|
||||
const defaultHostname = "github.com"
|
||||
|
||||
// Interface describes an object that represents a GitHub repository
|
||||
type Interface interface {
|
||||
RepoName() string
|
||||
RepoOwner() string
|
||||
}
|
||||
|
||||
// New instantiates a GitHub repository from owner and name arguments
|
||||
func New(owner, repo string) Interface {
|
||||
return &ghRepo{
|
||||
owner: owner,
|
||||
name: repo,
|
||||
}
|
||||
}
|
||||
|
||||
// FullName serializes a GitHub repository into an "OWNER/REPO" string
|
||||
func FullName(r Interface) string {
|
||||
return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName())
|
||||
}
|
||||
|
||||
// FromFullName extracts the GitHub repository inforation from an "OWNER/REPO" string
|
||||
func FromFullName(nwo string) Interface {
|
||||
var r ghRepo
|
||||
parts := strings.SplitN(nwo, "/", 2)
|
||||
|
|
@ -32,8 +37,9 @@ func FromFullName(nwo string) Interface {
|
|||
return &r
|
||||
}
|
||||
|
||||
// FromURL extracts the GitHub repository information from a URL
|
||||
func FromURL(u *url.URL) (Interface, error) {
|
||||
if !strings.EqualFold(u.Hostname(), defaultHostname) {
|
||||
if !strings.EqualFold(u.Hostname(), defaultHostname) && !strings.EqualFold(u.Hostname(), "www."+defaultHostname) {
|
||||
return nil, fmt.Errorf("unsupported hostname: %s", u.Hostname())
|
||||
}
|
||||
parts := strings.SplitN(strings.TrimPrefix(u.Path, "/"), "/", 3)
|
||||
|
|
@ -43,6 +49,7 @@ func FromURL(u *url.URL) (Interface, error) {
|
|||
return New(parts[0], strings.TrimSuffix(parts[1], ".git")), nil
|
||||
}
|
||||
|
||||
// IsSame compares two GitHub repositories
|
||||
func IsSame(a, b Interface) bool {
|
||||
return strings.EqualFold(a.RepoOwner(), b.RepoOwner()) &&
|
||||
strings.EqualFold(a.RepoName(), b.RepoName())
|
||||
|
|
|
|||
|
|
@ -1,40 +1,66 @@
|
|||
package ghrepo
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_repoFromURL(t *testing.T) {
|
||||
u, _ := url.Parse("http://github.com/monalisa/octo-cat.git")
|
||||
repo, err := FromURL(u)
|
||||
if err != nil {
|
||||
t.Fatalf("got error %q", err)
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
result string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "github.com URL",
|
||||
input: "https://github.com/monalisa/octo-cat.git",
|
||||
result: "monalisa/octo-cat",
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "www.github.com URL",
|
||||
input: "http://www.GITHUB.com/monalisa/octo-cat.git",
|
||||
result: "monalisa/octo-cat",
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
name: "unsupported hostname",
|
||||
input: "https://example.com/one/two",
|
||||
result: "",
|
||||
err: errors.New("unsupported hostname: example.com"),
|
||||
},
|
||||
{
|
||||
name: "filesystem path",
|
||||
input: "/path/to/file",
|
||||
result: "",
|
||||
err: errors.New("unsupported hostname: "),
|
||||
},
|
||||
}
|
||||
if repo.RepoOwner() != "monalisa" {
|
||||
t.Errorf("got owner %q", repo.RepoOwner())
|
||||
}
|
||||
if repo.RepoName() != "octo-cat" {
|
||||
t.Errorf("got name %q", repo.RepoName())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_repoFromURL_invalid(t *testing.T) {
|
||||
cases := [][]string{
|
||||
[]string{
|
||||
"https://example.com/one/two",
|
||||
"unsupported hostname: example.com",
|
||||
},
|
||||
[]string{
|
||||
"/path/to/disk",
|
||||
"unsupported hostname: ",
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
u, _ := url.Parse(c[0])
|
||||
_, err := FromURL(u)
|
||||
if err == nil || err.Error() != c[1] {
|
||||
t.Errorf("got %q", err)
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
u, err := url.Parse(tt.input)
|
||||
if err != nil {
|
||||
t.Fatalf("got error %q", err)
|
||||
}
|
||||
|
||||
repo, err := FromURL(u)
|
||||
if err != nil {
|
||||
if tt.err == nil {
|
||||
t.Fatalf("got error %q", err)
|
||||
} else if tt.err.Error() == err.Error() {
|
||||
return
|
||||
}
|
||||
t.Fatalf("got error %q", err)
|
||||
}
|
||||
|
||||
got := fmt.Sprintf("%s/%s", repo.RepoOwner(), repo.RepoName())
|
||||
if tt.result != got {
|
||||
t.Errorf("expected %q, got %q", tt.result, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// OutputStub implements a simple utils.Runnable
|
||||
|
|
@ -74,3 +76,14 @@ func UseTempGitRepo() *TempGitRepo {
|
|||
|
||||
return &TempGitRepo{Remote: remotePath, TearDown: tearDown}
|
||||
}
|
||||
|
||||
func ExpectLines(t *testing.T, output string, lines ...string) {
|
||||
var r *regexp.Regexp
|
||||
for _, l := range lines {
|
||||
r = regexp.MustCompile(l)
|
||||
if !r.MatchString(output) {
|
||||
t.Errorf("output did not match regexp /%s/\n> output\n%s\n", r, output)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue