Merge pull request #6382 from aacoakley/merge-author-email-flag

Add flag to pr merge allowing the selection of the merge commit email
This commit is contained in:
Nate Smith 2022-10-04 14:50:57 -05:00 committed by GitHub
commit 9e7a6ebe4e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 0 deletions

View file

@ -36,6 +36,7 @@ type mergePayload struct {
commitBody string
setCommitBody bool
expectedHeadOid string
authorEmail string
}
// TODO: drop after githubv4 gets updated
@ -60,6 +61,10 @@ func mergePullRequest(client *http.Client, payload mergePayload) error {
input.MergeMethod = &m
}
if payload.authorEmail != "" {
authorEmail := githubv4.String(payload.authorEmail)
input.AuthorEmail = &authorEmail
}
if payload.commitSubject != "" {
commitHeadline := githubv4.String(payload.commitSubject)
input.CommitHeadline = &commitHeadline

View file

@ -39,6 +39,8 @@ type MergeOptions struct {
AutoMergeEnable bool
AutoMergeDisable bool
AuthorEmail string
Body string
BodySet bool
Subject string
@ -177,6 +179,7 @@ func NewCmdMerge(f *cmdutil.Factory, runF func(*MergeOptions) error) *cobra.Comm
cmd.Flags().BoolVar(&opts.AutoMergeEnable, "auto", false, "Automatically merge only after necessary requirements are met")
cmd.Flags().BoolVar(&opts.AutoMergeDisable, "disable-auto", false, "Disable auto-merge for this pull request")
cmd.Flags().StringVar(&opts.MatchHeadCommit, "match-head-commit", "", "Commit `SHA` that the pull request head must match to allow merge")
cmd.Flags().StringVarP(&opts.AuthorEmail, "author-email", "A", "", "Email `text` for merge commit author")
return cmd
}
@ -280,6 +283,7 @@ func (m *mergeContext) merge() error {
commitBody: m.opts.Body,
setCommitBody: m.opts.BodySet,
expectedHeadOid: m.opts.MatchHeadCommit,
authorEmail: m.opts.AuthorEmail,
}
if m.shouldAddToMergeQueue() {

View file

@ -55,6 +55,7 @@ func Test_NewCmdMerge(t *testing.T) {
MergeStrategyEmpty: true,
Body: "",
BodySet: false,
AuthorEmail: "",
},
},
{
@ -70,6 +71,7 @@ func Test_NewCmdMerge(t *testing.T) {
MergeStrategyEmpty: true,
Body: "",
BodySet: false,
AuthorEmail: "",
},
},
{
@ -85,6 +87,7 @@ func Test_NewCmdMerge(t *testing.T) {
MergeStrategyEmpty: true,
Body: "a body from file",
BodySet: true,
AuthorEmail: "",
},
},
{
@ -101,6 +104,7 @@ func Test_NewCmdMerge(t *testing.T) {
MergeStrategyEmpty: true,
Body: "this is on standard input",
BodySet: true,
AuthorEmail: "",
},
},
{
@ -116,6 +120,7 @@ func Test_NewCmdMerge(t *testing.T) {
MergeStrategyEmpty: true,
Body: "cool",
BodySet: true,
AuthorEmail: "",
},
},
{
@ -132,6 +137,23 @@ func Test_NewCmdMerge(t *testing.T) {
Body: "",
BodySet: false,
MatchHeadCommit: "555",
AuthorEmail: "",
},
},
{
name: "author email",
args: "123 --author-email octocat@github.com",
isTTY: true,
want: MergeOptions{
SelectorArg: "123",
DeleteBranch: false,
IsDeleteBranchIndicated: false,
CanDeleteLocalBranch: true,
MergeMethod: PullRequestMergeMethodMerge,
MergeStrategyEmpty: true,
Body: "",
BodySet: false,
AuthorEmail: "octocat@github.com",
},
},
{
@ -205,6 +227,7 @@ func Test_NewCmdMerge(t *testing.T) {
assert.Equal(t, tt.want.Body, opts.Body)
assert.Equal(t, tt.want.BodySet, opts.BodySet)
assert.Equal(t, tt.want.MatchHeadCommit, opts.MatchHeadCommit)
assert.Equal(t, tt.want.AuthorEmail, opts.AuthorEmail)
})
}
}
@ -537,6 +560,48 @@ func TestPrMerge_withMatchCommitHeadFlag(t *testing.T) {
}
}
func TestPrMerge_withAuthorFlag(t *testing.T) {
http := initFakeHTTP()
defer http.Verify(t)
shared.RunCommandFinder(
"1",
&api.PullRequest{
ID: "THE-ID",
Number: 1,
State: "OPEN",
Title: "The title of the PR",
MergeStateStatus: "CLEAN",
},
baseRepo("OWNER", "REPO", "main"),
)
http.Register(
httpmock.GraphQL(`mutation PullRequestMerge\b`),
httpmock.GraphQLMutation(`{}`, func(input map[string]interface{}) {
assert.Equal(t, "THE-ID", input["pullRequestId"].(string))
assert.Equal(t, "MERGE", input["mergeMethod"].(string))
assert.Equal(t, "octocat@github.com", input["authorEmail"].(string))
assert.NotContains(t, input, "commitHeadline")
}),
)
cs, cmdTeardown := run.Stub()
defer cmdTeardown(t)
cs.Register(`git rev-parse --verify refs/heads/`, 0, "")
output, err := runCommand(http, "main", true, "pr merge 1 --merge --author-email octocat@github.com")
if err != nil {
t.Fatalf("error running command `pr merge`: %v", err)
}
r := regexp.MustCompile(`Merged pull request #1 \(The title of the PR\)`)
if !r.MatchString(output.Stderr()) {
t.Fatalf("output did not match regexp /%s/\n> output\n%q\n", r, output.Stderr())
}
}
func TestPrMerge_deleteBranch(t *testing.T) {
http := initFakeHTTP()
defer http.Verify(t)