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 05:59:31 UTC

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

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


##########
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) {

Review Comment:
   MakePipelinePlan should have a default implementation to keep plugin development simple.



##########
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)

Review Comment:
   if `op` just use in `if utils.StringsContains(scopeElem.Entities, core.DOMAIN_TYPE_CODE) {`, it should defined in `if` statement.



##########
plugins/github/impl/impl.go:
##########
@@ -174,3 +134,7 @@ func (plugin Github) ApiResources() map[string]map[string]core.ApiResourceHandle
 		},
 	}
 }
+
+func (plugin Github) MakePipelinePlan(connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, error) {

Review Comment:
   maybe connectionId is not used in some plugins?



##########
plugins/github/impl/impl.go:
##########
@@ -174,3 +134,7 @@ func (plugin Github) ApiResources() map[string]map[string]core.ApiResourceHandle
 		},
 	}
 }
+
+func (plugin Github) MakePipelinePlan(connectionId uint64, scope []*core.BlueprintScopeV100) (core.PipelinePlan, error) {

Review Comment:
   what the `entity` in `BlueprintScopeV100` means?



##########
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:
   If I start the github & the ae plugin together and they both add `gitextractor`, will `gitextractor` be duplicated?



##########
plugins/github/tasks/comment_extractor.go:
##########
@@ -33,6 +34,7 @@ var ExtractApiCommentsMeta = core.SubTaskMeta{
 	EnabledByDefault: true,
 	Description: "Extract raw comment data  into tool layer table github_pull_request_comments" +
 		"and github_issue_comments",
+	DomainTypes: []string{core.DOMAIN_TYPE_CODE, core.DOMAIN_TYPE_TICKET},

Review Comment:
   how can some plugins use without DomainTypes?



##########
plugins/helper/pipeline_plan.go:
##########
@@ -0,0 +1,49 @@
+/*
+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 helper
+
+import (
+	"fmt"
+
+	"github.com/apache/incubator-devlake/plugins/core"
+	"github.com/apache/incubator-devlake/utils"
+)
+
+// MakePipelinePlanSubtasks generates subtasks list based on sub-task meta information and entities wanted by user
+func MakePipelinePlanSubtasks(subtaskMetas []core.SubTaskMeta, entities []string) ([]string, error) {

Review Comment:
   It's confusing between running all tasks and running no task 



##########
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:
   why gitextractor should be added by the GitHub plugin instead of defined in the plan?



-- 
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