/
be.go
219 lines (196 loc) · 5.56 KB
/
be.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
package main
import (
"encoding/json"
"fmt"
"github.com/driusan/bug/bugs"
"io/ioutil"
"os"
"sort"
"strings"
)
func beImportComments(b bugs.Bug, directory string, includeHeaders bool) string {
file := directory + "/body"
data, _ := ioutil.ReadFile(file)
if includeHeaders == false {
return string(data)
}
/* Be appears to store comment metadata in a JSON file named values
with the format:
{
"Author": "Dave MacFarlane <driusan@gmail.com>",
"Content-type": "text/plain",
"Date": "Tue, 12 Jan 2016 21:44:24 +0000"
}
*/
type BeValues struct {
Author string `json:Author`
ContentType string `json:Content-type`
Date string `json:Date`
}
file = directory + "/values"
jsonVal, _ := ioutil.ReadFile(file)
var beComment BeValues
json.Unmarshal([]byte(jsonVal), &beComment)
return "---------- Comment ---------\nFrom:" + beComment.Author + "\nDate:" + beComment.Date + "\n\n" + string(data)
}
func beImportBug(identifier, issuesDir, fullbepath string) {
/* BE appears to store the top level data of a bug
ins a json file named values with the format:
{
"creator": "Dave MacFarlane <driusan@gmail.com>",
"reporter": "Dave MacFarlane <driusan@gmail.com>",
"severity": "minor",
"status": "open",
"summary": "abc",
"time": "Tue, 12 Jan 2016 00:05:28 +0000"
}
and the description of bugs entirely in comments.
All we really care about is the summary so that we
can get the directory name for the issues/ directory,
but the severity+status can also be used as a status
to ensure that we have at least 1 file to be tracked
by git.
*/
type BeValues struct {
Creator string `json:creator`
Reporter string `json:reporter`
Severity string `json:severity`
Status string `json:status`
Summary string `json:summary`
Time string `json:time`
}
file := fullbepath + "/values"
fmt.Printf("File: %s\n", file)
data, _ := ioutil.ReadFile(file)
var beBug BeValues
err := json.Unmarshal([]byte(data), &beBug)
if err != nil {
fmt.Printf("Error unmarshalling data: %s\n", err.Error())
}
fmt.Printf("%s\n", beBug)
bugdir := bugs.TitleToDir(beBug.Summary)
b := bugs.Bug{bugs.Directory(issuesDir) + bugdir}
if dir := b.GetDirectory(); dir != "" {
os.Mkdir(string(dir), 0755)
}
if beBug.Status != "" && beBug.Severity != "" {
b.SetStatus(beBug.Status + ":" + beBug.Severity)
}
comments := fullbepath + "/comments/"
dir, err := os.Open(comments)
files, err := dir.Readdir(-1)
var Description string
if len(files) > 0 && err == nil {
for _, file := range files {
if file.IsDir() {
Description = Description + "\n" +
beImportComments(b, comments+file.Name(), len(files) > 1)
}
}
}
b.SetDescription(Description)
b.SetIdentifier(identifier)
}
func beImportBugs(prefix, issuesdir, bedir, dirname string) {
bugsdir := bedir + "/" + dirname + "/bugs"
dir, err := os.Open(bugsdir)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open bug directory %s\n", bugsdir)
}
files, _ := dir.Readdir(-1)
lastIdentifier := ""
nextIdentifier := ""
sort.Sort(fileSorter(files))
for idx, file := range files {
if file.IsDir() {
if idx < len(files)-1 {
nextIdentifier = files[idx+1].Name()
}
name := shortestPrefix(file.Name(), nextIdentifier, lastIdentifier, 3)
identifier := fmt.Sprintf("%s/%s", prefix, name)
beImportBug(identifier, issuesdir, bugsdir+"/"+file.Name())
lastIdentifier = file.Name()
}
}
}
type fileSorter []os.FileInfo
func (a fileSorter) Len() int {
return len(a)
}
func (a fileSorter) Less(i, j int) bool {
return a[i].Name() < a[j].Name()
}
func (a fileSorter) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func beImport() {
wd, err := os.Getwd()
if dir := walkAndSearch(wd, []string{".be"}); err != nil || dir == nil {
fmt.Fprintf(os.Stderr, "Could not find any Bugs Everywhere repository relative to current path.\n")
os.Exit(3)
} else {
files, err := dir.Readdir(-1)
sort.Sort(fileSorter(files))
if err != nil {
fmt.Fprintf(os.Stderr, "Error importing BE database: %s\n", err.Error())
os.Exit(4)
}
issuesDir := bugs.GetIssuesDir()
lastIdentifier := ""
nextIdentifier := ""
for idx, file := range files {
if file.IsDir() {
if idx < len(files)-1 {
nextIdentifier = files[idx+1].Name()
}
name := shortestPrefix(file.Name(), nextIdentifier, lastIdentifier, 3)
beImportBugs(name, string(issuesDir), dir.Name(), file.Name())
lastIdentifier = file.Name()
}
}
}
}
func walkAndSearch(startpath string, dirnames []string) *os.File {
for _, dirname := range dirnames {
if dirinfo, err := os.Stat(startpath + "/" + dirname); err == nil && dirinfo.IsDir() {
file, err := os.Open(startpath + "/" + dirname)
if err != nil {
return nil
}
return file
}
}
pieces := strings.Split(startpath, "/")
for i := len(pieces); i > 0; i -= 1 {
dir := strings.Join(pieces[0:i], "/")
for _, dirname := range dirnames {
if dirinfo, err := os.Stat(dir + "/" + dirname); err == nil && dirinfo.IsDir() {
file, err := os.Open(dir + "/" + dirname)
if err != nil {
return nil
}
return file
}
}
}
return nil
}
func shortestPrefix(name, nextIdentifier, lastIdentifier string, min int) string {
for i := 0; i < len(name); i += 1 {
if i < min {
continue
}
if nextIdentifier != "" && len(nextIdentifier) <= i {
if name[0:i] == nextIdentifier[0:i] {
continue
}
}
if lastIdentifier != "" && len(lastIdentifier) <= i {
if name[0:i] == lastIdentifier[0:i] {
continue
}
}
return name[0:i]
}
return name
}