1package random_bugs
2
3import (
4 "math/rand"
5 "strings"
6 "time"
7
8 "github.com/MichaelMure/git-bug/bug"
9 "github.com/MichaelMure/git-bug/bug/operations"
10 "github.com/MichaelMure/git-bug/repository"
11 "github.com/icrowley/fake"
12)
13
14type opsGenerator func(bug.Interface, bug.Person)
15
16type Options struct {
17 BugNumber int
18 PersonNumber int
19 MinOp int
20 MaxOp int
21}
22
23func DefaultOptions() Options {
24 return Options{
25 BugNumber: 15,
26 PersonNumber: 5,
27 MinOp: 3,
28 MaxOp: 20,
29 }
30}
31
32func CommitRandomBugs(repo repository.Repo, opts Options) {
33 CommitRandomBugsWithSeed(repo, opts, time.Now().UnixNano())
34}
35
36func CommitRandomBugsWithSeed(repo repository.Repo, opts Options, seed int64) {
37 bugs := GenerateRandomBugsWithSeed(opts, seed)
38
39 for _, b := range bugs {
40 err := b.Commit(repo)
41 if err != nil {
42 panic(err)
43 }
44 }
45}
46
47func GenerateRandomBugs(opts Options) []*bug.Bug {
48 return GenerateRandomBugsWithSeed(opts, time.Now().UnixNano())
49}
50
51func GenerateRandomBugsWithSeed(opts Options, seed int64) []*bug.Bug {
52 rand.Seed(seed)
53 fake.Seed(seed)
54
55 opsGenerators := []opsGenerator{
56 comment,
57 comment,
58 title,
59 labels,
60 operations.Open,
61 operations.Close,
62 }
63
64 result := make([]*bug.Bug, opts.BugNumber)
65
66 for i := 0; i < opts.BugNumber; i++ {
67 addedLabels = []string{}
68
69 b, err := operations.Create(randomPerson(opts.PersonNumber), fake.Sentence(), paragraphs())
70
71 if err != nil {
72 panic(err)
73 }
74
75 nOps := opts.MinOp + rand.Intn(opts.MaxOp-opts.MinOp)
76 for j := 0; j < nOps; j++ {
77 index := rand.Intn(len(opsGenerators))
78 opsGenerators[index](b, randomPerson(opts.PersonNumber))
79 }
80
81 result[i] = b
82 }
83
84 return result
85}
86
87func GenerateRandomOperationPacks(packNumber int, opNumber int) []*bug.OperationPack {
88 return GenerateRandomOperationPacksWithSeed(packNumber, opNumber, time.Now().UnixNano())
89}
90
91func GenerateRandomOperationPacksWithSeed(packNumber int, opNumber int, seed int64) []*bug.OperationPack {
92 // Note: this is a bit crude, only generate a Create + Comments
93
94 rand.Seed(seed)
95 fake.Seed(seed)
96
97 result := make([]*bug.OperationPack, packNumber)
98
99 for i := 0; i < packNumber; i++ {
100 opp := &bug.OperationPack{}
101
102 var op bug.Operation
103
104 op = operations.NewCreateOp(randomPerson(5), fake.Sentence(), paragraphs(), nil)
105
106 opp.Append(op)
107
108 for j := 0; j < opNumber-1; j++ {
109 op = operations.NewAddCommentOp(randomPerson(5), paragraphs(), nil)
110 opp.Append(op)
111 }
112
113 result[i] = opp
114 }
115
116 return result
117}
118
119func person() bug.Person {
120 return bug.Person{
121 Name: fake.FullName(),
122 Email: fake.EmailAddress(),
123 }
124}
125
126var persons []bug.Person
127
128func randomPerson(personNumber int) bug.Person {
129 if len(persons) == 0 {
130 persons = make([]bug.Person, personNumber)
131 for i := range persons {
132 persons[i] = person()
133 }
134 }
135
136 index := rand.Intn(personNumber)
137 return persons[index]
138}
139
140func paragraphs() string {
141 p := fake.Paragraphs()
142 return strings.Replace(p, "\t", "\n\n", -1)
143}
144
145func comment(b bug.Interface, p bug.Person) {
146 operations.Comment(b, p, paragraphs())
147}
148
149func title(b bug.Interface, p bug.Person) {
150 operations.SetTitle(b, p, fake.Sentence())
151}
152
153var addedLabels []string
154
155func labels(b bug.Interface, p bug.Person) {
156 var removed []string
157 nbRemoved := rand.Intn(3)
158 for nbRemoved > 0 && len(addedLabels) > 0 {
159 index := rand.Intn(len(addedLabels))
160 removed = append(removed, addedLabels[index])
161 addedLabels[index] = addedLabels[len(addedLabels)-1]
162 addedLabels = addedLabels[:len(addedLabels)-1]
163 }
164
165 var added []string
166 nbAdded := rand.Intn(3)
167 for i := 0; i < nbAdded; i++ {
168 label := fake.Word()
169 added = append(added, label)
170 addedLabels = append(addedLabels, label)
171 }
172
173 operations.ChangeLabels(nil, b, p, added, removed)
174}