From 6d3b97aa311196687322cb0be26b4267b79b4ba2 Mon Sep 17 00:00:00 2001 From: Ashok Siyani Date: Wed, 19 Jun 2024 16:19:58 +0100 Subject: [PATCH] store PR runs per commit ID --- runner/runner.go | 2 +- sysutil/redis.go | 18 +++++++++--------- sysutil/redis_mock.go | 39 +++++++++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/runner/runner.go b/runner/runner.go index 5599e5f..5366f6a 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -458,7 +458,7 @@ func (r *Runner) updateRedis(ctx context.Context, run *tfaplv1beta1.Run) error { // if its PR run only update relevant PR key if run.Request.Type == tfaplv1beta1.PRPlan { - return r.Redis.SetPRLastRun(ctx, run) + return r.Redis.SetPRRun(ctx, run) } // set default last run diff --git a/sysutil/redis.go b/sysutil/redis.go index 5e48139..a581662 100644 --- a/sysutil/redis.go +++ b/sysutil/redis.go @@ -21,13 +21,13 @@ var ( type RedisInterface interface { DefaultLastRun(ctx context.Context, module types.NamespacedName) (*tfaplv1beta1.Run, error) DefaultApply(ctx context.Context, module types.NamespacedName) (*tfaplv1beta1.Run, error) - PRLastRun(ctx context.Context, module types.NamespacedName, pr int) (*tfaplv1beta1.Run, error) + PRRun(ctx context.Context, module types.NamespacedName, pr int, hash string) (*tfaplv1beta1.Run, error) Runs(ctx context.Context, module types.NamespacedName) ([]*tfaplv1beta1.Run, error) GetCommitHash(ctx context.Context, key string) (string, error) SetDefaultLastRun(ctx context.Context, run *tfaplv1beta1.Run) error SetDefaultApply(ctx context.Context, run *tfaplv1beta1.Run) error - SetPRLastRun(ctx context.Context, run *tfaplv1beta1.Run) error + SetPRRun(ctx context.Context, run *tfaplv1beta1.Run) error } type Redis struct { @@ -46,8 +46,8 @@ func defaultLastApplyKey(module types.NamespacedName) string { return fmt.Sprintf("%sdefault:lastApply", keyPrefix(module)) } -func DefaultPRLastRunsKey(module types.NamespacedName, pr int) string { - return fmt.Sprintf("%sPR:%d:lastRun", keyPrefix(module), pr) +func DefaultPRLastRunsKey(module types.NamespacedName, pr int, hash string) string { + return fmt.Sprintf("%sPR:%d:%s", keyPrefix(module), pr, hash) } // DefaultLastRun will return last run result for the default branch @@ -61,8 +61,8 @@ func (r Redis) DefaultApply(ctx context.Context, module types.NamespacedName) (* } // PRLastRun will return last run result for the given PR branch -func (r Redis) PRLastRun(ctx context.Context, module types.NamespacedName, pr int) (*tfaplv1beta1.Run, error) { - return r.getKV(ctx, DefaultPRLastRunsKey(module, pr)) +func (r Redis) PRRun(ctx context.Context, module types.NamespacedName, pr int, hash string) (*tfaplv1beta1.Run, error) { + return r.getKV(ctx, DefaultPRLastRunsKey(module, pr, hash)) } // Runs will return all the runs stored for the given module @@ -94,9 +94,9 @@ func (r Redis) SetDefaultApply(ctx context.Context, run *tfaplv1beta1.Run) error return r.setKV(ctx, defaultLastApplyKey(run.Module), run, 0) } -// SetPRLastRun puts given run in to cache with expiration -func (r Redis) SetPRLastRun(ctx context.Context, run *tfaplv1beta1.Run) error { - return r.setKV(ctx, DefaultPRLastRunsKey(run.Module, run.Request.PR.Number), run, PRKeyExpirationDur) +// SetPRRun puts given run in to cache with expiration +func (r Redis) SetPRRun(ctx context.Context, run *tfaplv1beta1.Run) error { + return r.setKV(ctx, DefaultPRLastRunsKey(run.Module, run.Request.PR.Number, run.CommitHash), run, PRKeyExpirationDur) } func (r Redis) setKV(ctx context.Context, key string, run *tfaplv1beta1.Run, exp time.Duration) error { diff --git a/sysutil/redis_mock.go b/sysutil/redis_mock.go index 0a52d9a..81a28c8 100644 --- a/sysutil/redis_mock.go +++ b/sysutil/redis_mock.go @@ -66,19 +66,34 @@ func (mr *MockRedisInterfaceMockRecorder) DefaultLastRun(arg0, arg1 interface{}) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DefaultLastRun", reflect.TypeOf((*MockRedisInterface)(nil).DefaultLastRun), arg0, arg1) } -// PRLastRun mocks base method. -func (m *MockRedisInterface) PRLastRun(arg0 context.Context, arg1 types.NamespacedName, arg2 int) (*v1beta1.Run, error) { +// GetCommitHash mocks base method. +func (m *MockRedisInterface) GetCommitHash(arg0 context.Context, arg1 string) (string, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "PRLastRun", arg0, arg1, arg2) + ret := m.ctrl.Call(m, "GetCommitHash", arg0, arg1) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetCommitHash indicates an expected call of GetCommitHash. +func (mr *MockRedisInterfaceMockRecorder) GetCommitHash(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCommitHash", reflect.TypeOf((*MockRedisInterface)(nil).GetCommitHash), arg0, arg1) +} + +// PRRun mocks base method. +func (m *MockRedisInterface) PRRun(arg0 context.Context, arg1 types.NamespacedName, arg2 int, arg3 string) (*v1beta1.Run, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PRRun", arg0, arg1, arg2, arg3) ret0, _ := ret[0].(*v1beta1.Run) ret1, _ := ret[1].(error) return ret0, ret1 } -// PRLastRun indicates an expected call of PRLastRun. -func (mr *MockRedisInterfaceMockRecorder) PRLastRun(arg0, arg1, arg2 interface{}) *gomock.Call { +// PRRun indicates an expected call of PRRun. +func (mr *MockRedisInterfaceMockRecorder) PRRun(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PRLastRun", reflect.TypeOf((*MockRedisInterface)(nil).PRLastRun), arg0, arg1, arg2) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PRRun", reflect.TypeOf((*MockRedisInterface)(nil).PRRun), arg0, arg1, arg2, arg3) } // Runs mocks base method. @@ -124,16 +139,16 @@ func (mr *MockRedisInterfaceMockRecorder) SetDefaultLastRun(arg0, arg1 interface return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetDefaultLastRun", reflect.TypeOf((*MockRedisInterface)(nil).SetDefaultLastRun), arg0, arg1) } -// SetPRLastRun mocks base method. -func (m *MockRedisInterface) SetPRLastRun(arg0 context.Context, arg1 *v1beta1.Run) error { +// SetPRRun mocks base method. +func (m *MockRedisInterface) SetPRRun(arg0 context.Context, arg1 *v1beta1.Run) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "SetPRLastRun", arg0, arg1) + ret := m.ctrl.Call(m, "SetPRRun", arg0, arg1) ret0, _ := ret[0].(error) return ret0 } -// SetPRLastRun indicates an expected call of SetPRLastRun. -func (mr *MockRedisInterfaceMockRecorder) SetPRLastRun(arg0, arg1 interface{}) *gomock.Call { +// SetPRRun indicates an expected call of SetPRRun. +func (mr *MockRedisInterfaceMockRecorder) SetPRRun(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetPRLastRun", reflect.TypeOf((*MockRedisInterface)(nil).SetPRLastRun), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetPRRun", reflect.TypeOf((*MockRedisInterface)(nil).SetPRRun), arg0, arg1) }