Skip to content

Commit

Permalink
mr_create: Add -f option to open editor
Browse files Browse the repository at this point in the history
A request was made to add an option to open the editor when a file was
specified for the title and description.  Opening the editor would allow
project owners and maintainers to deploy MR templates that could be edited
by the MR submitter.

Add a --file-open_editor/-f option that opens the editor with the file
contents.

Closes #739

Suggested-by: David Arcari <darcari@redhat.com>
Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Sep 21, 2021
1 parent a395410 commit 527e709
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
40 changes: 36 additions & 4 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var mrCreateCmd = &cobra.Command{
lab mr create my_remote --draft
lab mr create my_remote -F a_file.txt
lab mr create my_remote -F a_file.txt --force-linebreak
lab mr create my_remote -f a_file.txt
lab mr create my_remote -l bug -l confirmed
lab mr create my_remote -m "A title message"
lab mr create my_remote -m "A MR title" -m "A MR description"
Expand All @@ -54,7 +55,8 @@ func init() {
mrCreateCmd.Flags().BoolP("squash", "s", false, "squash commits when merging")
mrCreateCmd.Flags().Bool("allow-collaboration", false, "allow commits from other members")
mrCreateCmd.Flags().String("milestone", "", "set milestone by milestone title or ID")
mrCreateCmd.Flags().StringP("file", "F", "", "use the given file as the Description")
mrCreateCmd.Flags().StringP("file", "F", "", "use the given file as the Title and Description")
mrCreateCmd.Flags().StringP("file-edit", "f", "", "use the given file as the Title and Description and open the editor")
mrCreateCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
mrCreateCmd.Flags().BoolP("cover-letter", "c", false, "comment changelog and diffstat")
mrCreateCmd.Flags().Bool("draft", false, "mark the merge request as draft")
Expand Down Expand Up @@ -111,6 +113,22 @@ func runMRCreate(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}

ofilename, err := cmd.Flags().GetString("file-edit")
if err != nil {
log.Fatal(err)
}

if ofilename != "" && filename != "" {
log.Fatalf("Cannot specify both -F and -f options.")
}

openEditor := false
if ofilename != "" {
filename = ofilename
openEditor = true
}

coverLetterFormat, err := cmd.Flags().GetBool("cover-letter")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -230,14 +248,28 @@ func runMRCreate(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}

if openEditor {
msg, err := mrText(sourceRemote, sourceBranch, targetRemote, targetBranch, coverLetterFormat, false)
if err != nil {
log.Fatal(err)
}

msg = title + body + msg

title, body, err = git.Edit("MERGEREQ", msg)
if err != nil {
log.Fatal(err)
}
}
} else if len(msgs) > 0 {
if coverLetterFormat {
log.Fatal("option -m cannot be combined with -c/-F")
}

title, body = msgs[0], strings.Join(msgs[1:], "\n\n")
} else {
msg, err := mrText(sourceRemote, sourceBranch, targetRemote, targetBranch, coverLetterFormat)
msg, err := mrText(sourceRemote, sourceBranch, targetRemote, targetBranch, coverLetterFormat, true)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -303,13 +335,13 @@ func runMRCreate(cmd *cobra.Command, args []string) {
}
}

func mrText(sourceRemote, sourceBranch, targetRemote, targetBranch string, coverLetterFormat bool) (string, error) {
func mrText(sourceRemote, sourceBranch, targetRemote, targetBranch string, coverLetterFormat bool, generateCommitMsg bool) (string, error) {
target := fmt.Sprintf("%s/%s", targetRemote, targetBranch)
source := fmt.Sprintf("%s/%s", sourceRemote, sourceBranch)
commitMsg := ""

numCommits := git.NumberCommits(target, source)
if numCommits == 1 {
if numCommits == 1 && generateCommitMsg {
var err error
commitMsg, err = git.LastCommitMessage(source)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/mr_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// MR Create is tested in cmd/mr_test.go

func Test_mrText(t *testing.T) {
text, err := mrText("origin", "mrtest", "origin", "master", false)
text, err := mrText("origin", "mrtest", "origin", "master", false, false)
if err != nil {
t.Log(text)
t.Fatal(err)
Expand All @@ -29,7 +29,7 @@ I am the default merge request template for lab
}

func Test_mrText_CoverLetter(t *testing.T) {
coverLetter, err := mrText("origin", "mrtest", "origin", "master", true)
coverLetter, err := mrText("origin", "mrtest", "origin", "master", true, false)
if err != nil {
t.Log(coverLetter)
t.Fatal(err)
Expand Down

0 comments on commit 527e709

Please sign in to comment.