op_create.go

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