You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by kl...@apache.org on 2023/02/03 09:54:42 UTC

[incubator-devlake] branch main updated: refactor: simplify jenkins apiclient creation (#4318)

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

klesh 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 54a021376 refactor: simplify jenkins apiclient creation (#4318)
54a021376 is described below

commit 54a021376c955ab5bebf3db2fb2bb1da261292b0
Author: Klesh Wong <zh...@merico.dev>
AuthorDate: Fri Feb 3 17:54:37 2023 +0800

    refactor: simplify jenkins apiclient creation (#4318)
    
    * fix: gitlab testconnection
    
    * refactor: simplify jenkins apiclient creation
    
    * fix: jenkins unit test
---
 backend/plugins/gitlab/api/connection.go           |  3 ++-
 backend/plugins/gitlab/tasks/api_client.go         | 13 +++++-------
 backend/plugins/jenkins/api/blueprint_v100_test.go | 18 +++++++++--------
 backend/plugins/jenkins/api/connection.go          | 23 +++++-----------------
 backend/plugins/jenkins/impl/impl.go               |  6 +++---
 backend/plugins/jenkins/models/connection.go       | 20 ++++++-------------
 .../jenkins/tasks/{client.go => api_client.go}     |  7 +------
 7 files changed, 32 insertions(+), 58 deletions(-)

diff --git a/backend/plugins/gitlab/api/connection.go b/backend/plugins/gitlab/api/connection.go
index f96595668..c9ca68d21 100644
--- a/backend/plugins/gitlab/api/connection.go
+++ b/backend/plugins/gitlab/api/connection.go
@@ -42,8 +42,9 @@ func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput,
 	if err = api.Decode(input.Body, &connection, vld); err != nil {
 		return nil, err
 	}
+
 	// test connection
-	apiClient, err := api.NewApiClientFromConnection(context.TODO(), basicRes, connection)
+	apiClient, err := api.NewApiClientFromConnection(context.TODO(), basicRes, &connection)
 	if err != nil {
 		return nil, errors.Convert(err)
 	}
diff --git a/backend/plugins/gitlab/tasks/api_client.go b/backend/plugins/gitlab/tasks/api_client.go
index e972dd563..333378473 100644
--- a/backend/plugins/gitlab/tasks/api_client.go
+++ b/backend/plugins/gitlab/tasks/api_client.go
@@ -18,22 +18,19 @@ limitations under the License.
 package tasks
 
 import (
-	"fmt"
+	"net/http"
+	"strconv"
+	"time"
+
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 	"github.com/apache/incubator-devlake/plugins/gitlab/models"
-	"net/http"
-	"strconv"
-	"time"
 )
 
 func NewGitlabApiClient(taskCtx plugin.TaskContext, connection *models.GitlabConnection) (*api.ApiAsyncClient, errors.Error) {
 	// create synchronize api client so we can calculate api rate limit dynamically
-	headers := map[string]string{
-		"Authorization": fmt.Sprintf("Bearer %v", connection.Token),
-	}
-	apiClient, err := api.NewApiClient(taskCtx.GetContext(), connection.Endpoint, headers, 0, connection.Proxy, taskCtx)
+	apiClient, err := api.NewApiClientFromConnection(taskCtx.GetContext(), taskCtx, connection)
 	if err != nil {
 		return nil, err
 	}
diff --git a/backend/plugins/jenkins/api/blueprint_v100_test.go b/backend/plugins/jenkins/api/blueprint_v100_test.go
index 6f766d15f..630f7a0f1 100644
--- a/backend/plugins/jenkins/api/blueprint_v100_test.go
+++ b/backend/plugins/jenkins/api/blueprint_v100_test.go
@@ -42,14 +42,16 @@ func TestProcessScope(t *testing.T) {
 				ID: 1,
 			},
 		},
-		RestConnection: helper.RestConnection{
-			Endpoint:         "https://api.github.com/",
-			Proxy:            "",
-			RateLimitPerHour: 0,
-		},
-		BasicAuth: helper.BasicAuth{
-			Username: "Username",
-			Password: "Password",
+		JenkinsConn: models.JenkinsConn{
+			RestConnection: helper.RestConnection{
+				Endpoint:         "https://api.github.com/",
+				Proxy:            "",
+				RateLimitPerHour: 0,
+			},
+			BasicAuth: helper.BasicAuth{
+				Username: "Username",
+				Password: "Password",
+			},
 		},
 	}
 
diff --git a/backend/plugins/jenkins/api/connection.go b/backend/plugins/jenkins/api/connection.go
index 2c93417a7..3f505ba2c 100644
--- a/backend/plugins/jenkins/api/connection.go
+++ b/backend/plugins/jenkins/api/connection.go
@@ -19,20 +19,18 @@ package api
 
 import (
 	"context"
-	"fmt"
+	"net/http"
+
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
-	"github.com/apache/incubator-devlake/core/utils"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 	"github.com/apache/incubator-devlake/plugins/jenkins/models"
-	"net/http"
-	"time"
 )
 
 // @Summary test jenkins connection
 // @Description Test Jenkins Connection
 // @Tags plugins/jenkins
-// @Param body body models.TestConnectionRequest true "json body"
+// @Param body body models.JenkinsConn true "json body"
 // @Success 200  {object} shared.ApiBody "Success"
 // @Failure 400  {string} errcode.Error "Bad Request"
 // @Failure 500  {string} errcode.Error "Internal Error"
@@ -40,24 +38,13 @@ import (
 func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
 	// decode
 	var err errors.Error
-	var connection models.TestConnectionRequest
+	var connection models.JenkinsConn
 	err = api.Decode(input.Body, &connection, vld)
 	if err != nil {
 		return nil, err
 	}
 	// test connection
-	encodedToken := utils.GetEncodedToken(connection.Username, connection.Password)
-
-	apiClient, err := api.NewApiClient(
-		context.TODO(),
-		connection.Endpoint,
-		map[string]string{
-			"Authorization": fmt.Sprintf("Basic %v", encodedToken),
-		},
-		3*time.Second,
-		connection.Proxy,
-		basicRes,
-	)
+	apiClient, err := api.NewApiClientFromConnection(context.TODO(), basicRes, &connection)
 	if err != nil {
 		return nil, err
 	}
diff --git a/backend/plugins/jenkins/impl/impl.go b/backend/plugins/jenkins/impl/impl.go
index 46982e128..3088d0624 100644
--- a/backend/plugins/jenkins/impl/impl.go
+++ b/backend/plugins/jenkins/impl/impl.go
@@ -19,11 +19,12 @@ package impl
 
 import (
 	"fmt"
-	"github.com/apache/incubator-devlake/core/context"
-	"github.com/apache/incubator-devlake/core/dal"
 	"strings"
 	"time"
 
+	"github.com/apache/incubator-devlake/core/context"
+	"github.com/apache/incubator-devlake/core/dal"
+
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
 	helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -69,7 +70,6 @@ func (p Jenkins) GetTablesInfo() []dal.Tabler {
 		&models.JenkinsJob{},
 		&models.JenkinsJobDag{},
 		&models.JenkinsPipeline{},
-		&models.JenkinsResponse{},
 		&models.JenkinsStage{},
 		&models.JenkinsTask{},
 	}
diff --git a/backend/plugins/jenkins/models/connection.go b/backend/plugins/jenkins/models/connection.go
index 0fd81082c..0967ebb79 100644
--- a/backend/plugins/jenkins/models/connection.go
+++ b/backend/plugins/jenkins/models/connection.go
@@ -21,24 +21,16 @@ import (
 	helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 )
 
-// This object conforms to what the frontend currently sends.
-type JenkinsConnection struct {
-	helper.BaseConnection `mapstructure:",squash"`
+// JenkinsConn holds the essential information to connect to the Jenkins API
+type JenkinsConn struct {
 	helper.RestConnection `mapstructure:",squash"`
 	helper.BasicAuth      `mapstructure:",squash"`
 }
 
-type JenkinsResponse struct {
-	ID   int    `json:"id"`
-	Name string `json:"name"`
-	JenkinsConnection
-}
-
-type TestConnectionRequest struct {
-	Endpoint string `json:"endpoint" validate:"required"`
-	Username string `json:"username" validate:"required"`
-	Password string `json:"password" validate:"required"`
-	Proxy    string `json:"proxy"`
+// JenkinsConnection holds JenkinsConn plus ID/Name for database storage
+type JenkinsConnection struct {
+	helper.BaseConnection `mapstructure:",squash"`
+	JenkinsConn           `mapstructure:",squash"`
 }
 
 func (JenkinsConnection) TableName() string {
diff --git a/backend/plugins/jenkins/tasks/client.go b/backend/plugins/jenkins/tasks/api_client.go
similarity index 86%
rename from backend/plugins/jenkins/tasks/client.go
rename to backend/plugins/jenkins/tasks/api_client.go
index afe3940cf..eabff5cf4 100644
--- a/backend/plugins/jenkins/tasks/client.go
+++ b/backend/plugins/jenkins/tasks/api_client.go
@@ -18,7 +18,6 @@ limitations under the License.
 package tasks
 
 import (
-	"fmt"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -27,11 +26,7 @@ import (
 
 func CreateApiClient(taskCtx plugin.TaskContext, connection *models.JenkinsConnection) (*api.ApiAsyncClient, errors.Error) {
 	// create synchronize api client so we can calculate api rate limit dynamically
-	headers := map[string]string{
-		"Authorization": fmt.Sprintf("Basic %v", connection.GetEncodedToken()),
-	}
-
-	apiClient, err := api.NewApiClient(taskCtx.GetContext(), connection.Endpoint, headers, 0, connection.Proxy, taskCtx)
+	apiClient, err := api.NewApiClientFromConnection(taskCtx.GetContext(), taskCtx, connection)
 	if err != nil {
 		return nil, err
 	}