From 24a332bb1b73a2660963d4d8d7f228b378207a20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 31 Mar 2020 13:32:44 +0200 Subject: [PATCH 1/2] Use remote tracking branch for base when detecting commits for `pr create` This is to avoid errors when a local base branch is missing or out of date. --- command/pr_create.go | 6 +++++- command/pr_create_test.go | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/command/pr_create.go b/command/pr_create.go index 238057bf8..96448175c 100644 --- a/command/pr_create.go +++ b/command/pr_create.go @@ -122,7 +122,11 @@ func prCreate(cmd *cobra.Command, _ []string) error { return fmt.Errorf("could not parse body: %w", err) } - defs, defaultsErr := computeDefaults(baseBranch, headBranch) + baseTrackingBranch := baseBranch + if baseRemote, err := remotes.FindByRepo(baseRepo.RepoOwner(), baseRepo.RepoName()); err == nil { + baseTrackingBranch = fmt.Sprintf("%s/%s", baseRemote.Name, baseBranch) + } + defs, defaultsErr := computeDefaults(baseTrackingBranch, headBranch) isWeb, err := cmd.Flags().GetBool("web") if err != nil { diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 01b5516d3..5189ca5c8 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -514,7 +514,7 @@ func TestPRCreate_defaults_error_autofill(t *testing.T) { _, err := RunCommand(prCreateCmd, "pr create -f") - eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature") + eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between origin/master and feature") } func TestPRCreate_defaults_error_web(t *testing.T) { @@ -532,7 +532,7 @@ func TestPRCreate_defaults_error_web(t *testing.T) { _, err := RunCommand(prCreateCmd, "pr create -w") - eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between master and feature") + eq(t, err.Error(), "could not compute title or body defaults: could not find any commits between origin/master and feature") } func TestPRCreate_defaults_error_interactive(t *testing.T) { From 219e3ecc5bbb92cf70e3632b823736f2d16c6d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 31 Mar 2020 13:34:16 +0200 Subject: [PATCH 2/2] Stop querying base branch commit hash from API This code was put in place in preparation for a feature that never shipped. Namely, we wanted to use the commit hash for the base branch so we can get an accurate `git log` involving the changes in a pull request. However, getting the commit hash from API is not the way to go because the latest commit might not be available in the person's local repository, and using a local tracking branch for base such as `origin/master` works quite well in most cases without dereferencing it. --- api/fake_http.go | 9 +++------ api/queries_repo.go | 6 +----- command/pr_create_test.go | 9 +++------ 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/api/fake_http.go b/api/fake_http.go index 92b904bd5..ab726fb34 100644 --- a/api/fake_http.go +++ b/api/fake_http.go @@ -54,8 +54,7 @@ func (f *FakeHTTP) StubRepoResponse(owner, repo string) { "name": "%s", "owner": {"login": "%s"}, "defaultBranchRef": { - "name": "master", - "target": {"oid": "deadbeef"} + "name": "master" }, "viewerPermission": "WRITE" } } } @@ -76,8 +75,7 @@ func (f *FakeHTTP) StubForkedRepoResponse(forkFullName, parentFullName string) { "name": "%s", "owner": {"login": "%s"}, "defaultBranchRef": { - "name": "master", - "target": {"oid": "deadbeef"} + "name": "master" }, "viewerPermission": "ADMIN", "parent": { @@ -85,8 +83,7 @@ func (f *FakeHTTP) StubForkedRepoResponse(forkFullName, parentFullName string) { "name": "%s", "owner": {"login": "%s"}, "defaultBranchRef": { - "name": "master", - "target": {"oid": "deadbeef"} + "name": "master" }, "viewerPermission": "READ" } diff --git a/api/queries_repo.go b/api/queries_repo.go index 5a5061eda..5a2e8e1da 100644 --- a/api/queries_repo.go +++ b/api/queries_repo.go @@ -28,10 +28,7 @@ type Repository struct { HasIssuesEnabled bool ViewerPermission string DefaultBranchRef struct { - Name string - Target struct { - OID string - } + Name string } Parent *Repository @@ -127,7 +124,6 @@ func RepoNetwork(client *Client, repos []ghrepo.Interface) (RepoNetworkResult, e viewerPermission defaultBranchRef { name - target { oid } } isPrivate } diff --git a/command/pr_create_test.go b/command/pr_create_test.go index 5189ca5c8..fa0fbd0ce 100644 --- a/command/pr_create_test.go +++ b/command/pr_create_test.go @@ -215,8 +215,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) { "name": "REPO", "owner": {"login": "OWNER"}, "defaultBranchRef": { - "name": "default", - "target": {"oid": "deadbeef"} + "name": "default" }, "viewerPermission": "READ" }, @@ -226,8 +225,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) { "name": "REPO", "owner": {"login": "OWNER"}, "defaultBranchRef": { - "name": "default", - "target": {"oid": "deadbeef"} + "name": "default" }, "viewerPermission": "READ" }, @@ -235,8 +233,7 @@ func TestPRCreate_cross_repo_same_branch(t *testing.T) { "name": "REPO", "owner": {"login": "MYSELF"}, "defaultBranchRef": { - "name": "default", - "target": {"oid": "deadbeef"} + "name": "default" }, "viewerPermission": "WRITE" } } }