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

klesh 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 4a2e505ac fix: scopeGenericHelper updates the wrong fields (#6172)
4a2e505ac is described below

commit 4a2e505ace79f80d36943ccc432b4c13d860440e
Author: abeizn <[email protected]>
AuthorDate: Thu Sep 28 16:12:30 2023 +0800

    fix: scopeGenericHelper updates the wrong fields (#6172)
---
 .../pluginhelper/api/scope_generic_helper.go       | 30 +++++++++---------
 .../helpers/pluginhelper/api/scope_helper_test.go  | 37 +++++++++++-----------
 2 files changed, 33 insertions(+), 34 deletions(-)

diff --git a/backend/helpers/pluginhelper/api/scope_generic_helper.go 
b/backend/helpers/pluginhelper/api/scope_generic_helper.go
index c77c6f727..4b6d71817 100644
--- a/backend/helpers/pluginhelper/api/scope_generic_helper.go
+++ b/backend/helpers/pluginhelper/api/scope_generic_helper.go
@@ -174,7 +174,7 @@ func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) 
PutScopes(input *plug
        }
        now := time.Now()
        for _, scope := range scopes {
-               // Set the connection ID, CreatedDate, and UpdatedDate fields
+               // Set the connection ID, CreatedAt, and UpdatedAt fields
                gs.setScopeFields(scope, params.connectionId, &now, &now)
                err = gs.verifyScope(scope, gs.validator)
                if err != nil {
@@ -426,7 +426,7 @@ func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) 
createRawParams(conne
        return plugin.MarshalScopeParams(paramsMap)
 }
 
-func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) setScopeFields(p 
interface{}, connectionId uint64, createdDate *time.Time, updatedDate 
*time.Time) {
+func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) setScopeFields(p 
interface{}, connectionId uint64, createdAt *time.Time, updatedAt *time.Time) {
        pType := reflect.TypeOf(p)
        if pType.Kind() != reflect.Ptr {
                panic("expected a pointer to a struct")
@@ -444,24 +444,24 @@ func (gs *GenericScopeApiHelper[Conn, Scope, 
ScopeConfig]) setScopeFields(p inte
        scopeIdField := pValue.FieldByName(gs.reflectionParams.ScopeIdFieldName)
        rawParams.Set(reflect.ValueOf(gs.createRawParams(connectionId, 
scopeIdField.Interface())))
 
-       // set CreatedDate
-       createdDateField := pValue.FieldByName("CreatedDate")
-       if createdDateField.IsValid() && 
createdDateField.Type().AssignableTo(reflect.TypeOf(createdDate)) {
-               createdDateField.Set(reflect.ValueOf(createdDate))
+       // set CreatedAt
+       createdAtField := pValue.FieldByName("CreatedAt")
+       if createdAtField.IsValid() && 
createdAtField.Type().AssignableTo(reflect.TypeOf(createdAt)) {
+               createdAtField.Set(reflect.ValueOf(createdAt))
        }
 
-       // set UpdatedDate
-       updatedDateField := pValue.FieldByName("UpdatedDate")
-       if !updatedDateField.IsValid() || (updatedDate != nil && 
!updatedDateField.Type().AssignableTo(reflect.TypeOf(updatedDate))) {
+       // set UpdatedAt
+       updatedAtField := pValue.FieldByName("UpdatedAt")
+       if !updatedAtField.IsValid() || (updatedAt != nil && 
!updatedAtField.Type().AssignableTo(reflect.TypeOf(updatedAt))) {
                return
        }
-       if updatedDate == nil {
-               // if updatedDate is nil, set UpdatedDate to be nil
-               updatedDateField.Set(reflect.Zero(updatedDateField.Type()))
+       if updatedAt == nil {
+               // if updatedAt is nil, set UpdatedAt to be nil
+               updatedAtField.Set(reflect.Zero(updatedAtField.Type()))
        } else {
-               // if updatedDate is not nil, set UpdatedDate to be the value
-               updatedDateFieldValue := reflect.ValueOf(updatedDate)
-               updatedDateField.Set(updatedDateFieldValue)
+               // if updatedAt is not nil, set UpdatedAt to be the value
+               updatedAtFieldValue := reflect.ValueOf(updatedAt)
+               updatedAtField.Set(updatedAtFieldValue)
        }
 }
 
diff --git a/backend/helpers/pluginhelper/api/scope_helper_test.go 
b/backend/helpers/pluginhelper/api/scope_helper_test.go
index ca69688cd..8956ad09b 100644
--- a/backend/helpers/pluginhelper/api/scope_helper_test.go
+++ b/backend/helpers/pluginhelper/api/scope_helper_test.go
@@ -189,11 +189,10 @@ func (TestScopeConfig) TableName() string {
 func TestSetScopeFields(t *testing.T) {
        // create a struct
        type P struct {
-               ConnectionId uint64 `json:"connectionId" 
mapstructure:"connectionId" gorm:"primaryKey"`
-               GitlabId     int    `json:"gitlabId" mapstructure:"gitlabId" 
gorm:"primaryKey"`
-
-               CreatedDate      *time.Time `json:"createdDate" 
mapstructure:"-"`
-               UpdatedDate      *time.Time `json:"updatedDate" 
mapstructure:"-"`
+               ConnectionId     uint64     `json:"connectionId" 
mapstructure:"connectionId" gorm:"primaryKey"`
+               GitlabId         int        `json:"gitlabId" 
mapstructure:"gitlabId" gorm:"primaryKey"`
+               CreatedAt        *time.Time `json:"createdAt" mapstructure:"-"`
+               UpdatedAt        *time.Time `json:"updatedAt" mapstructure:"-"`
                common.NoPKModel `json:"-" mapstructure:"-"`
        }
        p := P{}
@@ -201,38 +200,38 @@ func TestSetScopeFields(t *testing.T) {
 
        // call setScopeFields to assign value
        connectionId := uint64(123)
-       createdDate := time.Now()
-       updatedDate := &createdDate
-       apiHelper.setScopeFields(&p, connectionId, &createdDate, updatedDate)
+       createdAt := time.Now()
+       updatedAt := &createdAt
+       apiHelper.setScopeFields(&p, connectionId, &createdAt, updatedAt)
 
        // verify fields
        if p.ConnectionId != connectionId {
                t.Errorf("ConnectionId not set correctly, expected: %v, got: 
%v", connectionId, p.ConnectionId)
        }
 
-       if !p.CreatedDate.Equal(createdDate) {
-               t.Errorf("CreatedDate not set correctly, expected: %v, got: 
%v", createdDate, p.CreatedDate)
+       if !p.CreatedAt.Equal(createdAt) {
+               t.Errorf("CreatedAt not set correctly, expected: %v, got: %v", 
createdAt, p.CreatedAt)
        }
 
-       if p.UpdatedDate == nil {
-               t.Errorf("UpdatedDate not set correctly, expected: %v, got: 
%v", updatedDate, p.UpdatedDate)
-       } else if !p.UpdatedDate.Equal(*updatedDate) {
-               t.Errorf("UpdatedDate not set correctly, expected: %v, got: 
%v", updatedDate, p.UpdatedDate)
+       if p.UpdatedAt == nil {
+               t.Errorf("UpdatedDate not set correctly, expected: %v, got: 
%v", updatedAt, p.UpdatedAt)
+       } else if !p.UpdatedAt.Equal(*updatedAt) {
+               t.Errorf("UpdatedDate not set correctly, expected: %v, got: 
%v", updatedAt, p.UpdatedAt)
        }
 
-       apiHelper.setScopeFields(&p, connectionId, &createdDate, nil)
+       apiHelper.setScopeFields(&p, connectionId, &createdAt, nil)
 
        // verify fields
        if p.ConnectionId != connectionId {
                t.Errorf("ConnectionId not set correctly, expected: %v, got: 
%v", connectionId, p.ConnectionId)
        }
 
-       if !p.CreatedDate.Equal(createdDate) {
-               t.Errorf("CreatedDate not set correctly, expected: %v, got: 
%v", createdDate, p.CreatedDate)
+       if !p.CreatedAt.Equal(createdAt) {
+               t.Errorf("CreatedDate not set correctly, expected: %v, got: 
%v", createdAt, p.CreatedAt)
        }
 
-       if p.UpdatedDate != nil {
-               t.Errorf("UpdatedDate not set correctly, expected: %v, got: 
%v", nil, p.UpdatedDate)
+       if p.UpdatedAt != nil {
+               t.Errorf("UpdatedDate not set correctly, expected: %v, got: 
%v", nil, p.UpdatedAt)
        }
 }
 

Reply via email to