You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2023/08/02 11:54:10 UTC
[incubator-devlake] branch main updated: feat: check dbUrl for zentao (#5796)
This is an automated email from the ASF dual-hosted git repository.
abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new 9e7b403ad feat: check dbUrl for zentao (#5796)
9e7b403ad is described below
commit 9e7b403ad843ecb66c0273a398219fada2e497be
Author: Liang Zhang <li...@merico.dev>
AuthorDate: Wed Aug 2 19:54:04 2023 +0800
feat: check dbUrl for zentao (#5796)
---
backend/core/runner/db.go | 39 ++++++++++++++--------
backend/plugins/zentao/api/connection.go | 22 ++++++++++--
.../plugins/zentao/e2e/check_connection_test.go | 37 ++++++++++++++++++++
3 files changed, 82 insertions(+), 16 deletions(-)
diff --git a/backend/core/runner/db.go b/backend/core/runner/db.go
index 1e1baba17..a255fa761 100644
--- a/backend/core/runner/db.go
+++ b/backend/core/runner/db.go
@@ -79,20 +79,7 @@ func NewGormDbEx(configReader config.ConfigReader, logger log.Logger, sessionCon
if dbUrl == "" {
return nil, errors.BadInput.New("DB_URL is required, please set it in environment variable or .env file")
}
- u, err := url.Parse(dbUrl)
- if err != nil {
- return nil, errors.Convert(err)
- }
- var db *gorm.DB
- switch strings.ToLower(u.Scheme) {
- case "mysql":
- dbUrl = fmt.Sprintf("%s@tcp(%s)%s?%s", getUserString(u), u.Host, u.Path, addLocal(u.Query()))
- db, err = gorm.Open(mysql.Open(dbUrl), dbConfig)
- case "postgresql", "postgres", "pg":
- db, err = gorm.Open(postgres.Open(dbUrl), dbConfig)
- default:
- return nil, errors.BadInput.New(fmt.Sprintf("invalid DB_URL:%s", dbUrl))
- }
+ db, err := getDbConnection(dbUrl, dbConfig)
if err != nil {
return nil, errors.Convert(err)
}
@@ -123,3 +110,27 @@ func addLocal(query url.Values) string {
}
return query.Encode()
}
+
+func getDbConnection(dbUrl string, conf *gorm.Config) (*gorm.DB, error) {
+ u, err := url.Parse(dbUrl)
+ if err != nil {
+ return nil, err
+ }
+ switch strings.ToLower(u.Scheme) {
+ case "mysql":
+ dbUrl = fmt.Sprintf("%s@tcp(%s)%s?%s", getUserString(u), u.Host, u.Path, addLocal(u.Query()))
+ return gorm.Open(mysql.Open(dbUrl), conf)
+ case "postgresql", "postgres", "pg":
+ return gorm.Open(postgres.Open(dbUrl), conf)
+ default:
+ return nil, fmt.Errorf("invalid DB_URL:%s", dbUrl)
+ }
+}
+
+func CheckDbConnection(dbUrl string) errors.Error {
+ db, err := getDbConnection(dbUrl, &gorm.Config{})
+ if err != nil {
+ return errors.Convert(err)
+ }
+ return errors.Convert(db.Exec("SELECT 1").Error)
+}
diff --git a/backend/plugins/zentao/api/connection.go b/backend/plugins/zentao/api/connection.go
index bcc8209b6..9c3fb608d 100644
--- a/backend/plugins/zentao/api/connection.go
+++ b/backend/plugins/zentao/api/connection.go
@@ -19,6 +19,7 @@ package api
import (
"context"
+ "github.com/apache/incubator-devlake/core/runner"
"net/http"
"github.com/apache/incubator-devlake/server/api/shared"
@@ -51,11 +52,28 @@ func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
}
// try to create apiClient
- _, err = helper.NewApiClientFromConnection(context.TODO(), basicRes, &connection)
+ client, err := helper.NewApiClientFromConnection(context.TODO(), basicRes, &connection)
if err != nil {
return nil, err
}
- body := ZentaoTestConnResponse{}
+ resp, err := client.Get("/user", nil, nil)
+ if err != nil {
+ return nil, err
+ }
+ var body ZentaoTestConnResponse
+ if resp.StatusCode != http.StatusOK {
+ body.Success = false
+ body.Message = err.Error()
+ return &plugin.ApiResourceOutput{Body: body, Status: http.StatusBadRequest}, nil
+ }
+ if connection.DbUrl != "" {
+ err = runner.CheckDbConnection(connection.DbUrl)
+ if err != nil {
+ body.Success = false
+ body.Message = "invalid DbUrl"
+ return &plugin.ApiResourceOutput{Body: body, Status: http.StatusBadRequest}, nil
+ }
+ }
body.Success = true
body.Message = "success"
body.Connection = &connection
diff --git a/backend/plugins/zentao/e2e/check_connection_test.go b/backend/plugins/zentao/e2e/check_connection_test.go
new file mode 100644
index 000000000..69a01c7ba
--- /dev/null
+++ b/backend/plugins/zentao/e2e/check_connection_test.go
@@ -0,0 +1,37 @@
+/*
+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 e2e
+
+import (
+ "github.com/apache/incubator-devlake/core/runner"
+ "testing"
+
+ "github.com/apache/incubator-devlake/core/config"
+)
+
+func TestZentaoCheckDbConnection(t *testing.T) {
+ cfg := config.GetConfig()
+ dbUrl := cfg.GetString("E2E_DB_URL")
+ if dbUrl == "" {
+ return
+ }
+ err := runner.CheckDbConnection(dbUrl)
+ if err != nil {
+ t.Error(err)
+ }
+}