Merge remote-tracking branch 'origin/master' into pr-status-no-commits

This commit is contained in:
vilmibm 2020-03-25 12:00:58 -05:00
commit f4b8851011
15 changed files with 527 additions and 262 deletions

View file

@ -340,7 +340,7 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
return &resp.Repository.PullRequest, nil
}
func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string) (*PullRequest, error) {
func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, headBranch string) (*PullRequest, error) {
type response struct {
Repository struct {
PullRequests struct {
@ -376,9 +376,9 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string)
}
}`
branchWithoutOwner := branch
if idx := strings.Index(branch, ":"); idx >= 0 {
branchWithoutOwner = branch[idx+1:]
branchWithoutOwner := headBranch
if idx := strings.Index(headBranch, ":"); idx >= 0 {
branchWithoutOwner = headBranch[idx+1:]
}
variables := map[string]interface{}{
@ -394,12 +394,17 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, branch string)
}
for _, pr := range resp.Repository.PullRequests.Nodes {
if pr.HeadLabel() == branch {
if pr.HeadLabel() == headBranch {
if baseBranch != "" {
if pr.BaseRefName != baseBranch {
continue
}
}
return &pr, nil
}
}
return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", branch)}
return nil, &NotFoundError{fmt.Errorf("no open pull requests found for branch %q", headBranch)}
}
// CreatePullRequest creates a pull request in a GitHub repository

View file

@ -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{}{
@ -154,7 +158,7 @@ func RepoNetwork(client *Client, repos []ghrepo.Interface) (RepoNetworkResult, e
keys = append(keys, key)
}
// sort keys to ensure `repo_{N}` entries are processed in order
sort.Sort(sort.StringSlice(keys))
sort.Strings(keys)
// Iterate over keys of GraphQL response data and, based on its name,
// dynamically allocate the target struct an individual message gets decoded to.
@ -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")
}

View file

@ -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)
}
}

View file

@ -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 := run.SetPrepareCmd(func(cmd *exec.Cmd) run.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 := run.SetPrepareCmd(func(cmd *exec.Cmd) run.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 := run.SetPrepareCmd(func(cmd *exec.Cmd) run.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 := run.SetPrepareCmd(func(cmd *exec.Cmd) run.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)
}

View file

@ -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,
}
@ -255,12 +255,24 @@ func prView(cmd *cobra.Command, args []string) error {
return err
}
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
var baseRepo ghrepo.Interface
var prArg string
if len(args) > 0 {
prArg = args[0]
if prNum, repo := prFromURL(prArg); repo != nil {
prArg = prNum
baseRepo = repo
}
}
preview, err := cmd.Flags().GetBool("preview")
if baseRepo == nil {
baseRepo, err = determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
}
web, err := cmd.Flags().GetBool("web")
if err != nil {
return err
}
@ -268,7 +280,7 @@ func prView(cmd *cobra.Command, args []string) error {
var openURL string
var pr *api.PullRequest
if len(args) > 0 {
pr, err = prFromArg(apiClient, baseRepo, args[0])
pr, err = prFromArg(apiClient, baseRepo, prArg)
if err != nil {
return err
}
@ -281,14 +293,14 @@ 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
}
}
} else {
pr, err = api.PullRequestForBranch(apiClient, baseRepo, branchWithOwner)
pr, err = api.PullRequestForBranch(apiClient, baseRepo, "", branchWithOwner)
if err != nil {
return err
}
@ -297,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)
}
}
@ -331,17 +343,19 @@ func printPrPreview(out io.Writer, pr *api.PullRequest) error {
var prURLRE = regexp.MustCompile(`^https://github\.com/([^/]+)/([^/]+)/pull/(\d+)`)
func prFromURL(arg string) (string, ghrepo.Interface) {
if m := prURLRE.FindStringSubmatch(arg); m != nil {
return m[3], ghrepo.New(m[1], m[2])
}
return "", nil
}
func prFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string) (*api.PullRequest, error) {
if prNumber, err := strconv.Atoi(strings.TrimPrefix(arg, "#")); err == nil {
return api.PullRequestByNumber(apiClient, baseRepo, prNumber)
}
if m := prURLRE.FindStringSubmatch(arg); m != nil {
prNumber, _ := strconv.Atoi(m[3])
return api.PullRequestByNumber(apiClient, baseRepo, prNumber)
}
return api.PullRequestForBranch(apiClient, baseRepo, arg)
return api.PullRequestForBranch(apiClient, baseRepo, "", arg)
}
func prSelectorForCurrentBranch(ctx context.Context, baseRepo ghrepo.Interface) (prNumber int, prHeadRef string, err error) {

View file

@ -6,9 +6,11 @@ import (
"os"
"os/exec"
"github.com/cli/cli/git"
"github.com/cli/cli/internal/run"
"github.com/spf13/cobra"
"github.com/cli/cli/git"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/internal/run"
)
func prCheckout(cmd *cobra.Command, args []string) error {
@ -18,21 +20,38 @@ func prCheckout(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
// FIXME: duplicates logic from fsContext.BaseRepo
baseRemote, err := remotes.FindByName("upstream", "github", "origin", "*")
if err != nil {
return err
}
apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
}
pr, err := prFromArg(apiClient, baseRemote, args[0])
var baseRepo ghrepo.Interface
prArg := args[0]
if prNum, repo := prFromURL(prArg); repo != nil {
prArg = prNum
baseRepo = repo
}
if baseRepo == nil {
baseRepo, err = determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
}
pr, err := prFromArg(apiClient, baseRepo, prArg)
if err != nil {
return err
}
baseRemote, _ := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName())
// baseRemoteSpec is a repository URL or a remote name to be used in git fetch
baseURLOrName := fmt.Sprintf("https://github.com/%s.git", ghrepo.FullName(baseRepo))
if baseRemote != nil {
baseURLOrName = baseRemote.Name
}
headRemote := baseRemote
if pr.IsCrossRepository {
headRemote, _ = remotes.FindByRepo(pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name)
@ -67,15 +86,15 @@ func prCheckout(cmd *cobra.Command, args []string) error {
ref := fmt.Sprintf("refs/pull/%d/head", pr.Number)
if newBranchName == currentBranch {
// PR head matches currently checked out branch
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseRemote.Name, ref})
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseURLOrName, ref})
cmdQueue = append(cmdQueue, []string{"git", "merge", "--ff-only", "FETCH_HEAD"})
} else {
// create a new branch
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseRemote.Name, fmt.Sprintf("%s:%s", ref, newBranchName)})
cmdQueue = append(cmdQueue, []string{"git", "fetch", baseURLOrName, fmt.Sprintf("%s:%s", ref, newBranchName)})
cmdQueue = append(cmdQueue, []string{"git", "checkout", newBranchName})
}
remote := baseRemote.Name
remote := baseURLOrName
mergeRef := ref
if pr.MaintainerCanModify {
remote = fmt.Sprintf("https://github.com/%s/%s.git", pr.HeadRepositoryOwner.Login, pr.HeadRepository.Name)

View file

@ -2,6 +2,8 @@ package command
import (
"bytes"
"encoding/json"
"io/ioutil"
"os/exec"
"strings"
"testing"
@ -21,6 +23,7 @@ func TestPRCheckout_sameRepo(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -112,6 +115,68 @@ func TestPRCheckout_urlArg(t *testing.T) {
eq(t, strings.Join(ranCommands[1], " "), "git checkout -b feature --no-track origin/feature")
}
func TestPRCheckout_urlArg_differentBase(t *testing.T) {
ctx := context.NewBlank()
ctx.SetBranch("master")
ctx.SetRemotes(map[string]string{
"origin": "OWNER/REPO",
})
initContext = func() context.Context {
return ctx
}
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
"number": 123,
"headRefName": "feature",
"headRepositoryOwner": {
"login": "hubot"
},
"headRepository": {
"name": "POE",
"defaultBranchRef": {
"name": "master"
}
},
"isCrossRepository": false,
"maintainerCanModify": false
} } } }
`))
ranCommands := [][]string{}
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
switch strings.Join(cmd.Args, " ") {
case "git show-ref --verify --quiet refs/heads/feature":
return &errorStub{"exit status: 1"}
default:
ranCommands = append(ranCommands, cmd.Args)
return &test.OutputStub{}
}
})
defer restoreCmd()
output, err := RunCommand(prCheckoutCmd, `pr checkout https://github.com/OTHER/POE/pull/123/files`)
eq(t, err, nil)
eq(t, output.String(), "")
bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body)
reqBody := struct {
Variables struct {
Owner string
Repo string
}
}{}
json.Unmarshal(bodyBytes, &reqBody)
eq(t, reqBody.Variables.Owner, "OTHER")
eq(t, reqBody.Variables.Repo, "POE")
eq(t, len(ranCommands), 5)
eq(t, strings.Join(ranCommands[1], " "), "git fetch https://github.com/OTHER/POE.git refs/pull/123/head:feature")
eq(t, strings.Join(ranCommands[3], " "), "git config branch.feature.remote https://github.com/OTHER/POE.git")
}
func TestPRCheckout_branchArg(t *testing.T) {
ctx := context.NewBlank()
ctx.SetBranch("master")
@ -122,6 +187,7 @@ func TestPRCheckout_branchArg(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequests": { "nodes": [
@ -171,6 +237,7 @@ func TestPRCheckout_existingBranch(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -223,6 +290,7 @@ func TestPRCheckout_differentRepo_remoteExists(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -275,6 +343,7 @@ func TestPRCheckout_differentRepo(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -327,6 +396,7 @@ func TestPRCheckout_differentRepo_existingBranch(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -377,6 +447,7 @@ func TestPRCheckout_differentRepo_currentBranch(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -427,6 +498,7 @@ func TestPRCheckout_maintainerCanModify(t *testing.T) {
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {

View file

@ -132,13 +132,13 @@ func prCreate(cmd *cobra.Command, _ []string) error {
if headRepo != nil && !ghrepo.IsSame(baseRepo, headRepo) {
headBranchLabel = fmt.Sprintf("%s:%s", headRepo.RepoOwner(), headBranch)
}
existingPR, err := api.PullRequestForBranch(client, baseRepo, headBranchLabel)
existingPR, err := api.PullRequestForBranch(client, baseRepo, baseBranch, headBranchLabel)
var notFound *api.NotFoundError
if err != nil && !errors.As(err, &notFound) {
return fmt.Errorf("error checking for existing pull request: %w", err)
}
if err == nil {
return fmt.Errorf("a pull request for branch %q already exists:\n%s", headBranchLabel, existingPR.URL)
return fmt.Errorf("a pull request for branch %q into branch %q already exists:\n%s", headBranchLabel, baseBranch, existingPR.URL)
}
}

View file

@ -65,7 +65,8 @@ func TestPRCreate_alreadyExists(t *testing.T) {
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequests": { "nodes": [
{ "url": "https://github.com/OWNER/REPO/pull/123",
"headRefName": "feature" }
"headRefName": "feature",
"baseRefName": "master" }
] } } } }
`))
@ -79,11 +80,37 @@ func TestPRCreate_alreadyExists(t *testing.T) {
if err == nil {
t.Fatal("error expected, got nil")
}
if err.Error() != "a pull request for branch \"feature\" already exists:\nhttps://github.com/OWNER/REPO/pull/123" {
if err.Error() != "a pull request for branch \"feature\" into branch \"master\" already exists:\nhttps://github.com/OWNER/REPO/pull/123" {
t.Errorf("got error %q", err)
}
}
func TestPRCreate_alreadyExistsDifferentBase(t *testing.T) {
initBlankContext("OWNER/REPO", "feature")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequests": { "nodes": [
{ "url": "https://github.com/OWNER/REPO/pull/123",
"headRefName": "feature",
"baseRefName": "master" }
] } } } }
`))
http.StubResponse(200, bytes.NewBufferString("{}"))
cs, cmdTeardown := initCmdStubber()
defer cmdTeardown()
cs.Stub("") // git status
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
cs.Stub("") // git rev-parse
_, err := RunCommand(prCreateCmd, `pr create -BanotherBase -t"cool" -b"nah"`)
if err != nil {
t.Errorf("got unexpected error %q", err)
}
}
func TestPRCreate_web(t *testing.T) {
initBlankContext("OWNER/REPO", "feature")
http := initFakeHTTP()

View file

@ -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,10 +611,9 @@ 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()
http.StubRepoResponse("OWNER", "REPO")
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
@ -650,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)
}
@ -664,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")
@ -684,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)
}
@ -698,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")
@ -719,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)
}

