op_create.go

  1package bug
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/MichaelMure/git-bug/util/git"
  8	"github.com/MichaelMure/git-bug/util/text"
  9)
 10
 11var _ Operation = &CreateOperation{}
 12
 13// CreateOperation define the initial creation of a bug
 14type CreateOperation struct {
 15	OpBase
 16	Title   string     `json:"title"`
 17	Message string     `json:"message"`
 18	Files   []git.Hash `json:"files"`
 19}
 20
 21func (op *CreateOperation) base() *OpBase {
 22	return &op.OpBase
 23}
 24
 25func (op *CreateOperation) Hash() (git.Hash, error) {
 26	return hashOperation(op)
 27}
 28
 29func (op *CreateOperation) Apply(snapshot *Snapshot) {
 30	snapshot.Title = op.Title
 31
 32	comment := Comment{
 33		Message:  op.Message,
 34		Author:   op.Author,
 35		UnixTime: Timestamp(op.UnixTime),
 36	}
 37
 38	snapshot.Comments = []Comment{comment}
 39	snapshot.Author = op.Author
 40	snapshot.CreatedAt = op.Time()
 41
 42	hash, err := op.Hash()
 43	if err != nil {
 44		// Should never error unless a programming error happened
 45		// (covered in OpBase.Validate())
 46		panic(err)
 47	}
 48
 49	snapshot.Timeline = []TimelineItem{
 50		&CreateTimelineItem{
 51			CommentTimelineItem: NewCommentTimelineItem(hash, comment),
 52		},
 53	}
 54}
 55
 56func (op *CreateOperation) GetFiles() []git.Hash {
 57	return op.Files
 58}
 59
 60func (op *CreateOperation) Validate() error {
 61	if err := opBaseValidate(op, CreateOp); err != nil {
 62		return err
 63	}
 64
 65	if text.Empty(op.Title) {
 66		return fmt.Errorf("title is empty")
 67	}
 68
 69	if strings.Contains(op.Title, "\n") {
 70		return fmt.Errorf("title should be a single line")
 71	}
 72
 73	if !text.Safe(op.Title) {
 74		return fmt.Errorf("title is not fully printable")
 75	}
 76
 77	if !text.Safe(op.Message) {
 78		return fmt.Errorf("message is not fully printable")
 79	}
 80
 81	return nil
 82}
 83
 84// Sign post method for gqlgen
 85func (op *CreateOperation) IsAuthored() {}
 86
 87func NewCreateOp(author Person, unixTime int64, title, message string, files []git.Hash) *CreateOperation {
 88	return &CreateOperation{
 89		OpBase:  newOpBase(CreateOp, author, unixTime),
 90		Title:   title,
 91		Message: message,
 92		Files:   files,
 93	}
 94}
 95
 96// CreateTimelineItem replace a Create operation in the Timeline and hold its edition history
 97type CreateTimelineItem struct {
 98	CommentTimelineItem
 99}
100
101// Convenience function to apply the operation
102func Create(author Person, unixTime int64, title, message string) (*Bug, *CreateOperation, error) {
103	return CreateWithFiles(author, unixTime, title, message, nil)
104}
105
106func CreateWithFiles(author Person, unixTime int64, title, message string, files []git.Hash) (*Bug, *CreateOperation, error) {
107	newBug := NewBug()
108	createOp := NewCreateOp(author, unixTime, title, message, files)
109
110	if err := createOp.Validate(); err != nil {
111		return nil, createOp, err
112	}
113
114	newBug.Append(createOp)
115
116	return newBug, createOp, nil
117}