diff options
author | Michael Muré <batolettre@gmail.com> | 2022-08-20 10:52:11 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-08-20 10:52:11 +0200 |
commit | 8818d091e85087766d7f493b7dfaf1aedc3a4ab0 (patch) | |
tree | a3bd60a9f533fc6fc11a094038d095510210cd23 /entities/bug | |
parent | 58df94d38d754bff4dcca11e2ae4b99326a9a87e (diff) | |
download | git-bug-8818d091e85087766d7f493b7dfaf1aedc3a4ab0.tar.gz |
move bug.Status in entities/common for reuse
Diffstat (limited to 'entities/bug')
-rw-r--r-- | entities/bug/bug.go | 3 | ||||
-rw-r--r-- | entities/bug/op_set_status.go | 11 | ||||
-rw-r--r-- | entities/bug/op_set_status_test.go | 3 | ||||
-rw-r--r-- | entities/bug/operation_test.go | 13 | ||||
-rw-r--r-- | entities/bug/snapshot.go | 3 | ||||
-rw-r--r-- | entities/bug/status.go | 86 |
6 files changed, 19 insertions, 100 deletions
diff --git a/entities/bug/bug.go b/entities/bug/bug.go index 213a4ca4..4ce10c5b 100644 --- a/entities/bug/bug.go +++ b/entities/bug/bug.go @@ -4,6 +4,7 @@ package bug import ( "fmt" + "github.com/MichaelMure/git-bug/entities/common" "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/entity/dag" @@ -149,7 +150,7 @@ func (bug *Bug) Operations() []Operation { func (bug *Bug) Compile() *Snapshot { snap := &Snapshot{ id: bug.Id(), - Status: OpenStatus, + Status: common.OpenStatus, } for _, op := range bug.Operations() { diff --git a/entities/bug/op_set_status.go b/entities/bug/op_set_status.go index 5e73d982..cf17901a 100644 --- a/entities/bug/op_set_status.go +++ b/entities/bug/op_set_status.go @@ -3,6 +3,7 @@ package bug import ( "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entities/common" "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/entity/dag" @@ -14,7 +15,7 @@ var _ Operation = &SetStatusOperation{} // SetStatusOperation will change the status of a bug type SetStatusOperation struct { dag.OpBase - Status Status `json:"status"` + Status common.Status `json:"status"` } func (op *SetStatusOperation) Id() entity.Id { @@ -47,7 +48,7 @@ func (op *SetStatusOperation) Validate() error { return nil } -func NewSetStatusOp(author identity.Interface, unixTime int64, status Status) *SetStatusOperation { +func NewSetStatusOp(author identity.Interface, unixTime int64, status common.Status) *SetStatusOperation { return &SetStatusOperation{ OpBase: dag.NewOpBase(SetStatusOp, author, unixTime), Status: status, @@ -58,7 +59,7 @@ type SetStatusTimelineItem struct { id entity.Id Author identity.Interface UnixTime timestamp.Timestamp - Status Status + Status common.Status } func (s SetStatusTimelineItem) Id() entity.Id { @@ -70,7 +71,7 @@ func (s SetStatusTimelineItem) IsAuthored() {} // Open is a convenience function to change a bugs state to Open func Open(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*SetStatusOperation, error) { - op := NewSetStatusOp(author, unixTime, OpenStatus) + op := NewSetStatusOp(author, unixTime, common.OpenStatus) for key, value := range metadata { op.SetMetadata(key, value) } @@ -83,7 +84,7 @@ func Open(b Interface, author identity.Interface, unixTime int64, metadata map[s // Close is a convenience function to change a bugs state to Close func Close(b Interface, author identity.Interface, unixTime int64, metadata map[string]string) (*SetStatusOperation, error) { - op := NewSetStatusOp(author, unixTime, ClosedStatus) + op := NewSetStatusOp(author, unixTime, common.ClosedStatus) for key, value := range metadata { op.SetMetadata(key, value) } diff --git a/entities/bug/op_set_status_test.go b/entities/bug/op_set_status_test.go index 7ec78704..5bb30265 100644 --- a/entities/bug/op_set_status_test.go +++ b/entities/bug/op_set_status_test.go @@ -3,12 +3,13 @@ package bug import ( "testing" + "github.com/MichaelMure/git-bug/entities/common" "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity/dag" ) func TestSetStatusSerialize(t *testing.T) { dag.SerializeRoundTripTest(t, func(author identity.Interface, unixTime int64) *SetStatusOperation { - return NewSetStatusOp(author, unixTime, ClosedStatus) + return NewSetStatusOp(author, unixTime, common.ClosedStatus) }) } diff --git a/entities/bug/operation_test.go b/entities/bug/operation_test.go index fe8080c3..4bec49ae 100644 --- a/entities/bug/operation_test.go +++ b/entities/bug/operation_test.go @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/MichaelMure/git-bug/entities/common" "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity/dag" "github.com/MichaelMure/git-bug/repository" @@ -30,7 +31,7 @@ func TestValidate(t *testing.T) { NewCreateOp(rene, unix, "title", "message", nil), NewSetTitleOp(rene, unix, "title2", "title1"), NewAddCommentOp(rene, unix, "message2", nil), - NewSetStatusOp(rene, unix, ClosedStatus), + NewSetStatusOp(rene, unix, common.ClosedStatus), NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"}), } @@ -42,11 +43,11 @@ func TestValidate(t *testing.T) { bad := []Operation{ // opbase - NewSetStatusOp(makeIdentity(t, "", "rene@descartes.fr"), unix, ClosedStatus), - NewSetStatusOp(makeIdentity(t, "René Descartes\u001b", "rene@descartes.fr"), unix, ClosedStatus), - NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@descartes.fr\u001b"), unix, ClosedStatus), - NewSetStatusOp(makeIdentity(t, "René \nDescartes", "rene@descartes.fr"), unix, ClosedStatus), - NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@\ndescartes.fr"), unix, ClosedStatus), + NewSetStatusOp(makeIdentity(t, "", "rene@descartes.fr"), unix, common.ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René Descartes\u001b", "rene@descartes.fr"), unix, common.ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@descartes.fr\u001b"), unix, common.ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René \nDescartes", "rene@descartes.fr"), unix, common.ClosedStatus), + NewSetStatusOp(makeIdentity(t, "René Descartes", "rene@\ndescartes.fr"), unix, common.ClosedStatus), &CreateOperation{OpBase: dag.NewOpBase(CreateOp, rene, 0), Title: "title", Message: "message", diff --git a/entities/bug/snapshot.go b/entities/bug/snapshot.go index cece09b8..442496f7 100644 --- a/entities/bug/snapshot.go +++ b/entities/bug/snapshot.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "github.com/MichaelMure/git-bug/entities/common" "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/entity/dag" @@ -15,7 +16,7 @@ var _ dag.Snapshot = &Snapshot{} type Snapshot struct { id entity.Id - Status Status + Status common.Status Title string Comments []Comment Labels []Label diff --git a/entities/bug/status.go b/entities/bug/status.go deleted file mode 100644 index b8fba609..00000000 --- a/entities/bug/status.go +++ /dev/null @@ -1,86 +0,0 @@ -package bug - -import ( - "fmt" - "io" - "strconv" - "strings" -) - -type Status int - -const ( - _ Status = iota - OpenStatus - ClosedStatus -) - -func (s Status) String() string { - switch s { - case OpenStatus: - return "open" - case ClosedStatus: - return "closed" - default: - return "unknown status" - } -} - -func (s Status) Action() string { - switch s { - case OpenStatus: - return "opened" - case ClosedStatus: - return "closed" - default: - return "unknown status" - } -} - -func StatusFromString(str string) (Status, error) { - cleaned := strings.ToLower(strings.TrimSpace(str)) - - switch cleaned { - case "open": - return OpenStatus, nil - case "closed": - return ClosedStatus, nil - default: - return 0, fmt.Errorf("unknown status") - } -} - -func (s Status) Validate() error { - if s != OpenStatus && s != ClosedStatus { - return fmt.Errorf("invalid") - } - - return nil -} - -func (s Status) MarshalGQL(w io.Writer) { - switch s { - case OpenStatus: - _, _ = fmt.Fprintf(w, strconv.Quote("OPEN")) - case ClosedStatus: - _, _ = fmt.Fprintf(w, strconv.Quote("CLOSED")) - default: - panic("missing case") - } -} - -func (s *Status) UnmarshalGQL(v interface{}) error { - str, ok := v.(string) - if !ok { - return fmt.Errorf("enums must be strings") - } - switch str { - case "OPEN": - *s = OpenStatus - case "CLOSED": - *s = ClosedStatus - default: - return fmt.Errorf("%s is not a valid Status", str) - } - return nil -} |