This is an automated email from the ASF dual-hosted git repository.
warren 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 6b94f7297 feat: return transformationRuleName in put scopes for
config-ui (#4897)
6b94f7297 is described below
commit 6b94f7297a82c90edc0160a1cb6c03937181e325
Author: Likyh <[email protected]>
AuthorDate: Thu Apr 13 10:47:27 2023 +0800
feat: return transformationRuleName in put scopes for config-ui (#4897)
---
backend/helpers/pluginhelper/api/scope_helper.go | 69 ++++++++++++++----------
1 file changed, 41 insertions(+), 28 deletions(-)
diff --git a/backend/helpers/pluginhelper/api/scope_helper.go
b/backend/helpers/pluginhelper/api/scope_helper.go
index f0f28e6f9..2571517a9 100644
--- a/backend/helpers/pluginhelper/api/scope_helper.go
+++ b/backend/helpers/pluginhelper/api/scope_helper.go
@@ -117,6 +117,7 @@ func (c *ScopeApiHelper[Conn, Scope, Tr]) Put(input
*plugin.ApiResourceInput) (*
return nil, err
}
}
+ // Save the scopes to the database
if req.Data != nil && len(req.Data) > 0 {
err = c.save(&req.Data)
if err != nil {
@@ -124,8 +125,12 @@ func (c *ScopeApiHelper[Conn, Scope, Tr]) Put(input
*plugin.ApiResourceInput) (*
}
}
- // Save the scopes to the database
- return &plugin.ApiResourceOutput{Body: req.Data, Status:
http.StatusOK}, nil
+ apiScopes, err := c.addTransformationName(req.Data)
+ if err != nil {
+ return nil, err
+ }
+
+ return &plugin.ApiResourceOutput{Body: apiScopes, Status:
http.StatusOK}, nil
}
func (c *ScopeApiHelper[Conn, Scope, Tr]) Update(input
*plugin.ApiResourceInput, fieldName string) (*plugin.ApiResourceOutput,
errors.Error) {
@@ -196,32 +201,9 @@ func (c *ScopeApiHelper[Conn, Scope, Tr])
GetScopeList(input *plugin.ApiResource
return nil, err
}
- var ruleIds []uint64
- for _, scope := range scopes {
- valueRepoRuleId :=
reflect.ValueOf(scope).Elem().FieldByName("TransformationRuleId")
- if !valueRepoRuleId.IsValid() {
- return &plugin.ApiResourceOutput{Body: scopes, Status:
http.StatusOK}, nil
- }
- ruleId :=
reflect.ValueOf(scope).Elem().FieldByName("TransformationRuleId").Uint()
- if ruleId > 0 {
- ruleIds = append(ruleIds, ruleId)
- }
- }
- var rules []*Tr
- if len(ruleIds) > 0 {
- err = c.db.All(&rules, dal.Where("id IN (?)", ruleIds))
- if err != nil {
- return nil, err
- }
- }
- names := make(map[uint64]string)
- for _, rule := range rules {
- // Get the reflect.Value of the i-th struct pointer in the slice
- names[reflect.ValueOf(rule).Elem().FieldByName("ID").Uint()] =
reflect.ValueOf(rule).Elem().FieldByName("Name").String()
- }
- apiScopes := make([]ScopeRes[Scope], 0)
- for _, scope := range scopes {
- apiScopes = append(apiScopes, ScopeRes[Scope]{*scope,
names[reflect.ValueOf(scope).Elem().FieldByName("TransformationRuleId").Uint()]})
+ apiScopes, err := c.addTransformationName(scopes)
+ if err != nil {
+ return nil, err
}
return &plugin.ApiResourceOutput{Body: apiScopes, Status:
http.StatusOK}, nil
}
@@ -277,6 +259,37 @@ func (c *ScopeApiHelper[Conn, Scope, Tr])
VerifyConnection(connId uint64) errors
return nil
}
+func (c *ScopeApiHelper[Conn, Scope, Tr]) addTransformationName(scopes
[]*Scope) ([]ScopeRes[Scope], errors.Error) {
+ var ruleIds []uint64
+ for _, scope := range scopes {
+ valueRepoRuleId :=
reflect.ValueOf(scope).Elem().FieldByName("TransformationRuleId")
+ if !valueRepoRuleId.IsValid() {
+ break
+ }
+ ruleId :=
reflect.ValueOf(scope).Elem().FieldByName("TransformationRuleId").Uint()
+ if ruleId > 0 {
+ ruleIds = append(ruleIds, ruleId)
+ }
+ }
+ var rules []*Tr
+ if len(ruleIds) > 0 {
+ err := c.db.All(&rules, dal.Where("id IN (?)", ruleIds))
+ if err != nil {
+ return nil, err
+ }
+ }
+ names := make(map[uint64]string)
+ for _, rule := range rules {
+ // Get the reflect.Value of the i-th struct pointer in the slice
+ names[reflect.ValueOf(rule).Elem().FieldByName("ID").Uint()] =
reflect.ValueOf(rule).Elem().FieldByName("Name").String()
+ }
+ apiScopes := make([]ScopeRes[Scope], 0)
+ for _, scope := range scopes {
+ apiScopes = append(apiScopes, ScopeRes[Scope]{*scope,
names[reflect.ValueOf(scope).Elem().FieldByName("TransformationRuleId").Uint()]})
+ }
+ return apiScopes, nil
+}
+
func (c *ScopeApiHelper[Conn, Scope, Tr]) save(scope interface{}) errors.Error
{
err := c.db.CreateOrUpdate(scope)
if err != nil {