klesh commented on code in PR #4896:
URL: 
https://github.com/apache/incubator-devlake/pull/4896#discussion_r1220750630


##########
backend/helpers/pluginhelper/subTaskMetaSorter/interface.go:
##########
@@ -0,0 +1,24 @@
+/*
+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 subTaskMetaSorter
+
+import "github.com/apache/incubator-devlake/core/plugin"
+
+type SubTaskMetaSorter interface {
+       Sort() ([]plugin.SubTaskMeta, error)

Review Comment:
   Pointer would be better



##########
backend/plugins/gitlab/tasks/register_test.go:
##########
@@ -0,0 +1,81 @@
+/*

Review Comment:
   The unit test case should be testing the Algo instead of Actual Biz Logic, 
do NOT re-use the actual dependency from a plugin, or we have to update the 
test case whenever we add new subtasks.
   Please move this test case `plugin_helper` alongside `DependencySorter` and 
fabricate some fake dependency to verify the Algo is working correctly.
   



##########
backend/helpers/pluginhelper/subTaskMetaSorter/dependency.go:
##########
@@ -0,0 +1,118 @@
+/*
+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 subTaskMetaSorter
+
+import (
+       "fmt"
+       "github.com/apache/incubator-devlake/core/plugin"
+       "sort"
+)
+
+type DependencySorter struct {
+       metas []*plugin.SubTaskMeta
+}
+
+func GetDependencySorter(metas []*plugin.SubTaskMeta) SubTaskMetaSorter {
+       return &DependencySorter{metas: metas}
+}
+
+func (d *DependencySorter) Sort() ([]plugin.SubTaskMeta, error) {
+       return topologicalSort(d.metas)
+}
+
+// stable topological sort
+func topologicalSort(metas []*plugin.SubTaskMeta) ([]plugin.SubTaskMeta, 
error) {
+       // which state will make a cycle
+       dependenciesMap := make(map[string][]string)
+       nameMetaMap := make(map[string]plugin.SubTaskMeta)
+
+       for _, item := range metas {
+               if item != nil {
+                       nameMetaMap[item.Name] = *item
+               }
+
+               if _, ok := dependenciesMap[item.Name]; !ok {
+                       if len(item.Dependencies) != 0 {
+                               dependenciesMap[item.Name] = make([]string, 0)
+                               for _, dependencyItem := range 
item.Dependencies {
+                                       dependenciesMap[item.Name] = 
append(dependenciesMap[item.Name], dependencyItem.Name)
+                               }
+                       } else {
+                               dependenciesMap[item.Name] = make([]string, 0)
+                       }
+               } else {
+                       return nil, fmt.Errorf("got duplicate subtaskmeta in 
list")
+               }
+       }
+
+       orderedSubtaskList := make([]plugin.SubTaskMeta, 0)
+       for {
+               if len(dependenciesMap) == 0 {
+                       break
+               }
+
+               tmpList := make([]string, 0)
+               for key, item := range dependenciesMap {
+                       if len(item) == 0 {
+                               tmpList = append(tmpList, key)
+                       }
+               }
+               if len(tmpList) == 0 {
+                       return nil, fmt.Errorf("got error dependency map %v, 
please check if exist loop", dependenciesMap)

Review Comment:
   Suggested Error message:  cyclic dependency detected



##########
backend/helpers/pluginhelper/subTaskMetaSorter/dependency.go:
##########
@@ -0,0 +1,118 @@
+/*
+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 subTaskMetaSorter
+
+import (
+       "fmt"
+       "github.com/apache/incubator-devlake/core/plugin"
+       "sort"
+)
+
+type DependencySorter struct {
+       metas []*plugin.SubTaskMeta
+}
+
+func GetDependencySorter(metas []*plugin.SubTaskMeta) SubTaskMetaSorter {
+       return &DependencySorter{metas: metas}
+}
+
+func (d *DependencySorter) Sort() ([]plugin.SubTaskMeta, error) {
+       return topologicalSort(d.metas)
+}
+
+// stable topological sort
+func topologicalSort(metas []*plugin.SubTaskMeta) ([]plugin.SubTaskMeta, 
error) {
+       // which state will make a cycle
+       dependenciesMap := make(map[string][]string)
+       nameMetaMap := make(map[string]plugin.SubTaskMeta)

Review Comment:
   It would be better to use pointer `*plugin.SubTaskMeta` to avoid memory copy



##########
backend/helpers/pluginhelper/subTaskMetaSorter/dependency.go:
##########
@@ -0,0 +1,118 @@
+/*
+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 subTaskMetaSorter
+
+import (
+       "fmt"
+       "github.com/apache/incubator-devlake/core/plugin"
+       "sort"
+)
+
+type DependencySorter struct {
+       metas []*plugin.SubTaskMeta
+}
+
+func GetDependencySorter(metas []*plugin.SubTaskMeta) SubTaskMetaSorter {

Review Comment:
   `NewDependencySorter` would be better since the function creates a new 
instance on each call.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to