Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
feat: fill MR desc with all commit bodies
Browse files Browse the repository at this point in the history
adds --fill-commit-body flag to add all the commit bodies to the MR description
when creating an my with --fill

closes #795
  • Loading branch information
guyfedwards committed Aug 12, 2021
1 parent b8a02b5 commit 52126c4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
36 changes: 27 additions & 9 deletions commands/mr/create/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"os"
"regexp"
"strings"

"github.com/profclems/glab/pkg/iostreams"
Expand Down Expand Up @@ -40,14 +41,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)
Expand Down Expand Up @@ -84,6 +86,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) {
Expand Down Expand Up @@ -121,6 +124,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-commit-body should be used with --fill")}
}
// Remove this once --yes does more than just skip the prompts that --web happen to skip
// by design
if opts.Yes && opts.Web {
Expand All @@ -135,6 +141,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. Can only be used with --fill")
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")
Expand Down Expand Up @@ -501,7 +508,18 @@ 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
}
re := regexp.MustCompile(`\r?\n\n`)
commitBody = re.ReplaceAllString(commitBody, " \n")
fmt.Fprintf(&body, "%s\n", commitBody)
}
}
opts.Description = body.String()
}
Expand Down
33 changes: 33 additions & 0 deletions commands/mr/create/mr_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,39 @@ 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)
})
}

0 comments on commit 52126c4

Please sign in to comment.