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.
This commit is contained in:
Mislav Marohnić 2021-02-17 14:06:27 +01:00
parent 2b36b09abf
commit 12cf8ef65b
4 changed files with 48 additions and 8 deletions

View file

@ -88,8 +88,6 @@ type PullRequest struct {
ReactionGroups ReactionGroups
Reviews PullRequestReviews
ReviewRequests ReviewRequests
ViewerMergeBodyText string
}
type ReviewRequests struct {
@ -578,7 +576,6 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
milestone{
title
}
viewerMergeBodyText
` + commentsFragment() + `
` + reactionGroupsFragment() + `
}

View file

@ -3,6 +3,7 @@ package merge
import (
"context"
"net/http"
"strings"
"github.com/cli/cli/internal/ghinstance"
"github.com/cli/cli/internal/ghrepo"
@ -98,3 +99,40 @@ func disableAutoMerge(client *http.Client, repo ghrepo.Interface, prID string) e
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), client)
return gql.MutateNamed(context.Background(), "PullRequestAutoMergeDisable", &mutation, variables)
}
func getMergeText(client *http.Client, repo ghrepo.Interface, prID string, mergeMethod PullRequestMergeMethod) (string, error) {
var method githubv4.PullRequestMergeMethod
switch mergeMethod {
case PullRequestMergeMethodMerge:
method = githubv4.PullRequestMergeMethodMerge
case PullRequestMergeMethodRebase:
method = githubv4.PullRequestMergeMethodRebase
case PullRequestMergeMethodSquash:
method = githubv4.PullRequestMergeMethodSquash
}
var query struct {
Node struct {
PullRequest struct {
ViewerMergeBodyText string `graphql:"viewerMergeBodyText(mergeType: $method)"`
} `graphql:"...on PullRequest"`
} `graphql:"node(id: $prID)"`
}
variables := map[string]interface{}{
"prID": githubv4.ID(prID),
"method": method,
}
gql := graphql.NewClient(ghinstance.GraphQLEndpoint(repo.RepoHost()), client)
err := gql.QueryNamed(context.Background(), "PullRequestMergeText", &query, variables)
if err != nil {
// Tolerate this API missing in older GitHub Enterprise
if strings.Contains(err.Error(), "Field 'viewerMergeBodyText' doesn't exist") {
return "", nil
}
return "", err
}
return query.Node.PullRequest.ViewerMergeBodyText, nil
}

View file

@ -204,11 +204,10 @@ func mergeRun(opts *MergeOptions) error {
return err
}
if payload.commitBody == "" {
if payload.method == PullRequestMergeMethodMerge {
payload.commitBody = pr.Title
} else {
payload.commitBody = pr.ViewerMergeBodyText
if !payload.setCommitBody {
payload.commitBody, err = getMergeText(httpClient, baseRepo, pr.ID, payload.method)
if err != nil {
return err
}
}

View file

@ -703,6 +703,12 @@ func TestPRMerge_interactiveSquashEditCommitMsg(t *testing.T) {
"rebaseMergeAllowed": true,
"squashMergeAllowed": true
} } }`))
http.Register(
httpmock.GraphQL(`query PullRequestMergeText\b`),
httpmock.StringResponse(`
{ "data": { "node": {
"viewerMergeBodyText": ""
} } }`))
http.Register(
httpmock.GraphQL(`mutation PullRequestMerge\b`),
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {