You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by GitBox <gi...@apache.org> on 2022/06/23 06:20:08 UTC

[GitHub] [incubator-devlake] warren830 commented on a diff in pull request #2331: Github blueprint normal mode support

warren830 commented on code in PR #2331:
URL: https://github.com/apache/incubator-devlake/pull/2331#discussion_r904621435


##########
plugins/github/api/blueprint.go:
##########
@@ -0,0 +1,133 @@
+/*
+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 (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"strings"
+	"time"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/plugins/core/dal"
+	"github.com/apache/incubator-devlake/plugins/github/models"
+	"github.com/apache/incubator-devlake/plugins/github/tasks"
+	"github.com/apache/incubator-devlake/plugins/helper"
+	"github.com/apache/incubator-devlake/utils"
+)
+
+func MakePipelinePlan(subtaskMetas []core.SubTaskMeta, connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, error) {
+	var err error
+	plan := make(core.PipelinePlan, len(scope))
+	for i, scopeElem := range scope {
+		// handle taskOptions and transformationRules, by dumping them to taskOptions
+		taskOptions := make(map[string]interface{})
+		err = json.Unmarshal(scopeElem.Options, &taskOptions)
+		if err != nil {
+			return nil, err
+		}
+		err = json.Unmarshal(scopeElem.Transformation, &taskOptions)
+		if err != nil {
+			return nil, err
+		}
+		taskOptions["connectionId"] = connectionId
+		op, err := tasks.DecodeAndValidateTaskOptions(taskOptions)
+		if err != nil {
+			return nil, err
+		}
+		// subtasks
+		subtasks, err := helper.MakePipelinePlanSubtasks(subtaskMetas, scopeElem.Entities)
+		if err != nil {
+			return nil, err
+		}
+		stage := core.PipelineStage{
+			{
+				Plugin:   "github",
+				Subtasks: subtasks,
+				Options:  taskOptions,
+			},
+		}
+		// collect git data by gitextractor if CODE was requested
+		if utils.StringsContains(scopeElem.Entities, core.DOMAIN_TYPE_CODE) {
+			// here is the tricky part, we have to obtain the repo id beforehand
+			repoId := 0
+			//   first, try to get from db?
+			githubRepo := new(models.GithubRepo)
+			err = basicRes.GetDal().First(
+				githubRepo,
+				dal.Where(
+					"connection_id = ? AND owner_login = ? AND name = ?",
+					connectionId, op.Owner, op.Repo,
+				),
+			)
+			if err == nil {
+				repoId = githubRepo.GithubId
+			}
+			// no luck, fetch it from api
+			if err == dal.ErrRecordNotFound {
+				connection := new(models.GithubConnection)
+				err = connectionHelper.FirstById(connection, connectionId)
+				if err != nil {
+					return nil, err
+				}
+				token := strings.Split(connection.Token, ",")[0]
+				apiClient, err := helper.NewApiClient(
+					connection.Endpoint,
+					map[string]string{
+						"Authorization": fmt.Sprintf("Bearer %s", token),
+					},
+					10*time.Second,
+					connection.Proxy,
+					nil,
+				)
+				if err != nil {
+					return nil, err
+				}
+				res, err := apiClient.Get(fmt.Sprintf("repos/%s/%s", op.Owner, op.Repo), nil, nil)
+				if err != nil {
+					return nil, err
+				}
+				defer res.Body.Close()
+				body, err := ioutil.ReadAll(res.Body)
+				if err != nil {
+					return nil, err
+				}
+				apiRepo := new(tasks.GithubApiRepo)
+				err = json.Unmarshal(body, apiRepo)
+				if err != nil {
+					return nil, err
+				}
+				repoId = apiRepo.GithubId
+			}
+			stage = append(stage, &core.PipelineTask{
+				Plugin: "gitextractor",
+				Options: map[string]interface{}{
+					// TODO: url should be configuration
+					// TODO: to support private repo: username is needed for repo cloning, and we have to take
+					//       multi-token support into consideration, this is hairy
+					"url":    fmt.Sprintf("https://github.com/%s/%s.git", op.Owner, op.Repo),
+					"repoId": fmt.Sprintf("github:GithubRepo:%d", repoId),
+				},
+			})
+			// TODO, add refdiff in the future

Review Comment:
   > 
   
   I think only github and gitlab need gitex to collect commits and refs 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@devlake.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org