-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
239 lines (209 loc) · 6.73 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
// Copyright 2020 Steve Jefferson. All rights reserved.
// Use of this source code is governed by a GPL-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"github.com/stevejefferson/trac2gitea/importer"
"github.com/stevejefferson/trac2gitea/markdown"
"github.com/spf13/pflag"
"github.com/stevejefferson/trac2gitea/accessor/gitea"
"github.com/stevejefferson/trac2gitea/accessor/trac"
"github.com/stevejefferson/trac2gitea/log"
)
var dbOnly bool
var wikiOnly bool
var wikiPush bool
var overwrite bool
var verbose bool
var wikiConvertPredefineds bool
var generateMaps bool
var tracRootDir string
var giteaRootDir string
var giteaUser string
var giteaRepo string
var userMapInputFile string
var userMapOutputFile string
var labelMapInputFile string
var labelMapOutputFile string
var giteaWikiRepoURL string
var giteaWikiRepoToken string
var giteaWikiRepoDir string
// parseArgs parses the command line arguments, populating the variables above.
func parseArgs() {
wikiURLParam := pflag.String("wiki-url", "",
"URL of wiki repository - defaults to <server-root-url>/<gitea-user>/<gitea-repo>.wiki.git")
wikiTokenParam := pflag.String("wiki-token", "",
"password/token for accessing wiki repository (ignored if wiki-url provided)")
wikiDirParam := pflag.String("wiki-dir", "",
"directory into which to checkout (clone) wiki repository - defaults to cwd")
wikiConvertPredefinedsParam := pflag.Bool("wiki-convert-predefined", false,
"convert Trac predefined wiki pages - by default we skip these")
generateMapsParam := pflag.Bool("generate-maps", false,
"generate default user/label mappings into provided map files (note: no conversion will be performed in this case)")
dbOnlyParam := pflag.Bool("db-only", false,
"convert database only")
wikiOnlyParam := pflag.Bool("wiki-only", false,
"convert wiki only")
wikiNoPushParam := pflag.Bool("no-wiki-push", false,
"do not push wiki on completion")
overwriteParam := pflag.Bool("overwrite", false,
"overwrite existing data (by default previously-imported issues, labels, wiki pages etc are skipped)")
verboseParam := pflag.Bool("verbose", false,
"verbose output")
pflag.Usage = func() {
fmt.Fprintf(os.Stderr,
"Usage: %s [options] <trac-root> <gitea-root> <gitea-user> <gitea-repo> [<user-map>] [<label-map>]\n",
os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
pflag.PrintDefaults()
}
pflag.Parse()
verbose = *verboseParam
overwrite = *overwriteParam
dbOnly = *dbOnlyParam
wikiOnly = *wikiOnlyParam
wikiPush = !*wikiNoPushParam
generateMaps = *generateMapsParam
if dbOnly && wikiOnly {
log.Fatal("cannot generate only database AND only wiki!")
}
wikiConvertPredefineds = *wikiConvertPredefinedsParam
giteaWikiRepoURL = *wikiURLParam
giteaWikiRepoToken = *wikiTokenParam
giteaWikiRepoDir = *wikiDirParam
if (pflag.NArg() < 4) || (pflag.NArg() > 6) {
pflag.Usage()
os.Exit(1)
}
tracRootDir = pflag.Arg(0)
giteaRootDir = pflag.Arg(1)
giteaUser = pflag.Arg(2)
giteaRepo = pflag.Arg(3)
if pflag.NArg() > 4 {
userMapFile := pflag.Arg(4)
if generateMaps {
userMapOutputFile = userMapFile
} else {
userMapInputFile = userMapFile
}
}
if pflag.NArg() > 5 {
labelMapFile := pflag.Arg(5)
if generateMaps {
labelMapOutputFile = labelMapFile
} else {
labelMapInputFile = labelMapFile
}
}
}
// importData imports the non-wiki Trac data.
func importData(dataImporter *importer.Importer, userMap, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap map[string]string) error {
var err error
if err = dataImporter.ImportComponents(componentMap); err != nil {
return err
}
if err = dataImporter.ImportPriorities(priorityMap); err != nil {
return err
}
if err = dataImporter.ImportResolutions(resolutionMap); err != nil {
return err
}
if err = dataImporter.ImportSeverities(severityMap); err != nil {
return err
}
if err = dataImporter.ImportTypes(typeMap); err != nil {
return err
}
if err = dataImporter.ImportVersions(versionMap); err != nil {
return err
}
if err = dataImporter.ImportMilestones(); err != nil {
return err
}
if err = dataImporter.ImportTickets(userMap, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap); err != nil {
return err
}
return nil
}
// performImport performs the actual import
func performImport(dataImporter *importer.Importer, userMap, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap map[string]string) error {
if !wikiOnly {
if err := importData(dataImporter, userMap, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap); err != nil {
dataImporter.RollbackImport()
return err
}
}
if !dbOnly {
if err := dataImporter.ImportWiki(userMap); err != nil {
dataImporter.RollbackImport()
return err
}
}
return dataImporter.CommitImport()
}
// createImporter creates and configures the importer
func createImporter() (*importer.Importer, error) {
tracAccessor, err := trac.CreateDefaultAccessor(tracRootDir)
if err != nil {
return nil, err
}
giteaAccessor, err := gitea.CreateDefaultAccessor(
giteaRootDir, giteaUser, giteaRepo, giteaWikiRepoURL, giteaWikiRepoToken, giteaWikiRepoDir, overwrite, wikiPush)
if err != nil {
return nil, err
}
markdownConverter := markdown.CreateDefaultConverter(tracAccessor, giteaAccessor)
dataImporter, err := importer.CreateImporter(tracAccessor, giteaAccessor, markdownConverter, giteaUser, wikiConvertPredefineds)
if err != nil {
return nil, err
}
return dataImporter, nil
}
func main() {
parseArgs()
var logLevel = log.INFO
if verbose {
logLevel = log.TRACE
}
log.SetLevel(logLevel)
dataImporter, err := createImporter()
if err != nil {
log.Fatal("%+v", err)
return
}
userMap, err := readUserMap(userMapInputFile, dataImporter)
if err != nil {
log.Fatal("%+v", err)
return
}
componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap, err := readLabelMaps(labelMapInputFile, dataImporter)
if err != nil {
log.Fatal("%+v", err)
return
}
if generateMaps {
// note: no need to commit or rollback transaction here - nothing has been imported yet
if userMapOutputFile != "" {
if err = writeUserMapToFile(userMapOutputFile, userMap); err != nil {
log.Fatal("%+v", err)
return
}
log.Info("wrote user map to %s", userMapOutputFile)
}
if labelMapOutputFile != "" {
if err = writeLabelMapsToFile(labelMapOutputFile, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap); err != nil {
log.Fatal("%+v", err)
return
}
log.Info("wrote label map to %s", labelMapOutputFile)
}
return
}
err = performImport(dataImporter, userMap, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap)
if err != nil {
log.Fatal("%+v", err)
return
}
}