Skip to content

Commit

Permalink
refactor: Using breaksync for notifier
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 19, 2022
1 parent 645189c commit 41e8c4d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
10 changes: 10 additions & 0 deletions pkg/model/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func NewKetchup(pattern, version string, frequency KetchupFrequency, updateWhenN
}
}

// Key for breaksync
func (k Ketchup) Key() string {
return fmt.Sprintf("%d|%s", k.Repository.ID, k.Pattern)
}

// WithID generate ID of the ketchup
func (k Ketchup) WithID() Ketchup {
k.ID = sha.New(k)[:8]
Expand Down Expand Up @@ -130,6 +135,11 @@ func NewRelease(repository Repository, pattern string, version semver.Version) R
}
}

// Key for breaksync
func (r Release) Key() string {
return fmt.Sprintf("%d|%s", r.Repository.ID, r.Pattern)
}

// SetUpdated marks released as auto updated
func (r Release) SetUpdated(status uint) Release {
r.Updated = status
Expand Down
33 changes: 16 additions & 17 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/ViBiOh/flags"
"github.com/ViBiOh/httputils/v4/pkg/breaksync"
"github.com/ViBiOh/httputils/v4/pkg/clock"
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/ketchup/pkg/model"
Expand Down Expand Up @@ -167,27 +168,25 @@ func (a App) syncReleasesByUser(ctx context.Context, releases []model.Release, k
sort.Sort(model.ReleaseByRepositoryIDAndPattern(releases))
sort.Sort(model.KetchupByRepositoryIDAndPattern(ketchups))

index := 0
size := len(ketchups)
releaseRupture := breaksync.NewRupture("release", func(releaseID string) string {
return releaseID
})

for _, release := range releases {
for index < size {
current := ketchups[index]

if release.Repository.ID < current.Repository.ID || (release.Repository.ID == current.Repository.ID && release.Pattern < current.Pattern) {
break // release is out of sync, we need to go forward release
}
err := breaksync.NewSynchronization().AddSources(breaksync.NewSliceSource(releases, releaseRupture), breaksync.NewSliceSource(ketchups, nil)).AddRuptures(releaseRupture).Run(func(synchronised uint64, items []any) error {
if synchronised != 0 {
return nil
}

index++
release := items[0].(model.Release)
ketchup := items[1].(model.Ketchup)

if release.Repository.ID != current.Repository.ID || release.Pattern != current.Pattern {
continue // ketchup is not sync with release, we need for go forward ketchup
}

if current.Version != release.Version.Name {
a.handleKetchupNotification(ctx, usersToNotify, current, release)
}
if ketchup.Version != release.Version.Name {
a.handleKetchupNotification(ctx, usersToNotify, ketchup, release)
}
return nil
})
if err != nil {
logger.Error("unable to synchronise releases and ketchups: %s", err)
}

return usersToNotify
Expand Down

0 comments on commit 41e8c4d

Please sign in to comment.