aboutsummaryrefslogblamecommitdiffstats
path: root/operations/set_status.go
blob: 3e7eae4ace529f4454057e4435225a9ce3f15e51 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12

                  

                                            
                               
 





                                                     
                   
                                         

 





                                                                        











                                                                       
                                                                                              
                                  
                                                                         



                               
                                              

                                                                     


                                             
                    
                  

 
                                              

                                                                      


                                             
                    
                  
 
package operations

import (
	"github.com/MichaelMure/git-bug/bug"
	"github.com/pkg/errors"
)

// SetStatusOperation will change the status of a bug

var _ bug.Operation = SetStatusOperation{}

type SetStatusOperation struct {
	*bug.OpBase
	Status bug.Status `json:"status"`
}

func (op SetStatusOperation) Apply(snapshot bug.Snapshot) bug.Snapshot {
	snapshot.Status = op.Status

	return snapshot
}

func (op SetStatusOperation) Validate() error {
	if err := bug.OpBaseValidate(op, bug.SetStatusOp); err != nil {
		return err
	}

	if err := op.Status.Validate(); err != nil {
		return errors.Wrap(err, "status")
	}

	return nil
}

func NewSetStatusOp(author bug.Person, unixTime int64, status bug.Status) SetStatusOperation {
	return SetStatusOperation{
		OpBase: bug.NewOpBase(bug.SetStatusOp, author, unixTime),
		Status: status,
	}
}

// Convenience function to apply the operation
func Open(b bug.Interface, author bug.Person, unixTime int64) error {
	op := NewSetStatusOp(author, unixTime, bug.OpenStatus)
	if err := op.Validate(); err != nil {
		return err
	}
	b.Append(op)
	return nil
}

// Convenience function to apply the operation
func Close(b bug.Interface, author bug.Person, unixTime int64) error {
	op := NewSetStatusOp(author, unixTime, bug.ClosedStatus)
	if err := op.Validate(); err != nil {
		return err
	}
	b.Append(op)
	return nil
}