This is an automated email from the ASF dual-hosted git repository.
likyh 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 43e57e097 refactor: simplify gitee apiclient creation (#4314)
43e57e097 is described below
commit 43e57e0975952c060f06a23f4fee777403462167
Author: Klesh Wong <[email protected]>
AuthorDate: Fri Feb 3 17:22:24 2023 +0800
refactor: simplify gitee apiclient creation (#4314)
* refactor: simplify gitee apiclient creation
* fix: gitee unit test
---
backend/plugins/gitee/api/blueprint_test.go | 17 ++++++++------
backend/plugins/gitee/api/connection.go | 35 +++++++++--------------------
backend/plugins/gitee/impl/impl.go | 2 +-
backend/plugins/gitee/models/connection.go | 33 ++++++++++++++++-----------
backend/plugins/gitee/tasks/api_client.go | 16 +++++--------
5 files changed, 46 insertions(+), 57 deletions(-)
diff --git a/backend/plugins/gitee/api/blueprint_test.go
b/backend/plugins/gitee/api/blueprint_test.go
index 986872d8d..226464ab9 100644
--- a/backend/plugins/gitee/api/blueprint_test.go
+++ b/backend/plugins/gitee/api/blueprint_test.go
@@ -43,13 +43,16 @@ func TestMakePipelinePlan(t *testing.T) {
ID: 1,
},
},
- RestConnection: helper.RestConnection{
- Endpoint: "https://api.github.com/",
- Proxy: "",
- RateLimitPerHour: 0,
- },
- AccessToken: helper.AccessToken{
- Token: "123",
+ GiteeConn: models.GiteeConn{
+
+ RestConnection: helper.RestConnection{
+ Endpoint: "https://api.github.com/",
+ Proxy: "",
+ RateLimitPerHour: 0,
+ },
+ GiteeAccessToken: models.GiteeAccessToken{
+ Token: "123",
+ },
},
}
mockApiCLient := mockapi.NewApiClientGetter(t)
diff --git a/backend/plugins/gitee/api/connection.go
b/backend/plugins/gitee/api/connection.go
index 5b48a9e14..0aa16b4ba 100644
--- a/backend/plugins/gitee/api/connection.go
+++ b/backend/plugins/gitee/api/connection.go
@@ -19,49 +19,34 @@ package api
import (
"context"
+ "net/http"
+
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/plugin"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
"github.com/apache/incubator-devlake/plugins/gitee/models"
- "net/http"
- "net/url"
- "time"
-
- "github.com/mitchellh/mapstructure"
)
// @Summary test gitee connection
-// @Description Test gitee Connection
+// @Description Test gitee Connection. endpoint: https://gitee.com/api/v5/
// @Tags plugins/gitee
-// @Param body body models.TestConnectionRequest true "json body"
+// @Param body body models.GiteeConn true "json body"
// @Success 200 {object} shared.ApiBody "Success"
// @Failure 400 {string} errcode.Error "Bad Request"
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/gitee/test [POST]
func TestConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
- var connection models.TestConnectionRequest
- if err := mapstructure.Decode(input.Body, &connection); err != nil {
+ var err errors.Error
+ var connection models.GiteeConn
+ if err = helper.Decode(input.Body, &connection, vld); err != nil {
return nil, errors.BadInput.Wrap(err, "could not decode request
parameters")
}
- if err := vld.Struct(connection); err != nil {
- return nil, errors.BadInput.Wrap(err, "could not validate
request parameters")
- }
- // test connection
- apiClient, err := helper.NewApiClient(
- context.TODO(),
- connection.Endpoint,
- nil,
- 3*time.Second,
- connection.Proxy,
- basicRes,
- )
+
+ apiClient, err := helper.NewApiClientFromConnection(context.TODO(),
basicRes, &connection)
if err != nil {
return nil, err
}
- query := url.Values{}
- query.Set("access_token", connection.Token)
-
- res, err := apiClient.Get("user", query, nil)
+ res, err := apiClient.Get("user", nil, nil)
if err != nil {
return nil, err
}
diff --git a/backend/plugins/gitee/impl/impl.go
b/backend/plugins/gitee/impl/impl.go
index 1f9a81282..964c34c92 100644
--- a/backend/plugins/gitee/impl/impl.go
+++ b/backend/plugins/gitee/impl/impl.go
@@ -19,6 +19,7 @@ package impl
import (
"fmt"
+
"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/dal"
"github.com/apache/incubator-devlake/core/errors"
@@ -60,7 +61,6 @@ func (p Gitee) GetTablesInfo() []dal.Tabler {
&models.GiteePullRequestLabel{},
&models.GiteeRepo{},
&models.GiteeRepoCommit{},
- &models.GiteeResponse{},
&models.GiteeReviewer{},
}
}
diff --git a/backend/plugins/gitee/models/connection.go
b/backend/plugins/gitee/models/connection.go
index b7c72f846..4d280d4e5 100644
--- a/backend/plugins/gitee/models/connection.go
+++ b/backend/plugins/gitee/models/connection.go
@@ -18,19 +18,32 @@ limitations under the License.
package models
import (
+ "net/http"
+
+ "github.com/apache/incubator-devlake/core/errors"
helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
)
-type GiteeConnection struct {
- helper.BaseConnection `mapstructure:",squash"`
+type GiteeAccessToken helper.AccessToken
+
+// SetupAuthentication sets up the HTTP Request Authentication
+func (gat GiteeAccessToken) SetupAuthentication(req *http.Request)
errors.Error {
+ query := req.URL.Query()
+ query.Set("access_token", gat.Token)
+ req.URL.RawQuery = query.Encode()
+ return nil
+}
+
+// GiteeConn holds the essential information to connect to the Gitee API
+type GiteeConn struct {
helper.RestConnection `mapstructure:",squash"`
- helper.AccessToken `mapstructure:",squash"`
+ GiteeAccessToken `mapstructure:",squash"`
}
-type GiteeResponse struct {
- Name string `json:"name"`
- ID int `json:"id"`
- GiteeConnection
+// GiteeConnection holds GiteeConn plus ID/Name for database storage
+type GiteeConnection struct {
+ helper.BaseConnection `mapstructure:",squash"`
+ GiteeConn `mapstructure:",squash"`
}
type ApiUserResponse struct {
@@ -38,12 +51,6 @@ type ApiUserResponse struct {
Name string `json:"name"`
}
-type TestConnectionRequest struct {
- Endpoint string `json:"endpoint" validate:"required"`
- Proxy string `json:"proxy"`
- helper.AccessToken `mapstructure:",squash"`
-}
-
type TransformationRules struct {
PrType string `mapstructure:"prType" env:"GITEE_PR_TYPE"
json:"prType"`
PrComponent string `mapstructure:"prComponent"
env:"GITEE_PR_COMPONENT" json:"prComponent"`
diff --git a/backend/plugins/gitee/tasks/api_client.go
b/backend/plugins/gitee/tasks/api_client.go
index 76e0c4c5f..adc33d2cd 100644
--- a/backend/plugins/gitee/tasks/api_client.go
+++ b/backend/plugins/gitee/tasks/api_client.go
@@ -18,28 +18,22 @@ limitations under the License.
package tasks
import (
+ "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/gitee/models"
- "net/http"
- "strconv"
- "time"
)
func NewGiteeApiClient(taskCtx plugin.TaskContext, connection
*models.GiteeConnection) (*api.ApiAsyncClient, errors.Error) {
- 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
}
- apiClient.SetBeforeFunction(func(req *http.Request) errors.Error {
- query := req.URL.Query()
- query.Set("access_token", connection.Token)
- req.URL.RawQuery = query.Encode()
- return nil
- })
-
rateLimiter := &api.ApiRateLimitCalculator{
UserRateLimitPerHour: connection.RateLimitPerHour,
DynamicRateLimit: func(res *http.Response) (int, time.Duration,
errors.Error) {