From c9f79271b17d3cb727b3586d4e50fc9a5fad8fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Heinrichs?= Date: Wed, 20 Jan 2021 23:51:27 +0100 Subject: [PATCH] Add --maintainer-edit flag (#2250) * Add --maintainer-edit flag Closes #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 --- api/queries_pr.go | 2 +- pkg/cmd/pr/create/create.go | 23 ++++++++++---- pkg/cmd/pr/create/create_test.go | 51 ++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/api/queries_pr.go b/api/queries_pr.go index 7e2f05140..23896ce28 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -751,7 +751,7 @@ func CreatePullRequest(client *Client, repo *Repository, params map[string]inter } for key, val := range params { switch key { - case "title", "body", "draft", "baseRefName", "headRefName": + case "title", "body", "draft", "baseRefName", "headRefName", "maintainerCanModify": inputParams[key] = val } } diff --git a/pkg/cmd/pr/create/create.go b/pkg/cmd/pr/create/create.go index db623acff..009c567a7 100644 --- a/pkg/cmd/pr/create/create.go +++ b/pkg/cmd/pr/create/create.go @@ -53,6 +53,8 @@ type CreateOptions struct { Labels []string Projects []string Milestone string + + MaintainerCanModify bool } type CreateContext struct { @@ -91,6 +93,10 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co A prompt will also ask for the title and the body of the pull request. Use '--title' and '--body' to skip this, or use '--fill' to autofill these values from git commits. + + By default users with write access to the base respository can add new commits to your branch. + If undesired, you may disable access of maintainers by using '--no-maintainer-edit' + You can always change this setting later via the web interface. `), Example: heredoc.Doc(` $ 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 opts.TitleProvided = cmd.Flags().Changed("title") opts.BodyProvided = cmd.Flags().Changed("body") opts.RepoOverride, _ = cmd.Flags().GetString("repo") + noMaintainerEdit, _ := cmd.Flags().GetBool("no-maintainer-edit") + opts.MaintainerCanModify = !noMaintainerEdit if !opts.IO.CanPrompt() && opts.RecoverFile != "" { 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 if len(opts.Reviewers) > 0 && opts.WebMode { return errors.New("the --reviewer flag is not supported with --web") } + if cmd.Flags().Changed("no-maintainer-edit") && opts.WebMode { + return errors.New("the --no-maintainer-edit flag is not supported with --web") + } if runF != nil { return runF(opts) @@ -139,6 +150,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co fl.StringSliceVarP(&opts.Labels, "label", "l", nil, "Add labels by `name`") fl.StringSliceVarP(&opts.Projects, "project", "p", nil, "Add the pull request to projects by `name`") fl.StringVarP(&opts.Milestone, "milestone", "m", "", "Add the pull request to a milestone by `name`") + fl.Bool("no-maintainer-edit", false, "Disable maintainer's ability to modify pull request") fl.StringVar(&opts.RecoverFile, "recover", "", "Recover input from a failed run of create") return cmd @@ -561,11 +573,12 @@ func submitPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataS client := ctx.Client params := map[string]interface{}{ - "title": state.Title, - "body": state.Body, - "draft": state.Draft, - "baseRefName": ctx.BaseBranch, - "headRefName": ctx.HeadBranchLabel, + "title": state.Title, + "body": state.Body, + "draft": state.Draft, + "baseRefName": ctx.BaseBranch, + "headRefName": ctx.HeadBranchLabel, + "maintainerCanModify": opts.MaintainerCanModify, } if params["title"] == "" { diff --git a/pkg/cmd/pr/create/create_test.go b/pkg/cmd/pr/create/create_test.go index 939e4116b..6809a2040 100644 --- a/pkg/cmd/pr/create/create_test.go +++ b/pkg/cmd/pr/create/create_test.go @@ -304,6 +304,57 @@ func TestPRCreate(t *testing.T) { assert.Equal(t, "\nCreating pull request for feature into master in OWNER/REPO\n\n", output.Stderr()) } +func TestPRCreate_NoMaintainerModify(t *testing.T) { + // TODO update this copypasta + http := initFakeHTTP() + defer http.Verify(t) + + http.StubRepoInfoResponse("OWNER", "REPO", "master") + http.StubRepoResponse("OWNER", "REPO") + http.Register( + httpmock.GraphQL(`query UserCurrent\b`), + httpmock.StringResponse(`{"data": {"viewer": {"login": "OWNER"} } }`)) + http.Register( + httpmock.GraphQL(`query PullRequestForBranch\b`), + httpmock.StringResponse(` + { "data": { "repository": { "pullRequests": { "nodes" : [ + ] } } } } + `)) + http.Register( + httpmock.GraphQL(`mutation PullRequestCreate\b`), + httpmock.GraphQLMutation(` + { "data": { "createPullRequest": { "pullRequest": { + "URL": "https://github.com/OWNER/REPO/pull/12" + } } } } + `, func(input map[string]interface{}) { + assert.Equal(t, false, input["maintainerCanModify"].(bool)) + assert.Equal(t, "REPOID", input["repositoryId"].(string)) + assert.Equal(t, "my title", input["title"].(string)) + assert.Equal(t, "my body", input["body"].(string)) + assert.Equal(t, "master", input["baseRefName"].(string)) + assert.Equal(t, "feature", input["headRefName"].(string)) + })) + + cs, cmdTeardown := test.InitCmdStubber() + defer cmdTeardown() + + cs.Stub("") // git config --get-regexp (determineTrackingBranch) + cs.Stub("") // git show-ref --verify (determineTrackingBranch) + cs.Stub("") // git status + cs.Stub("1234567890,commit 0\n2345678901,commit 1") // git log + cs.Stub("") // git push + + ask, cleanupAsk := prompt.InitAskStubber() + defer cleanupAsk() + ask.StubOne(0) + + output, err := runCommand(http, nil, "feature", true, `-t "my title" -b "my body" --no-maintainer-edit`) + require.NoError(t, err) + + assert.Equal(t, "https://github.com/OWNER/REPO/pull/12\n", output.String()) + assert.Equal(t, "\nCreating pull request for feature into master in OWNER/REPO\n\n", output.Stderr()) +} + func TestPRCreate_createFork(t *testing.T) { http := initFakeHTTP() defer http.Verify(t)