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

klesh pushed a commit to branch kw-6336-tapd-storychangelog
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit d2e8695da6e27cf4ffb3798ad36e4b1ab48ce558
Author: Klesh Wong <[email protected]>
AuthorDate: Thu Oct 26 18:09:00 2023 +0800

    fix: tapd story changelog connecting to record from another connection
---
 backend/helpers/e2ehelper/data_flow_tester.go       | 21 ++++++++++++++++++++-
 backend/plugins/tapd/e2e/story_changelog_test.go    |  7 +++++++
 .../plugins/tapd/tasks/story_changelog_converter.go | 13 +++++++------
 3 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/backend/helpers/e2ehelper/data_flow_tester.go 
b/backend/helpers/e2ehelper/data_flow_tester.go
index 5bc8fac3d..3df847f52 100644
--- a/backend/helpers/e2ehelper/data_flow_tester.go
+++ b/backend/helpers/e2ehelper/data_flow_tester.go
@@ -21,16 +21,18 @@ import (
        "context"
        "database/sql"
        "fmt"
-       "github.com/spf13/cast"
        "io/fs"
        "os"
        "path/filepath"
+       "reflect"
        "strconv"
        "strings"
        "sync"
        "testing"
        "time"
 
+       "github.com/spf13/cast"
+
        "github.com/apache/incubator-devlake/core/config"
        "github.com/apache/incubator-devlake/core/dal"
        "github.com/apache/incubator-devlake/core/errors"
@@ -576,3 +578,20 @@ func (t *DataFlowTester) VerifyTableWithOptions(dst 
schema.Tabler, opts TableOpt
 
        assert.Equal(t.T, expectedRowTotal, actualRowTotal, 
fmt.Sprintf(`records in %s count not match expectation 
count,[expected(CSV):%d][actual(DB):%d]`, dst.TableName(), expectedRowTotal, 
actualRowTotal))
 }
+
+// DuplicateRecords duplicates all records in the specified table and change 
the specified fields to the specified values
+func DuplicateRecords[T dal.Tabler](t *DataFlowTester, fields 
map[string]interface{}) {
+       all := make([]*T, 0)
+       errors.Must(t.Dal.All(&all))
+       for _, row := range all {
+               val := reflect.ValueOf(row)
+               for k, v := range fields {
+                       if f, ok := v.(func() interface{}); ok {
+                               v = f()
+                       }
+                       f1 := val.Elem().FieldByName(k)
+                       f1.Set(reflect.ValueOf(v))
+               }
+       }
+       errors.Must(t.Dal.Create(all))
+}
diff --git a/backend/plugins/tapd/e2e/story_changelog_test.go 
b/backend/plugins/tapd/e2e/story_changelog_test.go
index 22085ae07..dc70b3062 100644
--- a/backend/plugins/tapd/e2e/story_changelog_test.go
+++ b/backend/plugins/tapd/e2e/story_changelog_test.go
@@ -119,6 +119,13 @@ func TestTapdStoryChangelogDataFlow(t *testing.T) {
                ),
        )
 
+       // duplicate existing changelog items to simulate the case that 2 
connections having the same changelog items
+       e2ehelper.DuplicateRecords[models.TapdStoryChangelogItem](
+               dataflowTester,
+               map[string]interface{}{
+                       "ConnectionId": uint64(2),
+               },
+       )
        dataflowTester.FlushTabler(&ticket.IssueChangelogs{})
        dataflowTester.Subtask(tasks.ConvertStoryChangelogMeta, taskData)
        dataflowTester.VerifyTable(
diff --git a/backend/plugins/tapd/tasks/story_changelog_converter.go 
b/backend/plugins/tapd/tasks/story_changelog_converter.go
index eb6701d61..c075d2f59 100644
--- a/backend/plugins/tapd/tasks/story_changelog_converter.go
+++ b/backend/plugins/tapd/tasks/story_changelog_converter.go
@@ -19,6 +19,9 @@ package tasks
 
 import (
        "fmt"
+       "reflect"
+       "time"
+
        "github.com/apache/incubator-devlake/core/dal"
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/models/common"
@@ -28,8 +31,6 @@ import (
        "github.com/apache/incubator-devlake/core/plugin"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "github.com/apache/incubator-devlake/plugins/tapd/models"
-       "reflect"
-       "time"
 )
 
 type StoryChangelogItemResult struct {
@@ -69,10 +70,10 @@ func ConvertStoryChangelog(taskCtx plugin.SubTaskContext) 
errors.Error {
        issueIdGen := didgen.NewDomainIdGenerator(&models.TapdStory{})
        stdTypeMappings := getStdTypeMappings(data)
        clauses := []dal.Clause{
-               dal.Select("tc.created, tc.id, tc.workspace_id, tc.story_id, 
tc.creator, _tool_tapd_story_changelog_items.*"),
-               dal.From(&models.TapdStoryChangelogItem{}),
-               dal.Join("left join _tool_tapd_story_changelogs tc on tc.id = 
_tool_tapd_story_changelog_items.changelog_id "),
-               dal.Where("tc.connection_id = ? AND tc.workspace_id = ?", 
data.Options.ConnectionId, data.Options.WorkspaceId),
+               dal.Select("c.created, c.id, c.workspace_id, c.story_id, 
c.creator, i.*"),
+               dal.From("_tool_tapd_story_changelog_items i"),
+               dal.Join("left join _tool_tapd_story_changelogs c on c.id = 
i.changelog_id and c.connection_id = i.connection_id"),
+               dal.Where("c.connection_id = ? AND c.workspace_id = ?", 
data.Options.ConnectionId, data.Options.WorkspaceId),
                dal.Orderby("created DESC"),
        }
 

Reply via email to