Skip to content

Commit

Permalink
enhance(schedule): add next run field to schedule (#1131)
Browse files Browse the repository at this point in the history
* Add next run field to Schedule

* Lint code

* Update integration test

* Populate next run field in ToAPI

* Handle err from gronx NextTickAfter

* Fix conditional
  • Loading branch information
wsan3 authored May 17, 2024
1 parent 178e678 commit c35d01e
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 4 deletions.
29 changes: 29 additions & 0 deletions api/types/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(`{
Expand All @@ -351,6 +378,7 @@ func (s *Schedule) String() string {
UpdatedBy: %s,
Branch: %s,
Error: %s,
NextRun: %d,
}`,
s.GetActive(),
s.GetCreatedAt(),
Expand All @@ -364,5 +392,6 @@ func (s *Schedule) String() string {
s.GetUpdatedBy(),
s.GetBranch(),
s.GetError(),
s.GetNextRun(),
)
}
12 changes: 12 additions & 0 deletions api/types/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
}
})
}
}
Expand All @@ -191,6 +200,7 @@ func TestTypes_Schedule_String(t *testing.T) {
UpdatedBy: %s,
Branch: %s,
Error: %s,
NextRun: %d,
}`,
s.GetActive(),
s.GetCreatedAt(),
Expand All @@ -204,6 +214,7 @@ func TestTypes_Schedule_String(t *testing.T) {
s.GetUpdatedBy(),
s.GetBranch(),
s.GetError(),
s.GetNextRun(),
)

got := s.String()
Expand All @@ -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
}
9 changes: 9 additions & 0 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions database/schedule/count_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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() }()
Expand Down
10 changes: 10 additions & 0 deletions database/schedule/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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() }()
Expand Down
10 changes: 10 additions & 0 deletions database/schedule/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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() }()
Expand Down
6 changes: 6 additions & 0 deletions database/schedule/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
6 changes: 6 additions & 0 deletions database/schedule/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
6 changes: 6 additions & 0 deletions database/schedule/get_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
6 changes: 6 additions & 0 deletions database/schedule/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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() }()
Expand Down
Loading

0 comments on commit c35d01e

Please sign in to comment.