-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
executable file
·287 lines (256 loc) · 6.6 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/urfave/cli"
)
type userMap struct {
JiraUsername string
CHProjectID int
CHID string
}
func main() {
app := cli.NewApp()
app.Name = "Jira to Clubhouse"
app.Usage = "Jira To Clubhouse"
app.Version = "0.0.5"
app.Commands = []cli.Command{
{
Name: "export",
Aliases: []string{"e"},
Usage: "Export Jira XMl into a clubhouse-esque json file",
Flags: []cli.Flag{
cli.StringFlag{
Name: "in, i",
Usage: "The Jira XML file you want to read in.",
},
cli.StringFlag{
Name: "map, m",
Usage: "The JSON file containing user mappings",
},
cli.StringFlag{
Name: "out, o",
Usage: "The destination file",
},
},
Action: func(c *cli.Context) error {
jiraFile := c.String("in")
exportFile := c.String("out")
mapFile := c.String("map")
if jiraFile == "" {
fmt.Println("An input file must be specified.")
return nil
}
if exportFile == "" {
fmt.Println("An output file must be specified.")
return nil
}
if mapFile == "" {
fmt.Println("A user map JSON file must be specified.")
return nil
}
userMaps, err := GetUserMap(mapFile)
if err != nil {
fmt.Println(err)
return err
}
err = ExportToJSON(jiraFile, userMaps, exportFile)
if err != nil {
fmt.Println(err)
return err
}
return nil
},
}, {
Name: "import",
Aliases: []string{"i"},
Usage: "Import Jira XMl into Clubhouse",
Flags: []cli.Flag{
cli.StringFlag{
Name: "in, i",
Usage: "The Jira XML file you want to read in.",
},
cli.StringFlag{
Name: "map, m",
Usage: "The JSON file containing user mappings",
},
cli.StringFlag{
Name: "token, t",
Usage: "Your API token",
},
cli.BoolFlag{
Name: "test, T",
Hidden: false,
Usage: "Test mode: Does not execute remote requests",
},
},
Action: func(c *cli.Context) error {
jiraFile := c.String("in")
token := c.String("token")
mapFile := c.String("map")
testMode := c.Bool("test")
if jiraFile == "" {
fmt.Println("An input XML file must be specified.")
return nil
}
if token == "" && !testMode {
fmt.Println("A token must be specified.")
return nil
}
if mapFile == "" {
fmt.Println("A user map JSON file must be specified.")
return nil
}
userMaps, err := GetUserMap(mapFile)
if err != nil {
fmt.Println(err)
return err
}
err = UploadToClubhouse(jiraFile, userMaps, token, testMode)
if err != nil {
fmt.Println(err)
return err
}
return nil
},
},
}
app.Run(os.Args)
}
func GetUserMap(mapFile string) ([]userMap, error){
jsonFile, err := os.Open(mapFile)
if err != nil {
return []userMap{}, err
}
defer jsonFile.Close()
JSONData, err := ioutil.ReadAll(jsonFile)
if err != nil {
return []userMap{}, err
}
// userMaps := []userMap
var userMaps []userMap
err = json.Unmarshal(JSONData, &userMaps)
if err != nil {
return []userMap{}, err
}
return userMaps, nil
}
// ExportToJSON will import the XML and then export the data to the file specified.
func ExportToJSON(jiraFile string, userMaps []userMap, exportFile string) error {
export, err := GetDataFromXMLFile(jiraFile)
if err != nil {
return err
}
data, err := json.Marshal(export.GetDataForClubhouse(userMaps))
if err != nil {
return err
}
err = ioutil.WriteFile(exportFile, data, 0644)
if err != nil {
return err
}
return nil
}
// UploadToClubhouse will import the XML, and upload it to Clubhouse
func UploadToClubhouse(jiraFile string, userMaps []userMap, token string, testMode bool) error {
export, err := GetDataFromXMLFile(jiraFile)
if err != nil {
return err
}
data := export.GetDataForClubhouse(userMaps)
fmt.Printf("Found %d epics and %d stories.\n\n", len(data.Epics), len(data.Stories))
if !testMode{
fmt.Println("Sending data to Clubhouse...")
err = SendData(token, data)
if err != nil {
return err
}
}
return nil
}
// SendData will send the data to Clubhouse
func SendData(token string, data ClubHouseData) error {
// epicMap is used to get the return from the submitting of the ClubHouseCreateEpic to get the ID created by the API so stories can be mapped to the correct epic.
epicMap := make(map[string]int64)
client := &http.Client{}
for _, epic := range data.Epics {
jsonStr, _ := json.Marshal(epic)
req, err := http.NewRequest("POST", GetURL("epics", token), bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
}
body, _ := ioutil.ReadAll(resp.Body)
newEpic := ClubHouseEpic{}
json.Unmarshal(body, &newEpic)
epicMap[epic.key] = newEpic.ID
}
for _, story := range data.Stories {
if story.epicLink != "" {
story.EpicID = epicMap[story.epicLink]
}
jsonStr, err := json.Marshal(story)
if err != nil {
return err
}
req, err := http.NewRequest("POST", GetURL("stories", token), bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode > 299 {
fmt.Println("--------- *** Request Failed")
fmt.Println("response Status:", resp.Status)
// fmt.Println("response Headers:", resp.Header)
fmt.Println("Request: ", story)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
fmt.Println("---------")
}
// CH has a 200 requests / min rate limit
time.Sleep(350 * time.Millisecond)
}
return nil
}
// GetURL will get the use the REST API v1 address, the resource provided and the API token to get the URL for transactions
func GetURL(kind string, token string) string {
return fmt.Sprintf("%s%s?token=%s", "https://api.clubhouse.io/api/v2/", kind, token)
}
// GetDataFromXMLFile will Unmarshal the XML file into the objects used by the application.
func GetDataFromXMLFile(jiraFile string) (JiraExport, error) {
xmlFile, err := os.Open(jiraFile)
if err != nil {
return JiraExport{}, err
}
defer xmlFile.Close()
XMLData, err := ioutil.ReadAll(xmlFile)
if err != nil {
return JiraExport{}, err
}
jiraExport := JiraExport{}
err = xml.Unmarshal(XMLData, &jiraExport)
if err != nil {
return JiraExport{}, err
}
return jiraExport, nil
}