Commit 77bddf6

mo khan <mo@mokhan.ca>
2024-06-19 17:04:03
Rename Paramable to Entity
1 parent ee8260d
Changed files (5)
pkg/db/client.go → pkg/db/storage.go
@@ -12,15 +12,15 @@ import (
 	"github.com/xlgmokha/x/pkg/x"
 )
 
-type Paramable interface {
-	ToParam() string
+type Entity interface {
+	Identifier() string
 }
 
-type Storage[T Paramable] struct {
+type Storage[T Entity] struct {
 	dir string
 }
 
-func New[T Paramable](dir string) *Storage[T] {
+func New[T Entity](dir string) *Storage[T] {
 	fullPath := x.Must(filepath.Abs(dir))
 	x.Check(os.MkdirAll(fullPath, 0700))
 
@@ -35,9 +35,10 @@ func (db *Storage[T]) Save(item T) error {
 	if env.Fetch("DUMP", "") != "" {
 		fmt.Println(w.String())
 	}
-	return ioutil.WriteFile(
-		fmt.Sprintf("%v/%v.yaml", db.dir, item.ToParam()),
-		w.Bytes(),
-		0700,
-	)
+
+	return ioutil.WriteFile(db.filePathFor(item), w.Bytes(), 0700)
+}
+
+func (db *Storage[T]) filePathFor(item T) string {
+	return fmt.Sprintf("%v/%v.yaml", db.dir, item.Identifier())
 }
pkg/gitlab/issue.go
@@ -53,7 +53,7 @@ type Issue struct {
 	TaskStatus          string     `json:"task_status" yaml:"task_status"`
 }
 
-func (issue *Issue) ToParam() string {
+func (issue *Issue) Identifier() string {
 	return fmt.Sprintf("%v", issue.ID)
 }
 
pkg/gitlab/issue_test.go → test/gitlab/issue_test.go
@@ -10,6 +10,7 @@ import (
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
 	"github.com/xlgmokha/x/pkg/x"
+	"gitlab.com/mokhax/stanuki/pkg/gitlab"
 )
 
 func TestIssue(t *testing.T) {
@@ -17,7 +18,7 @@ func TestIssue(t *testing.T) {
 		t.Run("parses the array of issues from IO", func(t *testing.T) {
 			body := strings.NewReader(`[{"ID":1,"title":"Issue Title"}]`)
 
-			results, err := FromIssues(body)
+			results, err := gitlab.FromIssues(body)
 
 			require.NoError(t, err)
 			require.Equal(t, 1, len(results))
@@ -32,7 +33,7 @@ func TestIssue(t *testing.T) {
 			require.NoError(t, err)
 
 			reader := bufio.NewReader(file)
-			results, err := FromIssues(reader)
+			results, err := gitlab.FromIssues(reader)
 			require.NoError(t, err)
 
 			assert.Len(t, results, 20)
@@ -44,7 +45,7 @@ func TestIssue(t *testing.T) {
 			assert.Equal(t, 40549124, result.ProjectID)
 			assert.Contains(t, result.Title, "`gitlab-org/gitlab` broken `master` with rspec unit")
 			assert.Contains(t, result.Description, "## How to close this incident\n\n- Follow the steps in the")
-			assert.Equal(t, IssueClosed, result.State)
+			assert.Equal(t, gitlab.IssueClosed, result.State)
 			assert.Equal(t, x.Must(time.Parse(time.RFC3339Nano, "2024-05-18T17:39:14.548Z")), result.CreatedAt)
 			assert.Equal(t, x.Must(time.Parse(time.RFC3339Nano, "2024-05-18T18:14:37.830Z")), result.UpdatedAt)
 			assert.Equal(t, x.Must(time.Parse(time.RFC3339Nano, "2024-05-18T17:39:16.837Z")), result.ClosedAt)
@@ -53,7 +54,7 @@ func TestIssue(t *testing.T) {
 				user := result.ClosedBy
 				assert.Equal(t, 1786152, user.ID)
 				assert.Equal(t, "gitlab-bot", user.Username)
-				assert.Equal(t, UserActive, user.State)
+				assert.Equal(t, gitlab.UserActive, user.State)
 				assert.Equal(t, false, user.Locked)
 				assert.Equal(t, "https://gitlab.com/uploads/-/system/user/avatar/1786152/avatar.png", user.AvatarUrl)
 				assert.Equal(t, "https://gitlab.com/gitlab-bot", user.WebUrl)
@@ -69,7 +70,7 @@ func TestIssue(t *testing.T) {
 			assert.Empty(t, result.Assignees)
 
 			assert.Equal(t, 1786152, result.Author.ID)
-			assert.Equal(t, IssueTypeIncident, result.Type)
+			assert.Equal(t, gitlab.IssueTypeIncident, result.Type)
 			assert.Nil(t, result.Assignee)
 			assert.Equal(t, 4, result.UserNotesCount)
 			assert.Equal(t, 0, result.MergeRequestsCount)
go.mod
@@ -4,14 +4,17 @@ go 1.22
 
 require (
 	github.com/magefile/mage v1.15.0
-	github.com/stretchr/testify v1.8.0
+	github.com/stretchr/testify v1.9.0
 	github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8
 )
 
 require (
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/google/jsonapi v1.0.0 // indirect
+	github.com/kr/pretty v0.3.1 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
+	github.com/rogpeppe/go-internal v1.11.0 // indirect
+	gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )
go.sum
@@ -1,23 +1,31 @@
-github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/google/jsonapi v1.0.0 h1:qIGgO5Smu3yJmSs+QlvhQnrscdZfFhiV6S8ryJAglqU=
 github.com/google/jsonapi v1.0.0/go.mod h1:YYHiRPJT8ARXGER8In9VuLv4qvLfDmA9ULQqptbLE4s=
+github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
+github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
+github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
+github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
 github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
 github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
+github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
-github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
-github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
-github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
-github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
+github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
+github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
+github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
+github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8 h1:Hmyf8pgNUs3l8TW0YdUarBVAU+hWX87efBukspg4nWc=
 github.com/xlgmokha/x v0.0.0-20240605230110-5cbcac4d8ff8/go.mod h1:C9MUZ3A7PTPbrLNTvu2lKhpM0dFpPHt5yH8YGuYzmKQ=
-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
+gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
-gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=