Skip to content

Commit

Permalink
Merge pull request #355 from vilmibm/pr-parent-repo
Browse files Browse the repository at this point in the history
More nuanced handling of parent/fork in various commands
  • Loading branch information
Nate Smith committed Feb 12, 2020
2 parents 1a65c9b + 2ff3190 commit c27d780
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 309 deletions.
21 changes: 21 additions & 0 deletions api/fake_http.go
@@ -1,6 +1,7 @@
package api

import (
"bytes"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -35,3 +36,23 @@ func (f *FakeHTTP) RoundTrip(req *http.Request) (*http.Response, error) {
f.Requests = append(f.Requests, req)
return resp, nil
}

func (f *FakeHTTP) StubRepoResponse(owner, repo string) {
body := bytes.NewBufferString(fmt.Sprintf(`
{ "data": { "repo_000": {
"id": "REPOID",
"name": "%s",
"owner": {"login": "%s"},
"defaultBranchRef": {
"name": "master",
"target": {"oid": "deadbeef"}
},
"viewerPermission": "WRITE"
} } }
`, repo, owner))
resp := &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(body),
}
f.responseStubs = append(f.responseStubs, resp)
}
45 changes: 27 additions & 18 deletions command/issue.go
Expand Up @@ -83,7 +83,7 @@ func issueList(cmd *cobra.Command, args []string) error {
return err
}

baseRepo, err := ctx.BaseRepo()
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
Expand All @@ -108,9 +108,9 @@ func issueList(cmd *cobra.Command, args []string) error {
return err
}

fmt.Fprintf(colorableErr(cmd), "\nIssues for %s\n\n", ghrepo.FullName(baseRepo))
fmt.Fprintf(colorableErr(cmd), "\nIssues for %s\n\n", ghrepo.FullName(*baseRepo))

issues, err := api.IssueList(apiClient, baseRepo, state, labels, assignee, limit)
issues, err := api.IssueList(apiClient, *baseRepo, state, labels, assignee, limit)
if err != nil {
return err
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func issueStatus(cmd *cobra.Command, args []string) error {
return err
}

baseRepo, err := ctx.BaseRepo()
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}
Expand All @@ -168,15 +168,15 @@ func issueStatus(cmd *cobra.Command, args []string) error {
return err
}

issuePayload, err := api.IssueStatus(apiClient, baseRepo, currentUser)
issuePayload, err := api.IssueStatus(apiClient, *baseRepo, currentUser)
if err != nil {
return err
}

out := colorableOut(cmd)

fmt.Fprintln(out, "")
fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo))
fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(*baseRepo))
fmt.Fprintln(out, "")

printHeader(out, "Issues assigned to you")
Expand Down Expand Up @@ -210,16 +210,17 @@ func issueStatus(cmd *cobra.Command, args []string) error {
func issueView(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)

baseRepo, err := ctx.BaseRepo()
apiClient, err := apiClientForContext(ctx)
if err != nil {
return err
}
apiClient, err := apiClientForContext(ctx)

baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}

issue, err := issueFromArg(apiClient, baseRepo, args[0])
issue, err := issueFromArg(apiClient, *baseRepo, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -278,22 +279,30 @@ func issueFromArg(apiClient *api.Client, baseRepo ghrepo.Interface, arg string)
func issueCreate(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)

baseRepo, err := ctx.BaseRepo()
// NB no auto forking like over in pr create
baseRepo, err := determineBaseRepo(cmd, ctx)
if err != nil {
return err
}

fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(baseRepo))
fmt.Fprintf(colorableErr(cmd), "\nCreating issue in %s\n\n", ghrepo.FullName(*baseRepo))

baseOverride, err := cmd.Flags().GetString("repo")
if err != nil {
return err
}

