X Tutup
Skip to content

Commit 12cf8ef

Browse files
committed
Separately query viewerMergeBodyText for GHE compatibility
GHE versions 2.22 and older will not have this GraphQL field. Avoid the resulting error and have the command proceeed with empty text as the default.
1 parent 2b36b09 commit 12cf8ef

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

api/queries_pr.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ type PullRequest struct {
8888
ReactionGroups ReactionGroups
8989
Reviews PullRequestReviews
9090
ReviewRequests ReviewRequests
91-
92-
ViewerMergeBodyText string
9391
}
9492

9593
type ReviewRequests struct {
@@ -578,7 +576,6 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
578576
milestone{
579577
title
580578
}
581-
viewerMergeBodyText
582579
` + commentsFragment() + `
583580
` + reactionGroupsFragment() + `
584581
}

pkg/cmd/pr/merge/http.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package merge
33
import (
44
"context"
55
"net/http"
6+
"strings"
67

78
"github.com/cli/cli/internal/ghinstance"
89
"github.com/cli/cli/internal/ghrepo"
@@ -98,3 +99,40 @@ func disableAutoMerge(client *http.Client, repo ghrepo.Interface, prID string) e
9899
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), client)
99100
return gql.MutateNamed(context.Background(), "PullRequestAutoMergeDisable", &mutation, variables)
100101
}
102+
103+
func getMergeText(client *http.Client, repo ghrepo.Interface, prID string, mergeMethod PullRequestMergeMethod) (string, error) {
104+
var method githubv4.PullRequestMergeMethod
105+
switch mergeMethod {
106+
case PullRequestMergeMethodMerge:
107+
method = githubv4.PullRequestMergeMethodMerge
108+
case PullRequestMergeMethodRebase:
109+
method = githubv4.PullRequestMergeMethodRebase
110+
case PullRequestMergeMethodSquash:
111+
method = githubv4.PullRequestMergeMethodSquash
112+
}
113+
114+
var query struct {
115+
Node struct {
116+
PullRequest struct {
117+
ViewerMergeBodyText string `graphql:"viewerMergeBodyText(mergeType: $method)"`
118+
} `graphql:"...on PullRequest"`
119+
} `graphql:"node(id: $prID)"`
120+
}
121+
122+
variables := map[string]interface{}{
123+
"prID": githubv4.ID(prID),
124+
"method": method,
125+
}
126+
127+
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), client)
128+
err := gql.QueryNamed(context.Background(), "PullRequestMergeText", &query, variables)
129+
if err != nil {
130+
// Tolerate this API missing in older GitHub Enterprise
131+
if strings.Contains(err.Error(), "Field 'viewerMergeBodyText' doesn't exist") {
132+
return "", nil
133+
}
134+
return "", err
135+
}
136+
137+
return query.Node.PullRequest.ViewerMergeBodyText, nil
138+
}

pkg/cmd/pr/merge/merge.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,10 @@ func mergeRun(opts *MergeOptions) error {
204204
return err
205205
}
206206

207-
if payload.commitBody == "" {
208-
if payload.method == PullRequestMergeMethodMerge {
209-
payload.commitBody = pr.Title
210-
} else {
211-
payload.commitBody = pr.ViewerMergeBodyText
207+
if !payload.setCommitBody {
208+
payload.commitBody, err = getMergeText(httpClient, baseRepo, pr.ID, payload.method)
209+
if err != nil {
210+
return err
212211
}
213212
}
214213

pkg/cmd/pr/merge/merge_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,12 @@ func TestPRMerge_interactiveSquashEditCommitMsg(t *testing.T) {
703703
"rebaseMergeAllowed": true,
704704
"squashMergeAllowed": true
705705
} } }`))
706+
http.Register(
707+
httpmock.GraphQL(`query PullRequestMergeText\b`),
708+
httpmock.StringResponse(`
709+
{ "data": { "node": {
710+
"viewerMergeBodyText": ""
711+
} } }`))
706712
http.Register(
707713
httpmock.GraphQL(`mutation PullRequestMerge\b`),
708714
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {

0 commit comments

Comments
 (0)
X Tutup