gitlab.com/prarit/lab@v0.14.0/cmd/issue_create.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"log"
     7  	"runtime"
     8  	"strconv"
     9  	"strings"
    10  	"text/template"
    11  
    12  	"github.com/spf13/cobra"
    13  	gitlab "github.com/xanzy/go-gitlab"
    14  	"github.com/zaquestion/lab/internal/git"
    15  	lab "github.com/zaquestion/lab/internal/gitlab"
    16  )
    17  
    18  var issueCreateCmd = &cobra.Command{
    19  	Use:     "create [remote]",
    20  	Aliases: []string{"new"},
    21  	Short:   "Open an issue on GitLab",
    22  	Long:    ``,
    23  	Args:    cobra.MaximumNArgs(1),
    24  	Run: func(cmd *cobra.Command, args []string) {
    25  		msgs, err := cmd.Flags().GetStringSlice("message")
    26  		if err != nil {
    27  			log.Fatal(err)
    28  		}
    29  		assignees, err := cmd.Flags().GetStringSlice("assignees")
    30  		if err != nil {
    31  			log.Fatal(err)
    32  		}
    33  		labels, err := cmd.Flags().GetStringSlice("label")
    34  		if err != nil {
    35  			log.Fatal(err)
    36  		}
    37  		remote := forkedFromRemote
    38  		if len(args) > 0 {
    39  			ok, err := git.IsRemote(args[0])
    40  			if err != nil {
    41  				log.Fatal(err)
    42  			}
    43  			if ok {
    44  				remote = args[0]
    45  			}
    46  		}
    47  		rn, err := git.PathWithNameSpace(remote)
    48  		if err != nil {
    49  			log.Fatal(err)
    50  		}
    51  
    52  		title, body, err := issueMsg(msgs)
    53  		if err != nil {
    54  			_, f, l, _ := runtime.Caller(0)
    55  			log.Fatal(f+":"+strconv.Itoa(l)+" ", err)
    56  		}
    57  		if title == "" {
    58  			log.Fatal("aborting issue due to empty issue msg")
    59  		}
    60  
    61  		assigneeIDs := make([]int, len(assignees))
    62  		for i, a := range assignees {
    63  			assigneeIDs[i] = *getAssigneeID(a)
    64  		}
    65  
    66  		issueURL, err := lab.IssueCreate(rn, &gitlab.CreateIssueOptions{
    67  			Title:       &title,
    68  			Description: &body,
    69  			Labels:      gitlab.Labels(labels),
    70  			AssigneeIDs: assigneeIDs,
    71  		})
    72  		if err != nil {
    73  			log.Fatal(err)
    74  		}
    75  		fmt.Println(issueURL)
    76  	},
    77  }
    78  
    79  func issueMsg(msgs []string) (string, string, error) {
    80  	if len(msgs) > 0 {
    81  		return msgs[0], strings.Join(msgs[1:], "\n\n"), nil
    82  	}
    83  
    84  	text, err := issueText()
    85  	if err != nil {
    86  		return "", "", err
    87  	}
    88  	return git.Edit("ISSUE", text)
    89  }
    90  
    91  func issueText() (string, error) {
    92  	const tmpl = `{{.InitMsg}}
    93  {{.CommentChar}} Write a message for this issue. The first block
    94  {{.CommentChar}} of text is the title and the rest is the description.`
    95  
    96  	issueTmpl := lab.LoadGitLabTmpl(lab.TmplIssue)
    97  
    98  	initMsg := "\n"
    99  	if issueTmpl != "" {
   100  		initMsg = "\n\n" + issueTmpl
   101  	}
   102  
   103  	commentChar := git.CommentChar()
   104  
   105  	t, err := template.New("tmpl").Parse(tmpl)
   106  	if err != nil {
   107  		return "", err
   108  	}
   109  
   110  	msg := &struct {
   111  		InitMsg     string
   112  		CommentChar string
   113  	}{
   114  		InitMsg:     initMsg,
   115  		CommentChar: commentChar,
   116  	}
   117  
   118  	var b bytes.Buffer
   119  	err = t.Execute(&b, msg)
   120  	if err != nil {
   121  		return "", err
   122  	}
   123  
   124  	return b.String(), nil
   125  }
   126  
   127  func init() {
   128  	issueCreateCmd.Flags().StringSliceP("message", "m", []string{}, "Use the given <msg>; multiple -m are concatenated as separate paragraphs")
   129  	issueCreateCmd.Flags().StringSliceP("label", "l", []string{}, "Set the given label(s) on the created issue")
   130  	issueCreateCmd.Flags().StringSliceP("assignees", "a", []string{}, "Set assignees by username")
   131  	issueCmd.AddCommand(issueCreateCmd)
   132  }