You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by wa...@apache.org on 2022/07/18 12:57:08 UTC

[incubator-devlake] branch main updated: feat: revert to check multi token in github plugin

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

warren 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 a242c24b feat: revert to check multi token in github plugin
a242c24b is described below

commit a242c24bfc963572a40852e9fae11024344d3c02
Author: linyh <ya...@meri.co>
AuthorDate: Mon Jul 18 20:33:40 2022 +0800

    feat: revert to check multi token in github plugin
---
 plugins/github/api/connection.go | 87 ++++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 25 deletions(-)

diff --git a/plugins/github/api/connection.go b/plugins/github/api/connection.go
index 5890175d..549e3269 100644
--- a/plugins/github/api/connection.go
+++ b/plugins/github/api/connection.go
@@ -22,6 +22,7 @@ import (
 	"fmt"
 	"github.com/apache/incubator-devlake/api/shared"
 	"net/http"
+	"strings"
 	"time"
 
 	"github.com/apache/incubator-devlake/plugins/github/models"
@@ -61,39 +62,75 @@ func TestConnection(input *core.ApiResourceInput) (*core.ApiResourceOutput, erro
 	if err != nil {
 		return nil, err
 	}
-	apiClient, err := helper.NewApiClient(
-		context.TODO(),
-		params.Endpoint,
-		map[string]string{
-			"Authorization": fmt.Sprintf("Bearer %s", params.Token),
-		},
-		3*time.Second,
-		params.Proxy,
-		basicRes,
-	)
-	if err != nil {
-		return nil, err
+	tokens := strings.Split(params.Token, ",")
+
+	// verify multiple token in parallel
+	type VerifyResult struct {
+		err   error
+		login string
 	}
-	res, err := apiClient.Get("user", nil, nil)
-	if err != nil {
-		return nil, err
+	results := make(chan VerifyResult)
+	for i := 0; i < len(tokens); i++ {
+		token := tokens[i]
+		j := i + 1
+		go func() {
+			apiClient, err := helper.NewApiClient(
+				context.TODO(),
+				params.Endpoint,
+				map[string]string{
+					"Authorization": fmt.Sprintf("Bearer %s", token),
+				},
+				3*time.Second,
+				params.Proxy,
+				basicRes,
+			)
+			if err != nil {
+				results <- VerifyResult{err: fmt.Errorf("verify token failed for #%v %s %w", j, token, err)}
+				return
+			}
+			res, err := apiClient.Get("user", nil, nil)
+			if err != nil {
+				results <- VerifyResult{err: fmt.Errorf("verify token failed for #%v %s %w", j, token, err)}
+				return
+			}
+			githubUserOfToken := &models.GithubUserOfToken{}
+			err = helper.UnmarshalResponse(res, githubUserOfToken)
+			if err != nil {
+				results <- VerifyResult{err: fmt.Errorf("verify token failed for #%v %s %w", j, token, err)}
+				return
+			} else if githubUserOfToken.Login == "" {
+				results <- VerifyResult{err: fmt.Errorf("invalid token for #%v %s", j, token)}
+				return
+			}
+			results <- VerifyResult{login: githubUserOfToken.Login}
+		}()
+	}
+
+	// collect verification results
+	logins := make([]string, 0)
+	msgs := make([]string, 0)
+	i := 0
+	for result := range results {
+		if result.err != nil {
+			msgs = append(msgs, result.err.Error())
+		}
+		logins = append(logins, result.login)
+		i++
+		if i == len(tokens) {
+			close(results)
+		}
 	}
+	if len(msgs) > 0 {
+		return nil, fmt.Errorf(strings.Join(msgs, "\n"))
+	}
+
 	var githubApiResponse struct {
 		shared.ApiBody
 		Login string `json:"login"`
 	}
-	githubUserOfToken := &models.GithubUserOfToken{}
-	err = helper.UnmarshalResponse(res, githubUserOfToken)
-	if err != nil {
-		return nil, err
-	} else if githubUserOfToken.Login == "" {
-		return nil, fmt.Errorf("invalid token")
-	}
 	githubApiResponse.Success = true
 	githubApiResponse.Message = "success"
-	githubApiResponse.Login = githubUserOfToken.Login
-
-	// output
+	githubApiResponse.Login = strings.Join(logins, `,`)
 	return &core.ApiResourceOutput{Body: githubApiResponse, Status: http.StatusOK}, nil
 }