forked from na-Itms/trac2gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelMap.go
145 lines (121 loc) · 4.28 KB
/
labelMap.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
// 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 (
"bufio"
"fmt"
"os"
"strings"
"github.com/stevejefferson/trac2gitea/importer"
)
const (
componentTypeName = "component"
priorityTypeName = "priority"
resolutionTypeName = "resolution"
severityTypeName = "severity"
typeTypeName = "type"
versionTypeName = "version"
)
func readDefaultLabelMaps(dataImporter *importer.Importer) (componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap map[string]string, err error) {
componentMap, err = dataImporter.DefaultComponentLabelMap()
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
priorityMap, err = dataImporter.DefaultPriorityLabelMap()
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
resolutionMap, err = dataImporter.DefaultResolutionLabelMap()
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
severityMap, err = dataImporter.DefaultSeverityLabelMap()
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
typeMap, err = dataImporter.DefaultTypeLabelMap()
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
versionMap, err = dataImporter.DefaultVersionLabelMap()
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
return
}
// readLabelMaps reads the label maps from the provided file, if no file provided, import default maps using the provided importer
func readLabelMaps(mapFile string, dataImporter *importer.Importer) (componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap map[string]string, err error) {
if mapFile == "" {
return readDefaultLabelMaps(dataImporter)
}
fd, err := os.Open(mapFile)
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
defer fd.Close()
componentMap = make(map[string]string)
priorityMap = make(map[string]string)
resolutionMap = make(map[string]string)
severityMap = make(map[string]string)
typeMap = make(map[string]string)
versionMap = make(map[string]string)
scanner := bufio.NewScanner(fd)
for scanner.Scan() {
mapLine := scanner.Text()
equalsPos := strings.LastIndex(mapLine, "=")
if equalsPos == -1 {
return nil, nil, nil, nil, nil, nil, fmt.Errorf("badly formatted label map file %s: expecting '=', found %s", mapFile, mapLine)
}
tracLabelAndType := strings.Trim(mapLine[0:equalsPos], " ")
colonPos := strings.LastIndex(tracLabelAndType, ":")
if equalsPos == -1 {
return nil, nil, nil, nil, nil, nil, fmt.Errorf("badly formatted label map file %s: expecting ':', found %s", mapFile, mapLine)
}
labelType := strings.Trim(tracLabelAndType[0:colonPos], " ")
tracLabel := strings.Trim(tracLabelAndType[colonPos+1:], " ")
giteaLabel := strings.Trim(mapLine[equalsPos+1:], " ")
switch labelType {
case componentTypeName:
componentMap[tracLabel] = giteaLabel
case priorityTypeName:
priorityMap[tracLabel] = giteaLabel
case resolutionTypeName:
resolutionMap[tracLabel] = giteaLabel
case severityTypeName:
severityMap[tracLabel] = giteaLabel
case typeTypeName:
typeMap[tracLabel] = giteaLabel
case versionTypeName:
versionMap[tracLabel] = giteaLabel
default:
return nil, nil, nil, nil, nil, nil, fmt.Errorf("badly formatted label map file %s: expecting Trac label type before ':', found %s", mapFile, mapLine)
}
}
if err := scanner.Err(); err != nil {
return nil, nil, nil, nil, nil, nil, err
}
return
}
func writeLabelMapToFile(fd *os.File, labelType string, labelMap map[string]string) error {
for tracLabel, giteaLabel := range labelMap {
if _, err := fd.WriteString(labelType + ":" + tracLabel + " = " + giteaLabel + "\n"); err != nil {
return err
}
}
return nil
}
func writeLabelMapsToFile(mapFile string, componentMap, priorityMap, resolutionMap, severityMap, typeMap, versionMap map[string]string) error {
fd, err := os.Create(mapFile)
if err != nil {
return err
}
defer fd.Close()
writeLabelMapToFile(fd, componentTypeName, componentMap)
writeLabelMapToFile(fd, priorityTypeName, priorityMap)
writeLabelMapToFile(fd, resolutionTypeName, resolutionMap)
writeLabelMapToFile(fd, severityTypeName, severityMap)
writeLabelMapToFile(fd, typeTypeName, typeMap)
writeLabelMapToFile(fd, versionTypeName, versionMap)
return nil
}