This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch onboard-project-api
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/onboard-project-api by this 
push:
     new 1b3bb8766 feat: add onboard keyvalue api
1b3bb8766 is described below

commit 1b3bb87663131d0d45729f160a33cf26750da57c
Author: abeizn <[email protected]>
AuthorDate: Mon Mar 18 22:35:44 2024 +0800

    feat: add onboard keyvalue api
---
 .../migrationscripts/20240318_add_kvstore.go       | 54 +++++++++++++++
 backend/core/models/migrationscripts/register.go   |  1 +
 backend/core/models/project.go                     | 19 ++++++
 backend/python/plugins/azuredevops/poetry.lock     | 20 +++---
 backend/server/api/project/kvstore.go              | 77 ++++++++++++++++++++++
 backend/server/api/router.go                       |  3 +
 backend/server/services/kvstore.go                 | 76 +++++++++++++++++++++
 7 files changed, 239 insertions(+), 11 deletions(-)

diff --git a/backend/core/models/migrationscripts/20240318_add_kvstore.go 
b/backend/core/models/migrationscripts/20240318_add_kvstore.go
new file mode 100644
index 000000000..842b945e2
--- /dev/null
+++ b/backend/core/models/migrationscripts/20240318_add_kvstore.go
@@ -0,0 +1,54 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package migrationscripts
+
+import (
+       "encoding/json"
+       "time"
+
+       "github.com/apache/incubator-devlake/core/context"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/plugin"
+)
+
+var _ plugin.MigrationScript = (*addKvstore)(nil)
+
+type kvstore20240318 struct {
+       StoreKey   string          `gorm:"primaryKey;type:varchar(255)"`
+       StoreValue json.RawMessage `gorm:"type:json;serializer:json"`
+       CreatedAt  time.Time       `json:"createdAt" mapstructure:"createdAt"`
+       UpdatedAt  time.Time       `json:"updatedAt" mapstructure:"updatedAt"`
+}
+
+func (kvstore20240318) TableName() string {
+       return "_devlake_kvstore"
+}
+
+type addKvstore struct{}
+
+func (*addKvstore) Up(basicRes context.BasicRes) errors.Error {
+       return basicRes.GetDal().AutoMigrate(kvstore20240318{})
+}
+
+func (*addKvstore) Version() uint64 {
+       return 20240318111246
+}
+
+func (*addKvstore) Name() string {
+       return "add _devlake_kvstore table"
+}
diff --git a/backend/core/models/migrationscripts/register.go 
b/backend/core/models/migrationscripts/register.go
index 03319bf92..88db72024 100644
--- a/backend/core/models/migrationscripts/register.go
+++ b/backend/core/models/migrationscripts/register.go
@@ -109,5 +109,6 @@ func All() []plugin.MigrationScript {
                new(modifyRefsIdLength),
                
new(addOriginalEnvironmentToCicdDeploymentsAndCicdDeploymentCommits),
                new(addSubtabknameToDeployment),
+               new(addKvstore),
        }
 }
diff --git a/backend/core/models/project.go b/backend/core/models/project.go
index 6a935e97e..d104aae34 100644
--- a/backend/core/models/project.go
+++ b/backend/core/models/project.go
@@ -18,6 +18,9 @@ limitations under the License.
 package models
 
 import (
+       "encoding/json"
+       "time"
+
        "github.com/apache/incubator-devlake/core/models/common"
 )
 
@@ -67,3 +70,19 @@ type ApiOutputProject struct {
        Blueprint    *Blueprint    `json:"blueprint" mapstructure:"blueprint"`
        LastPipeline *Pipeline     `json:"lastPipeline,omitempty" 
mapstructure:"lastPipeline"`
 }
