-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
190 lines (160 loc) · 4.2 KB
/
migrate.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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/andygrunwald/go-jira"
"github.com/bregydoc/gtranslate"
"github.com/gocarina/gocsv"
"github.com/tkanos/gonfig"
"github.com/trivago/tgo/tcontainer"
)
type Configuration struct {
JiraUrl string
JiraUserName string
JiraUserToken string
}
type CsvIssue struct {
Key string `csv:"Issue_key"`
}
func main() {
// open Jira Connections
jiraClientSrc := openJira("secrets_src.json")
jiraClientDst := openJira("secrets_dst.json")
// // Initial Test
// issue, _, _ := jiraClientSrc.Issue.Get("MSS-260", nil)
// printIssue(issue)
// issue, _, _ = jiraClientDst.Issue.Get("DMOB-1202", nil)
// printIssue(issue)
// Read csv
issues := readCSV("Jira.csv")
for _, i := range issues {
fmt.Printf("------------------------------\n")
fmt.Println("** Processing Issue: ", i.Key)
// get Issue from SRC
issue, _, _ := jiraClientSrc.Issue.Get(i.Key, nil)
// translate issue fields
issue.Fields.Summary = translateSummary(issue.Fields.Summary)
if issue.Fields.Description != "" {
issue.Fields.Description = translateDescription(issue.Fields.Description)
}
import_message := "\n*Imported* from Jira Certi: https://xxxxxxxx.atlassian.net/browse/" + issue.Key
issue.Fields.Description = issue.Fields.Description + import_message
printIssue(issue)
// create Issue on DST
createFromIssue(jiraClientDst, issue)
}
}
func openJira(conf_file string) *jira.Client {
configuration := Configuration{}
err := gonfig.GetConf(conf_file, &configuration)
if err != nil {
panic(err)
}
fmt.Printf("Start Jira with username: %s\n", configuration.JiraUserName)
base := configuration.JiraUrl
tp := jira.BasicAuthTransport{
Username: configuration.JiraUserName,
Password: configuration.JiraUserToken,
}
jiraClient, err := jira.NewClient(tp.Client(), base)
if err != nil {
panic(err)
}
return jiraClient
}
func readCSV(csv_file string) []*CsvIssue {
in, err := os.Open(csv_file)
if err != nil {
panic(err)
}
defer in.Close()
issues := []*CsvIssue{}
if err := gocsv.UnmarshalFile(in, &issues); err != nil {
panic(err)
}
return issues
}
func printIssue(issue *jira.Issue) {
fmt.Printf("Self :%s\n", issue.Self)
fmt.Printf("Key :%s\n", issue.Key)
fmt.Printf("Summary: %+v\n", issue.Fields.Summary)
fmt.Printf("Type: %s\n", issue.Fields.Type.Name)
fmt.Printf("Priority: %s\n", issue.Fields.Priority.Name)
fmt.Printf("Description: %s\n", issue.Fields.Description)
fmt.Printf("Labels: %s\n", issue.Fields.Labels)
}
func translateSummary(text string) string {
translated, err := gtranslate.TranslateWithParams(
text,
gtranslate.TranslationParams{
From: "pt",
To: "en",
},
)
if err != nil {
panic(err)
}
return translated
}
func translateDescription(text string) string {
translated, err := gtranslate.TranslateWithParams(
text,
gtranslate.TranslationParams{
From: "pt",
To: "en",
},
)
if err != nil {
panic(err)
}
header := "\n----\n*[pt-br original]*\n"
return translated + header + text + "\n"
}
func createFromIssue(client *jira.Client, issueFrom *jira.Issue) {
// Story points: import from custom field
unknowns := tcontainer.NewMarshalMap()
unknowns["customfield_10002"] = issueFrom.Fields.Unknowns["customfield_10002"]
// Labels: original labels plus component name
labels := issueFrom.Fields.Labels
labels = append(labels, issueFrom.Key) // add orign key
for _, c := range issueFrom.Fields.Components {
labels = append(labels, c.Name)
}
// IssueType mapping
issueType := issueFrom.Fields.Type.Name
if issueType == "Improvement" {
issueType = "New Feature"
}
if issueType == "Sub-task" {
issueType = "Task"
}
i := jira.Issue{
Fields: &jira.IssueFields{
Assignee: &jira.User{
Name: "lribas",
},
Description: issueFrom.Fields.Description,
Type: jira.IssueType{
Name: issueType,
},
Project: jira.Project{
Key: "DMOB",
},
Summary: issueFrom.Fields.Summary,
Unknowns: unknowns,
Labels: labels,
},
}
issue, resp, err := client.Issue.Create(&i)
if err != nil {
body, _ := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf(string(body))
}
panic(err)
}
fmt.Printf("CREATED:")
issue, _, _ = client.Issue.Get(issue.Key, nil)
printIssue(issue)
}