aboutsummaryrefslogtreecommitdiffstats
path: root/bug/bug.go
blob: 9d19a42cbdc3d58e440ba63975ca70f0d5bdeb36 (plain) (blame)
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
// Package bug contains the bug data model and low-level related functions
package bug

import (
	"fmt"

	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/entity/dag"
	"github.com/MichaelMure/git-bug/identity"
	"github.com/MichaelMure/git-bug/repository"
)

var _ Interface = &Bug{}
var _ entity.Interface = &Bug{}

// 1: original format
// 2: no more legacy identities
// 3: Ids are generated from the create operation serialized data instead of from the first git commit
// 4: with DAG entity framework
const formatVersion = 4

var def = dag.Definition{
	Typename:             "bug",
	Namespace:            "bugs",
	OperationUnmarshaler: operationUnmarshaller,
	FormatVersion:        formatVersion,
}

var ClockLoader = dag.ClockLoader(def)

// Bug hold the data of a bug thread, organized in a way close to
// how it will be persisted inside Git. This is the data structure
// used to merge two different version of the same Bug.
type Bug struct {
	*dag.Entity
}

// NewBug create a new Bug
func NewBug() *Bug {
	return &Bug{
		Entity: dag.New(def),
	}
}

// Read will read a bug from a repository
func Read(repo repository.ClockedRepo, id entity.Id) (*Bug, error) {
	e, err := dag.Read(def, repo, identity.NewSimpleResolver(repo), id)
	if err != nil {
		return nil, err
	}
	return &Bug{Entity: e}, nil
}

// ReadWithResolver will read a bug from its Id, with a custom identity.Resolver
func ReadWithResolver(repo repository.ClockedRepo, identityResolver identity.Resolver, id entity.Id) (*Bug, error) {
	e, err := dag.Read(def, repo, identityResolver, id)
	if err != nil {
		return nil, err
	}
	return &Bug{Entity: e}, nil
}

type StreamedBug struct {
	Bug *Bug
	Err error
}

// ReadAll read and parse all local bugs
func ReadAll(repo repository.ClockedRepo) <-chan StreamedBug {
	return readAll(repo, identity.NewSimpleResolver(repo))
}

// ReadAllWithResolver read and parse all local bugs
func ReadAllWithResolver(repo repository.ClockedRepo, identityResolver identity.Resolver) <-chan StreamedBug {
	return readAll(repo, identityResolver)
}

// Read and parse all available bug with a given ref prefix
func readAll(repo repository.ClockedRepo, identityResolver identity.Resolver) <-chan StreamedBug {
	out := make(chan StreamedBug)

	go func() {
		defer close(out)

		for streamedEntity := range dag.ReadAll(def, repo, identityResolver) {
			if streamedEntity.Err != nil {
				out <- StreamedBug{
					Err: streamedEntity.Err,
				}
			} else {
				out <- StreamedBug{
					Bug: &Bug{Entity: streamedEntity.Entity},
				}
			}
		}
	}()

	return out
}

// ListLocalIds list all the available local bug ids
func ListLocalIds(repo repository.Repo) ([]entity.Id, error) {
	return dag.ListLocalIds(def, repo)
}

// Validate check if the Bug data is valid
func (bug *Bug) Validate() error {
	if err := bug.Entity.Validate(); err != nil {
		return err
	}

	// The very first Op should be a CreateOp
	firstOp := bug.FirstOp()
	if firstOp == nil || firstOp.Type() != CreateOp {
		return fmt.Errorf("first operation should be a Create op")
	}

	// Check that there is no more CreateOp op
	for i, op := range bug.Operations() {
		if i == 0 {
			continue
		}
		if op.Type() == CreateOp {
			return fmt.Errorf("only one Create op allowed")
		}
	}

	return nil
}

// Append add a new Operation to the Bug
func (bug *Bug) Append(op Operation) {
	bug.Entity.Append(op)
}

// Operations return the ordered operations
func (bug *Bug) Operations() []Operation {
	source := bug.Entity.Operations()
	result := make([]Operation, len(source))
	for i, op := range source {
		result[i] = op.(Operation)
	}
	return result
}

// Compile a bug in a easily usable snapshot
func (bug *Bug) Compile() Snapshot {
	snap := Snapshot{
		id:     bug.Id(),
		Status: OpenStatus,
	}

	for _, op := range bug.Operations() {
		op.Apply(&snap)
		snap.Operations = append(snap.Operations, op)
	}

	return snap
}

// Lookup for the very first operation of the bug.
// For a valid Bug, this operation should be a CreateOp
func (bug *Bug) FirstOp() Operation {
	if fo := bug.Entity.FirstOp(); fo != nil {
		return fo.(Operation)
	}
	return nil
}

// Lookup for the very last operation of the bug.
// For a valid Bug, should never be nil
func (bug *Bug) LastOp() Operation {
	if lo := bug.Entity.LastOp(); lo != nil {
		return lo.(Operation)
	}
	return nil
}