-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
241 lines (190 loc) · 5.39 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"flag"
"fmt"
"os"
"strings"
"gopkg.in/ini.v1"
)
const (
iniConfig = "/jisub/jisub-config.ini"
)
var (
version = "dev"
configFlag string
subTasksFlag string
fieldsFlag string
versionFlag bool
helpFlag bool
)
func init() {
flag.Usage = func() {
h := "Usage:\n"
h += " jisub [OPTIONS] JIRA-39106\n\n"
h += "Options:\n"
h += " -h, --help Print usage\n"
h += " -v, --version Print version info\n"
h += " -c, --config Create/Update jira configuration\n"
h += " -st, --sub-tasks Sub tasks to create for provided parent issue\n"
h += " -f, --fields Field name, value to update for provided issue\n\n"
h += "Examples:\n"
h += " jisub --config \"jira.url https://jira-api.com/jira/rest/api/2\" \n"
h += " jisub --config \"user.token <token>\"\n"
h += " jisub --sub-tasks \"QA:2 BE:3 FE:4\" --fields \"storypoints:4 dealsize:2,3,4\" JIRA-39106\n"
fmt.Fprint(os.Stderr, h)
}
// jisub jira configuration
flag.StringVar(&configFlag, "config", "", "Create/Update jira configuration")
flag.StringVar(&configFlag, "c", "", "Create/Update jira configuration")
// list of issue subtasks
flag.StringVar(&subTasksFlag, "sub-tasks", "", "Sub tasks to create for provided parent issue")
flag.StringVar(&subTasksFlag, "st", "", "Sub tasks to create for provided parent issue")
// fields to update
flag.StringVar(&fieldsFlag, "fields", "", "Field name, value to update for provided issue")
flag.StringVar(&fieldsFlag, "f", "", "Field name, value to update for provided issue")
// version
flag.BoolVar(&versionFlag, "version", false, "Version information")
flag.BoolVar(&versionFlag, "v", false, "Version information")
// help
flag.BoolVar(&helpFlag, "help", false, "Usage example")
flag.BoolVar(&helpFlag, "h", false, "Usage example")
}
// > jisub --config "user.token RandomTokenValueStr"
// > jisub --config "jira.url "https://jira-api.com/jira/rest/api/2"
// > jisub --sub-tasks "QA:2 BE:3 FE:4" --fields "storypoints:4 dealsize:2,3,4" JIRA-39106
func main() {
flag.Parse()
if versionFlag {
fmt.Println("jisub version " + version)
return
}
if helpFlag {
flag.Usage()
return
}
if len(configFlag) > 0 {
err := updateConfig(configFlag)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
return
}
err := updateIssue(subTasksFlag, fieldsFlag, flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
}
func updateIssue(subtaskArg, fieldsArg, issueKey string) error {
if issueKey == "" {
return fmt.Errorf("missing required issue key")
}
jira, err := buildNewJiraFromConfig()
if err != nil {
return fmt.Errorf("error creating jira client %w", err)
}
issue, err := jira.Issue(issueKey)
if err != nil {
return fmt.Errorf("issue not found %v %w", issueKey, err)
}
if subtaskArg != "" {
err = createSubTasks(*jira, *issue, subtaskArg)
if err != nil {
return fmt.Errorf("error creating sub tasks %v, %v %w", subtaskArg, issueKey, err)
}
}
if fieldsArg != "" {
err = updateIssueFields(*jira, *issue, fieldsArg)
if err != nil {
return fmt.Errorf("error updating issue fields %v, %v %w", fieldsArg, issueKey, err)
}
}
return nil
}
func createSubTasks(j Jira, parent Issue, subtasksArg string) error {
subTasksMap := make(map[string]string)
err := stringToMap(subtasksArg, subTasksMap)
if err != nil {
return err
}
result, err := j.SubTasks(parent, subTasksMap)
if err != nil {
return err
}
fmt.Println("sub tasks:")
for _, issue := range result.Issues {
fmt.Println(issue.Key)
}
return nil
}
func updateIssueFields(j Jira, issue Issue, fieldsArg string) error {
fieldsUpdatesMap := make(map[string]string)
err := stringToMap(fieldsArg, fieldsUpdatesMap)
if err != nil {
return err
}
err = j.IssueUpdate(issue, fieldsUpdatesMap)
if err != nil {
return err
}
fmt.Printf("issue updated %v\n", issue.Key)
return nil
}
func updateConfig(arg string) error {
items := strings.Split(arg, " ")
// expect key value pair
if len(items) < 2 {
return fmt.Errorf("wrong number of arguments provided")
}
// load config, in case not loaded create empty
pwd, _ := os.Getwd()
cfg, err := ini.Load(pwd + iniConfig)
if err != nil {
cfg = ini.Empty()
}
// parse config section name, key
sectionKey := strings.Split(items[0], ".")
if len(sectionKey) < 2 {
return fmt.Errorf("incorrect value format, expect: section.key")
}
section := cfg.Section(sectionKey[0])
section.Key(sectionKey[1]).SetValue(items[1])
err = cfg.SaveTo(pwd + iniConfig)
if err != nil {
return err
}
return nil
}
func buildNewJiraFromConfig() (*Jira, error) {
pwd, _ := os.Getwd()
cfg, err := ini.Load(pwd + iniConfig)
if err != nil {
return nil, err
}
baseUrl := cfg.Section("jira").Key("url").Value()
if len(baseUrl) == 0 {
return nil, fmt.Errorf("missing jira.url value")
}
userToken := cfg.Section("user").Key("token").Value()
if len(userToken) == 0 {
return nil, fmt.Errorf("missing user.token value")
}
return NewJira(baseUrl, BearerAuth(userToken)), nil
}
func stringToMap(str string, resultMap map[string]string) error {
items := strings.Split(str, " ")
// no map items provided
if len(items) == 0 {
return nil
}
for _, v := range items {
// key:value
mapEntry := strings.Split(strings.ReplaceAll(v, " ", ""), ":")
if len(mapEntry) != 2 {
return fmt.Errorf("incorrect value format, expect: key:value")
}
resultMap[mapEntry[0]] = mapEntry[1]
}
return nil
}