Skip to content

Commit a8ed05c

Browse files
fix: scopeGenericHelper updates the wrong fields (#6172) (#6175)
Co-authored-by: abeizn <[email protected]>
1 parent fb54d95 commit a8ed05c

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
lines changed

backend/helpers/pluginhelper/api/scope_generic_helper.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) PutScopes(input *plug
174174
}
175175
now := time.Now()
176176
for _, scope := range scopes {
177-
// Set the connection ID, CreatedDate, and UpdatedDate fields
177+
// Set the connection ID, CreatedAt, and UpdatedAt fields
178178
gs.setScopeFields(scope, params.connectionId, &now, &now)
179179
err = gs.verifyScope(scope, gs.validator)
180180
if err != nil {
@@ -459,7 +459,7 @@ func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) createRawParams(conne
459459
return plugin.MarshalScopeParams(paramsMap)
460460
}
461461

462-
func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) setScopeFields(p interface{}, connectionId uint64, createdDate *time.Time, updatedDate *time.Time) {
462+
func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) setScopeFields(p interface{}, connectionId uint64, createdAt *time.Time, updatedAt *time.Time) {
463463
pType := reflect.TypeOf(p)
464464
if pType.Kind() != reflect.Ptr {
465465
panic("expected a pointer to a struct")
@@ -477,24 +477,24 @@ func (gs *GenericScopeApiHelper[Conn, Scope, ScopeConfig]) setScopeFields(p inte
477477
scopeIdField := pValue.FieldByName(gs.reflectionParams.ScopeIdFieldName)
478478
rawParams.Set(reflect.ValueOf(gs.createRawParams(connectionId, scopeIdField.Interface())))
479479

480-
// set CreatedDate
481-
createdDateField := pValue.FieldByName("CreatedDate")
482-
if createdDateField.IsValid() && createdDateField.Type().AssignableTo(reflect.TypeOf(createdDate)) {
483-
createdDateField.Set(reflect.ValueOf(createdDate))
480+
// set CreatedAt
481+
createdAtField := pValue.FieldByName("CreatedAt")
482+
if createdAtField.IsValid() && createdAtField.Type().AssignableTo(reflect.TypeOf(createdAt)) {
483+
createdAtField.Set(reflect.ValueOf(createdAt))
484484
}
485485

486-
// set UpdatedDate
487-
updatedDateField := pValue.FieldByName("UpdatedDate")
488-
if !updatedDateField.IsValid() || (updatedDate != nil && !updatedDateField.Type().AssignableTo(reflect.TypeOf(updatedDate))) {
486+
// set UpdatedAt
487+
updatedAtField := pValue.FieldByName("UpdatedAt")
488+
if !updatedAtField.IsValid() || (updatedAt != nil && !updatedAtField.Type().AssignableTo(reflect.TypeOf(updatedAt))) {
489489
return
490490
}
491-
if updatedDate == nil {
492-
// if updatedDate is nil, set UpdatedDate to be nil
493-
updatedDateField.Set(reflect.Zero(updatedDateField.Type()))
491+
if updatedAt == nil {
492+
// if updatedAt is nil, set UpdatedAt to be nil
493+
updatedAtField.Set(reflect.Zero(updatedAtField.Type()))
494494
} else {
495-
// if updatedDate is not nil, set UpdatedDate to be the value
496-
updatedDateFieldValue := reflect.ValueOf(updatedDate)
497-
updatedDateField.Set(updatedDateFieldValue)
495+
// if updatedAt is not nil, set UpdatedAt to be the value
496+
updatedAtFieldValue := reflect.ValueOf(updatedAt)
497+
updatedAtField.Set(updatedAtFieldValue)
498498
}
499499
}
500500

backend/helpers/pluginhelper/api/scope_helper_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -184,50 +184,49 @@ func (TestScopeConfig) TableName() string {
184184
func TestSetScopeFields(t *testing.T) {
185185
// create a struct
186186
type P struct {
187-
ConnectionId uint64 `json:"connectionId" mapstructure:"connectionId" gorm:"primaryKey"`
188-
GitlabId int `json:"gitlabId" mapstructure:"gitlabId" gorm:"primaryKey"`
189-
190-
CreatedDate *time.Time `json:"createdDate" mapstructure:"-"`
191-
UpdatedDate *time.Time `json:"updatedDate" mapstructure:"-"`
187+
ConnectionId uint64 `json:"connectionId" mapstructure:"connectionId" gorm:"primaryKey"`
188+
GitlabId int `json:"gitlabId" mapstructure:"gitlabId" gorm:"primaryKey"`
189+
CreatedAt *time.Time `json:"createdAt" mapstructure:"-"`
190+
UpdatedAt *time.Time `json:"updatedAt" mapstructure:"-"`
192191
common.NoPKModel `json:"-" mapstructure:"-"`
193192
}
194193
p := P{}
195194
apiHelper := createMockScopeHelper[TestFakeGitlabRepo]("GitlabId")
196195

197196
// call setScopeFields to assign value
198197
connectionId := uint64(123)
199-
createdDate := time.Now()
200-
updatedDate := &createdDate
201-
apiHelper.setScopeFields(&p, connectionId, &createdDate, updatedDate)
198+
createdAt := time.Now()
199+
updatedAt := &createdAt
200+
apiHelper.setScopeFields(&p, connectionId, &createdAt, updatedAt)
202201

203202
// verify fields
204203
if p.ConnectionId != connectionId {
205204
t.Errorf("ConnectionId not set correctly, expected: %v, got: %v", connectionId, p.ConnectionId)
206205
}
207206

208-
if !p.CreatedDate.Equal(createdDate) {
209-
t.Errorf("CreatedDate not set correctly, expected: %v, got: %v", createdDate, p.CreatedDate)
207+
if !p.CreatedAt.Equal(createdAt) {
208+
t.Errorf("CreatedAt not set correctly, expected: %v, got: %v", createdAt, p.CreatedAt)
210209
}
211210

212-
if p.UpdatedDate == nil {
213-
t.Errorf("UpdatedDate not set correctly, expected: %v, got: %v", updatedDate, p.UpdatedDate)
214-
} else if !p.UpdatedDate.Equal(*updatedDate) {
215-
t.Errorf("UpdatedDate not set correctly, expected: %v, got: %v", updatedDate, p.UpdatedDate)
211+
if p.UpdatedAt == nil {
212+
t.Errorf("UpdatedDate not set correctly, expected: %v, got: %v", updatedAt, p.UpdatedAt)
213+
} else if !p.UpdatedAt.Equal(*updatedAt) {
214+
t.Errorf("UpdatedDate not set correctly, expected: %v, got: %v", updatedAt, p.UpdatedAt)
216215
}
217216

218-
apiHelper.setScopeFields(&p, connectionId, &createdDate, nil)
217+
apiHelper.setScopeFields(&p, connectionId, &createdAt, nil)
219218

220219
// verify fields
221220
if p.ConnectionId != connectionId {
222221
t.Errorf("ConnectionId not set correctly, expected: %v, got: %v", connectionId, p.ConnectionId)
223222
}
224223

225-
if !p.CreatedDate.Equal(createdDate) {
226-
t.Errorf("CreatedDate not set correctly, expected: %v, got: %v", createdDate, p.CreatedDate)
224+
if !p.CreatedAt.Equal(createdAt) {
225+
t.Errorf("CreatedDate not set correctly, expected: %v, got: %v", createdAt, p.CreatedAt)
227226
}
228227

229-
if p.UpdatedDate != nil {
230-
t.Errorf("UpdatedDate not set correctly, expected: %v, got: %v", nil, p.UpdatedDate)
228+
if p.UpdatedAt != nil {
229+
t.Errorf("UpdatedDate not set correctly, expected: %v, got: %v", nil, p.UpdatedAt)
231230
}
232231
}
233232

0 commit comments

Comments
 (0)