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

Commit

Permalink
refactor: additional flags for merge request
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Aug 15, 2020
1 parent 09ba137 commit acf2e6e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 44 deletions.
2 changes: 1 addition & 1 deletion commands/issue_view.go
Expand Up @@ -80,7 +80,7 @@ var issueViewCmd = &cobra.Command{
}
assignees = strings.Trim(assignees, ", ")
table := uitable.New()
table.MaxColWidth = 50
table.MaxColWidth = 70
table.Wrap = true
table.AddRow("Project ID:", issue.ProjectID)
table.AddRow("Labels:", prettifyNilEmptyValues(labels, "None"))
Expand Down
104 changes: 63 additions & 41 deletions commands/mr_create.go
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/xanzy/go-gitlab"
"glab/internal/git"
"glab/internal/manip"
"log"
"strings"
)

Expand All @@ -16,59 +15,82 @@ var mrCreateCmd = &cobra.Command{
Long: ``,
Aliases: []string{"new"},
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
cmdErr(cmd, args)
return
return nil
}
l := &gitlab.CreateMergeRequestOptions{}
var sourceBranch string
var targetBranch string
targetBranch := "master"
var mergeTitle string
var mergeLabel string
var mergeDescription string
if title, _ := cmd.Flags().GetString("title"); title != "" {
mergeTitle = strings.Trim(title, " ")
} else {
mergeTitle = manip.AskQuestionWithInput("Title:", "", true)
}
if label, _ := cmd.Flags().GetString("label"); label != "" {
mergeLabel = strings.Trim(label, "[] ")
} else {
mergeLabel = manip.AskQuestionWithInput("Label(s) [Comma Separated]:", "", false)
}
if desc, _ := cmd.Flags().GetString("description"); desc != "" {
mergeDescription = strings.Trim(desc, " ")
} else {
if editor, _ := cmd.Flags().GetBool("no-editor"); editor {
mergeDescription = manip.AskQuestionMultiline("Description:", "")
} else {
mergeDescription = manip.Editor(manip.EditorOptions{
Label: "Description:",
Help : "Enter the MR description. ",
FileName : "*_MERGE_REQUEST_EDITMSG.md",
})
}
l := &gitlab.CreateMergeRequestOptions{}

gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
if source, _ := cmd.Flags().GetString("source-branch"); source != "" {
sourceBranch = strings.Trim(source, "[] ")
} else {
if c, _ := cmd.Flags().GetBool("create-source-branch"); c {
if c, _ := cmd.Flags().GetBool("create-source-branch"); c && sourceBranch=="" {
sourceBranch = manip.ReplaceNonAlphaNumericChars(mergeTitle, "-")
} else {
b, err := git.CurrentBranch()
if err != nil {
er(err)
return
return err
}
sourceBranch = b
}
}
if title, _ := cmd.Flags().GetString("title"); title != "" {
if title, _ := cmd.Flags().GetString("title"); title != "" {
mergeTitle = strings.Trim(title, " ")
} else {
mergeTitle = manip.AskQuestionWithInput("Title:", "", true)
}
if desc, _ := cmd.Flags().GetString("description"); desc != "" {
mergeDescription = desc
} else {
if editor, _ := cmd.Flags().GetBool("no-editor"); editor {
mergeDescription = manip.AskQuestionMultiline("Description:", "")
} else {
mergeDescription = manip.Editor(manip.EditorOptions{
Label: "Description:",
Help: "Enter the MR description. ",
FileName: "*_MR_EDITMSG.md",
})
}
sourceBranch = manip.AskQuestionWithInput("Source Branch (Defaults to current branch):", b, false)
}
} else {
branch, _ := git.CurrentBranch()
commit, _ := git.LatestCommit(branch)
_, err := getCommit(repo, targetBranch)
if err != nil {
return fmt.Errorf("target branch %s does not exist on remote. Specify target branch with --target-branch flag", targetBranch)
}
mergeDescription, err = git.CommitBody(branch)
if err != nil {
return err
}
mergeTitle = commit.Title
//fmt.Println(git.CommitBody(branchRef[0].))
}
if t, _ := cmd.Flags().GetString("target-branch"); t != "" {
targetBranch = strings.Trim(t, "[] ")
} else {
targetBranch = manip.AskQuestionWithInput("Target Branch:", "", true)
targetBranch = manip.AskQuestionWithInput("Target Branch (Default is " + targetBranch + "):", targetBranch, false)
}
isDraft, _ := cmd.Flags().GetBool("draft")
isWIP, _ := cmd.Flags().GetBool("wip")
if isDraft || isWIP {
if isDraft {
mergeTitle = "Draft: "+mergeTitle
} else {
mergeTitle = "WIP: " + mergeTitle
}
}
mergeLabel, _ := cmd.Flags().GetString("label")
l.Title = gitlab.String(mergeTitle)
l.Description = gitlab.String(mergeDescription)
l.Labels = &gitlab.Labels{mergeLabel}
Expand Down Expand Up @@ -96,11 +118,6 @@ var mrCreateCmd = &cobra.Command{
}
l.AssigneeIDs = t2
}

gitlabClient, repo := git.InitGitlabClient()
if r, _ := cmd.Flags().GetString("repo"); r != "" {
repo = r
}
if c, _ := cmd.Flags().GetBool("create-source-branch"); c {
lb := &gitlab.CreateBranchOptions{
Branch: gitlab.String(sourceBranch),
Expand All @@ -117,19 +134,24 @@ var mrCreateCmd = &cobra.Command{

mr, _, err := gitlabClient.MergeRequests.CreateMergeRequest(repo, l)
if err != nil {
log.Fatal(err)
return err
}
displayMergeRequest(mr)
return nil
},
}

func init() {
mrCreateCmd.Flags().BoolP("fill", "f", false, "Do not prompt for title/description and just use commit info")
mrCreateCmd.Flags().BoolP("draft", "", false, "Mark merge request as a draft")
mrCreateCmd.Flags().BoolP("wip", "", false, "Mark merge request as a work in progress. Alternative to --draft")
mrCreateCmd.Flags().BoolP("push", "", false, "Push commit changes after creating merge request. Make sure you have committed changes")
mrCreateCmd.Flags().StringP("title", "t", "", "Supply a title for merge request")
mrCreateCmd.Flags().StringP("description", "d", "", "Supply a description for merge request")
mrCreateCmd.Flags().StringP("label", "l", "", "Add label by name. Multiple labels should be comma separated")
mrCreateCmd.Flags().StringP("assignee", "a", "", "Assign merge request to people by their IDs. Multiple values should be comma separated ")
mrCreateCmd.Flags().StringP("source-branch", "s", "", "Source Branch for merge request")
mrCreateCmd.Flags().StringP("target-branch", "g", "", "Target Branch for merge request")
mrCreateCmd.Flags().StringP("source-branch", "s", "", "The Branch you are creating the merge request. Default is the current branch.")
mrCreateCmd.Flags().StringP("target-branch", "b", "", "The target or base branch into which you want your code merged")
mrCreateCmd.Flags().IntP("target-project", "", -1, "Add target project by id")
mrCreateCmd.Flags().BoolP("create-source-branch", "", false, "Create source branch if it does not exist")
mrCreateCmd.Flags().IntP("milestone", "m", -1, "add milestone by <id> for merge request")
Expand Down
2 changes: 1 addition & 1 deletion commands/mr_view.go
Expand Up @@ -86,7 +86,7 @@ var mrViewCmd = &cobra.Command{
}
assignees = strings.Trim(assignees, ", ")
table := uitable.New()
table.MaxColWidth = 50
table.MaxColWidth = 70
table.Wrap = true
table.AddRow("Project ID:", mr.ProjectID)
table.AddRow("Labels:", prettifyNilEmptyValues(labels, "None"))
Expand Down
18 changes: 18 additions & 0 deletions internal/git/git.go
Expand Up @@ -203,6 +203,24 @@ type Commit struct {
Title string
}

func LatestCommit(ref string) (*Commit, error) {
logCmd := GitCommand("show", "-s", "--format='%h %s'", ref)
output, err := run.PrepareCmd(logCmd).Output()
if err != nil {
return &Commit{}, err
}
commit := &Commit{}
split := strings.SplitN(string(output), " ", 2)
if len(split) != 2 {
return commit, fmt.Errorf("could not find commit for %s", ref)
}
commit = &Commit{
Sha: split[0],
Title: split[1],
}
return commit, nil
}

func Commits(baseRef, headRef string) ([]*Commit, error) {
logCmd := GitCommand(
"-c", "log.ShowSignature=false",
Expand Down
3 changes: 2 additions & 1 deletion internal/git/git_test.go
Expand Up @@ -18,7 +18,8 @@ func TestCommits(t *testing.T) {
}{
{
name: "Commit",
args: args{"trunk","HEAD"},
args: args{"trunk","origin/trunk"},
wantErr: true,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit acf2e6e

Please sign in to comment.