op_create.go

  1package bug
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6
  7	"github.com/MichaelMure/git-bug/entity"
  8	"github.com/MichaelMure/git-bug/entity/dag"
  9	"github.com/MichaelMure/git-bug/identity"
 10	"github.com/MichaelMure/git-bug/repository"
 11	"github.com/MichaelMure/git-bug/util/text"
 12	"github.com/MichaelMure/git-bug/util/timestamp"
 13)
 14
 15var _ Operation = &CreateOperation{}
 16var _ dag.OperationWithFiles = &CreateOperation{}
 17
 18// CreateOperation define the initial creation of a bug
 19type CreateOperation struct {
 20	OpBase
 21	Title   string            `json:"title"`
 22	Message string            `json:"message"`
 23	Files   []repository.Hash `json:"files"`
 24}
 25
 26func (op *CreateOperation) Id() entity.Id {
 27	return idOperation(op, &op.OpBase)
 28}
 29
 30// OVERRIDE
 31func (op *CreateOperation) SetMetadata(key string, value string) {
 32	// sanity check: we make sure we are not in the following scenario:
 33	// - the bug is created with a first operation
 34	// - Id() is used
 35	// - metadata are added, which will change the Id
 36	// - Id() is used again
 37
 38	if op.id != entity.UnsetId {
 39		panic("usage of Id() after changing the first operation")
 40	}
 41
 42	op.OpBase.SetMetadata(key, value)
 43}
 44
 45func (op *CreateOperation) Apply(snapshot *Snapshot) {
 46	// sanity check: will fail when adding a second Create
 47	if snapshot.id != "" && snapshot.id != entity.UnsetId && snapshot.id != op.Id() {
 48		panic("adding a second Create operation")
 49	}
 50
 51	snapshot.id = op.Id()
 52
 53	snapshot.addActor(op.Author_)
 54	snapshot.addParticipant(op.Author_)
 55
 56	snapshot.Title = op.Title
 57
 58	comment := Comment{
 59		id:       entity.CombineIds(snapshot.Id(), op.Id()),
 60		Message:  op.Message,
 61		Author:   op.Author_,
 62		UnixTime: timestamp.Timestamp(op.UnixTime),
 63	}
 64
 65	snapshot.Comments = []Comment{comment}
 66	snapshot.Author = op.Author_
 67	snapshot.CreateTime = op.Time()
 68
 69	snapshot.Timeline = []TimelineItem{
 70		&CreateTimelineItem{
 71			CommentTimelineItem: NewCommentTimelineItem(comment),
 72		},
 73	}
 74}
 75
 76func (op *CreateOperation) GetFiles() []repository.Hash {
 77	return op.Files
 78}
 79
 80func (op *CreateOperation) Validate() error {
 81	if err := op.OpBase.Validate(op, CreateOp); err != nil {
 82		return err
 83	}
 84
 85	if len(op.Nonce) > 64 {
 86		return fmt.Errorf("create nonce is too big")
 87	}
 88	if len(op.Nonce) < 20 {
 89		return fmt.Errorf("create nonce is too small")
 90	}
 91
 92	if text.Empty(op.Title) {
 93		return fmt.Errorf("title is empty")
 94	}
 95	if !text.SafeOneLine(op.Title) {
 96		return fmt.Errorf("title has unsafe characters")
 97	}
 98
 99	if !text.Safe(op.Message) {
100		return fmt.Errorf("message is not fully printable")
101	}
102
103	return nil
104}
105
106// UnmarshalJSON is a two step JSON unmarshalling
107// This workaround is necessary to avoid the inner OpBase.MarshalJSON
108// overriding the outer op's MarshalJSON
109func (op *CreateOperation) UnmarshalJSON(data []byte) error {
110	// Unmarshal OpBase and the op separately
111
112	base := OpBase{}
113	err := json.Unmarshal(data, &base)
114	if err != nil {
115		return err
116	}
117
118	aux := struct {
119		Nonce   []byte            `json:"nonce"`
120		Title   string            `json:"title"`
121		Message string            `json:"message"`
122		Files   []repository.Hash `json:"files"`
123	}{}
124
125	err = json.Unmarshal(data, &aux)
126	if err != nil {
127		return err
128	}
129
130	op.OpBase = base
131	op.Nonce = aux.Nonce
132	op.Title = aux.Title
133	op.Message = aux.Message
134	op.Files = aux.Files
135
136	return nil
137}
138
139// Sign post method for gqlgen
140func (op *CreateOperation) IsAuthored() {}
141
142func NewCreateOp(author identity.Interface, unixTime int64, title, message string, files []repository.Hash) *CreateOperation {
143	return &CreateOperation{
144		OpBase:  newOpBase(CreateOp, author, unixTime),
145		Title:   title,
146		Message: message,
147		Files:   files,
148	}
149}
150
151// CreateTimelineItem replace a Create operation in the Timeline and hold its edition history
152type CreateTimelineItem struct {
153	CommentTimelineItem
154}
155
156// Sign post method for gqlgen
157func (c *CreateTimelineItem) IsAuthored() {}
158
159// Convenience function to apply the operation
160func Create(author identity.Interface, unixTime int64, title, message string) (*Bug, *CreateOperation, error) {
161	return CreateWithFiles(author, unixTime, title, message, nil)
162}
163
164func CreateWithFiles(author identity.Interface, unixTime int64, title, message string, files []repository.Hash) (*Bug, *CreateOperation, error) {
165	newBug := NewBug()
166	createOp := NewCreateOp(author, unixTime, title, message, files)
167
168	if err := createOp.Validate(); err != nil {
169		return nil, createOp, err
170	}
171
172	newBug.Append(createOp)
173
174	return newBug, createOp, nil
175}