View file

@ -6,6 +6,7 @@ import (
"os"
"path"
"strings"
"text/template"
"time"
"github.com/AlecAivazis/survey/v2"
@ -36,6 +37,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{
@ -79,9 +81,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,
}
@ -296,7 +298,7 @@ func repoFork(cmd *cobra.Command, args []string) error {
greenCheck := utils.Green("✓")
out := colorableOut(cmd)
s := utils.Spinner()
s := utils.Spinner(out)
loading := utils.Gray("Forking ") + utils.Bold(utils.Gray(ghrepo.FullName(toFork))) + utils.Gray("...")
s.Suffix = " " + loading
s.FinalMSG = utils.Gray(fmt.Sprintf("- %s\n", loading))
@ -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
}

View file

@ -137,16 +137,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) {
@ -169,16 +162,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) {
@ -208,16 +194,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) {
@ -290,16 +269,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) {
@ -580,7 +552,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")
@ -595,7 +567,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)
}
@ -610,7 +582,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 {
@ -628,7 +600,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)
}
@ -643,7 +615,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 {
@ -660,7 +632,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)
}
@ -674,3 +646,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")
}

View file

@ -59,7 +59,7 @@ mainLoop:
}
}
sort.Sort(sort.StringSlice(results))
sort.Strings(results)
return results
}

View file

@ -4,6 +4,8 @@ import (
"errors"
"fmt"
"os/exec"
"regexp"
"testing"
"github.com/cli/cli/internal/run"
)
@ -61,3 +63,14 @@ func createStubbedPrepareCmd(cs *CmdStubber) func(*exec.Cmd) run.Runnable {
return cs.Stubs[call]
}
}
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
}
}
}

View file

@ -2,6 +2,7 @@ package utils
import (
"fmt"
"io"
"strings"
"time"
@ -69,6 +70,8 @@ func Humanize(s string) string {
return strings.Map(h, s)
}
func Spinner() *spinner.Spinner {
return spinner.New(spinner.CharSets[11], 400*time.Millisecond)
func Spinner(w io.Writer) *spinner.Spinner {
s := spinner.New(spinner.CharSets[11], 400*time.Millisecond)
s.Writer = w
return s
}