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

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

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

zhangliang2022 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 dd985ee41 refactor: simplify bitbucket apiclient creation (#4310)
dd985ee41 is described below

commit dd985ee419dd8ceb36503a07b3da059578e28ac1
Author: Klesh Wong <zh...@merico.dev>
AuthorDate: Fri Feb 3 14:24:44 2023 +0800

    refactor: simplify bitbucket apiclient creation (#4310)
    
    * refactor: simplify bitbucket apiclient creation
    
    * fix: 4298 bitbuctk unit-test and linting
---
 backend/plugins/bitbucket/api/blueprint_test.go | 18 +++++++++--------
 backend/plugins/bitbucket/api/connection.go     | 20 +++++-------------
 backend/plugins/bitbucket/models/connection.go  | 27 ++++++++-----------------
 backend/plugins/bitbucket/tasks/api_client.go   | 11 +---------
 4 files changed, 24 insertions(+), 52 deletions(-)

diff --git a/backend/plugins/bitbucket/api/blueprint_test.go b/backend/plugins/bitbucket/api/blueprint_test.go
index 30bfd1be1..43b875178 100644
--- a/backend/plugins/bitbucket/api/blueprint_test.go
+++ b/backend/plugins/bitbucket/api/blueprint_test.go
@@ -44,14 +44,16 @@ func TestMakePipelinePlan(t *testing.T) {
 				ID: 1,
 			},
 		},
-		RestConnection: helper.RestConnection{
-			Endpoint:         "https://TestBitBucket/",
-			Proxy:            "",
-			RateLimitPerHour: 0,
-		},
-		BasicAuth: helper.BasicAuth{
-			Username: "Username",
-			Password: "Password",
+		BitbucketConn: models.BitbucketConn{
+			RestConnection: helper.RestConnection{
+				Endpoint:         "https://TestBitBucket/",
+				Proxy:            "",
+				RateLimitPerHour: 0,
+			},
+			BasicAuth: helper.BasicAuth{
+				Username: "Username",
+				Password: "Password",
+			},
 		},
 	}
 
diff --git a/backend/plugins/bitbucket/api/connection.go b/backend/plugins/bitbucket/api/connection.go
index 1296f5c2f..0479d2f89 100644
--- a/backend/plugins/bitbucket/api/connection.go
+++ b/backend/plugins/bitbucket/api/connection.go
@@ -19,20 +19,19 @@ package api
 
 import (
 	"context"
-	"fmt"
+	"net/http"
+
 	"github.com/apache/incubator-devlake/core/errors"
 	plugin "github.com/apache/incubator-devlake/core/plugin"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 	"github.com/apache/incubator-devlake/plugins/bitbucket/models"
 	_ "github.com/apache/incubator-devlake/server/api/shared"
-	"net/http"
-	"time"
 )
 
 // @Summary test bitbucket connection
 // @Description Test bitbucket Connection
 // @Tags plugins/bitbucket
-// @Param body body models.TestConnectionRequest true "json body"
+// @Param body body models.BitbucketConn true "json body"
 // @Success 200  {object} shared.ApiBody "Success"
 // @Failure 400  {string} errcode.Error "Bad Request"
 // @Failure 500  {string} errcode.Error "Internal Error"
@@ -40,21 +39,12 @@ import (
 func TestConnection(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
 	// decode
 	var err errors.Error
-	var connection models.TestConnectionRequest
+	var connection models.BitbucketConn
 	if err := api.Decode(input.Body, &connection, vld); err != nil {
 		return nil, errors.BadInput.Wrap(err, "could not decode request parameters")
 	}
 	// test connection
-	apiClient, err := api.NewApiClient(
-		context.TODO(),
-		connection.Endpoint,
-		map[string]string{
-			"Authorization": fmt.Sprintf("Basic %v", connection.GetEncodedToken()),
-		},
-		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/bitbucket/models/connection.go b/backend/plugins/bitbucket/models/connection.go
index fe9d24a39..9ddb865e0 100644
--- a/backend/plugins/bitbucket/models/connection.go
+++ b/backend/plugins/bitbucket/models/connection.go
@@ -21,23 +21,6 @@ import (
 	helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 )
 
-type EpicResponse struct {
-	Id    int
-	Title string
-	Value string
-}
-
-type TestConnectionRequest struct {
-	Endpoint         string `json:"endpoint"`
-	Proxy            string `json:"proxy"`
-	helper.BasicAuth `mapstructure:",squash"`
-}
-
-type BoardResponse struct {
-	Id    int
-	Title string
-	Value string
-}
 type TransformationRules struct {
 	IssueStatusTODO       []string `mapstructure:"issueStatusTodo" json:"issueStatusTodo"`
 	IssueStatusINPROGRESS []string `mapstructure:"issueStatusInProgress" json:"issueStatusInProgress"`
@@ -45,12 +28,18 @@ type TransformationRules struct {
 	IssueStatusOTHER      []string `mapstructure:"issueStatusOther" json:"issueStatusOther"`
 }
 
-type BitbucketConnection struct {
-	helper.BaseConnection `mapstructure:",squash"`
+// BitbucketConn holds the essential information to connect to the Bitbucket API
+type BitbucketConn struct {
 	helper.RestConnection `mapstructure:",squash"`
 	helper.BasicAuth      `mapstructure:",squash"`
 }
 
+// BitbucketConnection holds BitbucketConn plus ID/Name for database storage
+type BitbucketConnection struct {
+	helper.BaseConnection `mapstructure:",squash"`
+	BitbucketConn         `mapstructure:",squash"`
+}
+
 func (BitbucketConnection) TableName() string {
 	return "_tool_bitbucket_connections"
 }
diff --git a/backend/plugins/bitbucket/tasks/api_client.go b/backend/plugins/bitbucket/tasks/api_client.go
index 9404b5f08..bb9563532 100644
--- a/backend/plugins/bitbucket/tasks/api_client.go
+++ b/backend/plugins/bitbucket/tasks/api_client.go
@@ -18,27 +18,18 @@ limitations under the License.
 package tasks
 
 import (
-	"fmt"
 	"github.com/apache/incubator-devlake/core/errors"
 	plugin "github.com/apache/incubator-devlake/core/plugin"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 	"github.com/apache/incubator-devlake/plugins/bitbucket/models"
-	"net/http"
 )
 
 func CreateApiClient(taskCtx plugin.TaskContext, connection *models.BitbucketConnection) (*api.ApiAsyncClient, errors.Error) {
-	// load configuration
-	token := connection.GetEncodedToken()
 	// create synchronize api client so we can calculate api rate limit dynamically
-	apiClient, err := api.NewApiClient(taskCtx.GetContext(), connection.Endpoint, nil, 0, connection.Proxy, taskCtx)
+	apiClient, err := api.NewApiClientFromConnection(taskCtx.GetContext(), taskCtx, connection)
 	if err != nil {
 		return nil, err
 	}
-	// Rotates token on each request.
-	apiClient.SetBeforeFunction(func(req *http.Request) errors.Error {
-		req.Header.Set("Authorization", fmt.Sprintf("Basic %v", token))
-		return nil
-	})
 
 	// create rate limit calculator
 	rateLimiter := &api.ApiRateLimitCalculator{