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.
This commit is contained in:
Chris Westra 2022-09-14 14:54:43 -04:00
parent 48512176d8
commit bc72d6b983
3 changed files with 55 additions and 8 deletions

View file

@ -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,

View file

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

View file

@ -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"