var templateFiles []string
if rootDir, err := git.ToplevelDir(); err == nil {
// TODO: figure out how to stub this in tests
templateFiles = githubtemplate.Find(rootDir, "ISSUE_TEMPLATE")
if baseOverride == "" {
if rootDir, err := git.ToplevelDir(); err == nil {
// TODO: figure out how to stub this in tests
templateFiles = githubtemplate.Find(rootDir, "ISSUE_TEMPLATE")
}
}

if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb {
// TODO: move URL generation into GitHubRepository
openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(baseRepo))
openURL := fmt.Sprintf("https://github.com/%s/issues/new", ghrepo.FullName(*baseRepo))
if len(templateFiles) > 1 {
openURL += "/choose"
}
Expand All @@ -306,12 +315,12 @@ func issueCreate(cmd *cobra.Command, args []string) error {
return err
}

repo, err := api.GitHubRepo(apiClient, baseRepo)
repo, err := api.GitHubRepo(apiClient, *baseRepo)
if err != nil {
return err
}
if !repo.HasIssuesEnabled {
return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(baseRepo))
return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(*baseRepo))
}

action := SubmitAction
Expand Down Expand Up @@ -352,7 +361,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
if action == PreviewAction {
openURL := fmt.Sprintf(
"https://github.com/%s/issues/new/?title=%s&body=%s",
ghrepo.FullName(baseRepo),
ghrepo.FullName(*baseRepo),
url.QueryEscape(title),
url.QueryEscape(body),
)
Expand Down
22 changes: 18 additions & 4 deletions command/issue_test.go
Expand Up @@ -15,6 +15,7 @@ import (
func TestIssueStatus(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

jsonFile, _ := os.Open("../test/fixtures/issueStatus.json")
defer jsonFile.Close()
Expand Down Expand Up @@ -43,6 +44,7 @@ func TestIssueStatus(t *testing.T) {
func TestIssueStatus_blankSlate(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand Down Expand Up @@ -79,6 +81,7 @@ Issues opened by you
func TestIssueStatus_disabledIssues(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -95,6 +98,7 @@ func TestIssueStatus_disabledIssues(t *testing.T) {
func TestIssueList(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

jsonFile, _ := os.Open("../test/fixtures/issueList.json")
defer jsonFile.Close()
Expand Down Expand Up @@ -122,6 +126,7 @@ func TestIssueList(t *testing.T) {
func TestIssueList_withFlags(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -142,7 +147,7 @@ Issues for OWNER/REPO
No issues match your search
`)

bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body)
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
reqBody := struct {
Variables struct {
Assignee string
Expand All @@ -160,6 +165,7 @@ No issues match your search
func TestIssueList_nullAssigneeLabels(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -173,7 +179,7 @@ func TestIssueList_nullAssigneeLabels(t *testing.T) {
t.Errorf("error running command `issue list`: %v", err)
}

bodyBytes, _ := ioutil.ReadAll(http.Requests[0].Body)
bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
reqBody := struct {
Variables map[string]interface{}
}{}
Expand All @@ -188,6 +194,7 @@ func TestIssueList_nullAssigneeLabels(t *testing.T) {
func TestIssueList_disabledIssues(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -204,6 +211,7 @@ func TestIssueList_disabledIssues(t *testing.T) {
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": {
Expand Down Expand Up @@ -237,6 +245,7 @@ func TestIssueView(t *testing.T) {
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": {
Expand Down Expand Up @@ -309,6 +318,7 @@ func TestIssueView_notFound(t *testing.T) {
func TestIssueView_disabledIssues(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -326,6 +336,7 @@ func TestIssueView_disabledIssues(t *testing.T) {
func TestIssueView_urlArg(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "hasIssuesEnabled": true, "issue": {
Expand Down Expand Up @@ -358,6 +369,7 @@ func TestIssueView_urlArg(t *testing.T) {
func TestIssueCreate(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -376,7 +388,7 @@ func TestIssueCreate(t *testing.T) {
t.Errorf("error running command `issue create`: %v", err)
}

bodyBytes, _ := ioutil.ReadAll(http.Requests[1].Body)
bodyBytes, _ := ioutil.ReadAll(http.Requests[2].Body)
reqBody := struct {
Variables struct {
Input struct {
Expand All @@ -398,6 +410,7 @@ func TestIssueCreate(t *testing.T) {
func TestIssueCreate_disabledIssues(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": {
Expand All @@ -414,7 +427,8 @@ func TestIssueCreate_disabledIssues(t *testing.T) {

func TestIssueCreate_web(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
initFakeHTTP()
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")

var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {
Expand Down

0 comments on commit c27d780

Please sign in to comment.