From bc72d6b983a9afcb18dbf404e632ea2f688a50b6 Mon Sep 17 00:00:00 2001 From: Chris Westra Date: Wed, 14 Sep 2022 14:54:43 -0400 Subject: [PATCH] Omit name param from mutation when blank As it currently sits the backend won't ignore the param. I'll be looking to add a PR for this but for now we'll remove it. --- api/queries_branch_issue_reference.go | 20 +++++++++---- pkg/cmd/issue/develop/develop.go | 2 +- pkg/cmd/issue/develop/develop_test.go | 41 +++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/api/queries_branch_issue_reference.go b/api/queries_branch_issue_reference.go index f1265b484..7c04110de 100644 --- a/api/queries_branch_issue_reference.go +++ b/api/queries_branch_issue_reference.go @@ -1,18 +1,29 @@ package api -import "github.com/cli/cli/v2/internal/ghrepo" +import ( + "fmt" + + "github.com/cli/cli/v2/internal/ghrepo" +) type BranchIssueReference struct { ID string BranchName string } +func nameParam(params map[string]interface{}) string { + if params["name"] != "" { + return "name: $name," + } + return "" +} + func CreateBranchIssueReference(client *Client, repo *Repository, params map[string]interface{}) (*BranchIssueReference, error) { - query := ` + query := fmt.Sprintf(` mutation CreateLinkedBranch($issueId: ID!, $oid: GitObjectID!, $name: String, $repositoryId: ID) { createLinkedBranch(input: { issueId: $issueId, - name: $name, + %[1]s oid: $oid, repositoryId: $repositoryId }) { @@ -23,8 +34,7 @@ func CreateBranchIssueReference(client *Client, repo *Repository, params map[str } } } - } - ` + }`, nameParam(params)) inputParams := map[string]interface{}{ "repositoryId": repo.ID, diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index a8e73add0..99eec45b5 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -67,7 +67,7 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. fl.StringVarP(&opts.BaseBranch, "base-branch", "b", "", "Name of the base branch") fl.BoolVarP(&opts.Checkout, "checkout", "c", false, "Checkout the branch after creating it") fl.StringVarP(&opts.IssueRepoSelector, "issue-repo", "i", "", "Name or URL of the issue's repository") - fl.BoolVarP(&opts.List, "list", "l", false, "List branches for the issue") + 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") return cmd } diff --git a/pkg/cmd/issue/develop/develop_test.go b/pkg/cmd/issue/develop/develop_test.go index fd7296559..b14c2df9d 100644 --- a/pkg/cmd/issue/develop/develop_test.go +++ b/pkg/cmd/issue/develop/develop_test.go @@ -215,7 +215,7 @@ func Test_developRun(t *testing.T) { }, wantErr: "issue repo in url cli/test-repo does not match the repo from --issue-repo cli/other", }, - {name: "develop new branch", + {name: "develop new branch with a name provided", setup: func(opts *DevelopOptions, t *testing.T) func() { opts.Name = "my-branch" opts.BaseBranch = "main" @@ -240,7 +240,7 @@ func Test_developRun(t *testing.T) { httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) reg.Register( - httpmock.GraphQL(`mutation CreateLinkedBranch\b`), + httpmock.GraphQL(`(?s)mutation CreateLinkedBranch\b.*issueId: \$issueId,\s+name: \$name,\s+oid: \$oid,`), httpmock.GraphQLQuery(`{ "data": { "createLinkedBranch": { "linkedBranch": {"id": "2", "ref": {"name": "my-branch"} } } } }`, func(query string, inputs map[string]interface{}) { assert.Equal(t, "REPOID", inputs["repositoryId"]) @@ -252,6 +252,43 @@ func Test_developRun(t *testing.T) { }, expectedOut: "github.com/OWNER/REPO/tree/my-branch\n", }, + {name: "develop new branch without a name provided omits the param from the mutation", + setup: func(opts *DevelopOptions, t *testing.T) func() { + opts.Name = "" + opts.BaseBranch = "main" + opts.IssueSelector = "123" + return func() {} + }, + httpStubs: func(reg *httpmock.Registry, t *testing.T) { + + reg.Register( + httpmock.GraphQL(`query RepositoryInfo\b`), + httpmock.StringResponse(` + { "data": { "repository": { + "id": "REPOID", + "hasIssuesEnabled": true + } } }`), + ) + reg.Register( + httpmock.GraphQL(`query IssueByNumber\b`), + httpmock.StringResponse(`{"data":{"repository":{ "hasIssuesEnabled": true, "issue":{"id": "yar", "number":123, "title":"my issue"} }}}`)) + reg.Register( + httpmock.GraphQL(`query BranchIssueReferenceFindBaseOid\b`), + httpmock.StringResponse(`{"data":{"repository":{"ref":{"target":{"oid":"123"}}}}}`)) + + reg.Register( + httpmock.GraphQL(`(?s)mutation CreateLinkedBranch\b.*issueId: \$issueId,\s+oid: \$oid,`), + httpmock.GraphQLQuery(`{ "data": { "createLinkedBranch": { "linkedBranch": {"id": "2", "ref": {"name": "my-issue-1"} } } } }`, + func(query string, inputs map[string]interface{}) { + assert.Equal(t, "REPOID", inputs["repositoryId"]) + assert.Equal(t, "", inputs["name"]) + assert.Equal(t, "yar", inputs["issueId"]) + }), + ) + + }, + expectedOut: "github.com/OWNER/REPO/tree/my-issue-1\n", + }, {name: "develop providing an issue url and specifying a different repo returns an error", setup: func(opts *DevelopOptions, t *testing.T) func() { opts.IssueSelector = "https://github.com/cli/test-repo/issues/42"