+
+type Kvstore struct {
+       StoreKey   string          `gorm:"primaryKey;type:varchar(255)"`
+       StoreValue json.RawMessage `gorm:"type:json;serializer:json"`
+       CreatedAt  time.Time       `json:"createdAt" mapstructure:"createdAt"`
+       UpdatedAt  time.Time       `json:"updatedAt" mapstructure:"updatedAt"`
+}
+
+func (Kvstore) TableName() string {
+       return "_devlake_kvstore"
+}
+
+type ApiKvstore struct {
+       StoreKey   string          `gorm:"primaryKey;type:varchar(255)"`
+       StoreValue json.RawMessage `gorm:"type:json;serializer:json"`
+}
diff --git a/backend/python/plugins/azuredevops/poetry.lock 
b/backend/python/plugins/azuredevops/poetry.lock
index 2f08a0707..191f3e37d 100644
--- a/backend/python/plugins/azuredevops/poetry.lock
+++ b/backend/python/plugins/azuredevops/poetry.lock
@@ -275,20 +275,18 @@ files = [
 
 [[package]]
 name = "mysqlclient"
-version = "2.2.4"
+version = "2.1.1"
 description = "Python interface to MySQL"
 optional = false
-python-versions = ">=3.8"
+python-versions = ">=3.5"
 files = [
-    {file = "mysqlclient-2.2.4-cp310-cp310-win_amd64.whl", hash = 
"sha256:ac44777eab0a66c14cb0d38965572f762e193ec2e5c0723bcd11319cc5b693c5"},
-    {file = "mysqlclient-2.2.4-cp311-cp311-win_amd64.whl", hash = 
"sha256:329e4eec086a2336fe3541f1ce095d87a6f169d1cc8ba7b04ac68bcb234c9711"},
-    {file = "mysqlclient-2.2.4-cp312-cp312-win_amd64.whl", hash = 
"sha256:e1ebe3f41d152d7cb7c265349fdb7f1eca86ccb0ca24a90036cde48e00ceb2ab"},
-    {file = "mysqlclient-2.2.4-cp38-cp38-win_amd64.whl", hash = 
"sha256:3c318755e06df599338dad7625f884b8a71fcf322a9939ef78c9b3db93e1de7a"},
-    {file = "mysqlclient-2.2.4-cp39-cp39-win_amd64.whl", hash = 
"sha256:9d4c015480c4a6b2b1602eccd9846103fc70606244788d04aa14b31c4bd1f0e2"},
-    {file = "mysqlclient-2.2.4-pp310-pypy310_pp73-win_amd64.whl", hash = 
"sha256:d43987bb9626096a302ca6ddcdd81feaeca65ced1d5fe892a6a66b808326aa54"},
-    {file = "mysqlclient-2.2.4-pp38-pypy38_pp73-win_amd64.whl", hash = 
"sha256:4e80dcad884dd6e14949ac6daf769123223a52a6805345608bf49cdaf7bc8b3a"},
-    {file = "mysqlclient-2.2.4-pp39-pypy39_pp73-win_amd64.whl", hash = 
"sha256:9d3310295cb682232cadc28abd172f406c718b9ada41d2371259098ae37779d3"},
-    {file = "mysqlclient-2.2.4.tar.gz", hash = 
"sha256:33bc9fb3464e7d7c10b1eaf7336c5ff8f2a3d3b88bab432116ad2490beb3bf41"},
+    {file = "mysqlclient-2.1.1-cp310-cp310-win_amd64.whl", hash = 
"sha256:c1ed71bd6244993b526113cca3df66428609f90e4652f37eb51c33496d478b37"},
+    {file = "mysqlclient-2.1.1-cp311-cp311-win_amd64.whl", hash = 
"sha256:c812b67e90082a840efb82a8978369e6e69fc62ce1bda4ca8f3084a9d862308b"},
+    {file = "mysqlclient-2.1.1-cp38-cp38-win_amd64.whl", hash = 
"sha256:0d1cd3a5a4d28c222fa199002810e8146cffd821410b67851af4cc80aeccd97c"},
+    {file = "mysqlclient-2.1.1-cp39-cp39-win_amd64.whl", hash = 
"sha256:b355c8b5a7d58f2e909acdbb050858390ee1b0e13672ae759e5e784110022994"},
+    {file = "mysqlclient-2.1.1-pp38-pypy38_pp73-win_amd64.whl", hash = 
"sha256:996924f3483fd36a34a5812210c69e71dea5a3d5978d01199b78b7f6d485c855"},
+    {file = "mysqlclient-2.1.1-pp39-pypy39_pp73-win_amd64.whl", hash = 
"sha256:dea88c8d3f5a5d9293dfe7f087c16dd350ceb175f2f6631c9cf4caf3e19b7a96"},
+    {file = "mysqlclient-2.1.1.tar.gz", hash = 
"sha256:828757e419fb11dd6c5ed2576ec92c3efaa93a0f7c39e263586d1ee779c3d782"},
 ]
 
 [[package]]
diff --git a/backend/server/api/project/kvstore.go 
b/backend/server/api/project/kvstore.go
new file mode 100644
index 000000000..4afd7396a
--- /dev/null
+++ b/backend/server/api/project/kvstore.go
@@ -0,0 +1,77 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package project
+
+import (
+       "fmt"
+       "net/http"
+
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/models"
+       "github.com/apache/incubator-devlake/server/api/shared"
+       "github.com/apache/incubator-devlake/server/services"
+
+       "github.com/gin-gonic/gin"
+)
+
+// @Summary Get list of info by onboard
+// @Description GET onboard info
+// @Tags framework/projects
+// @Param onboard path string true "onboard"
+// @Success 200  {object} models.Kvstore
+// @Failure 400  {string} errcode.Error "Bad Request"
+// @Failure 500  {string} errcode.Error "Internal Error"
+// @Router /kvstore/{onboard} [get]
+func GetKvstore(c *gin.Context) {
+       storeKey := c.Param("onboard")
+       result, err := services.GetKvstore(storeKey)
+       if err != nil {
+               c.JSON(http.StatusInternalServerError, gin.H{"error": 
fmt.Sprintf("Error fetching %s data", storeKey)})
+               return
+       }
+
+       shared.ApiOutputSuccess(c, result, http.StatusOK)
+}
+
+// @Summary Put a on board project
+// @Description Put a board project
+// @Tags framework/projects
+// @Accept application/json
+// @Param project body models.ApiKvstore false "json"
+// @Success 200  {object} models.Kvstore
+// @Failure 400  {string} errcode.Error "Bad Request"
+// @Failure 500  {string} errcode.Error "Internal Error"
+// @Router /kvstore [PUT]
+func PutKvstore(c *gin.Context) {
+       var body models.Kvstore
+       err := c.ShouldBind(&body)
+       if err != nil {
+               shared.ApiOutputError(c, errors.BadInput.Wrap(err, 
shared.BadRequestBody))
+               return
+       }
+       storeKey := body.StoreKey
+       fmt.Println("storeKey: ", storeKey)
+
+       onBoardOutput, err := services.PutKvstore(storeKey, &body)
+       if err != nil {
+               shared.ApiOutputError(c, errors.Default.Wrap(err, "error put on 
board project"))
+               return
+       }
+
+       shared.ApiOutputSuccess(c, onBoardOutput, http.StatusCreated)
+}
diff --git a/backend/server/api/router.go b/backend/server/api/router.go
index 6957044de..e74e920bd 100644
--- a/backend/server/api/router.go
+++ b/backend/server/api/router.go
@@ -73,6 +73,9 @@ func RegisterRouter(r *gin.Engine, basicRes context.BasicRes) 
{
        r.DELETE("/projects/*projectName", project.DeleteProject)
        r.POST("/projects", project.PostProject)
        r.GET("/projects", project.GetProjects)
+       // on board api
+       r.GET("/kvstore/:onboard", project.GetKvstore)
+       r.PUT("/kvstore", project.PutKvstore)
 
        // api keys api
        r.GET("/api-keys", apikeys.GetApiKeys)
diff --git a/backend/server/services/kvstore.go 
b/backend/server/services/kvstore.go
new file mode 100644
index 000000000..2be71ce85
--- /dev/null
+++ b/backend/server/services/kvstore.go
@@ -0,0 +1,76 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package services
+
+import (
+       "fmt"
+
+       "github.com/apache/incubator-devlake/core/dal"
+       "github.com/apache/incubator-devlake/core/errors"
+       "github.com/apache/incubator-devlake/core/models"
+)
+
+// GetProjects returns a paginated list of Projects based on `query`
+func GetKvstore(storeKey string) (*models.Kvstore, errors.Error) {
+       clauses := []dal.Clause{
+               dal.From(&models.Kvstore{}),
+               dal.Where("store_key = ?", storeKey),
+       }
+
+       kvstore := &models.Kvstore{}
+       err := db.First(&kvstore, clauses...)
+       if err != nil {
+               return nil, errors.Default.Wrap(err, fmt.Sprintf("error finding 
%s on _devlake_kvstore table", storeKey))
+       }
+
+       return kvstore, nil
+}
+
+// PutOnboard accepts a project instance and insert it to database
+func PutKvstore(storeKey string, storeValue *models.Kvstore) (*models.Kvstore, 
errors.Error) {
+       // verify input
+       if err := VerifyStruct(storeValue); err != nil {
+               return nil, err
+       }
+
+       // create transaction to updte multiple tables
+       var err errors.Error
+       tx := db.Begin()
+       defer func() {
+               if r := recover(); r != nil || err != nil {
+                       err = tx.Rollback()
+                       if err != nil {
+                               logger.Error(err, "PutKvstore: failed to 
rollback")
+                       }
+               }
+       }()
+
+       result := &models.Kvstore{}
+       err = tx.CreateOrUpdate(storeValue, dal.Where("store_key = ?", 
storeKey))
+       if err != nil {
+               return nil, err
+       }
+
+       // all good, commit transaction
+       err = tx.Commit()
+       if err != nil {
+               return nil, err
+       }
+
+       return result, nil
+}

Reply via email to