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/11/24 09:30:28 UTC

[incubator-devlake] branch main updated: feat: impl make plan v200 for gitlab (#3793)

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 512857e61 feat: impl make plan v200 for gitlab (#3793)
512857e61 is described below

commit 512857e6114f3d0d50beaa40e8b885959cd2b84b
Author: mappjzc <zh...@merico.dev>
AuthorDate: Thu Nov 24 17:30:24 2022 +0800

    feat: impl make plan v200 for gitlab (#3793)
    
    impl make plan v200 for gitlab
    
    Nddtfjiang <zh...@merico.dev>
---
 plugins/gitlab/api/blueprint.go      |  3 +-
 plugins/gitlab/api/blueprint_v200.go | 80 ++++++++++++++++++++++++++++++++++++
 plugins/gitlab/impl/impl.go          | 26 ++++++++----
 utils/callframes.go                  | 15 +++++++
 4 files changed, 114 insertions(+), 10 deletions(-)

diff --git a/plugins/gitlab/api/blueprint.go b/plugins/gitlab/api/blueprint.go
index d10ffbb38..178fc4fd4 100644
--- a/plugins/gitlab/api/blueprint.go
+++ b/plugins/gitlab/api/blueprint.go
@@ -21,13 +21,14 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
-	"github.com/apache/incubator-devlake/errors"
 	"io"
 	"net/http"
 	"net/url"
 	"strings"
 	"time"
 
+	"github.com/apache/incubator-devlake/errors"
+
 	"github.com/apache/incubator-devlake/models/domainlayer/didgen"
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/apache/incubator-devlake/plugins/gitlab/models"
diff --git a/plugins/gitlab/api/blueprint_v200.go b/plugins/gitlab/api/blueprint_v200.go
new file mode 100644
index 000000000..3109c5994
--- /dev/null
+++ b/plugins/gitlab/api/blueprint_v200.go
@@ -0,0 +1,80 @@
+/*
+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 (
+	"github.com/apache/incubator-devlake/errors"
+	"github.com/go-playground/validator/v10"
+
+	"github.com/apache/incubator-devlake/models/domainlayer/code"
+	"github.com/apache/incubator-devlake/models/domainlayer/didgen"
+	"github.com/apache/incubator-devlake/models/domainlayer/ticket"
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/gitlab/models"
+	"github.com/apache/incubator-devlake/plugins/helper"
+)
+
+func MakeDataSourcePipelinePlanV200(connectionId uint64, scopes []*core.BlueprintScopeV200) (pp core.PipelinePlan, sc []core.Scope, err errors.Error) {
+	pp = make(core.PipelinePlan, 0, 1)
+	sc = make([]core.Scope, 0, 3*len(scopes))
+	err = nil
+
+	connectionHelper := helper.NewConnectionHelper(BasicRes, validator.New())
+
+	// get the connection info for url
+	connection := &models.GitlabConnection{}
+	err = connectionHelper.FirstById(connection, connectionId)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	ps := make(core.PipelineStage, 0, len(scopes))
+	for _, scope := range scopes {
+		var board ticket.Board
+		var repo code.Repo
+
+		id := didgen.NewDomainIdGenerator(&models.GitlabProject{}).Generate(connectionId, scope.Id)
+
+		repo.Id = id
+		repo.Name = scope.Name
+
+		board.Id = id
+		board.Name = scope.Name
+
+		sc = append(sc, &repo)
+		sc = append(sc, &board)
+
+		ps = append(ps, &core.PipelineTask{
+			Plugin: "gitlab",
+			Options: map[string]interface{}{
+				"name": scope.Name,
+			},
+		})
+
+		ps = append(ps, &core.PipelineTask{
+			Plugin: "gitextractor",
+			Options: map[string]interface{}{
+				"url": connection.Endpoint + scope.Name,
+			},
+		})
+	}
+
+	pp = append(pp, ps)
+
+	return pp, sc, nil
+}
diff --git a/plugins/gitlab/impl/impl.go b/plugins/gitlab/impl/impl.go
index 350656e23..388205635 100644
--- a/plugins/gitlab/impl/impl.go
+++ b/plugins/gitlab/impl/impl.go
@@ -21,25 +21,29 @@ import (
 	"fmt"
 
 	"github.com/apache/incubator-devlake/errors"
-
 	"github.com/apache/incubator-devlake/plugins/core"
 	"github.com/apache/incubator-devlake/plugins/gitlab/api"
 	"github.com/apache/incubator-devlake/plugins/gitlab/models"
 	"github.com/apache/incubator-devlake/plugins/gitlab/models/migrationscripts"
 	"github.com/apache/incubator-devlake/plugins/gitlab/tasks"
 	"github.com/apache/incubator-devlake/plugins/helper"
+
 	"github.com/spf13/viper"
 	"gorm.io/gorm"
 )
 
-var _ core.PluginMeta = (*Gitlab)(nil)
-var _ core.PluginInit = (*Gitlab)(nil)
-var _ core.PluginTask = (*Gitlab)(nil)
-var _ core.PluginApi = (*Gitlab)(nil)
-var _ core.PluginModel = (*Gitlab)(nil)
-var _ core.PluginMigration = (*Gitlab)(nil)
-var _ core.PluginBlueprintV100 = (*Gitlab)(nil)
-var _ core.CloseablePluginTask = (*Gitlab)(nil)
+type GitlabImpl interface {
+	core.PluginMeta
+	core.PluginInit
+	core.PluginTask
+	core.PluginModel
+	core.PluginMigration
+	core.PluginBlueprintV100
+	core.DataSourcePluginBlueprintV200
+	core.CloseablePluginTask
+}
+
+var _ GitlabImpl = (*Gitlab)(nil)
 
 type Gitlab string
 
@@ -48,6 +52,10 @@ func (plugin Gitlab) Init(config *viper.Viper, logger core.Logger, db *gorm.DB)
 	return nil
 }
 
+func (plugin Gitlab) MakeDataSourcePipelinePlanV200(connectionId uint64, scopes []*core.BlueprintScopeV200) (pp core.PipelinePlan, sc []core.Scope, err errors.Error) {
+	return api.MakeDataSourcePipelinePlanV200(connectionId, scopes)
+}
+
 func (plugin Gitlab) GetTablesInfo() []core.Tabler {
 	return []core.Tabler{
 		&models.GitlabConnection{},
diff --git a/utils/callframes.go b/utils/callframes.go
index 59e6b251a..db8862cc2 100644
--- a/utils/callframes.go
+++ b/utils/callframes.go
@@ -23,6 +23,21 @@ import (
 	"strings"
 )
 
+// RecoverToError call the recover to catch the panic and changed it to be an error
+func RecoverToError() error {
+	if r := recover(); r != nil {
+		switch e := r.(type) {
+		case error:
+			return e
+		case string:
+			return fmt.Errorf("%s", e)
+		default:
+			return fmt.Errorf("%v", e)
+		}
+	}
+	return nil
+}
+
 // GatherCallFrames FIXME ...
 func GatherCallFrames(delta int) string {
 	var name, file string