From ba3281dc9918fa49f10c2a166b5b631a931d2d51 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Wed, 18 Jul 2018 00:16:06 +0200 Subject: all operations now have an author and a timestamp --- bug/bug.go | 4 +++- bug/comment.go | 4 ++-- bug/operation.go | 17 +++++++++++++++++ bug/operations/add_comment.go | 15 ++++++--------- bug/operations/create.go | 13 ++++--------- bug/operations/create_test.go | 2 +- bug/operations/set_status.go | 9 ++++++--- bug/operations/set_title.go | 11 ++++++++--- bug/snapshot.go | 12 ++++++++---- 9 files changed, 55 insertions(+), 32 deletions(-) (limited to 'bug') diff --git a/bug/bug.go b/bug/bug.go index 7f068c05..398bdb9f 100644 --- a/bug/bug.go +++ b/bug/bug.go @@ -376,7 +376,9 @@ func (bug *Bug) Compile() Snapshot { it := NewOperationIterator(bug) for it.Next() { - snap = it.Value().Apply(snap) + op := it.Value() + snap = op.Apply(snap) + snap.Operations = append(snap.Operations, op) } return snap diff --git a/bug/comment.go b/bug/comment.go index 8ea4154f..63147776 100644 --- a/bug/comment.go +++ b/bug/comment.go @@ -8,9 +8,9 @@ type Comment struct { // Creation time of the comment. // Should be used only for human display, never for ordering as we can't rely on it in a distributed system. - Time int64 + UnixTime int64 } func (c Comment) FormatTime() string { - return time.Unix(c.Time, 0).Format(time.RFC822) + return time.Unix(c.UnixTime, 0).Format(time.RFC822) } diff --git a/bug/operation.go b/bug/operation.go index c9e7a555..15374f08 100644 --- a/bug/operation.go +++ b/bug/operation.go @@ -1,5 +1,7 @@ package bug +import "time" + type OperationType int const ( @@ -12,13 +14,28 @@ const ( type Operation interface { OpType() OperationType + Time() time.Time Apply(snapshot Snapshot) Snapshot } type OpBase struct { OperationType OperationType + Author Person + UnixTime int64 +} + +func NewOpBase(opType OperationType, author Person) OpBase { + return OpBase{ + OperationType: opType, + Author: author, + UnixTime: time.Now().Unix(), + } } func (op OpBase) OpType() OperationType { return op.OperationType } + +func (op OpBase) Time() time.Time { + return time.Unix(op.UnixTime, 0) +} diff --git a/bug/operations/add_comment.go b/bug/operations/add_comment.go index 2927295a..9ee8dc64 100644 --- a/bug/operations/add_comment.go +++ b/bug/operations/add_comment.go @@ -2,32 +2,29 @@ package operations import ( "github.com/MichaelMure/git-bug/bug" - "time" ) +// AddCommentOperation will add a new comment in the bug + var _ bug.Operation = AddCommentOperation{} type AddCommentOperation struct { bug.OpBase Message string - Author bug.Person - Time int64 } func NewAddCommentOp(author bug.Person, message string) AddCommentOperation { return AddCommentOperation{ - OpBase: bug.OpBase{OperationType: bug.AddCommentOp}, + OpBase: bug.NewOpBase(bug.AddCommentOp, author), Message: message, - Author: author, - Time: time.Now().Unix(), } } func (op AddCommentOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { comment := bug.Comment{ - Message: op.Message, - Author: op.Author, - Time: op.Time, + Message: op.Message, + Author: op.Author, + UnixTime: op.UnixTime, } snapshot.Comments = append(snapshot.Comments, comment) diff --git a/bug/operations/create.go b/bug/operations/create.go index ad1d99ac..72eba843 100644 --- a/bug/operations/create.go +++ b/bug/operations/create.go @@ -2,7 +2,6 @@ package operations import ( "github.com/MichaelMure/git-bug/bug" - "time" ) // CreateOperation define the initial creation of a bug @@ -13,17 +12,13 @@ type CreateOperation struct { bug.OpBase Title string Message string - Author bug.Person - Time int64 } func NewCreateOp(author bug.Person, title, message string) CreateOperation { return CreateOperation{ - OpBase: bug.OpBase{OperationType: bug.CreateOp}, + OpBase: bug.NewOpBase(bug.CreateOp, author), Title: title, Message: message, - Author: author, - Time: time.Now().Unix(), } } @@ -31,9 +26,9 @@ func (op CreateOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { snapshot.Title = op.Title snapshot.Comments = []bug.Comment{ { - Message: op.Message, - Author: op.Author, - Time: op.Time, + Message: op.Message, + Author: op.Author, + UnixTime: op.UnixTime, }, } return snapshot diff --git a/bug/operations/create_test.go b/bug/operations/create_test.go index c262a41c..8aade05f 100644 --- a/bug/operations/create_test.go +++ b/bug/operations/create_test.go @@ -21,7 +21,7 @@ func TestCreate(t *testing.T) { expected := bug.Snapshot{ Title: "title", Comments: []bug.Comment{ - {Author: rene, Message: "message", Time: create.Time}, + {Author: rene, Message: "message", UnixTime: create.UnixTime}, }, } diff --git a/bug/operations/set_status.go b/bug/operations/set_status.go index aa673bb1..7c718cd6 100644 --- a/bug/operations/set_status.go +++ b/bug/operations/set_status.go @@ -1,6 +1,8 @@ package operations -import "github.com/MichaelMure/git-bug/bug" +import ( + "github.com/MichaelMure/git-bug/bug" +) // SetStatusOperation will change the status of a bug @@ -11,14 +13,15 @@ type SetStatusOperation struct { Status bug.Status } -func NewSetStatusOp(status bug.Status) SetStatusOperation { +func NewSetStatusOp(author bug.Person, status bug.Status) SetStatusOperation { return SetStatusOperation{ - OpBase: bug.OpBase{OperationType: bug.SetStatusOp}, + OpBase: bug.NewOpBase(bug.SetStatusOp, author), Status: status, } } func (op SetStatusOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { snapshot.Status = op.Status + return snapshot } diff --git a/bug/operations/set_title.go b/bug/operations/set_title.go index c5e0ab0e..ef934db1 100644 --- a/bug/operations/set_title.go +++ b/bug/operations/set_title.go @@ -1,6 +1,10 @@ package operations -import "github.com/MichaelMure/git-bug/bug" +import ( + "github.com/MichaelMure/git-bug/bug" +) + +// SetTitleOperation will change the title of a bug var _ bug.Operation = SetTitleOperation{} @@ -9,14 +13,15 @@ type SetTitleOperation struct { Title string } -func NewSetTitleOp(title string) SetTitleOperation { +func NewSetTitleOp(author bug.Person, title string) SetTitleOperation { return SetTitleOperation{ - OpBase: bug.OpBase{OperationType: bug.SetTitleOp}, + OpBase: bug.NewOpBase(bug.SetTitleOp, author), Title: title, } } func (op SetTitleOperation) Apply(snapshot bug.Snapshot) bug.Snapshot { snapshot.Title = op.Title + return snapshot } diff --git a/bug/snapshot.go b/bug/snapshot.go index 9229e7a1..75d3a028 100644 --- a/bug/snapshot.go +++ b/bug/snapshot.go @@ -7,11 +7,14 @@ import ( // Snapshot is a compiled form of the Bug data structure used for storage and merge type Snapshot struct { - id string + id string + Status Status Title string Comments []Comment Labels []Label + + Operations []Operation } // Return the Bug identifier @@ -32,10 +35,11 @@ func (snap Snapshot) Summary() string { ) } +// Return the last time a bug was modified func (snap Snapshot) LastEdit() time.Time { - if len(snap.Comments) == 0 { + if len(snap.Operations) == 0 { return time.Unix(0, 0) } - lastEditTimestamp := snap.Comments[len(snap.Comments)-1].Time - return time.Unix(lastEditTimestamp, 0) + + return snap.Operations[len(snap.Operations)-1].Time() } -- cgit