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 2023/05/06 01:34:55 UTC

[incubator-devlake] branch release-v0.17 updated: fix: Fix tx rule update endpoint (#5094) (#5109)

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

klesh pushed a commit to branch release-v0.17
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/release-v0.17 by this push:
     new ad2f75bf6 fix: Fix tx rule update endpoint (#5094) (#5109)
ad2f75bf6 is described below

commit ad2f75bf6ad5baae00fdfbb15c892cbc170fdb7d
Author: Hezheng Yin <he...@merico.dev>
AuthorDate: Fri May 5 18:34:50 2023 -0700

    fix: Fix tx rule update endpoint (#5094) (#5109)
    
    Decoding the body should support conversion of string to time.Time.
    
    Co-authored-by: Camille Teruel <ca...@gmail.com>
    Co-authored-by: Camille Teruel <ca...@meri.co>
---
 backend/helpers/pluginhelper/api/mapstructure.go   |  1 +
 .../remote/plugin/transformation_rule_api.go       |  9 +++-
 backend/test/e2e/remote/python_plugin_test.go      | 50 +++++++++++++++++-----
 backend/test/helper/api.go                         | 23 ++++++++--
 4 files changed, 67 insertions(+), 16 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/mapstructure.go b/backend/helpers/pluginhelper/api/mapstructure.go
index ba93a5c84..3d217ed0b 100644
--- a/backend/helpers/pluginhelper/api/mapstructure.go
+++ b/backend/helpers/pluginhelper/api/mapstructure.go
@@ -64,6 +64,7 @@ func DecodeHook(f reflect.Type, t reflect.Type, data interface{}) (interface{},
 
 // DecodeMapStruct with time.Time and Iso8601Time support
 func DecodeMapStruct(input map[string]interface{}, result interface{}, zeroFields bool) errors.Error {
+	result = models.UnwrapObject(result)
 	decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
 		ZeroFields: zeroFields,
 		DecodeHook: mapstructure.ComposeDecodeHookFunc(DecodeHook),
diff --git a/backend/server/services/remote/plugin/transformation_rule_api.go b/backend/server/services/remote/plugin/transformation_rule_api.go
index 8a84beda1..a7a1a0d9b 100644
--- a/backend/server/services/remote/plugin/transformation_rule_api.go
+++ b/backend/server/services/remote/plugin/transformation_rule_api.go
@@ -34,7 +34,7 @@ func (pa *pluginAPI) PostTransformationRules(input *plugin.ApiResourceInput) (*p
 	}
 	txRule := pa.txRuleType.New()
 	input.Body[`connectionId`] = connectionId
-	err := api.Decode(input.Body, txRule, vld)
+	err := api.DecodeMapStruct(input.Body, txRule, false)
 	if err != nil {
 		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
 	}
@@ -60,11 +60,16 @@ func (pa *pluginAPI) PatchTransformationRule(input *plugin.ApiResourceInput) (*p
 	}
 
 	input.Body[`connectionId`] = connectionId
-	err = api.Decode(input.Body, txRule, vld)
+	input.Body[`id`] = trId
+	err = api.DecodeMapStruct(input.Body, txRule, false)
 	if err != nil {
 		return nil, errors.Default.Wrap(err, "decoding error")
 	}
 
+	err = api.CallDB(db.Update, txRule)
+	if err != nil {
+		return nil, err
+	}
 	return &plugin.ApiResourceOutput{Body: txRule.Unwrap(), Status: http.StatusOK}, nil
 }
 
diff --git a/backend/test/e2e/remote/python_plugin_test.go b/backend/test/e2e/remote/python_plugin_test.go
index d8d74e56e..8c7d385e4 100644
--- a/backend/test/e2e/remote/python_plugin_test.go
+++ b/backend/test/e2e/remote/python_plugin_test.go
@@ -73,11 +73,11 @@ func TestRemoteScopes(t *testing.T) {
 	require.Equal(t, "group1", *scope.ParentId)
 	require.Equal(t, "scope", scope.Type)
 	require.NotNil(t, scope.Data)
-	data := scope.Data.(map[string]interface{})
-	require.Equal(t, float64(connection.ID), data["connectionId"])
-	require.Equal(t, "p1", data["id"])
-	require.Equal(t, "Project 1", data["name"])
-	require.Equal(t, "http://fake.org/api/project/p1", data["url"])
+	cicdScope := helper.Cast[FakeProject](scope.Data)
+	require.Equal(t, connection.ID, cicdScope.ConnectionId)
+	require.Equal(t, "p1", cicdScope.Id)
+	require.Equal(t, "Project 1", cicdScope.Name)
+	require.Equal(t, "http://fake.org/api/project/p1", cicdScope.Url)
 }
 
 func TestCreateScope(t *testing.T) {
@@ -88,11 +88,11 @@ func TestCreateScope(t *testing.T) {
 
 	scopes := client.ListScopes(PLUGIN_NAME, connectionId)
 	require.Equal(t, 1, len(scopes))
-	cicd_scope := scopes[0].(map[string]interface{})
-	require.Equal(t, float64(connectionId), cicd_scope["connectionId"])
-	require.Equal(t, "p1", cicd_scope["id"])
-	require.Equal(t, "Project 1", cicd_scope["name"])
-	require.Equal(t, "http://fake.org/api/project/p1", cicd_scope["url"])
+	cicd_scope := helper.Cast[FakeProject](scopes[0])
+	require.Equal(t, connectionId, cicd_scope.ConnectionId)
+	require.Equal(t, "p1", cicd_scope.Id)
+	require.Equal(t, "Project 1", cicd_scope.Name)
+	require.Equal(t, "http://fake.org/api/project/p1", cicd_scope.Url)
 }
 
 func TestRunPipeline(t *testing.T) {
@@ -158,5 +158,33 @@ func TestBlueprintV200(t *testing.T) {
 
 	project := client.GetProject(projectName)
 	require.Equal(t, blueprint.Name, project.Blueprint.Name)
-	client.TriggerBlueprint(blueprint.ID)
+	pipeline := client.TriggerBlueprint(blueprint.ID)
+	require.Equal(t, pipeline.Status, models.TASK_COMPLETED)
+}
+
+func TestCreateTxRule(t *testing.T) {
+	client := CreateClient(t)
+	connection := CreateTestConnection(client)
+
+	res := client.CreateTransformationRule(PLUGIN_NAME, connection.ID, FakeTxRule{Name: "Tx rule", Env: "test env"})
+	txRule := helper.Cast[FakeTxRule](res)
+
+	res = client.GetTransformationRule(PLUGIN_NAME, connection.ID, txRule.Id)
+	txRule = helper.Cast[FakeTxRule](res)
+	require.Equal(t, "Tx rule", txRule.Name)
+	require.Equal(t, "test env", txRule.Env)
+}
+
+func TestUpdateTxRule(t *testing.T) {
+	client := CreateClient(t)
+	connection := CreateTestConnection(client)
+	res := client.CreateTransformationRule(PLUGIN_NAME, connection.ID, FakeTxRule{Name: "old name", Env: "old env"})
+	oldTxRule := helper.Cast[FakeTxRule](res)
+
+	client.PatchTransformationRule(PLUGIN_NAME, connection.ID, oldTxRule.Id, FakeTxRule{Name: "new name", Env: "new env"})
+
+	res = client.GetTransformationRule(PLUGIN_NAME, connection.ID, oldTxRule.Id)
+	txRule := helper.Cast[FakeTxRule](res)
+	require.Equal(t, "new name", txRule.Name)
+	require.Equal(t, "new env", txRule.Env)
 }
diff --git a/backend/test/helper/api.go b/backend/test/helper/api.go
index 5100fc737..9c8fb18c0 100644
--- a/backend/test/helper/api.go
+++ b/backend/test/helper/api.go
@@ -19,14 +19,15 @@ package helper
 
 import (
 	"fmt"
+	"net/http"
+	"reflect"
+	"strings"
+
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/models"
 	"github.com/apache/incubator-devlake/core/plugin"
 	apiProject "github.com/apache/incubator-devlake/server/api/project"
 	"github.com/stretchr/testify/require"
-	"net/http"
-	"reflect"
-	"strings"
 )
 
 // CreateConnection FIXME
@@ -173,6 +174,14 @@ func (d *DevlakeClient) CreateTransformationRule(pluginName string, connectionId
 		d.Endpoint, pluginName, connectionId), nil, rules)
 }
 
+func (d *DevlakeClient) PatchTransformationRule(pluginName string, connectionId uint64, txRuleId uint64, rule any) any {
+	return sendHttpRequest[any](d.testCtx, d.timeout, debugInfo{
+		print:      true,
+		inlineJson: false,
+	}, http.MethodPatch, fmt.Sprintf("%s/plugins/%s/connections/%d/transformation_rules/%d",
+		d.Endpoint, pluginName, connectionId, txRuleId), nil, rule)
+}
+
 func (d *DevlakeClient) ListTransformationRules(pluginName string, connectionId uint64) []any {
 	return sendHttpRequest[[]any](d.testCtx, d.timeout, debugInfo{
 		print:      true,
@@ -181,6 +190,14 @@ func (d *DevlakeClient) ListTransformationRules(pluginName string, connectionId
 		d.Endpoint, pluginName, connectionId), nil, nil)
 }
 
+func (d *DevlakeClient) GetTransformationRule(pluginName string, connectionId uint64, txRuleId uint64) any {
+	return sendHttpRequest[any](d.testCtx, d.timeout, debugInfo{
+		print:      true,
+		inlineJson: false,
+	}, http.MethodGet, fmt.Sprintf("%s/plugins/%s/connections/%d/transformation_rules/%d",
+		d.Endpoint, pluginName, connectionId, txRuleId), nil, nil)
+}
+
 func (d *DevlakeClient) RemoteScopes(query RemoteScopesQuery) RemoteScopesOutput {
 	url := fmt.Sprintf("%s/plugins/%s/connections/%d/remote-scopes",
 		d.Endpoint,