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 2022/08/17 11:01:10 UTC

[incubator-devlake] branch main updated: fix: add getAfterResponse to overwrite by helper (#2755)

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 cd9775bd fix: add getAfterResponse to overwrite by helper (#2755)
cd9775bd is described below

commit cd9775bd5dc82432b2ac704c8ceb121eacf0d2c4
Author: likyh <l...@likyh.com>
AuthorDate: Wed Aug 17 19:01:05 2022 +0800

    fix: add getAfterResponse to overwrite by helper (#2755)
    
    * fix: fix afterResponse callback will overwrite by helper
    
    * fix: fix for ci
    
    Co-authored-by: linyh <ya...@meri.co>
---
 plugins/feishu/tasks/api_client.go   |  9 ---------
 plugins/gitee/tasks/api_client.go    |  7 -------
 plugins/github/tasks/api_client.go   |  6 ------
 plugins/gitlab/tasks/api_client.go   |  6 ------
 plugins/helper/api_async_client.go   |  1 +
 plugins/helper/api_client.go         | 22 ++++++++++++++++------
 plugins/helper/api_collector.go      | 17 +++++++++++------
 plugins/helper/api_collector_test.go |  1 +
 plugins/icla/tasks/api_client.go     | 10 ----------
 plugins/jenkins/tasks/client.go      |  9 ---------
 plugins/jira/tasks/api_client.go     |  6 ------
 11 files changed, 29 insertions(+), 65 deletions(-)

diff --git a/plugins/feishu/tasks/api_client.go b/plugins/feishu/tasks/api_client.go
index f726dc74..b762a609 100644
--- a/plugins/feishu/tasks/api_client.go
+++ b/plugins/feishu/tasks/api_client.go
@@ -19,8 +19,6 @@ package tasks
 
 import (
 	"fmt"
-	"net/http"
-
 	"github.com/apache/incubator-devlake/plugins/feishu/apimodels"
 	"github.com/apache/incubator-devlake/plugins/feishu/models"
 
@@ -65,13 +63,6 @@ func NewFeishuApiClient(taskCtx core.TaskContext, connection *models.FeishuConne
 		"Authorization": fmt.Sprintf("Bearer %v", tokenResBody.TenantAccessToken),
 	})
 
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("feishu authentication failed, please check your AccessToken")
-		}
-		return nil
-	})
-
 	// create async api client
 	asyncApiCLient, err := helper.CreateAsyncApiClient(taskCtx, apiClient, &helper.ApiRateLimitCalculator{
 		UserRateLimitPerHour: connection.RateLimitPerHour,
diff --git a/plugins/gitee/tasks/api_client.go b/plugins/gitee/tasks/api_client.go
index 230f5d60..8449437a 100644
--- a/plugins/gitee/tasks/api_client.go
+++ b/plugins/gitee/tasks/api_client.go
@@ -42,13 +42,6 @@ func NewGiteeApiClient(taskCtx core.TaskContext, connection *models.GiteeConnect
 		return nil
 	})
 
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("authentication failed, please check your Basic Auth Token")
-		}
-		return nil
-	})
-
 	rateLimiter := &helper.ApiRateLimitCalculator{
 		UserRateLimitPerHour: connection.RateLimitPerHour,
 		DynamicRateLimit: func(res *http.Response) (int, time.Duration, error) {
diff --git a/plugins/github/tasks/api_client.go b/plugins/github/tasks/api_client.go
index 326d52c4..131467c3 100644
--- a/plugins/github/tasks/api_client.go
+++ b/plugins/github/tasks/api_client.go
@@ -46,12 +46,6 @@ func CreateApiClient(taskCtx core.TaskContext, connection *models.GithubConnecti
 		tokenIndex = (tokenIndex + 1) % len(tokens)
 		return nil
 	})
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("authentication failed, please check your AccessToken configuration")
-		}
-		return nil
-	})
 
 	// create rate limit calculator
 	rateLimiter := &helper.ApiRateLimitCalculator{
diff --git a/plugins/gitlab/tasks/api_client.go b/plugins/gitlab/tasks/api_client.go
index fe40a4ab..804f2ba4 100644
--- a/plugins/gitlab/tasks/api_client.go
+++ b/plugins/gitlab/tasks/api_client.go
@@ -38,12 +38,6 @@ func NewGitlabApiClient(taskCtx core.TaskContext, connection *models.GitlabConne
 	if err != nil {
 		return nil, err
 	}
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("authentication failed, please check your AccessToken")
-		}
-		return nil
-	})
 
 	// create rate limit calculator
 	rateLimiter := &helper.ApiRateLimitCalculator{
diff --git a/plugins/helper/api_async_client.go b/plugins/helper/api_async_client.go
index d86ac0bb..fe60a825 100644
--- a/plugins/helper/api_async_client.go
+++ b/plugins/helper/api_async_client.go
@@ -239,6 +239,7 @@ type RateLimitedApiClient interface {
 	HasError() bool
 	NextTick(task func() error)
 	GetNumOfWorkers() int
+	GetAfterFunction() common.ApiClientAfterResponse
 	SetAfterFunction(callback common.ApiClientAfterResponse)
 	Release()
 }
diff --git a/plugins/helper/api_client.go b/plugins/helper/api_client.go
index 6bebdb7b..2baf0783 100644
--- a/plugins/helper/api_client.go
+++ b/plugins/helper/api_client.go
@@ -47,7 +47,7 @@ type ApiClient struct {
 	endpoint      string
 	headers       map[string]string
 	beforeRequest common.ApiClientBeforeRequest
-	afterReponse  common.ApiClientAfterResponse
+	afterResponse common.ApiClientAfterResponse
 	ctx           context.Context
 	logger        core.Logger
 }
@@ -146,14 +146,24 @@ func (apiClient *ApiClient) GetHeaders() map[string]string {
 	return apiClient.headers
 }
 
-// SetBeforeFunction FIXME ...
+// GetBeforeFunction return beforeResponseFunction
+func (apiClient *ApiClient) GetBeforeFunction() common.ApiClientBeforeRequest {
+	return apiClient.beforeRequest
+}
+
+// SetBeforeFunction will set beforeResponseFunction
 func (apiClient *ApiClient) SetBeforeFunction(callback common.ApiClientBeforeRequest) {
 	apiClient.beforeRequest = callback
 }
 
-// SetAfterFunction FIXME ...
+// GetAfterFunction return afterResponseFunction
+func (apiClient *ApiClient) GetAfterFunction() common.ApiClientAfterResponse {
+	return apiClient.afterResponse
+}
+
+// SetAfterFunction will set afterResponseFunction
 func (apiClient *ApiClient) SetAfterFunction(callback common.ApiClientAfterResponse) {
-	apiClient.afterReponse = callback
+	apiClient.afterResponse = callback
 }
 
 // SetContext FIXME ...
@@ -250,8 +260,8 @@ func (apiClient *ApiClient) Do(
 	}
 
 	// after receive
-	if apiClient.afterReponse != nil {
-		err = apiClient.afterReponse(res)
+	if apiClient.afterResponse != nil {
+		err = apiClient.afterResponse(res)
 		if err == ErrIgnoreAndContinue {
 			res.Body.Close()
 			return res, err
diff --git a/plugins/helper/api_collector.go b/plugins/helper/api_collector.go
index 60be876f..6a4aaaec 100644
--- a/plugins/helper/api_collector.go
+++ b/plugins/helper/api_collector.go
@@ -109,22 +109,22 @@ func NewApiCollector(args ApiCollectorArgs) (*ApiCollector, error) {
 	if args.ResponseParser == nil {
 		return nil, fmt.Errorf("ResponseParser is required")
 	}
-	apicllector := &ApiCollector{
+	apiCollector := &ApiCollector{
 		RawDataSubTask: rawDataSubTask,
 		args:           &args,
 		urlTemplate:    tpl,
 	}
 	if args.AfterResponse != nil {
-		apicllector.SetAfterResponse(args.AfterResponse)
-	} else {
-		apicllector.SetAfterResponse(func(res *http.Response) error {
+		apiCollector.SetAfterResponse(args.AfterResponse)
+	} else if apiCollector.GetAfterResponse() == nil {
+		apiCollector.SetAfterResponse(func(res *http.Response) error {
 			if res.StatusCode == http.StatusUnauthorized {
 				return fmt.Errorf("authentication failed, please check your AccessToken")
 			}
 			return nil
 		})
 	}
-	return apicllector, nil
+	return apiCollector, nil
 }
 
 // Execute will start collection
@@ -303,7 +303,12 @@ func (collector *ApiCollector) generateUrl(pager *Pager, input interface{}) (str
 	return buf.String(), nil
 }
 
-// SetAfterResponse FIXME ...
+// GetAfterResponse return apiClient's afterResponseFunction
+func (collector *ApiCollector) GetAfterResponse() common.ApiClientAfterResponse {
+	return collector.args.ApiClient.GetAfterFunction()
+}
+
+// SetAfterResponse set apiClient's afterResponseFunction
 func (collector *ApiCollector) SetAfterResponse(f common.ApiClientAfterResponse) {
 	collector.args.ApiClient.SetAfterFunction(f)
 }
diff --git a/plugins/helper/api_collector_test.go b/plugins/helper/api_collector_test.go
index ee9cb961..73055f8a 100644
--- a/plugins/helper/api_collector_test.go
+++ b/plugins/helper/api_collector_test.go
@@ -72,6 +72,7 @@ func TestFetchPageUndetermined(t *testing.T) {
 	}).Twice()
 	mockApi.On("HasError").Return(false)
 	mockApi.On("WaitAsync").Return(nil)
+	mockApi.On("GetAfterFunction", mock.Anything).Return(nil)
 	mockApi.On("SetAfterFunction", mock.Anything).Return()
 	params := struct {
 		Name string
diff --git a/plugins/icla/tasks/api_client.go b/plugins/icla/tasks/api_client.go
index 6e695e2b..664b6a1f 100644
--- a/plugins/icla/tasks/api_client.go
+++ b/plugins/icla/tasks/api_client.go
@@ -19,8 +19,6 @@ package tasks
 
 import (
 	"fmt"
-	"net/http"
-
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/apache/incubator-devlake/plugins/helper"
 	"github.com/apache/incubator-devlake/utils"
@@ -50,14 +48,6 @@ func NewIclaApiClient(taskCtx core.TaskContext) (*helper.ApiAsyncClient, error)
 		"Authorization": fmt.Sprintf("Bearer %v", token),
 	})
 
-	// TODO add some check after request if necessary
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("authentication failed, please check your Bearer Auth Token")
-		}
-		return nil
-	})
-
 	// create async api client
 	asyncApiClient, err := helper.CreateAsyncApiClient(taskCtx, apiClient, &helper.ApiRateLimitCalculator{
 		UserRateLimitPerHour: userRateLimit,
diff --git a/plugins/jenkins/tasks/client.go b/plugins/jenkins/tasks/client.go
index b226b321..b0a98408 100644
--- a/plugins/jenkins/tasks/client.go
+++ b/plugins/jenkins/tasks/client.go
@@ -19,8 +19,6 @@ package tasks
 
 import (
 	"fmt"
-	"net/http"
-
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/apache/incubator-devlake/plugins/helper"
 	"github.com/apache/incubator-devlake/plugins/jenkins/models"
@@ -37,13 +35,6 @@ func CreateApiClient(taskCtx core.TaskContext, connection *models.JenkinsConnect
 		return nil, err
 	}
 
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("authentication failed, please check your Username/Password")
-		}
-		return nil
-	})
-
 	// create rate limit calculator
 	rateLimiter := &helper.ApiRateLimitCalculator{
 		UserRateLimitPerHour: connection.RateLimitPerHour,
diff --git a/plugins/jira/tasks/api_client.go b/plugins/jira/tasks/api_client.go
index 13f94aef..85312afc 100644
--- a/plugins/jira/tasks/api_client.go
+++ b/plugins/jira/tasks/api_client.go
@@ -36,12 +36,6 @@ func NewJiraApiClient(taskCtx core.TaskContext, connection *models.JiraConnectio
 	if err != nil {
 		return nil, err
 	}
-	apiClient.SetAfterFunction(func(res *http.Response) error {
-		if res.StatusCode == http.StatusUnauthorized {
-			return fmt.Errorf("authentication failed, please check your AccessToken")
-		}
-		return nil
-	})
 
 	// create rate limit calculator
 	rateLimiter := &helper.ApiRateLimitCalculator{