This commit is contained in:
Jingwen Owen Ou 2014-02-23 14:24:37 -08:00
Родитель 146516d3c3
Коммит b34682382e
3 изменённых файлов: 63 добавлений и 14 удалений

Просмотреть файл

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