diff --git a/commands/pull_request.go b/commands/pull_request.go index a12c86bb..576209dd 100644 --- a/commands/pull_request.go +++ b/commands/pull_request.go @@ -9,6 +9,7 @@ import ( "github.com/github/hub/git" "github.com/github/hub/github" "github.com/github/hub/utils" + "github.com/octokit/go-octokit/octokit" ) var cmdPullRequest = &Command{ @@ -163,17 +164,19 @@ func pullRequest(cmd *Command, args *Args) { args.Before(fmt.Sprintf("Would request a pull request to %s from %s", fullBase, fullHead), "") pullRequestURL = "PULL_REQUEST_URL" } else { + var ( + pr *octokit.PullRequest + err error + ) + if title != "" { - pr, err := client.CreatePullRequest(baseProject, base, fullHead, title, body) - utils.Check(err) - pullRequestURL = pr.HTMLURL + pr, err = client.CreatePullRequest(baseProject, base, fullHead, title, body) + } else if flagPullRequestIssue != "" { + pr, err = client.CreatePullRequestForIssue(baseProject, base, fullHead, flagPullRequestIssue) } - if flagPullRequestIssue != "" { - pr, err := client.CreatePullRequestForIssue(baseProject, base, fullHead, flagPullRequestIssue) - utils.Check(err) - pullRequestURL = pr.HTMLURL - } + utils.Check(err) + pullRequestURL = pr.HTMLURL } args.Replace("echo", "", pullRequestURL) diff --git a/github/client.go b/github/client.go index 13058676..e2f45f33 100644 --- a/github/client.go +++ b/github/client.go @@ -63,6 +63,10 @@ func (client *Client) CreatePullRequest(project *Project, base, head, title, bod pr, result := client.octokit().PullRequests(client.requestURL(url)).Create(params) if result.HasError() { err = formatError("creating pull request", result) + if e := warnExistenceOfRepo(project, result); e != nil { + err = fmt.Errorf("%s\n%s", err, e) + } + return } @@ -79,6 +83,10 @@ func (client *Client) CreatePullRequestForIssue(project *Project, base, head, is pr, result := client.octokit().PullRequests(client.requestURL(url)).Create(params) if result.HasError() { err = formatError("creating pull request", result) + if e := warnExistenceOfRepo(project, result); e != nil { + err = fmt.Errorf("%s\n%s", err, e) + } + return } @@ -380,7 +388,7 @@ func formatError(action string, result *octokit.Result) error { statusCode := e.Response.StatusCode var reason string if s := strings.SplitN(e.Response.Status, " ", 2); len(s) >= 2 { - reason = s[1] + reason = strings.TrimSpace(s[1]) } errStr := fmt.Sprintf("Error %s: %s (HTTP %d)", action, reason, statusCode) @@ -407,3 +415,17 @@ func formatError(action string, result *octokit.Result) error { return result.Err } + +func warnExistenceOfRepo(project *Project, result *octokit.Result) (err error) { + if e, ok := result.Err.(*octokit.ResponseError); ok && e.Response.StatusCode == 404 { + var url string + if s := strings.SplitN(project.WebURL("", "", ""), "://", 2); len(s) >= 2 { + url = s[1] + } + if url != "" { + err = fmt.Errorf("Are you sure that %s exists?", url) + } + } + + return +} diff --git a/github/client_test.go b/github/client_test.go index f6c0e82f..4c1f6158 100644 --- a/github/client_test.go +++ b/github/client_test.go @@ -22,21 +22,45 @@ func TestClient_ApiEndpoint(t *testing.T) { func TestClient_formatError(t *testing.T) { result := &octokit.Result{ - Response: &octokit.Response{ - Response: &http.Response{StatusCode: 401, Status: "401 Not Found"}, + Err: &octokit.ResponseError{ + Response: &http.Response{ + StatusCode: 401, + Status: "401 Not Found", + }, }, } err := formatError("action", result) assert.Equal(t, "Error action: Not Found (HTTP 401)", fmt.Sprintf("%s", err)) result = &octokit.Result{ - Response: &octokit.Response{ - Response: &http.Response{StatusCode: 422, Status: "422 Unprocessable Entity"}, - }, Err: &octokit.ResponseError{ + Response: &http.Response{ + StatusCode: 422, + Status: "422 Unprocessable Entity", + }, Message: "error message", }, } err = formatError("action", result) assert.Equal(t, "Error action: Unprocessable Entity (HTTP 422)\nerror message", fmt.Sprintf("%s", err)) } + +func TestClient_warnExistenceOfRepo(t *testing.T) { + project := &Project{ + Name: "hub", + Owner: "github", + Host: "github.com", + } + result := &octokit.Result{ + Err: &octokit.ResponseError{ + Response: &http.Response{ + StatusCode: 404, + Status: "404 Not Found", + }, + Message: "error message", + }, + } + + err := warnExistenceOfRepo(project, result) + assert.Equal(t, "Are you sure that github.com/github/hub exists?", fmt.Sprintf("%s", err)) +}