From 5511c230b678a181cc596238bf6669428d1b1902 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Thu, 18 Aug 2022 23:34:05 +0200 Subject: move {bug,identity} to /entities, move input to /commands --- entities/bug/op_set_title.go | 112 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 entities/bug/op_set_title.go (limited to 'entities/bug/op_set_title.go') diff --git a/entities/bug/op_set_title.go b/entities/bug/op_set_title.go new file mode 100644 index 00000000..75efd08e --- /dev/null +++ b/entities/bug/op_set_title.go @@ -0,0 +1,112 @@ +package bug + +import ( + "fmt" + + "github.com/MichaelMure/git-bug/entities/identity" + "github.com/MichaelMure/git-bug/entity" + "github.com/MichaelMure/git-bug/entity/dag" + "github.com/MichaelMure/git-bug/util/timestamp" + + "github.com/MichaelMure/git-bug/util/text" +) + +var _ Operation = &SetTitleOperation{} + +// SetTitleOperation will change the title of a bug +type SetTitleOperation struct { + dag.OpBase + Title string `json:"title"` + Was string `json:"was"` +} + +func (op *SetTitleOperation) Id() entity.Id { + return dag.IdOperation(op, &op.OpBase) +} + +func (op *SetTitleOperation) Apply(snapshot *Snapshot) { + snapshot.Title = op.Title + snapshot.addActor(op.Author()) + + item := &SetTitleTimelineItem{ + id: op.Id(), + Author: op.Author(), + UnixTime: timestamp.Timestamp(op.UnixTime), + Title: op.Title, + Was: op.Was, + } + + snapshot.Timeline = append(snapshot.Timeline, item) +} + +func (op *SetTitleOperation) Validate() error { + if err := op.OpBase.Validate(op, SetTitleOp); err != nil { + return err + } + + if text.Empty(op.Title) { + return fmt.Errorf("title is empty") + } + + if !text.SafeOneLine(op.Title) { + return fmt.Errorf("title has unsafe characters") + } + + if !text.SafeOneLine(op.Was) { + return fmt.Errorf("previous title has unsafe characters") + } + + return nil +} + +func NewSetTitleOp(author identity.Interface, unixTime int64, title string, was string) *SetTitleOperation { + return &SetTitleOperation{ + OpBase: dag.NewOpBase(SetTitleOp, author, unixTime), + Title: title, + Was: was, + } +} + +type SetTitleTimelineItem struct { + id entity.Id + Author identity.Interface + UnixTime timestamp.Timestamp + Title string + Was string +} + +func (s SetTitleTimelineItem) Id() entity.Id { + return s.id +} + +// IsAuthored is a sign post method for gqlgen +func (s SetTitleTimelineItem) IsAuthored() {} + +// SetTitle is a convenience function to change a bugs title +func SetTitle(b Interface, author identity.Interface, unixTime int64, title string, metadata map[string]string) (*SetTitleOperation, error) { + var lastTitleOp *SetTitleOperation + for _, op := range b.Operations() { + switch op := op.(type) { + case *SetTitleOperation: + lastTitleOp = op + } + } + + var was string + if lastTitleOp != nil { + was = lastTitleOp.Title + } else { + was = b.FirstOp().(*CreateOperation).Title + } + + op := NewSetTitleOp(author, unixTime, title, was) + for key, value := range metadata { + op.SetMetadata(key, value) + } + if err := op.Validate(); err != nil { + return nil, err + } + + b.Append(op) + return op, nil +} -- cgit From 45b04351d8d02e53b3401b0ee23f7cbe750b63cd Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Mon, 3 May 2021 11:45:15 +0200 Subject: bug: have a type for combined ids, fix https://github.com/MichaelMure/git-bug/issues/653 --- entities/bug/op_set_title.go | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'entities/bug/op_set_title.go') diff --git a/entities/bug/op_set_title.go b/entities/bug/op_set_title.go index 75efd08e..e9526b64 100644 --- a/entities/bug/op_set_title.go +++ b/entities/bug/op_set_title.go @@ -28,12 +28,14 @@ func (op *SetTitleOperation) Apply(snapshot *Snapshot) { snapshot.Title = op.Title snapshot.addActor(op.Author()) + id := op.Id() item := &SetTitleTimelineItem{ - id: op.Id(), - Author: op.Author(), - UnixTime: timestamp.Timestamp(op.UnixTime), - Title: op.Title, - Was: op.Was, + id: id, + combinedId: entity.CombineIds(snapshot.Id(), id), + Author: op.Author(), + UnixTime: timestamp.Timestamp(op.UnixTime), + Title: op.Title, + Was: op.Was, } snapshot.Timeline = append(snapshot.Timeline, item) @@ -68,19 +70,24 @@ func NewSetTitleOp(author identity.Interface, unixTime int64, title string, was } type SetTitleTimelineItem struct { - id entity.Id - Author identity.Interface - UnixTime timestamp.Timestamp - Title string - Was string + id entity.Id + combinedId entity.CombinedId + Author identity.Interface + UnixTime timestamp.Timestamp + Title string + Was string } func (s SetTitleTimelineItem) Id() entity.Id { return s.id } +func (s SetTitleTimelineItem) CombinedId() entity.CombinedId { + return s.combinedId +} + // IsAuthored is a sign post method for gqlgen -func (s SetTitleTimelineItem) IsAuthored() {} +func (s *SetTitleTimelineItem) IsAuthored() {} // SetTitle is a convenience function to change a bugs title func SetTitle(b Interface, author identity.Interface, unixTime int64, title string, metadata map[string]string) (*SetTitleOperation, error) { -- cgit