set_title.go

 1package operations
 2
 3import (
 4	"github.com/MichaelMure/git-bug/bug"
 5)
 6
 7// SetTitleOperation will change the title of a bug
 8
 9var _ bug.Operation = SetTitleOperation{}
10
11type SetTitleOperation struct {
12	bug.OpBase
13	Title string
14	Was   string
15}
16
17func (op SetTitleOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
18	snapshot.Title = op.Title
19
20	return snapshot
21}
22
23func NewSetTitleOp(author bug.Person, title string, was string) SetTitleOperation {
24	return SetTitleOperation{
25		OpBase: bug.NewOpBase(bug.SetTitleOp, author),
26		Title:  title,
27		Was:    was,
28	}
29}
30
31// Convenience function to apply the operation
32func SetTitle(b *bug.Bug, author bug.Person, title string) {
33	it := bug.NewOperationIterator(b)
34
35	var lastTitleOp bug.Operation
36	for it.Next() {
37		op := it.Value()
38		if op.OpType() == bug.SetTitleOp {
39			lastTitleOp = op
40		}
41	}
42
43	var was string
44	if lastTitleOp != nil {
45		was = lastTitleOp.(SetTitleOperation).Title
46	} else {
47		was = b.FirstOp().(CreateOperation).Title
48	}
49
50	setTitleOp := NewSetTitleOp(author, title, was)
51	b.Append(setTitleOp)
52}