From c35d01e017d10a96af292c08d5617a1cdefac492 Mon Sep 17 00:00:00 2001 From: Win San Date: Fri, 17 May 2024 15:02:32 -0500 Subject: [PATCH] enhance(schedule): add next run field to schedule (#1131) * Add next run field to Schedule * Lint code * Update integration test * Populate next run field in ToAPI * Handle err from gronx NextTickAfter * Fix conditional --- api/types/schedule.go | 29 ++++++++++++++++++++++++++ api/types/schedule_test.go | 12 +++++++++++ database/integration_test.go | 9 ++++++++ database/schedule/count_active_test.go | 10 +++++++++ database/schedule/count_repo_test.go | 10 +++++++++ database/schedule/count_test.go | 10 +++++++++ database/schedule/create_test.go | 6 ++++++ database/schedule/delete_test.go | 6 ++++++ database/schedule/get_repo_test.go | 6 ++++++ database/schedule/get_test.go | 6 ++++++ database/schedule/list_active_test.go | 10 +++++++++ database/schedule/list_repo_test.go | 10 +++++++++ database/schedule/list_test.go | 10 +++++++++ database/schedule/update.go | 2 +- database/schedule/update_test.go | 10 +++++++++ database/types/schedule.go | 10 +++++++++ database/types/schedule_test.go | 6 ++++++ mock/server/schedule.go | 9 +++++--- 18 files changed, 167 insertions(+), 4 deletions(-) diff --git a/api/types/schedule.go b/api/types/schedule.go index c21568a27..2fb174779 100644 --- a/api/types/schedule.go +++ b/api/types/schedule.go @@ -22,6 +22,7 @@ type Schedule struct { ScheduledAt *int64 `json:"scheduled_at,omitempty"` Branch *string `json:"branch,omitempty"` Error *string `json:"error,omitempty"` + NextRun *int64 `json:"next_run,omitempty"` } // GetID returns the ID field. @@ -180,6 +181,19 @@ func (s *Schedule) GetError() string { return *s.Error } +// GetNextRun returns the NextRun field. +// +// When the provided Schedule type is nil, or the field within +// the type is nil, it returns the zero value for the field. +func (s *Schedule) GetNextRun() int64 { + // return zero value if Schedule type or NextRun field is nil + if s == nil || s.NextRun == nil { + return 0 + } + + return *s.NextRun +} + // SetID sets the ID field. // // When the provided Schedule type is nil, it @@ -336,6 +350,19 @@ func (s *Schedule) SetError(err string) { s.Error = &err } +// SetNextRun sets the NextRun field. +// +// When the provided Schedule type is nil, it +// will set nothing and immediately return. +func (s *Schedule) SetNextRun(nextRun int64) { + // return if Schedule type is nil + if s == nil { + return + } + + s.NextRun = &nextRun +} + // String implements the Stringer interface for the Schedule type. func (s *Schedule) String() string { return fmt.Sprintf(`{ @@ -351,6 +378,7 @@ func (s *Schedule) String() string { UpdatedBy: %s, Branch: %s, Error: %s, + NextRun: %d, }`, s.GetActive(), s.GetCreatedAt(), @@ -364,5 +392,6 @@ func (s *Schedule) String() string { s.GetUpdatedBy(), s.GetBranch(), s.GetError(), + s.GetNextRun(), ) } diff --git a/api/types/schedule_test.go b/api/types/schedule_test.go index ca5bb09db..4d6becdbf 100644 --- a/api/types/schedule_test.go +++ b/api/types/schedule_test.go @@ -81,6 +81,10 @@ func TestTypes_Schedule_Getters(t *testing.T) { if test.schedule.GetError() != test.want.GetError() { t.Errorf("GetError is %v, want %v", test.schedule.GetError(), test.want.GetError()) } + + if test.schedule.GetNextRun() != test.want.GetNextRun() { + t.Errorf("GetNextRun is %v, want %v", test.schedule.GetNextRun(), test.want.GetNextRun()) + } }) } } @@ -123,6 +127,7 @@ func TestTypes_Schedule_Setters(t *testing.T) { test.schedule.SetScheduledAt(test.want.GetScheduledAt()) test.schedule.SetBranch(test.want.GetBranch()) test.schedule.SetError(test.want.GetError()) + test.schedule.SetNextRun(test.want.GetNextRun()) if test.schedule.GetID() != test.want.GetID() { t.Errorf("SetID is %v, want %v", test.schedule.GetID(), test.want.GetID()) @@ -171,6 +176,10 @@ func TestTypes_Schedule_Setters(t *testing.T) { if test.schedule.GetError() != test.want.GetError() { t.Errorf("SetError is %v, want %v", test.schedule.GetError(), test.want.GetError()) } + + if test.schedule.GetNextRun() != test.want.GetNextRun() { + t.Errorf("SetNextRun is %v, want %v", test.schedule.GetNextRun(), test.want.GetNextRun()) + } }) } } @@ -191,6 +200,7 @@ func TestTypes_Schedule_String(t *testing.T) { UpdatedBy: %s, Branch: %s, Error: %s, + NextRun: %d, }`, s.GetActive(), s.GetCreatedAt(), @@ -204,6 +214,7 @@ func TestTypes_Schedule_String(t *testing.T) { s.GetUpdatedBy(), s.GetBranch(), s.GetError(), + s.GetNextRun(), ) got := s.String() @@ -227,6 +238,7 @@ func testSchedule() *Schedule { s.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix()) s.SetBranch("main") s.SetError("unable to trigger build for schedule nightly: unknown character") + s.SetNextRun(time.Now().Add((time.Hour * 2) + 24).UTC().Unix()) return s } diff --git a/database/integration_test.go b/database/integration_test.go index db70091b3..e1bdcffd3 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -10,6 +10,7 @@ import ( "testing" "time" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -2547,6 +2548,9 @@ func newResources() *Resources { pipelineTwo.SetTemplates(false) pipelineTwo.SetData([]byte("version: 1")) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + scheduleOne := new(api.Schedule) scheduleOne.SetID(1) scheduleOne.SetRepo(repoOne) @@ -2560,6 +2564,10 @@ func newResources() *Resources { scheduleOne.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix()) scheduleOne.SetBranch("main") scheduleOne.SetError("no version: YAML property provided") + scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) scheduleTwo := new(api.Schedule) scheduleTwo.SetID(2) @@ -2574,6 +2582,7 @@ func newResources() *Resources { scheduleTwo.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix()) scheduleTwo.SetBranch("main") scheduleTwo.SetError("no version: YAML property provided") + scheduleTwo.SetNextRun(nextTime.Unix()) secretOrg := new(library.Secret) secretOrg.SetID(1) diff --git a/database/schedule/count_active_test.go b/database/schedule/count_active_test.go index 6c496965f..a94af19c6 100644 --- a/database/schedule/count_active_test.go +++ b/database/schedule/count_active_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -49,6 +51,9 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _scheduleOne := testutils.APISchedule() _scheduleOne.SetID(1) _scheduleOne.SetRepo(_repo) @@ -62,6 +67,10 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { _scheduleOne.SetScheduledAt(2013476291) _scheduleOne.SetBranch("main") _scheduleOne.SetError("no version: YAML property provided") + _scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) _scheduleTwo := testutils.APISchedule() _scheduleTwo.SetID(2) @@ -76,6 +85,7 @@ func TestSchedule_Engine_CountActiveSchedules(t *testing.T) { _scheduleTwo.SetScheduledAt(2013476291) _scheduleTwo.SetBranch("main") _scheduleTwo.SetError("no version: YAML property provided") + _scheduleTwo.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/count_repo_test.go b/database/schedule/count_repo_test.go index 360894fc1..2b95abd10 100644 --- a/database/schedule/count_repo_test.go +++ b/database/schedule/count_repo_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -49,6 +51,9 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _scheduleOne := testutils.APISchedule() _scheduleOne.SetID(1) _scheduleOne.SetRepo(_repo) @@ -62,6 +67,10 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { _scheduleOne.SetScheduledAt(2013476291) _scheduleOne.SetBranch("main") _scheduleOne.SetError("no version: YAML property provided") + _scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) _scheduleTwo := testutils.APISchedule() _scheduleTwo.SetID(2) @@ -76,6 +85,7 @@ func TestSchedule_Engine_CountSchedulesForRepo(t *testing.T) { _scheduleTwo.SetScheduledAt(2013476291) _scheduleTwo.SetBranch("main") _scheduleTwo.SetError("no version: YAML property provided") + _scheduleTwo.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/count_test.go b/database/schedule/count_test.go index 0338ff7fb..13be42a30 100644 --- a/database/schedule/count_test.go +++ b/database/schedule/count_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -49,6 +51,9 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _scheduleOne := testutils.APISchedule() _scheduleOne.SetID(1) _scheduleOne.SetRepo(_repo) @@ -62,6 +67,10 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { _scheduleOne.SetScheduledAt(2013476291) _scheduleOne.SetBranch("main") _scheduleOne.SetError("no version: YAML property provided") + _scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) _scheduleTwo := testutils.APISchedule() _scheduleTwo.SetID(2) @@ -76,6 +85,7 @@ func TestSchedule_Engine_CountSchedules(t *testing.T) { _scheduleTwo.SetScheduledAt(2013476291) _scheduleTwo.SetBranch("main") _scheduleTwo.SetError("no version: YAML property provided") + _scheduleTwo.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/create_test.go b/database/schedule/create_test.go index 7858d4014..ac028099e 100644 --- a/database/schedule/create_test.go +++ b/database/schedule/create_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -49,6 +51,9 @@ func TestSchedule_Engine_CreateSchedule(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _schedule := testutils.APISchedule() _schedule.SetID(1) _schedule.SetRepo(_repo) @@ -62,6 +67,7 @@ func TestSchedule_Engine_CreateSchedule(t *testing.T) { _schedule.SetScheduledAt(2013476291) _schedule.SetBranch("main") _schedule.SetError("no version: YAML property provided") + _schedule.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/delete_test.go b/database/schedule/delete_test.go index d633bf38e..971377d92 100644 --- a/database/schedule/delete_test.go +++ b/database/schedule/delete_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database/testutils" @@ -48,6 +50,9 @@ func TestSchedule_Engine_DeleteSchedule(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _schedule := testutils.APISchedule() _schedule.SetID(1) _schedule.SetRepo(_repo) @@ -61,6 +66,7 @@ func TestSchedule_Engine_DeleteSchedule(t *testing.T) { _schedule.SetScheduledAt(2013476291) _schedule.SetBranch("main") _schedule.SetError("no version: YAML property provided") + _schedule.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/get_repo_test.go b/database/schedule/get_repo_test.go index 40a92a761..704443112 100644 --- a/database/schedule/get_repo_test.go +++ b/database/schedule/get_repo_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -50,6 +52,9 @@ func TestSchedule_Engine_GetScheduleForRepo(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _schedule := testutils.APISchedule() _schedule.SetID(1) _schedule.SetRepo(_repo) @@ -63,6 +68,7 @@ func TestSchedule_Engine_GetScheduleForRepo(t *testing.T) { _schedule.SetScheduledAt(2013476291) _schedule.SetBranch("main") _schedule.SetError("no version: YAML property provided") + _schedule.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/get_test.go b/database/schedule/get_test.go index 2f5b81952..5d6f44fca 100644 --- a/database/schedule/get_test.go +++ b/database/schedule/get_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -50,6 +52,9 @@ func TestSchedule_Engine_GetSchedule(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _schedule := testutils.APISchedule() _schedule.SetID(1) _schedule.SetRepo(_repo) @@ -63,6 +68,7 @@ func TestSchedule_Engine_GetSchedule(t *testing.T) { _schedule.SetScheduledAt(2013476291) _schedule.SetBranch("main") _schedule.SetError("no version: YAML property provided") + _schedule.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/list_active_test.go b/database/schedule/list_active_test.go index e3179e020..385cdf595 100644 --- a/database/schedule/list_active_test.go +++ b/database/schedule/list_active_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -50,6 +52,9 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _scheduleOne := testutils.APISchedule() _scheduleOne.SetID(1) _scheduleOne.SetRepo(_repo) @@ -63,6 +68,10 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { _scheduleOne.SetScheduledAt(2013476291) _scheduleOne.SetBranch("main") _scheduleOne.SetError("no version: YAML property provided") + _scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) _scheduleTwo := testutils.APISchedule() _scheduleTwo.SetID(2) @@ -77,6 +86,7 @@ func TestSchedule_Engine_ListActiveSchedules(t *testing.T) { _scheduleTwo.SetScheduledAt(2013476291) _scheduleTwo.SetBranch("main") _scheduleTwo.SetError("no version: YAML property provided") + _scheduleTwo.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/list_repo_test.go b/database/schedule/list_repo_test.go index 43b1fcf58..0719e6b68 100644 --- a/database/schedule/list_repo_test.go +++ b/database/schedule/list_repo_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -50,6 +52,9 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _scheduleOne := testutils.APISchedule() _scheduleOne.SetID(1) _scheduleOne.SetRepo(_repo) @@ -63,6 +68,10 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { _scheduleOne.SetScheduledAt(2013476291) _scheduleOne.SetBranch("main") _scheduleOne.SetError("no version: YAML property provided") + _scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) _scheduleTwo := testutils.APISchedule() _scheduleTwo.SetID(2) @@ -77,6 +86,7 @@ func TestSchedule_Engine_ListSchedulesForRepo(t *testing.T) { _scheduleTwo.SetScheduledAt(2013476291) _scheduleTwo.SetBranch("main") _scheduleTwo.SetError("no version: YAML property provided") + _scheduleTwo.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/list_test.go b/database/schedule/list_test.go index 8338c5dc1..b699a12c4 100644 --- a/database/schedule/list_test.go +++ b/database/schedule/list_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -50,6 +52,9 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _scheduleOne := testutils.APISchedule() _scheduleOne.SetID(1) _scheduleOne.SetRepo(_repo) @@ -63,6 +68,10 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { _scheduleOne.SetScheduledAt(2013476291) _scheduleOne.SetBranch("main") _scheduleOne.SetError("no version: YAML property provided") + _scheduleOne.SetNextRun(nextTime.Unix()) + + currTime = time.Now().UTC() + nextTime, _ = gronx.NextTickAfter("0 * * * *", currTime, false) _scheduleTwo := testutils.APISchedule() _scheduleTwo.SetID(2) @@ -77,6 +86,7 @@ func TestSchedule_Engine_ListSchedules(t *testing.T) { _scheduleTwo.SetScheduledAt(2013476291) _scheduleTwo.SetBranch("main") _scheduleTwo.SetError("no version: YAML property provided") + _scheduleTwo.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/schedule/update.go b/database/schedule/update.go index 790ef89f3..7dcc1256d 100644 --- a/database/schedule/update.go +++ b/database/schedule/update.go @@ -41,7 +41,7 @@ func (e *engine) UpdateSchedule(ctx context.Context, s *api.Schedule, fields boo return nil, err } - // set repo to provided repo if creation successful + // set repo to provided repo if update successful result := schedule.ToAPI() result.SetRepo(s.GetRepo()) diff --git a/database/schedule/update_test.go b/database/schedule/update_test.go index f0b1ea2c0..c936b2ab1 100644 --- a/database/schedule/update_test.go +++ b/database/schedule/update_test.go @@ -5,8 +5,10 @@ package schedule import ( "context" "testing" + "time" "github.com/DATA-DOG/go-sqlmock" + "github.com/adhocore/gronx" "github.com/google/go-cmp/cmp" api "github.com/go-vela/server/api/types" @@ -49,6 +51,9 @@ func TestSchedule_Engine_UpdateSchedule_Config(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _schedule := testutils.APISchedule() _schedule.SetID(1) _schedule.SetRepo(_repo) @@ -62,6 +67,7 @@ func TestSchedule_Engine_UpdateSchedule_Config(t *testing.T) { _schedule.SetScheduledAt(2013476291) _schedule.SetBranch("main") _schedule.SetError("no version: YAML property provided") + _schedule.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() @@ -159,6 +165,9 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) { _repo.SetPreviousName("") _repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + _schedule := testutils.APISchedule() _schedule.SetID(1) _schedule.SetRepo(_repo) @@ -172,6 +181,7 @@ func TestSchedule_Engine_UpdateSchedule_NotConfig(t *testing.T) { _schedule.SetScheduledAt(2013476291) _schedule.SetBranch("main") _schedule.SetError("no version: YAML property provided") + _schedule.SetNextRun(nextTime.Unix()) _postgres, _mock := testPostgres(t) defer func() { _sql, _ := _postgres.client.DB(); _sql.Close() }() diff --git a/database/types/schedule.go b/database/types/schedule.go index 6d2ada21c..ecd578b48 100644 --- a/database/types/schedule.go +++ b/database/types/schedule.go @@ -5,6 +5,7 @@ package types import ( "database/sql" "errors" + "time" "github.com/adhocore/gronx" @@ -87,6 +88,9 @@ func (s *Schedule) Nullify() *Schedule { func (s *Schedule) ToAPI() *api.Schedule { schedule := new(api.Schedule) + t := time.Now().UTC() + nextTime, err := gronx.NextTickAfter(s.Entry.String, t, false) + schedule.SetID(s.ID.Int64) schedule.SetRepo(s.Repo.ToAPI()) schedule.SetActive(s.Active.Bool) @@ -100,6 +104,12 @@ func (s *Schedule) ToAPI() *api.Schedule { schedule.SetBranch(s.Branch.String) schedule.SetError(s.Error.String) + if err == nil { + schedule.SetNextRun(nextTime.Unix()) + } else { + schedule.SetNextRun(0) + } + return schedule } diff --git a/database/types/schedule_test.go b/database/types/schedule_test.go index dcf056218..aa7ad21be 100644 --- a/database/types/schedule_test.go +++ b/database/types/schedule_test.go @@ -8,6 +8,8 @@ import ( "testing" "time" + "github.com/adhocore/gronx" + api "github.com/go-vela/server/api/types" "github.com/go-vela/server/database/testutils" "github.com/go-vela/types/constants" @@ -93,6 +95,9 @@ func TestTypes_Schedule_ToAPI(t *testing.T) { repo.SetPreviousName("oldName") repo.SetApproveBuild(constants.ApproveNever) + currTime := time.Now().UTC() + nextTime, _ := gronx.NextTickAfter("0 0 * * *", currTime, false) + want := testutils.APISchedule() want.SetID(1) want.SetActive(true) @@ -106,6 +111,7 @@ func TestTypes_Schedule_ToAPI(t *testing.T) { want.SetScheduledAt(time.Now().Add(time.Hour * 2).UTC().Unix()) want.SetBranch("main") want.SetError("unable to trigger build for schedule nightly: unknown character") + want.SetNextRun(nextTime.Unix()) // run test got := testSchedule().ToAPI() diff --git a/mock/server/schedule.go b/mock/server/schedule.go index 390aa6b4a..73882e4af 100644 --- a/mock/server/schedule.go +++ b/mock/server/schedule.go @@ -73,7 +73,8 @@ const ( "updated_by": "octocat", "scheduled_at": 0, "branch": "main", - "error": "error message" + "error": "error message", + "next_run": 0 }` SchedulesResp = `[ { @@ -133,7 +134,8 @@ const ( "updated_by": "octocat", "scheduled_at": 0, "branch": "main", - "error": "error message" + "error": "error message", + "next_run": 0 }, { "id": 1, @@ -193,7 +195,8 @@ const ( "scheduled_at": 0, "repo_id": 1, "branch": "main", - "error": "error message" + "error": "error message", + "next_run": 0 }]` )