Skip to content

Commit

Permalink
Merge pull request profclems#577 from maxice8/respect-title-desc
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems authored Jan 23, 2021
2 parents cb1df20 + 150b8f4 commit 8f5df7f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
34 changes: 23 additions & 11 deletions commands/mr/create/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ func NewCmdCreate(f *cmdutils.Factory, runE func(opts *CreateOpts) error) *cobra
// disable interactive mode if title and description are explicitly defined
opts.IsInteractive = !(hasTitle && hasDescription)

if hasTitle && hasDescription && opts.Autofill {
return &cmdutils.FlagError{
Err: errors.New("usage of --title and --description completely override --autofill"),
}
}
if opts.IsInteractive && !opts.IO.PromptEnabled() && !opts.Autofill {
return &cmdutils.FlagError{Err: errors.New("--title or --fill required for non-interactive mode")}
}
Expand Down Expand Up @@ -447,7 +452,6 @@ func createRun(opts *CreateOpts) error {
}

if action == cmdutils.SubmitAction {

message := "\nCreating merge request for %s into %s in %s\n\n"
if opts.IsDraft || opts.IsWIP {
message = "\nCreating draft merge request for %s into %s in %s\n\n"
Expand Down Expand Up @@ -476,20 +480,28 @@ func mrBodyAndTitle(opts *CreateOpts) error {
return err
}
if len(commits) == 1 {
opts.Title = commits[0].Title
body, err := git.CommitBody(commits[0].Sha)
if err != nil {
return err
if opts.Title == "" {
opts.Title = commits[0].Title
}
if opts.Description == "" {
body, err := git.CommitBody(commits[0].Sha)
if err != nil {
return err
}
opts.Description = body
}
opts.Description = body
} else {
opts.Title = utils.Humanize(opts.SourceBranch)
if opts.Title == "" {
opts.Title = utils.Humanize(opts.SourceBranch)
}

var body strings.Builder
for i := len(commits) - 1; i >= 0; i-- {
fmt.Fprintf(&body, "- %s\n", commits[i].Title)
if opts.Description == "" {
var body strings.Builder
for i := len(commits) - 1; i >= 0; i-- {
fmt.Fprintf(&body, "- %s\n", commits[i].Title)
}
opts.Description = body.String()
}
opts.Description = body.String()
}
return nil
}
Expand Down
54 changes: 50 additions & 4 deletions commands/mr/create/mr_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ func TestMrBodyAndTitle(t *testing.T) {
TargetBranch: "master",
TargetTrackingBranch: "origin/master",
}
cs, csTeardown := test.InitCmdStubber()
defer csTeardown()
cs.Stub("d1sd2e,docs: add some changes to txt file") // git log
cs.Stub("Here, I am adding some commit body.\nLittle longer\n\nResolves #1\n") // git log
t.Run("", func(t *testing.T) {
cs, csTeardown := test.InitCmdStubber()
defer csTeardown()
cs.Stub("d1sd2e,docs: add some changes to txt file") // git log
cs.Stub("Here, I am adding some commit body.\nLittle longer\n\nResolves #1\n") // git log

if err := mrBodyAndTitle(opts); err != nil {
t.Errorf("unexpected error: %v", err)
return
Expand All @@ -232,4 +233,49 @@ func TestMrBodyAndTitle(t *testing.T) {
assert.Equal(t, "docs: add some changes to txt file", opts.Title)
assert.Equal(t, "Here, I am adding some commit body.\nLittle longer\n\nResolves #1\n", opts.Description)
})
t.Run("given-title", func(t *testing.T) {
cs, csTeardown := test.InitCmdStubber()
defer csTeardown()

cs.Stub("d1sd2e,docs: add some changes to txt file")
cs.Stub("Here, I am adding some commit body.\nLittle longer\n\nResolves #1\n") // git log

opts := *opts
opts.Title = "docs: make some other stuff"
if err := mrBodyAndTitle(&opts); err != nil {
t.Errorf("unexpected error: %v", err)
return
}

assert.Equal(t, "docs: make some other stuff", opts.Title)
assert.Equal(t, `Here, I am adding some commit body.
Little longer
Resolves #1
`, opts.Description)
})
t.Run("given-description", func(t *testing.T) {
cs, csTeardown := test.InitCmdStubber()
defer csTeardown()

cs.Stub("d1sd2e,docs: add some changes to txt file")

opts := *opts
opts.Description = `Make it multiple lines
like this
resolves #1
`
if err := mrBodyAndTitle(&opts); err != nil {
t.Errorf("unexpected error: %v", err)
return
}

assert.Equal(t, "docs: add some changes to txt file", opts.Title)
assert.Equal(t, `Make it multiple lines
like this
resolves #1
`, opts.Description)
})
}

0 comments on commit 8f5df7f

Please sign in to comment.