You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by zh...@apache.org on 2023/03/22 12:59:27 UTC

[incubator-devlake] branch main updated: refactor(framework): refactor trans rule helper (#4743)

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

zhangliang2022 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new ca56601e6 refactor(framework): refactor trans rule helper (#4743)
ca56601e6 is described below

commit ca56601e658041c61252b4c2df1d4f02aa4f0a96
Author: Warren Chen <yi...@merico.dev>
AuthorDate: Wed Mar 22 20:59:22 2023 +0800

    refactor(framework): refactor trans rule helper (#4743)
---
 backend/helpers/pluginhelper/api/scope_helper.go   |   4 +-
 .../pluginhelper/api/transformation_rule_helper.go | 121 +++++++++++++++++++++
 backend/plugins/bamboo/api/init.go                 |   5 +
 backend/plugins/bamboo/api/transformation_rule.go  |  61 +----------
 backend/plugins/bitbucket/api/init.go              |   5 +
 .../plugins/bitbucket/api/transformation_rule.go   |  61 +----------
 backend/plugins/github/api/init.go                 |   5 +
 backend/plugins/github/api/transformation_rule.go  |  61 +----------
 backend/plugins/gitlab/api/init.go                 |   5 +
 backend/plugins/gitlab/api/transformation_rule.go  |  61 +----------
 backend/plugins/jenkins/api/init.go                |   5 +
 backend/plugins/jenkins/api/transformation_rule.go |  61 +----------
 backend/plugins/jira/api/init.go                   |   6 +
 backend/plugins/jira/api/transformation_rule.go    |  64 ++++-------
 .../plugins/jira/models/transformation_rules.go    |   4 +-
 15 files changed, 196 insertions(+), 333 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/scope_helper.go b/backend/helpers/pluginhelper/api/scope_helper.go
index e0eaee3e6..8280e0e1d 100644
--- a/backend/helpers/pluginhelper/api/scope_helper.go
+++ b/backend/helpers/pluginhelper/api/scope_helper.go
@@ -37,7 +37,7 @@ import (
 	"reflect"
 )
 
-// ScopeApiHelper is used to write the CURD of connection
+// ScopeApiHelper is used to write the CURD of scopes
 type ScopeApiHelper[Conn any, Scope any, Tr any] struct {
 	log        log.Logger
 	db         dal.Dal
@@ -45,7 +45,7 @@ type ScopeApiHelper[Conn any, Scope any, Tr any] struct {
 	connHelper *ConnectionApiHelper
 }
 
-// NewScopeHelper creates a ScopeHelper for connection management
+// NewScopeHelper creates a ScopeHelper for scopes management
 func NewScopeHelper[Conn any, Scope any, Tr any](
 	basicRes context.BasicRes,
 	vld *validator.Validate,
diff --git a/backend/helpers/pluginhelper/api/transformation_rule_helper.go b/backend/helpers/pluginhelper/api/transformation_rule_helper.go
new file mode 100644
index 000000000..a819c10b7
--- /dev/null
+++ b/backend/helpers/pluginhelper/api/transformation_rule_helper.go
@@ -0,0 +1,121 @@
+/*
+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 (
+	"github.com/apache/incubator-devlake/core/context"
+	"github.com/apache/incubator-devlake/core/dal"
+	"github.com/apache/incubator-devlake/core/errors"
+	"github.com/apache/incubator-devlake/core/log"
+	"github.com/apache/incubator-devlake/core/models/common"
+	"github.com/apache/incubator-devlake/core/plugin"
+	"github.com/go-playground/validator/v10"
+	"github.com/mitchellh/mapstructure"
+	"net/http"
+	"strconv"
+)
+
+type AbstractTr interface {
+	dal.Tabler
+	common.Model
+}
+
+// TransformationRuleHelper is used to write the CURD of transformation rule
+type TransformationRuleHelper[Tr dal.Tabler] struct {
+	log       log.Logger
+	db        dal.Dal
+	validator *validator.Validate
+}
+
+// NewTransformationRuleHelper creates a TransformationRuleHelper for transformation rule management
+func NewTransformationRuleHelper[Tr dal.Tabler](
+	basicRes context.BasicRes,
+	vld *validator.Validate,
+) *TransformationRuleHelper[Tr] {
+	if vld == nil {
+		vld = validator.New()
+	}
+	return &TransformationRuleHelper[Tr]{
+		log:       basicRes.GetLogger(),
+		db:        basicRes.GetDal(),
+		validator: vld,
+	}
+}
+
+func (t TransformationRuleHelper[Tr]) Create(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
+	var rule Tr
+	err := Decode(input.Body, &rule, t.validator)
+	if err != nil {
+		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
+	}
+	err = t.db.Create(&rule)
+	if err != nil {
+		if t.db.IsDuplicationError(err) {
+			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
+		}
+		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
+	}
+	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+}
+
+func (t TransformationRuleHelper[Tr]) Update(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
+	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
+	if e != nil {
+		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
+	}
+	var old Tr
+	err := t.db.First(&old, dal.Where("id = ?", transformationRuleId))
+	if err != nil {
+		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
+	}
+	err = errors.Convert(mapstructure.Decode(input.Body, &old))
+	if err != nil {
+		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
+	}
+	err = t.db.Update(&old, dal.Where("id = ?", transformationRuleId))
+	if err != nil {
+		if t.db.IsDuplicationError(err) {
+			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
+		}
+		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
+	}
+	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+}
+
+func (t TransformationRuleHelper[Tr]) Get(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
+	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
+	if err != nil {
+		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
+	}
+	var rule Tr
+	err = t.db.First(&rule, dal.Where("id = ?", transformationRuleId))
+	if err != nil {
+		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
+	}
+	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+}
+
+func (t TransformationRuleHelper[Tr]) List(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
+	var rules []Tr
+	limit, offset := GetLimitOffset(input.Query, "pageSize", "page")
+	err := t.db.All(&rules, dal.Limit(limit), dal.Offset(offset))
+	if err != nil {
+		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
+	}
+	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+}
diff --git a/backend/plugins/bamboo/api/init.go b/backend/plugins/bamboo/api/init.go
index e70961d82..71b3bd589 100644
--- a/backend/plugins/bamboo/api/init.go
+++ b/backend/plugins/bamboo/api/init.go
@@ -28,6 +28,7 @@ var vld *validator.Validate
 var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.BambooConnection, models.BambooProject, models.BambooTransformationRule]
 var remoteHelper *api.RemoteApiHelper[models.BambooConnection, models.BambooProject, models.ApiBambooProject, api.NoRemoteGroupResponse]
+var trHelper *api.TransformationRuleHelper[models.BambooTransformationRule]
 
 var basicRes context.BasicRes
 
@@ -48,4 +49,8 @@ func Init(br context.BasicRes) {
 		vld,
 		connectionHelper,
 	)
+	trHelper = api.NewTransformationRuleHelper[models.BambooTransformationRule](
+		basicRes,
+		vld,
+	)
 }
diff --git a/backend/plugins/bamboo/api/transformation_rule.go b/backend/plugins/bamboo/api/transformation_rule.go
index d2115d86d..8e833e9c2 100644
--- a/backend/plugins/bamboo/api/transformation_rule.go
+++ b/backend/plugins/bamboo/api/transformation_rule.go
@@ -18,13 +18,8 @@ limitations under the License.
 package api
 
 import (
-	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
-	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
-	"github.com/apache/incubator-devlake/plugins/bamboo/models"
-	"net/http"
-	"strconv"
 )
 
 // CreateTransformationRule create transformation rule for Bamboo
@@ -38,19 +33,7 @@ import (
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bamboo/transformation_rules [POST]
 func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rule models.BambooTransformationRule
-	err := api.Decode(input.Body, &rule, vld)
-	if err != nil {
-		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
-	}
-	err = basicRes.GetDal().Create(&rule)
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Create(input)
 }
 
 // UpdateTransformationRule update transformation rule for Bamboo
@@ -65,28 +48,7 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bamboo/transformation_rules/{id} [PATCH]
 func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
-	if e != nil {
-		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
-	}
-	var old models.BambooTransformationRule
-	err := basicRes.GetDal().First(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
-	}
-	err = api.DecodeMapStruct(input.Body, &old)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
-	}
-	old.ID = transformationRuleId
-	err = basicRes.GetDal().Update(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+	return trHelper.Update(input)
 }
 
 // GetTransformationRule return one transformation rule
@@ -99,16 +61,7 @@ func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bamboo/transformation_rules/{id} [GET]
 func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
-	}
-	var rule models.BambooTransformationRule
-	err = basicRes.GetDal().First(&rule, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Get(input)
 }
 
 // GetTransformationRuleList return all transformation rules
@@ -122,11 +75,5 @@ func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bamboo/transformation_rules [GET]
 func GetTransformationRuleList(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rules []models.BambooTransformationRule
-	limit, offset := api.GetLimitOffset(input.Query, "pageSize", "page")
-	err := basicRes.GetDal().All(&rules, dal.Limit(limit), dal.Offset(offset))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
-	}
-	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+	return trHelper.List(input)
 }
diff --git a/backend/plugins/bitbucket/api/init.go b/backend/plugins/bitbucket/api/init.go
index 700593919..0337e0f8e 100644
--- a/backend/plugins/bitbucket/api/init.go
+++ b/backend/plugins/bitbucket/api/init.go
@@ -28,6 +28,7 @@ var vld *validator.Validate
 var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.BitbucketConnection, models.BitbucketRepo, models.BitbucketTransformationRule]
 var remoteHelper *api.RemoteApiHelper[models.BitbucketConnection, models.BitbucketRepo, models.BitbucketApiRepo, models.GroupResponse]
+var trHelper *api.TransformationRuleHelper[models.BitbucketTransformationRule]
 var basicRes context.BasicRes
 
 func Init(br context.BasicRes) {
@@ -47,4 +48,8 @@ func Init(br context.BasicRes) {
 		vld,
 		connectionHelper,
 	)
+	trHelper = api.NewTransformationRuleHelper[models.BitbucketTransformationRule](
+		basicRes,
+		vld,
+	)
 }
diff --git a/backend/plugins/bitbucket/api/transformation_rule.go b/backend/plugins/bitbucket/api/transformation_rule.go
index 2faa5168c..9b37da739 100644
--- a/backend/plugins/bitbucket/api/transformation_rule.go
+++ b/backend/plugins/bitbucket/api/transformation_rule.go
@@ -18,13 +18,8 @@ limitations under the License.
 package api
 
 import (
-	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
-	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
-	"github.com/apache/incubator-devlake/plugins/bitbucket/models"
-	"net/http"
-	"strconv"
 )
 
 // CreateTransformationRule create transformation rule for Bitbucket
@@ -38,19 +33,7 @@ import (
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bitbucket/transformation_rules [POST]
 func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rule models.BitbucketTransformationRule
-	err := api.Decode(input.Body, &rule, vld)
-	if err != nil {
-		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
-	}
-	err = basicRes.GetDal().Create(&rule)
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Create(input)
 }
 
 // UpdateTransformationRule update transformation rule for Bitbucket
@@ -65,28 +48,7 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bitbucket/transformation_rules/{id} [PATCH]
 func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
-	if e != nil {
-		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
-	}
-	var old models.BitbucketTransformationRule
-	err := basicRes.GetDal().First(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
-	}
-	err = api.DecodeMapStruct(input.Body, &old)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
-	}
-	old.ID = transformationRuleId
-	err = basicRes.GetDal().Update(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+	return trHelper.Update(input)
 }
 
 // GetTransformationRule return one transformation rule
@@ -99,16 +61,7 @@ func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bitbucket/transformation_rules/{id} [GET]
 func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
-	}
-	var rule models.BitbucketTransformationRule
-	err = basicRes.GetDal().First(&rule, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Get(input)
 }
 
 // GetTransformationRuleList return all transformation rules
@@ -122,11 +75,5 @@ func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/bitbucket/transformation_rules [GET]
 func GetTransformationRuleList(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rules []models.BitbucketTransformationRule
-	limit, offset := api.GetLimitOffset(input.Query, "pageSize", "page")
-	err := basicRes.GetDal().All(&rules, dal.Limit(limit), dal.Offset(offset))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
-	}
-	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+	return trHelper.List(input)
 }
diff --git a/backend/plugins/github/api/init.go b/backend/plugins/github/api/init.go
index 3044a56ba..179ea7ff4 100644
--- a/backend/plugins/github/api/init.go
+++ b/backend/plugins/github/api/init.go
@@ -28,6 +28,7 @@ var vld *validator.Validate
 var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.GithubConnection, models.GithubRepo, models.GithubTransformationRule]
 var basicRes context.BasicRes
+var trHelper *api.TransformationRuleHelper[models.GithubTransformationRule]
 
 func Init(br context.BasicRes) {
 	basicRes = br
@@ -41,4 +42,8 @@ func Init(br context.BasicRes) {
 		vld,
 		connectionHelper,
 	)
+	trHelper = api.NewTransformationRuleHelper[models.GithubTransformationRule](
+		basicRes,
+		vld,
+	)
 }
diff --git a/backend/plugins/github/api/transformation_rule.go b/backend/plugins/github/api/transformation_rule.go
index 925036bdf..f064e675a 100644
--- a/backend/plugins/github/api/transformation_rule.go
+++ b/backend/plugins/github/api/transformation_rule.go
@@ -18,13 +18,8 @@ limitations under the License.
 package api
 
 import (
-	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
-	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
-	"github.com/apache/incubator-devlake/plugins/github/models"
-	"net/http"
-	"strconv"
 )
 
 // CreateTransformationRule create transformation rule for Github
@@ -38,19 +33,7 @@ import (
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/github/transformation_rules [POST]
 func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rule models.GithubTransformationRule
-	err := api.Decode(input.Body, &rule, vld)
-	if err != nil {
-		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
-	}
-	err = basicRes.GetDal().Create(&rule)
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Create(input)
 }
 
 // UpdateTransformationRule update transformation rule for Github
@@ -65,28 +48,7 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/github/transformation_rules/{id} [PATCH]
 func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
-	if e != nil {
-		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
-	}
-	var old models.GithubTransformationRule
-	err := basicRes.GetDal().First(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
-	}
-	err = api.DecodeMapStruct(input.Body, &old)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
-	}
-	old.ID = transformationRuleId
-	err = basicRes.GetDal().Update(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+	return trHelper.Update(input)
 }
 
 // GetTransformationRule return one transformation rule
@@ -99,16 +61,7 @@ func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/github/transformation_rules/{id} [GET]
 func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
-	}
-	var rule models.GithubTransformationRule
-	err = basicRes.GetDal().First(&rule, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Get(input)
 }
 
 // GetTransformationRuleList return all transformation rules
@@ -122,11 +75,5 @@ func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/github/transformation_rules [GET]
 func GetTransformationRuleList(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rules []models.GithubTransformationRule
-	limit, offset := api.GetLimitOffset(input.Query, "pageSize", "page")
-	err := basicRes.GetDal().All(&rules, dal.Limit(limit), dal.Offset(offset))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
-	}
-	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+	return trHelper.List(input)
 }
diff --git a/backend/plugins/gitlab/api/init.go b/backend/plugins/gitlab/api/init.go
index 444534799..c29d34ac9 100644
--- a/backend/plugins/gitlab/api/init.go
+++ b/backend/plugins/gitlab/api/init.go
@@ -29,6 +29,7 @@ var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.GitlabConnection, models.GitlabProject, models.GitlabTransformationRule]
 var remoteHelper *api.RemoteApiHelper[models.GitlabConnection, models.GitlabProject, models.GitlabApiProject, models.GroupResponse]
 var basicRes context.BasicRes
+var trHelper *api.TransformationRuleHelper[models.GitlabTransformationRule]
 
 func Init(br context.BasicRes) {
 	basicRes = br
@@ -48,4 +49,8 @@ func Init(br context.BasicRes) {
 		vld,
 		connectionHelper,
 	)
+	trHelper = api.NewTransformationRuleHelper[models.GitlabTransformationRule](
+		basicRes,
+		vld,
+	)
 }
diff --git a/backend/plugins/gitlab/api/transformation_rule.go b/backend/plugins/gitlab/api/transformation_rule.go
index f15a2c449..10cfec795 100644
--- a/backend/plugins/gitlab/api/transformation_rule.go
+++ b/backend/plugins/gitlab/api/transformation_rule.go
@@ -18,13 +18,8 @@ limitations under the License.
 package api
 
 import (
-	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
-	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
-	"github.com/apache/incubator-devlake/plugins/gitlab/models"
-	"net/http"
-	"strconv"
 )
 
 // CreateTransformationRule create transformation rule for Gitlab
@@ -38,19 +33,7 @@ import (
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/gitlab/transformation_rules [POST]
 func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rule models.GitlabTransformationRule
-	err := api.Decode(input.Body, &rule, vld)
-	if err != nil {
-		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
-	}
-	err = basicRes.GetDal().Create(&rule)
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Create(input)
 }
 
 // UpdateTransformationRule update transformation rule for Gitlab
@@ -65,28 +48,7 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/gitlab/transformation_rules/{id} [PATCH]
 func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
-	if e != nil {
-		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
-	}
-	var old models.GitlabTransformationRule
-	err := basicRes.GetDal().First(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
-	}
-	err = api.DecodeMapStruct(input.Body, &old)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
-	}
-	old.ID = transformationRuleId
-	err = basicRes.GetDal().Update(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+	return trHelper.Update(input)
 }
 
 // GetTransformationRule return one transformation rule
@@ -99,16 +61,7 @@ func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/gitlab/transformation_rules/{id} [GET]
 func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
-	}
-	var rule models.GitlabTransformationRule
-	err = basicRes.GetDal().First(&rule, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Get(input)
 }
 
 // GetTransformationRuleList return all transformation rules
@@ -122,11 +75,5 @@ func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/gitlab/transformation_rules [GET]
 func GetTransformationRuleList(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rules []models.GitlabTransformationRule
-	limit, offset := api.GetLimitOffset(input.Query, "pageSize", "page")
-	err := basicRes.GetDal().All(&rules, dal.Limit(limit), dal.Offset(offset))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
-	}
-	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+	return trHelper.List(input)
 }
diff --git a/backend/plugins/jenkins/api/init.go b/backend/plugins/jenkins/api/init.go
index 40c3b7104..d0f125dd8 100644
--- a/backend/plugins/jenkins/api/init.go
+++ b/backend/plugins/jenkins/api/init.go
@@ -28,6 +28,7 @@ var vld *validator.Validate
 var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.JenkinsConnection, models.JenkinsJob, models.JenkinsTransformationRule]
 var basicRes context.BasicRes
+var trHelper *api.TransformationRuleHelper[models.JenkinsTransformationRule]
 
 func Init(br context.BasicRes) {
 	basicRes = br
@@ -41,4 +42,8 @@ func Init(br context.BasicRes) {
 		vld,
 		connectionHelper,
 	)
+	trHelper = api.NewTransformationRuleHelper[models.JenkinsTransformationRule](
+		basicRes,
+		vld,
+	)
 }
diff --git a/backend/plugins/jenkins/api/transformation_rule.go b/backend/plugins/jenkins/api/transformation_rule.go
index 22d67fd13..9cd27682a 100644
--- a/backend/plugins/jenkins/api/transformation_rule.go
+++ b/backend/plugins/jenkins/api/transformation_rule.go
@@ -18,13 +18,8 @@ limitations under the License.
 package api
 
 import (
-	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
-	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
-	"github.com/apache/incubator-devlake/plugins/jenkins/models"
-	"net/http"
-	"strconv"
 )
 
 // CreateTransformationRule create transformation rule for Jenkins
@@ -38,19 +33,7 @@ import (
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jenkins/transformation_rules [POST]
 func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rule models.JenkinsTransformationRule
-	err := api.Decode(input.Body, &rule, vld)
-	if err != nil {
-		return nil, errors.BadInput.Wrap(err, "error in decoding transformation rule")
-	}
-	err = basicRes.GetDal().Create(&rule)
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Create(input)
 }
 
 // UpdateTransformationRule update transformation rule for Jenkins
@@ -65,28 +48,7 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jenkins/transformation_rules/{id} [PATCH]
 func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
-	if e != nil {
-		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
-	}
-	var old models.JenkinsTransformationRule
-	err := basicRes.GetDal().First(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
-	}
-	err = api.DecodeMapStruct(input.Body, &old)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
-	}
-	old.ID = transformationRuleId
-	err = basicRes.GetDal().Update(&old, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+	return trHelper.Update(input)
 }
 
 // GetTransformationRule return one transformation rule
@@ -99,16 +61,7 @@ func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jenkins/transformation_rules/{id} [GET]
 func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
-	}
-	var rule models.JenkinsTransformationRule
-	err = basicRes.GetDal().First(&rule, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Update(input)
 }
 
 // GetTransformationRuleList return all transformation rules
@@ -122,11 +75,5 @@ func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jenkins/transformation_rules [GET]
 func GetTransformationRuleList(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rules []models.JenkinsTransformationRule
-	limit, offset := api.GetLimitOffset(input.Query, "pageSize", "page")
-	err := basicRes.GetDal().All(&rules, dal.Limit(limit), dal.Offset(offset))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
-	}
-	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+	return trHelper.List(input)
 }
diff --git a/backend/plugins/jira/api/init.go b/backend/plugins/jira/api/init.go
index a9050797e..eec4ee406 100644
--- a/backend/plugins/jira/api/init.go
+++ b/backend/plugins/jira/api/init.go
@@ -28,6 +28,7 @@ var vld *validator.Validate
 var connectionHelper *api.ConnectionApiHelper
 var scopeHelper *api.ScopeApiHelper[models.JiraConnection, models.JiraBoard, models.JiraTransformationRule]
 var basicRes context.BasicRes
+var trHelper *api.TransformationRuleHelper[models.JiraTransformationRule]
 
 func Init(br context.BasicRes) {
 	basicRes = br
@@ -41,4 +42,9 @@ func Init(br context.BasicRes) {
 		vld,
 		connectionHelper,
 	)
+
+	trHelper = api.NewTransformationRuleHelper[models.JiraTransformationRule](
+		basicRes,
+		vld,
+	)
 }
diff --git a/backend/plugins/jira/api/transformation_rule.go b/backend/plugins/jira/api/transformation_rule.go
index 5121b3474..e8b44e417 100644
--- a/backend/plugins/jira/api/transformation_rule.go
+++ b/backend/plugins/jira/api/transformation_rule.go
@@ -18,14 +18,12 @@ limitations under the License.
 package api
 
 import (
-	"github.com/apache/incubator-devlake/core/dal"
 	"github.com/apache/incubator-devlake/core/errors"
 	"github.com/apache/incubator-devlake/core/plugin"
 	"github.com/apache/incubator-devlake/helpers/pluginhelper/api"
 	"github.com/apache/incubator-devlake/plugins/jira/models"
 	"github.com/apache/incubator-devlake/plugins/jira/tasks"
-	"net/http"
-	"strconv"
+	"github.com/mitchellh/mapstructure"
 )
 
 // CreateTransformationRule create transformation rule for Jira
@@ -43,14 +41,13 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 	if err != nil {
 		return nil, errors.BadInput.Wrap(err, "error in makeJiraTransformationRule")
 	}
-	err = basicRes.GetDal().Create(&rule)
+	newRule := map[string]interface{}{}
+	err = errors.Convert(mapstructure.Decode(rule, &newRule))
 	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
+		return nil, errors.BadInput.Wrap(err, "error in makeJiraTransformationRule")
 	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	input.Body = newRule
+	return trHelper.Create(input)
 }
 
 // UpdateTransformationRule update transformation rule for Jira
@@ -65,32 +62,26 @@ func CreateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResour
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jira/transformation_rules/{id} [PATCH]
 func UpdateTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, e := strconv.ParseUint(input.Params["id"], 10, 64)
-	if e != nil {
-		return nil, errors.Default.Wrap(e, "the transformation rule ID should be an integer")
-	}
-	var old models.JiraTransformationRule
-	err := basicRes.GetDal().First(&old, dal.Where("id = ?", transformationRuleId))
+	rule, err := makeDbTransformationRuleFromInput(input)
 	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on saving TransformationRule")
+		return nil, errors.BadInput.Wrap(err, "error in makeJiraTransformationRule")
 	}
-	err = api.DecodeMapStruct(input.Body, &old)
+	newRule := map[string]interface{}{}
+	err = errors.Convert(mapstructure.Decode(rule, &newRule))
 	if err != nil {
-		return nil, errors.Default.Wrap(err, "error decoding map into transformationRule")
+		return nil, errors.BadInput.Wrap(err, "error in makeJiraTransformationRule")
 	}
-	old.ID = transformationRuleId
-	err = old.VerifyRegexp()
+	input.Body = newRule
+	output, err := trHelper.Update(input)
 	if err != nil {
-		return nil, errors.Default.Wrap(err, "error verify the regexps of transformationRule")
+		return nil, err
 	}
-	err = basicRes.GetDal().Update(&old, dal.Where("id = ?", transformationRuleId))
+	tr := output.Body.(models.JiraTransformationRule)
+	err = tr.VerifyRegexp()
 	if err != nil {
-		if basicRes.GetDal().IsDuplicationError(err) {
-			return nil, errors.BadInput.New("there was a transformation rule with the same name, please choose another name")
-		}
-		return nil, errors.BadInput.Wrap(err, "error on saving TransformationRule")
+		return nil, errors.Default.Wrap(err, "error verify the regexps of transformationRule")
 	}
-	return &plugin.ApiResourceOutput{Body: old, Status: http.StatusOK}, nil
+	return output, err
 }
 
 func makeDbTransformationRuleFromInput(input *plugin.ApiResourceInput) (*models.JiraTransformationRule, errors.Error) {
@@ -112,16 +103,7 @@ func makeDbTransformationRuleFromInput(input *plugin.ApiResourceInput) (*models.
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jira/transformation_rules/{id} [GET]
 func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	transformationRuleId, err := strconv.ParseUint(input.Params["id"], 10, 64)
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "the transformation rule ID should be an integer")
-	}
-	var rule models.JiraTransformationRule
-	err = basicRes.GetDal().First(&rule, dal.Where("id = ?", transformationRuleId))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule")
-	}
-	return &plugin.ApiResourceOutput{Body: rule, Status: http.StatusOK}, nil
+	return trHelper.Get(input)
 }
 
 // GetTransformationRuleList return all transformation rules
@@ -135,11 +117,5 @@ func GetTransformationRule(input *plugin.ApiResourceInput) (*plugin.ApiResourceO
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/jira/transformation_rules [GET]
 func GetTransformationRuleList(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	var rules []models.JiraTransformationRule
-	limit, offset := api.GetLimitOffset(input.Query, "pageSize", "page")
-	err := basicRes.GetDal().All(&rules, dal.Limit(limit), dal.Offset(offset))
-	if err != nil {
-		return nil, errors.Default.Wrap(err, "error on get TransformationRule list")
-	}
-	return &plugin.ApiResourceOutput{Body: rules, Status: http.StatusOK}, nil
+	return trHelper.List(input)
 }
diff --git a/backend/plugins/jira/models/transformation_rules.go b/backend/plugins/jira/models/transformation_rules.go
index 7e18dabe8..bb2b7e876 100644
--- a/backend/plugins/jira/models/transformation_rules.go
+++ b/backend/plugins/jira/models/transformation_rules.go
@@ -35,11 +35,11 @@ type JiraTransformationRule struct {
 	TypeMappings               json.RawMessage `mapstructure:"typeMappings,omitempty" json:"typeMappings"`
 }
 
-func (r *JiraTransformationRule) TableName() string {
+func (r JiraTransformationRule) TableName() string {
 	return "_tool_jira_transformation_rules"
 }
 
-func (r *JiraTransformationRule) VerifyRegexp() errors.Error {
+func (r JiraTransformationRule) VerifyRegexp() errors.Error {
 	var err error
 	if r.RemotelinkCommitShaPattern != "" {
 		_, err = regexp.Compile(r.RemotelinkCommitShaPattern)