X Tutup
Skip to content

Commit c9f7927

Browse files
BjoernAkAManfvilmibm
andauthored
Add --maintainer-edit flag (cli#2250)
* Add --maintainer-edit flag Closes cli#2213 while retaining backwards compatibility. * Fix linting * Adjust documentation and validation * Negate logic and fix build errors * rename to no-maintainer-edit * test * use a positive option instead of negative Co-authored-by: vilmibm <vilmibm@github.com>
1 parent 3bec668 commit c9f7927

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

api/queries_pr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter
751751
}
752752
for key, val := range params {
753753
switch key {
754-
case "title", "body", "draft", "baseRefName", "headRefName":
754+
case "title", "body", "draft", "baseRefName", "headRefName", "maintainerCanModify":
755755
inputParams[key] = val
756756
}
757757
}

pkg/cmd/pr/create/create.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type CreateOptions struct {
5353
Labels []string
5454
Projects []string
5555
Milestone string
56+
57+
MaintainerCanModify bool
5658
}
5759

5860
type CreateContext struct {
@@ -91,6 +93,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
9193
9294
A prompt will also ask for the title and the body of the pull request. Use '--title'
9395
and '--body' to skip this, or use '--fill' to autofill these values from git commits.
96+
97+
By default users with write access to the base respository can add new commits to your branch.
98+
If undesired, you may disable access of maintainers by using '--no-maintainer-edit'
99+
You can always change this setting later via the web interface.
94100
`),
95101
Example: heredoc.Doc(`
96102
$ gh pr create --title "The bug is fixed" --body "Everything works again"
@@ -103,6 +109,8 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
103109
opts.TitleProvided = cmd.Flags().Changed("title")
104110
opts.BodyProvided = cmd.Flags().Changed("body")
105111
opts.RepoOverride, _ = cmd.Flags().GetString("repo")
112+
noMaintainerEdit, _ := cmd.Flags().GetBool("no-maintainer-edit")
113+
opts.MaintainerCanModify = !noMaintainerEdit
106114

107115
if !opts.IO.CanPrompt() && opts.RecoverFile != "" {
108116
return &cmdutil.FlagError{Err: errors.New("--recover only supported when running interactively")}
@@ -118,6 +126,9 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
118126
if len(opts.Reviewers) > 0 && opts.WebMode {
119127
return errors.New("the --reviewer flag is not supported with --web")
120128
}
129+
if cmd.Flags().Changed("no-maintainer-edit") && opts.WebMode {
130+
return errors.New("the --no-maintainer-edit flag is not supported with --web")
131+
}
121132

122133
if runF != nil {
123134
return runF(opts)
@@ -139,6 +150,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
139150
fl.StringSliceVarP(&opts.Labels, "label", "l", nil, "Add labels by `name`")
140151
fl.StringSliceVarP(&opts.Projects, "project", "p", nil, "Add the pull request to projects by `name`")
141152
fl.StringVarP(&opts.Milestone, "milestone", "m", "", "Add the pull request to a milestone by `name`")
153+
fl.Bool("no-maintainer-edit", false, "Disable maintainer's ability to modify pull request")
142154
fl.StringVar(&opts.RecoverFile, "recover", "", "Recover input from a failed run of create")
143155

144156
return cmd
@@ -561,11 +573,12 @@ func submitPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataS
561573
client := ctx.Client
562574

563575
params := map[string]interface{}{
564-
"title": state.Title,
565-
"body": state.Body,
566-
"draft": state.Draft,
567-
"baseRefName": ctx.BaseBranch,
568-
"headRefName": ctx.HeadBranchLabel,
576+
"title": state.Title,
577+
"body": state.Body,
578+
"draft": state.Draft,
579+
"baseRefName": ctx.BaseBranch,
580+
"headRefName": ctx.HeadBranchLabel,
581+
"maintainerCanModify": opts.MaintainerCanModify,
569582
}
570583

571584
if params["title"] == "" {

pkg/cmd/pr/create/create_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,57 @@ func TestPRCreate(t *testing.T) {
304304
assert.Equal(t, "\nCreating pull request for feature into master in OWNER/REPO\n\n", output.Stderr())
305305
}
306306

307+
func TestPRCreate_NoMaintainerModify(t *testing.T) {
308+
// TODO update this copypasta
309+
http := initFakeHTTP()
310+
defer http.Verify(t)
311+
312+
http.StubRepoInfoResponse("OWNER", "REPO", "master")
313+
http.StubRepoResponse("OWNER", "REPO")
314+
http.Register(
315+
httpmock.GraphQL(`query UserCurrent\b`),
316+
httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`))
317+
http.Register(
318+
httpmock.GraphQL(`query PullRequestForBranch\b`),
319+
httpmock.StringResponse(`
320+
{ "data": { "repository": { "pullRequests": { "nodes" : [
321+
] } } } }
322+
`))
323+
http.Register(
324+
httpmock.GraphQL(`mutation PullRequestCreate\b`),
325+
httpmock.GraphQLMutation(`
326+
{ "data": { "createPullRequest": { "pullRequest": {
327+
"URL": "https://github.com/OWNER/REPO/pull/12"
328+
} } } }
329+
`, func(input map[string]interface{}) {
330+
assert.Equal(t, false, input["maintainerCanModify"].(bool))
331+
assert.Equal(t, "REPOID", input["repositoryId"].(string))
332+
assert.Equal(t, "my title", input["title"].(string))
333+
assert.Equal(t, "my body", input["body"].(string))
334+
assert.Equal(t, "master", input["baseRefName"].(string))
335+
assert.Equal(t, "feature", input["headRefName"].(string))
336+
}))
337+
338+
cs, cmdTeardown := test.InitCmdStubber()
339+
defer cmdTeardown()
340+
341+
cs.Stub("") // git config --get-regexp (determineTrackingBranch)
342+
cs.Stub("") // git show-ref --verify (determineTrackingBranch)
343+
cs.Stub("") // git status
344+
cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log
345+
cs.Stub("") // git push
346+
347+
ask, cleanupAsk := prompt.InitAskStubber()
348+
defer cleanupAsk()
349+
ask.StubOne(0)
350+
351+
output, err := runCommand(http, nil, "feature", true, `-t "my title" -b "my body" --no-maintainer-edit`)
352+
require.NoError(t, err)
353+
354+
assert.Equal(t, "https://github.com/OWNER/REPO/pull/12\n", output.String())
355+
assert.Equal(t, "\nCreating pull request for feature into master in OWNER/REPO\n\n", output.Stderr())
356+
}
357+
307358
func TestPRCreate_createFork(t *testing.T) {
308359
http := initFakeHTTP()
309360
defer http.Verify(t)

0 commit comments

Comments
 (0)
X Tutup