You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by kl...@apache.org on 2022/08/10 01:54:01 UTC

[incubator-devlake] branch main updated: feat(gitlab): add projects api (#2705)

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

klesh 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 256b1776 feat(gitlab): add projects api (#2705)
256b1776 is described below

commit 256b1776095930988e39d9c4bd3f8a474f161b63
Author: Warren Chen <yi...@merico.dev>
AuthorDate: Wed Aug 10 09:53:58 2022 +0800

    feat(gitlab): add projects api (#2705)
    
    relate to #2376
---
 plugins/gitlab/api/proxy.go | 73 +++++++++++++++++++++++++++++++++++++++++++++
 plugins/gitlab/impl/impl.go |  3 ++
 2 files changed, 76 insertions(+)

diff --git a/plugins/gitlab/api/proxy.go b/plugins/gitlab/api/proxy.go
new file mode 100644
index 00000000..6271fd9e
--- /dev/null
+++ b/plugins/gitlab/api/proxy.go
@@ -0,0 +1,73 @@
+/*
+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"
+	"io/ioutil"
+	"time"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/gitlab/models"
+	"github.com/apache/incubator-devlake/plugins/helper"
+)
+
+const (
+	TimeOut = 10 * time.Second
+)
+
+func Proxy(input *core.ApiResourceInput) (*core.ApiResourceOutput, error) {
+	connection := &models.GitlabConnection{}
+	err := connectionHelper.First(connection, input.Params)
+	if err != nil {
+		return nil, err
+	}
+	apiClient, err := helper.NewApiClient(
+		context.TODO(),
+		connection.Endpoint,
+		map[string]string{
+			"Authorization": fmt.Sprintf("Bearer %v", connection.Token),
+		},
+		30*time.Second,
+		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 := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+	// verify response body is json
+	var tmp interface{}
+	err = 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/gitlab/impl/impl.go b/plugins/gitlab/impl/impl.go
index 4b5e8de8..c3c67fe2 100644
--- a/plugins/gitlab/impl/impl.go
+++ b/plugins/gitlab/impl/impl.go
@@ -141,6 +141,9 @@ func (plugin Gitlab) ApiResources() map[string]map[string]core.ApiResourceHandle
 			"DELETE": api.DeleteConnection,
 			"GET":    api.GetConnection,
 		},
+		"connections/:connectionId/proxy/rest/*path": {
+			"GET": api.Proxy,
+		},
 	}
 }