提交 b3468238 编写于 作者: J Jingwen Owen Ou

Warn existence of URL

上级 146516d3
......@@ -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)
......
......@@ -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
}
......@@ -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))
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册