Skip to content

Commit

Permalink
fix: scopeGenericHelper updates the wrong fields (#6172) (#6175)
Browse files Browse the repository at this point in the history
Co-authored-by: abeizn <[email protected]>
  • Loading branch information
github-actions[bot] and abeizn authored Sep 28, 2023
1 parent fb54d95 commit a8ed05c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
30 changes: 15 additions & 15 deletions backend/helpers/pluginhelper/api/scope_generic_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -459,7 +459,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")
Expand All @@ -477,24 +477,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)
}
}

Expand Down
37 changes: 18 additions & 19 deletions backend/helpers/pluginhelper/api/scope_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,50 +184,49 @@ 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{}
apiHelper := createMockScopeHelper[TestFakeGitlabRepo]("GitlabId")

// 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)
}
}

Expand Down

0 comments on commit a8ed05c

Please sign in to comment.