create_random_bugs.go

  1// +build ignore
  2
  3package main
  4
  5import (
  6	"math/rand"
  7	"os"
  8	"strings"
  9	"time"
 10
 11	"github.com/MichaelMure/git-bug/bug"
 12	"github.com/MichaelMure/git-bug/bug/operations"
 13	"github.com/MichaelMure/git-bug/repository"
 14	"github.com/icrowley/fake"
 15)
 16
 17const bugNumber = 40
 18const personNumber = 5
 19const minOp = 3
 20const maxOp = 40
 21
 22type opsGenerator func(*bug.Bug, bug.Person)
 23
 24// This program will randomly generate a collection of bugs in the repository
 25// of the current path
 26func main() {
 27	rand.Seed(time.Now().UnixNano())
 28
 29	opsGenerators := []opsGenerator{
 30		comment,
 31		comment,
 32		title,
 33		labels,
 34		operations.Open,
 35		operations.Close,
 36	}
 37
 38	dir, err := os.Getwd()
 39	if err != nil {
 40		panic(err)
 41	}
 42
 43	repo, err := repository.NewGitRepo(dir, func(repo *repository.GitRepo) error {
 44		return nil
 45	})
 46	if err != nil {
 47		panic(err)
 48	}
 49
 50	for i := 0; i < bugNumber; i++ {
 51		addedLabels = []string{}
 52
 53		b, err := operations.Create(randomPerson(), fake.Sentence(), paragraphs())
 54
 55		if err != nil {
 56			panic(err)
 57		}
 58
 59		nOps := minOp + rand.Intn(maxOp-minOp)
 60		for j := 0; j < nOps; j++ {
 61			index := rand.Intn(len(opsGenerators))
 62			opsGenerators[index](b, randomPerson())
 63		}
 64
 65		err = b.Commit(repo)
 66		if err != nil {
 67			panic(err)
 68		}
 69	}
 70}
 71
 72func person() bug.Person {
 73	return bug.Person{
 74		Name:  fake.FullName(),
 75		Email: fake.EmailAddress(),
 76	}
 77}
 78
 79var persons []bug.Person
 80
 81func randomPerson() bug.Person {
 82	if len(persons) == 0 {
 83		persons = make([]bug.Person, personNumber)
 84		for i := range persons {
 85			persons[i] = person()
 86		}
 87	}
 88
 89	index := rand.Intn(personNumber)
 90	return persons[index]
 91}
 92
 93func paragraphs() string {
 94	p := fake.Paragraphs()
 95	return strings.Replace(p, "\t", "\n\n", -1)
 96}
 97
 98func comment(b *bug.Bug, p bug.Person) {
 99	operations.Comment(b, p, paragraphs())
100}
101
102func title(b *bug.Bug, p bug.Person) {
103	operations.SetTitle(b, p, fake.Sentence())
104}
105
106var addedLabels []string
107
108func labels(b *bug.Bug, p bug.Person) {
109	var removed []string
110	nbRemoved := rand.Intn(3)
111	for nbRemoved > 0 && len(addedLabels) > 0 {
112		index := rand.Intn(len(addedLabels))
113		removed = append(removed, addedLabels[index])
114		addedLabels[index] = addedLabels[len(addedLabels)-1]
115		addedLabels = addedLabels[:len(addedLabels)-1]
116	}
117
118	var added []string
119	nbAdded := rand.Intn(3)
120	for i := 0; i < nbAdded; i++ {
121		label := fake.Word()
122		added = append(added, label)
123		addedLabels = append(addedLabels, label)
124	}
125
126	operations.ChangeLabels(nil, b, p, added, removed)
127}