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