From 8cae6e24b56ff8bdb6ce66d76005434bdd811fec Mon Sep 17 00:00:00 2001 From: guyfedwards Date: Thu, 12 Aug 2021 00:45:26 +0100 Subject: [PATCH] feat: fill MR desc with all commit bodies adds --fill-commit-body flag to add all the commit bodies to the MR description when creating an my with --fill closes #795 --- commands/mr/create/mr_create.go | 33 +++++++++++++++++++-------- commands/mr/create/mr_create_test.go | 34 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/commands/mr/create/mr_create.go b/commands/mr/create/mr_create.go index 157f40636..aec47b532 100644 --- a/commands/mr/create/mr_create.go +++ b/commands/mr/create/mr_create.go @@ -40,14 +40,15 @@ type CreateOpts struct { RemoveSourceBranch bool AllowCollaboration bool - Autofill bool - IsDraft bool - IsWIP bool - ShouldPush bool - NoEditor bool - IsInteractive bool - Yes bool - Web bool + Autofill bool + FillCommitBody bool + IsDraft bool + IsWIP bool + ShouldPush bool + NoEditor bool + IsInteractive bool + Yes bool + Web bool IO *iostreams.IOStreams Branch func() (string, error) @@ -84,6 +85,7 @@ func NewCmdCreate(f *cmdutils.Factory, runE func(opts *CreateOpts) error) *cobra $ glab mr create -a username -t "fix annoying bug" $ glab mr create -f --draft --label RFC $ glab mr create --fill --yes --web + $ glab mr create --fill --fill-commit-body --yes `), Args: cobra.ExactArgs(0), PreRun: func(cmd *cobra.Command, args []string) { @@ -121,6 +123,9 @@ func NewCmdCreate(f *cmdutils.Factory, runE func(opts *CreateOpts) error) *cobra if cmd.Flags().Changed("wip") && cmd.Flags().Changed("draft") { return &cmdutils.FlagError{Err: errors.New("specify either of --draft or --wip")} } + if !opts.Autofill && opts.FillCommitBody { + return &cmdutils.FlagError{Err: errors.New("--fill required for --fill-commit-body")} + } // Remove this once --yes does more than just skip the prompts that --web happen to skip // by design if opts.Yes && opts.Web { @@ -135,6 +140,7 @@ func NewCmdCreate(f *cmdutils.Factory, runE func(opts *CreateOpts) error) *cobra }, } mrCreateCmd.Flags().BoolVarP(&opts.Autofill, "fill", "f", false, "Do not prompt for title/description and just use commit info") + mrCreateCmd.Flags().BoolVarP(&opts.FillCommitBody, "fill-commit-body", "", false, "Fill description with each commit body when multiple commits") mrCreateCmd.Flags().BoolVarP(&opts.IsDraft, "draft", "", false, "Mark merge request as a draft") mrCreateCmd.Flags().BoolVarP(&opts.IsWIP, "wip", "", false, "Mark merge request as a work in progress. Alternative to --draft") mrCreateCmd.Flags().BoolVarP(&opts.ShouldPush, "push", "", false, "Push committed changes after creating merge request. Make sure you have committed changes") @@ -501,7 +507,16 @@ func mrBodyAndTitle(opts *CreateOpts) error { if opts.Description == "" { var body strings.Builder for i := len(commits) - 1; i >= 0; i-- { - fmt.Fprintf(&body, "- %s\n", commits[i].Title) + // adds 2 spaces for markdown line wrapping + fmt.Fprintf(&body, "- %s \n", commits[i].Title) + + if opts.FillCommitBody { + commitBody, err := git.CommitBody(commits[i].Sha) + if err != nil { + return err + } + fmt.Fprintf(&body, "%s\n", commitBody) + } } opts.Description = body.String() } diff --git a/commands/mr/create/mr_create_test.go b/commands/mr/create/mr_create_test.go index 008dc91b8..605515e28 100644 --- a/commands/mr/create/mr_create_test.go +++ b/commands/mr/create/mr_create_test.go @@ -276,6 +276,40 @@ resolves #1 like this resolves #1 +`, opts.Description) + }) + t.Run("given-fill-commit-body", func(t *testing.T) { + opts = &CreateOpts{ + SourceBranch: "mr-autofill-test-br", + TargetBranch: "master", + TargetTrackingBranch: "origin/master", + } + cs, csTeardown := test.InitCmdStubber() + defer csTeardown() + + cs.Stub("d1sd2e,chore: some tidying\nd2asa3,docs: more changes to more things") + cs.Stub("Here, I am adding some commit body.\nLittle longer\n\nResolves #1\n") + cs.Stub("another body for another commit\ncloses 1234\n") + + opts := *opts + opts.FillCommitBody = true + + if err := mrBodyAndTitle(&opts); err != nil { + t.Errorf("unexpected error: %v", err) + return + } + + assert.Equal(t, "mr autofill test br", opts.Title) + assert.Equal(t, `- docs: more changes to more things +Here, I am adding some commit body. +Little longer + +Resolves #1 + +- chore: some tidying +another body for another commit +closes 1234 + `, opts.Description) }) }