From 3a4b8805dfd794cc25f57e99c73ddec651805af1 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 25 Aug 2024 20:40:23 +0200 Subject: core: make label a common type, in a similar fashion as for status (#1252) This will be useful for Board, and likely code review support later --- entities/bug/label.go | 95 ------------------------------------ entities/bug/label_test.go | 35 ------------- entities/bug/op_label_change.go | 29 +++++------ entities/bug/op_label_change_test.go | 7 +-- entities/bug/operation_test.go | 6 +-- entities/bug/snapshot.go | 2 +- entities/common/label.go | 95 ++++++++++++++++++++++++++++++++++++ entities/common/label_test.go | 35 +++++++++++++ 8 files changed, 153 insertions(+), 151 deletions(-) delete mode 100644 entities/bug/label.go delete mode 100644 entities/bug/label_test.go create mode 100644 entities/common/label.go create mode 100644 entities/common/label_test.go (limited to 'entities') diff --git a/entities/bug/label.go b/entities/bug/label.go deleted file mode 100644 index 948a5b47..00000000 --- a/entities/bug/label.go +++ /dev/null @@ -1,95 +0,0 @@ -package bug - -import ( - "crypto/sha256" - "fmt" - "image/color" - - fcolor "github.com/fatih/color" - - "github.com/git-bug/git-bug/util/text" -) - -type Label string - -func (l Label) String() string { - return string(l) -} - -// RGBA from a Label computed in a deterministic way -func (l Label) Color() LabelColor { - // colors from: https://material-ui.com/style/color/ - colors := []LabelColor{ - {R: 244, G: 67, B: 54, A: 255}, // red - {R: 233, G: 30, B: 99, A: 255}, // pink - {R: 156, G: 39, B: 176, A: 255}, // purple - {R: 103, G: 58, B: 183, A: 255}, // deepPurple - {R: 63, G: 81, B: 181, A: 255}, // indigo - {R: 33, G: 150, B: 243, A: 255}, // blue - {R: 3, G: 169, B: 244, A: 255}, // lightBlue - {R: 0, G: 188, B: 212, A: 255}, // cyan - {R: 0, G: 150, B: 136, A: 255}, // teal - {R: 76, G: 175, B: 80, A: 255}, // green - {R: 139, G: 195, B: 74, A: 255}, // lightGreen - {R: 205, G: 220, B: 57, A: 255}, // lime - {R: 255, G: 235, B: 59, A: 255}, // yellow - {R: 255, G: 193, B: 7, A: 255}, // amber - {R: 255, G: 152, B: 0, A: 255}, // orange - {R: 255, G: 87, B: 34, A: 255}, // deepOrange - {R: 121, G: 85, B: 72, A: 255}, // brown - {R: 158, G: 158, B: 158, A: 255}, // grey - {R: 96, G: 125, B: 139, A: 255}, // blueGrey - } - - id := 0 - hash := sha256.Sum256([]byte(l)) - for _, char := range hash { - id = (id + int(char)) % len(colors) - } - - return colors[id] -} - -func (l Label) Validate() error { - str := string(l) - - if text.Empty(str) { - return fmt.Errorf("empty") - } - - if !text.SafeOneLine(str) { - return fmt.Errorf("label has unsafe characters") - } - - return nil -} - -type LabelColor color.RGBA - -func (lc LabelColor) RGBA() color.RGBA { - return color.RGBA(lc) -} - -func (lc LabelColor) Term256() Term256 { - red := Term256(lc.R) * 6 / 256 - green := Term256(lc.G) * 6 / 256 - blue := Term256(lc.B) * 6 / 256 - - return red*36 + green*6 + blue + 16 -} - -type Term256 int - -func (t Term256) Escape() string { - if fcolor.NoColor { - return "" - } - return fmt.Sprintf("\x1b[38;5;%dm", t) -} - -func (t Term256) Unescape() string { - if fcolor.NoColor { - return "" - } - return "\x1b[0m" -} diff --git a/entities/bug/label_test.go b/entities/bug/label_test.go deleted file mode 100644 index 49401c49..00000000 --- a/entities/bug/label_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package bug - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestLabelRGBA(t *testing.T) { - rgba := Label("test1").Color() - expected := LabelColor{R: 0, G: 150, B: 136, A: 255} - - require.Equal(t, expected, rgba) -} - -func TestLabelRGBASimilar(t *testing.T) { - rgba := Label("test2").Color() - expected := LabelColor{R: 3, G: 169, B: 244, A: 255} - - require.Equal(t, expected, rgba) -} - -func TestLabelRGBAReverse(t *testing.T) { - rgba := Label("tset").Color() - expected := LabelColor{R: 63, G: 81, B: 181, A: 255} - - require.Equal(t, expected, rgba) -} - -func TestLabelRGBAEqual(t *testing.T) { - color1 := Label("test").Color() - color2 := Label("test").Color() - - require.Equal(t, color1, color2) -} diff --git a/entities/bug/op_label_change.go b/entities/bug/op_label_change.go index b65b8ffb..6a9fb19b 100644 --- a/entities/bug/op_label_change.go +++ b/entities/bug/op_label_change.go @@ -8,6 +8,7 @@ import ( "github.com/pkg/errors" + "github.com/git-bug/git-bug/entities/common" "github.com/git-bug/git-bug/entities/identity" "github.com/git-bug/git-bug/entity" "github.com/git-bug/git-bug/entity/dag" @@ -19,8 +20,8 @@ var _ Operation = &LabelChangeOperation{} // LabelChangeOperation define a Bug operation to add or remove labels type LabelChangeOperation struct { dag.OpBase - Added []Label `json:"added"` - Removed []Label `json:"removed"` + Added []common.Label `json:"added"` + Removed []common.Label `json:"removed"` } func (op *LabelChangeOperation) Id() entity.Id { @@ -96,7 +97,7 @@ func (op *LabelChangeOperation) Validate() error { return nil } -func NewLabelChangeOperation(author identity.Interface, unixTime int64, added, removed []Label) *LabelChangeOperation { +func NewLabelChangeOperation(author identity.Interface, unixTime int64, added, removed []common.Label) *LabelChangeOperation { return &LabelChangeOperation{ OpBase: dag.NewOpBase(LabelChangeOp, author, unixTime), Added: added, @@ -108,8 +109,8 @@ type LabelChangeTimelineItem struct { combinedId entity.CombinedId Author identity.Interface UnixTime timestamp.Timestamp - Added []Label - Removed []Label + Added []common.Label + Removed []common.Label } func (l LabelChangeTimelineItem) CombinedId() entity.CombinedId { @@ -121,13 +122,13 @@ func (l *LabelChangeTimelineItem) IsAuthored() {} // ChangeLabels is a convenience function to change labels on a bug func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string, metadata map[string]string) ([]LabelChangeResult, *LabelChangeOperation, error) { - var added, removed []Label + var added, removed []common.Label var results []LabelChangeResult snap := b.Compile() for _, str := range add { - label := Label(str) + label := common.Label(str) // check for duplicate if labelExist(added, label) { @@ -146,7 +147,7 @@ func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, r } for _, str := range remove { - label := Label(str) + label := common.Label(str) // check for duplicate if labelExist(removed, label) { @@ -187,14 +188,14 @@ func ChangeLabels(b Interface, author identity.Interface, unixTime int64, add, r // The intended use of this function is to allow importers to create legal but unexpected label changes, // like removing a label with no information of when it was added before. func ForceChangeLabels(b Interface, author identity.Interface, unixTime int64, add, remove []string, metadata map[string]string) (*LabelChangeOperation, error) { - added := make([]Label, len(add)) + added := make([]common.Label, len(add)) for i, str := range add { - added[i] = Label(str) + added[i] = common.Label(str) } - removed := make([]Label, len(remove)) + removed := make([]common.Label, len(remove)) for i, str := range remove { - removed[i] = Label(str) + removed[i] = common.Label(str) } op := NewLabelChangeOperation(author, unixTime, added, removed) @@ -211,7 +212,7 @@ func ForceChangeLabels(b Interface, author identity.Interface, unixTime int64, a return op, nil } -func labelExist(labels []Label, label Label) bool { +func labelExist(labels []common.Label, label common.Label) bool { for _, l := range labels { if l == label { return true @@ -272,7 +273,7 @@ func (l *LabelChangeStatus) UnmarshalGQL(v interface{}) error { } type LabelChangeResult struct { - Label Label + Label common.Label Status LabelChangeStatus } diff --git a/entities/bug/op_label_change_test.go b/entities/bug/op_label_change_test.go index d184e47a..abafa222 100644 --- a/entities/bug/op_label_change_test.go +++ b/entities/bug/op_label_change_test.go @@ -3,6 +3,7 @@ package bug import ( "testing" + "github.com/git-bug/git-bug/entities/common" "github.com/git-bug/git-bug/entities/identity" "github.com/git-bug/git-bug/entity" "github.com/git-bug/git-bug/entity/dag" @@ -10,12 +11,12 @@ import ( func TestLabelChangeSerialize(t *testing.T) { dag.SerializeRoundTripTest(t, operationUnmarshaler, func(author identity.Interface, unixTime int64) (*LabelChangeOperation, entity.Resolvers) { - return NewLabelChangeOperation(author, unixTime, []Label{"added"}, []Label{"removed"}), nil + return NewLabelChangeOperation(author, unixTime, []common.Label{"added"}, []common.Label{"removed"}), nil }) dag.SerializeRoundTripTest(t, operationUnmarshaler, func(author identity.Interface, unixTime int64) (*LabelChangeOperation, entity.Resolvers) { - return NewLabelChangeOperation(author, unixTime, []Label{"added"}, nil), nil + return NewLabelChangeOperation(author, unixTime, []common.Label{"added"}, nil), nil }) dag.SerializeRoundTripTest(t, operationUnmarshaler, func(author identity.Interface, unixTime int64) (*LabelChangeOperation, entity.Resolvers) { - return NewLabelChangeOperation(author, unixTime, nil, []Label{"removed"}), nil + return NewLabelChangeOperation(author, unixTime, nil, []common.Label{"removed"}), nil }) } diff --git a/entities/bug/operation_test.go b/entities/bug/operation_test.go index d24faa7a..a6f122b3 100644 --- a/entities/bug/operation_test.go +++ b/entities/bug/operation_test.go @@ -32,7 +32,7 @@ func TestValidate(t *testing.T) { NewSetTitleOp(rene, unix, "title2", "title1"), NewAddCommentOp(rene, unix, "message2", nil), NewSetStatusOp(rene, unix, common.ClosedStatus), - NewLabelChangeOperation(rene, unix, []Label{"added"}, []Label{"removed"}), + NewLabelChangeOperation(rene, unix, []common.Label{"added"}, []common.Label{"removed"}), } for _, op := range good { @@ -65,8 +65,8 @@ func TestValidate(t *testing.T) { NewAddCommentOp(rene, unix, "message", []repository.Hash{repository.Hash("invalid")}), NewSetStatusOp(rene, unix, 1000), NewSetStatusOp(rene, unix, 0), - NewLabelChangeOperation(rene, unix, []Label{}, []Label{}), - NewLabelChangeOperation(rene, unix, []Label{"multi\nline"}, []Label{}), + NewLabelChangeOperation(rene, unix, []common.Label{}, []common.Label{}), + NewLabelChangeOperation(rene, unix, []common.Label{"multi\nline"}, []common.Label{}), } for i, op := range bad { diff --git a/entities/bug/snapshot.go b/entities/bug/snapshot.go index ca352284..7f9e7e58 100644 --- a/entities/bug/snapshot.go +++ b/entities/bug/snapshot.go @@ -19,7 +19,7 @@ type Snapshot struct { Status common.Status Title string Comments []Comment - Labels []Label + Labels []common.Label Author identity.Interface Actors []identity.Interface Participants []identity.Interface diff --git a/entities/common/label.go b/entities/common/label.go new file mode 100644 index 00000000..92d6c242 --- /dev/null +++ b/entities/common/label.go @@ -0,0 +1,95 @@ +package common + +import ( + "crypto/sha256" + "fmt" + "image/color" + + fcolor "github.com/fatih/color" + + "github.com/git-bug/git-bug/util/text" +) + +type Label string + +func (l Label) String() string { + return string(l) +} + +// RGBA from a Label computed in a deterministic way +func (l Label) Color() LabelColor { + // colors from: https://material-ui.com/style/color/ + colors := []LabelColor{ + {R: 244, G: 67, B: 54, A: 255}, // red + {R: 233, G: 30, B: 99, A: 255}, // pink + {R: 156, G: 39, B: 176, A: 255}, // purple + {R: 103, G: 58, B: 183, A: 255}, // deepPurple + {R: 63, G: 81, B: 181, A: 255}, // indigo + {R: 33, G: 150, B: 243, A: 255}, // blue + {R: 3, G: 169, B: 244, A: 255}, // lightBlue + {R: 0, G: 188, B: 212, A: 255}, // cyan + {R: 0, G: 150, B: 136, A: 255}, // teal + {R: 76, G: 175, B: 80, A: 255}, // green + {R: 139, G: 195, B: 74, A: 255}, // lightGreen + {R: 205, G: 220, B: 57, A: 255}, // lime + {R: 255, G: 235, B: 59, A: 255}, // yellow + {R: 255, G: 193, B: 7, A: 255}, // amber + {R: 255, G: 152, B: 0, A: 255}, // orange + {R: 255, G: 87, B: 34, A: 255}, // deepOrange + {R: 121, G: 85, B: 72, A: 255}, // brown + {R: 158, G: 158, B: 158, A: 255}, // grey + {R: 96, G: 125, B: 139, A: 255}, // blueGrey + } + + id := 0 + hash := sha256.Sum256([]byte(l)) + for _, char := range hash { + id = (id + int(char)) % len(colors) + } + + return colors[id] +} + +func (l Label) Validate() error { + str := string(l) + + if text.Empty(str) { + return fmt.Errorf("empty") + } + + if !text.SafeOneLine(str) { + return fmt.Errorf("label has unsafe characters") + } + + return nil +} + +type LabelColor color.RGBA + +func (lc LabelColor) RGBA() color.RGBA { + return color.RGBA(lc) +} + +func (lc LabelColor) Term256() Term256 { + red := Term256(lc.R) * 6 / 256 + green := Term256(lc.G) * 6 / 256 + blue := Term256(lc.B) * 6 / 256 + + return red*36 + green*6 + blue + 16 +} + +type Term256 int + +func (t Term256) Escape() string { + if fcolor.NoColor { + return "" + } + return fmt.Sprintf("\x1b[38;5;%dm", t) +} + +func (t Term256) Unescape() string { + if fcolor.NoColor { + return "" + } + return "\x1b[0m" +} diff --git a/entities/common/label_test.go b/entities/common/label_test.go new file mode 100644 index 00000000..206fc4ab --- /dev/null +++ b/entities/common/label_test.go @@ -0,0 +1,35 @@ +package common + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestLabelRGBA(t *testing.T) { + rgba := Label("test1").Color() + expected := LabelColor{R: 0, G: 150, B: 136, A: 255} + + require.Equal(t, expected, rgba) +} + +func TestLabelRGBASimilar(t *testing.T) { + rgba := Label("test2").Color() + expected := LabelColor{R: 3, G: 169, B: 244, A: 255} + + require.Equal(t, expected, rgba) +} + +func TestLabelRGBAReverse(t *testing.T) { + rgba := Label("tset").Color() + expected := LabelColor{R: 63, G: 81, B: 181, A: 255} + + require.Equal(t, expected, rgba) +} + +func TestLabelRGBAEqual(t *testing.T) { + color1 := Label("test").Color() + color2 := Label("test").Color() + + require.Equal(t, color1, color2) +} -- cgit