You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by li...@apache.org on 2022/11/16 03:35:25 UTC

[incubator-devlake] branch main updated: feat: add proxy api for github (#3743)

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 91f8dd74d feat: add proxy api for github (#3743)
91f8dd74d is described below

commit 91f8dd74d0e0a81e3165fc70be82c794ee4a51fa
Author: Likyh <ya...@meri.co>
AuthorDate: Wed Nov 16 11:35:21 2022 +0800

    feat: add proxy api for github (#3743)
---
 plugins/github/api/proxy.go | 76 +++++++++++++++++++++++++++++++++++++++++++++
 plugins/github/impl/impl.go |  3 ++
 2 files changed, 79 insertions(+)

diff --git a/plugins/github/api/proxy.go b/plugins/github/api/proxy.go
new file mode 100644
index 000000000..c9023c946
--- /dev/null
+++ b/plugins/github/api/proxy.go
@@ -0,0 +1,76 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package api
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"github.com/apache/incubator-devlake/errors"
+	"io"
+	"strings"
+	"time"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/github/models"
+	"github.com/apache/incubator-devlake/plugins/helper"
+)
+
+const (
+	TimeOut = 10 * time.Second
+)
+
+func Proxy(input *core.ApiResourceInput) (*core.ApiResourceOutput, errors.Error) {
+	connection := &models.GithubConnection{}
+	err := connectionHelper.First(connection, input.Params)
+	if err != nil {
+		return nil, err
+	}
+	tokens := strings.Split(connection.Token, ",")
+	apiClient, err := helper.NewApiClient(
+		context.TODO(),
+		connection.Endpoint,
+		map[string]string{
+			"Authorization": fmt.Sprintf("Bearer %s", tokens[0]),
+		},
+		TimeOut,
+		connection.Proxy,
+		basicRes,
+	)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := apiClient.Get(input.Params["path"], input.Query, nil)
+	if err != nil {
+		return nil, err
+	}
+	defer resp.Body.Close()
+
+	body, err := errors.Convert01(io.ReadAll(resp.Body))
+	if err != nil {
+		return nil, err
+	}
+	// verify response body is json
+	var tmp interface{}
+	err = errors.Convert(json.Unmarshal(body, &tmp))
+	if err != nil {
+		return nil, err
+	}
+	return &core.ApiResourceOutput{Status: resp.StatusCode, Body: json.RawMessage(body)}, nil
+}
diff --git a/plugins/github/impl/impl.go b/plugins/github/impl/impl.go
index 84f7a3339..3091e085e 100644
--- a/plugins/github/impl/impl.go
+++ b/plugins/github/impl/impl.go
@@ -191,6 +191,9 @@ func (plugin Github) ApiResources() map[string]map[string]core.ApiResourceHandle
 			"PATCH":  api.PatchConnection,
 			"DELETE": api.DeleteConnection,
 		},
+		"connections/:connectionId/proxy/rest/*path": {
+			"GET": api.Proxy,
+		},
 	}
 }