Skip to content

Commit

Permalink
fix(ketchup): Fixing empty ketchup when not enriched
Browse files Browse the repository at this point in the history
  • Loading branch information
ViBiOh committed Jan 25, 2021
1 parent e8ff069 commit f7e6da9
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
22 changes: 16 additions & 6 deletions pkg/model/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ type Ketchup struct {
// KetchupByRepositoryID sort ketchup by repository ID
type KetchupByRepositoryID []Ketchup

func (a KetchupByRepositoryID) Len() int { return len(a) }
func (a KetchupByRepositoryID) Less(i, j int) bool { return a[i].Repository.ID < a[j].Repository.ID }
func (a KetchupByRepositoryID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a KetchupByRepositoryID) Len() int { return len(a) }
func (a KetchupByRepositoryID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a KetchupByRepositoryID) Less(i, j int) bool {
if a[i].Repository.ID == a[j].Repository.ID {
return a[i].Pattern < a[j].Pattern
}
return a[i].Repository.ID < a[j].Repository.ID
}

// KetchupByPriority sort ketchup by priority (outdated first, name then)
type KetchupByPriority []Ketchup
Expand Down Expand Up @@ -63,9 +68,14 @@ type Release struct {
// ReleaseByRepositoryID sort release by repository ID
type ReleaseByRepositoryID []Release

func (a ReleaseByRepositoryID) Len() int { return len(a) }
func (a ReleaseByRepositoryID) Less(i, j int) bool { return a[i].Repository.ID < a[j].Repository.ID }
func (a ReleaseByRepositoryID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ReleaseByRepositoryID) Len() int { return len(a) }
func (a ReleaseByRepositoryID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ReleaseByRepositoryID) Less(i, j int) bool {
if a[i].Repository.ID == a[j].Repository.ID {
return a[i].Pattern < a[j].Pattern
}
return a[i].Repository.ID < a[j].Repository.ID
}

// NewRelease creates a new version from its objects
func NewRelease(repository Repository, pattern string, version semver.Version) Release {
Expand Down
10 changes: 6 additions & 4 deletions pkg/model/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ func TestKetchupByRepositoryID(t *testing.T) {
"simple",
args{
array: []Ketchup{
{Repository: Repository{ID: 10}},
{Repository: Repository{ID: 1}},
{Pattern: DefaultPattern, Repository: Repository{ID: 10}},
{Pattern: DefaultPattern, Repository: Repository{ID: 1}},
{Pattern: "latest", Repository: Repository{ID: 1}},
},
},
[]Ketchup{
{Repository: Repository{ID: 1}},
{Repository: Repository{ID: 10}},
{Pattern: "latest", Repository: Repository{ID: 1}},
{Pattern: DefaultPattern, Repository: Repository{ID: 1}},
{Pattern: DefaultPattern, Repository: Repository{ID: 10}},
},
},
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (a app) Start(done <-chan struct{}) {

func (a app) ketchupNotify(_ time.Time) error {
logger.Info("Starting ketchup notifier")
defer logger.Info("Ending ketchup notifier")

ctx := authModel.StoreUser(context.Background(), authModel.NewUser(a.loginID, "scheduler"))

Expand Down Expand Up @@ -194,7 +195,7 @@ func (a app) getKetchupToNotify(ctx context.Context, releases []model.Release) (
for _, release := range releases {
for index < size {
current := ketchups[index]
if release.Repository.ID < current.Repository.ID {
if release.Repository.ID < current.Repository.ID || release.Pattern < current.Pattern {
break
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func TestGetNewReleases(t *testing.T) {
func TestGetKetchupToNotify(t *testing.T) {
firstRelease := []model.Release{
{
Pattern: model.DefaultPattern,
Version: semver.Version{
Name: "1.0.1",
},
Expand All @@ -174,6 +175,7 @@ func TestGetKetchupToNotify(t *testing.T) {

secondRelease := []model.Release{
{
Pattern: model.DefaultPattern,
Version: semver.Version{
Name: "1.0.1",
},
Expand All @@ -184,6 +186,7 @@ func TestGetKetchupToNotify(t *testing.T) {
},
},
{
Pattern: model.DefaultPattern,
Version: semver.Version{
Name: "1.0.1",
},
Expand Down
32 changes: 18 additions & 14 deletions pkg/service/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (a app) List(ctx context.Context, page, pageSize uint) ([]model.Ketchup, ui
return nil, 0, httpModel.WrapInternal(fmt.Errorf("unable to list: %w", err))
}

enrichedList := enrichSemver(list)
enrichedList := enrichKetchupsWithSemver(list)
sort.Sort(model.KetchupByPriority(enrichedList))

return enrichedList, total, nil
Expand All @@ -60,7 +60,7 @@ func (a app) ListForRepositories(ctx context.Context, repositories []model.Repos
return nil, httpModel.WrapInternal(fmt.Errorf("unable to list by ids: %w", err))
}

return enrichSemver(list), nil
return enrichKetchupsWithSemver(list), nil
}

func (a app) Create(ctx context.Context, item model.Ketchup) (model.Ketchup, error) {
Expand Down Expand Up @@ -170,23 +170,27 @@ func (a app) check(ctx context.Context, old, new model.Ketchup) error {
return service.ConcatError(output)
}

func enrichSemver(list []model.Ketchup) []model.Ketchup {
func enrichKetchupsWithSemver(list []model.Ketchup) []model.Ketchup {
output := make([]model.Ketchup, len(list))

for index, item := range list {
repositoryVersion, repositoryErr := semver.Parse(item.Repository.Versions[item.Pattern])
if repositoryErr != nil {
continue
}
output[index] = enrichKetchupWithSemver(item)
}

ketchupVersion, ketchupErr := semver.Parse(item.Version)
if ketchupErr != nil {
continue
}
return output
}

item.Semver = repositoryVersion.Compare(ketchupVersion)
output[index] = item
func enrichKetchupWithSemver(item model.Ketchup) model.Ketchup {
repositoryVersion, repositoryErr := semver.Parse(item.Repository.Versions[item.Pattern])
if repositoryErr != nil {
return item
}

return output
ketchupVersion, ketchupErr := semver.Parse(item.Version)
if ketchupErr != nil {
return item
}

item.Semver = repositoryVersion.Compare(ketchupVersion)
return item
}
10 changes: 5 additions & 5 deletions pkg/service/ketchup/ketchuptest/ketchuptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ func (tks app) ListForRepositories(ctx context.Context, repositories []model.Rep

if repositories[0].Name == "vibioh/ketchup" {
return []model.Ketchup{
{Repository: repositories[0], User: model.User{ID: 1, Email: "nobody@localhost"}},
{Repository: repositories[0], User: model.User{ID: 2, Email: "guest@nowhere"}},
{Pattern: model.DefaultPattern, Repository: repositories[0], User: model.User{ID: 1, Email: "nobody@localhost"}},
{Pattern: model.DefaultPattern, Repository: repositories[0], User: model.User{ID: 2, Email: "guest@nowhere"}},
}, nil
}

if repositories[0].Name == "vibioh/viws" {
return []model.Ketchup{
{Repository: repositories[0], User: model.User{ID: 1, Email: "nobody@localhost"}},
{Repository: repositories[0], User: model.User{ID: 2, Email: "guest@nowhere"}},
{Repository: repositories[1], User: model.User{ID: 2, Email: "guest@nowhere"}},
{Pattern: model.DefaultPattern, Repository: repositories[0], User: model.User{ID: 1, Email: "nobody@localhost"}},
{Pattern: model.DefaultPattern, Repository: repositories[0], User: model.User{ID: 2, Email: "guest@nowhere"}},
{Pattern: model.DefaultPattern, Repository: repositories[1], User: model.User{ID: 2, Email: "guest@nowhere"}},
}, nil
}

Expand Down

0 comments on commit f7e6da9

Please sign in to comment.