From 56e1cdae9e5e5f5f69ecc92cef8b49a7f2a33f0c Mon Sep 17 00:00:00 2001 From: benebsiny Date: Sat, 17 Aug 2024 12:04:09 +0800 Subject: [PATCH 1/4] fix behavior for `issue develop -b non-exist-branch` --- api/queries_branch_issue_reference.go | 6 +++++- pkg/cmd/issue/develop/develop.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/api/queries_branch_issue_reference.go b/api/queries_branch_issue_reference.go index 16e36831ba4..0b60ff9359c 100644 --- a/api/queries_branch_issue_reference.go +++ b/api/queries_branch_issue_reference.go @@ -138,7 +138,11 @@ func FindRepoBranchID(client *Client, repo ghrepo.Interface, ref string) (string branchID := query.Repository.Ref.Target.Oid if branchID == "" { - branchID = query.Repository.DefaultBranchRef.Target.Oid + if ref != "" { + return "", "", fmt.Errorf("could not find branch %q in %s", ref, ghrepo.FullName(repo)) + } else { + branchID = query.Repository.DefaultBranchRef.Target.Oid + } } return query.Repository.Id, branchID, nil diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index 32423af3546..16429140906 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -106,7 +106,7 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. fl := cmd.Flags() fl.StringVar(&opts.BranchRepo, "branch-repo", "", "Name or URL of the repository where you want to create your new branch") - fl.StringVarP(&opts.BaseBranch, "base", "b", "", "Name of the base branch you want to make your new branch from") + fl.StringVarP(&opts.BaseBranch, "base", "b", "", "Name of the remote base branch you want to make your new branch from") fl.BoolVarP(&opts.Checkout, "checkout", "c", false, "Checkout the branch after creating it") fl.BoolVarP(&opts.List, "list", "l", false, "List linked branches for the issue") fl.StringVarP(&opts.Name, "name", "n", "", "Name of the branch to create") From e269d43c5a5b2d4ef3dfce14427835a732426662 Mon Sep 17 00:00:00 2001 From: benebsiny Date: Sat, 17 Aug 2024 13:47:02 +0800 Subject: [PATCH 2/4] add testing --- pkg/cmd/issue/develop/develop_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/cmd/issue/develop/develop_test.go b/pkg/cmd/issue/develop/develop_test.go index 7532ffb38bb..c35bcb845fa 100644 --- a/pkg/cmd/issue/develop/develop_test.go +++ b/pkg/cmd/issue/develop/develop_test.go @@ -515,6 +515,31 @@ func TestDevelopRun(t *testing.T) { }, expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", }, + { + name: "develop with base branch which does not exist", + opts: &DevelopOptions{ + IssueSelector: "123", + BaseBranch: "does-not-exist-branch", + }, + remotes: map[string]string{ + "origin": "OWNER/REPO", + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + reg.Register( + httpmock.GraphQL(`query LinkedBranchFeature\b`), + httpmock.StringResponse(featureEnabledPayload), + ) + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{"hasIssuesEnabled":true,"issue":{"id": "SOMEID","number":123,"title":"my issue"}}}}`), + ) + reg.Register( + httpmock.GraphQL(`query FindRepoBranchID\b`), + httpmock.StringResponse(`{"data":{"repository":{"id":"REPOID","defaultBranchRef":{"target":{"oid":"DEFAULTOID"}},"ref":null}}}`), + ) + }, + wantErr: "could not find branch \"does-not-exist-branch\" in OWNER/REPO", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 04b4122e61484f4e43d6fe991e89d79ec4c9f707 Mon Sep 17 00:00:00 2001 From: EBIBO Date: Sun, 18 Aug 2024 09:25:30 +0800 Subject: [PATCH 3/4] Update api/queries_branch_issue_reference.go Co-authored-by: Andy Feller --- api/queries_branch_issue_reference.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/queries_branch_issue_reference.go b/api/queries_branch_issue_reference.go index 0b60ff9359c..54a9144b0ee 100644 --- a/api/queries_branch_issue_reference.go +++ b/api/queries_branch_issue_reference.go @@ -140,9 +140,8 @@ func FindRepoBranchID(client *Client, repo ghrepo.Interface, ref string) (string if branchID == "" { if ref != "" { return "", "", fmt.Errorf("could not find branch %q in %s", ref, ghrepo.FullName(repo)) - } else { - branchID = query.Repository.DefaultBranchRef.Target.Oid } + branchID = query.Repository.DefaultBranchRef.Target.Oid } return query.Repository.Id, branchID, nil From 08aafc5484a16886582ef5380c3d026c8dae1d06 Mon Sep 17 00:00:00 2001 From: EBIBO Date: Sun, 18 Aug 2024 09:25:39 +0800 Subject: [PATCH 4/4] Update pkg/cmd/issue/develop/develop.go Co-authored-by: Andy Feller --- pkg/cmd/issue/develop/develop.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index 16429140906..fa01a3dacbf 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -106,7 +106,7 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. fl := cmd.Flags() fl.StringVar(&opts.BranchRepo, "branch-repo", "", "Name or URL of the repository where you want to create your new branch") - fl.StringVarP(&opts.BaseBranch, "base", "b", "", "Name of the remote base branch you want to make your new branch from") + fl.StringVarP(&opts.BaseBranch, "base", "b", "", "Name of the remote branch you want to make your new branch from") fl.BoolVarP(&opts.Checkout, "checkout", "c", false, "Checkout the branch after creating it") fl.BoolVarP(&opts.List, "list", "l", false, "List linked branches for the issue") fl.StringVarP(&opts.Name, "name", "n", "", "Name of the branch to create")