Validate PR passed as pr view <url> before opening it

This commit is contained in:
Mislav Marohnić 2019-12-03 21:45:28 +01:00
parent b223176b37
commit 4e859fa7ca
2 changed files with 9 additions and 11 deletions

View file

@ -260,7 +260,8 @@ func prFromArg(apiClient *api.Client, baseRepo context.GitHubRepository, arg str
}
if m := prURLRE.FindStringSubmatch(arg); m != nil {
return &api.PullRequest{URL: m[0]}, nil
prNumber, _ := strconv.Atoi(m[3])
return api.PullRequestByNumber(apiClient, baseRepo, prNumber)
}
return api.PullRequestForBranch(apiClient, baseRepo, arg)
@ -332,15 +333,6 @@ func prCheckout(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if pr.Number == 0 {
// hydrate the pr object by fetching extra information from the API
m := prURLRE.FindStringSubmatch(pr.URL)
prNumber, _ := strconv.Atoi(m[3])
pr, err = api.PullRequestByNumber(apiClient, baseRemote, prNumber)
if err != nil {
return err
}
}
headRemote := baseRemote
if pr.IsCrossRepository {

View file

@ -256,7 +256,13 @@ func TestPRView_numberArg(t *testing.T) {
func TestPRView_urlArg(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
initFakeHTTP()
http := initFakeHTTP()
http.StubResponse(200, bytes.NewBufferString(`
{ "data": { "repository": { "pullRequest": {
"url": "https://github.com/OWNER/REPO/pull/23"
} } } }
`))
var seenCmd *exec.Cmd
restoreCmd := utils.SetPrepareCmd(func(cmd *exec.Cmd) utils.Runnable {