Skip to content

Commit

Permalink
feat(notifier): Handling no notify and update when notify case
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Dec 7, 2021
1 parent ff21055 commit ed61bf2
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/mocks/ketchup_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/mocks/ketchup_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/model/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type KetchupService interface {
List(ctx context.Context, pageSize uint, last string) ([]Ketchup, uint64, error)
ListForRepositories(ctx context.Context, repositories []Repository, frequency KetchupFrequency) ([]Ketchup, error)
ListOutdatedByFrequency(ctx context.Context, frequency KetchupFrequency, users ...User) ([]Ketchup, error)
ListSilentForRepositories(ctx context.Context, repositories []Repository) ([]Ketchup, error)
Create(ctx context.Context, item Ketchup) (Ketchup, error)
Update(ctx context.Context, oldPattern string, item Ketchup) (Ketchup, error)
UpdateAll(ctx context.Context) error
Expand All @@ -99,6 +100,7 @@ type KetchupStore interface {
List(ctx context.Context, page uint, last string) ([]Ketchup, uint64, error)
ListByRepositoriesID(ctx context.Context, ids []uint64, frequency KetchupFrequency) ([]Ketchup, error)
ListOutdatedByFrequency(ctx context.Context, frequency KetchupFrequency, usersIds ...uint64) ([]Ketchup, error)
ListSilentForRepositories(ctx context.Context, ids []uint64) ([]Ketchup, error)
GetByRepository(ctx context.Context, id uint64, pattern string, forUpdate bool) (Ketchup, error)
Create(ctx context.Context, o Ketchup) (uint64, error)
Update(ctx context.Context, o Ketchup, oldPattern string) error
Expand Down
30 changes: 30 additions & 0 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (a App) getKetchupToNotify(ctx context.Context, releases []model.Release) (
a.groupKetchupsToUsers(weeklyKetchups, userToNotify)
}

if err = a.updateSilentKetchup(ctx, repositories); err != nil {
logger.Error("unable to update silent ketchups: %s", err)
}

logger.Info("%d users to notify", len(userToNotify))

return userToNotify, nil
Expand Down Expand Up @@ -206,6 +210,32 @@ func (a App) syncReleasesByUser(releases []model.Release, ketchups []model.Ketch
return usersToNotify
}

func (a App) updateSilentKetchup(ctx context.Context, repositories []model.Repository) error {
ketchups, err := a.ketchupService.ListSilentForRepositories(ctx, repositories)
if err != nil {
return fmt.Errorf("unable to fetch silent ketchups: %s", err)
}

for _, ketchup := range ketchups {
log := logger.WithField("repository", ketchup.Repository.ID).WithField("user", ketchup.User.ID).WithField("pattern", ketchup.Pattern)

var releaseVersion string
if version, ok := ketchup.Repository.Versions[ketchup.Pattern]; !ok {
logger.Warn("version for pattern `%s` not found for ketchup %s", ketchup.Pattern, ketchup.ID)
continue
} else {
releaseVersion = version
}

log.Info("Auto-updating ketchup to %s", releaseVersion)
if err := a.ketchupService.UpdateVersion(context.Background(), ketchup.User.ID, ketchup.Repository.ID, ketchup.Pattern, releaseVersion); err != nil {
return fmt.Errorf("unable to update ketchup version for ketchup %s: %s", ketchup.ID, err)
}
}

return nil
}

func (a App) groupKetchupsToUsers(ketchups []model.Ketchup, usersToNotify map[model.User][]model.Release) {
for _, ketchup := range ketchups {
ketchupVersion, err := semver.Parse(ketchup.Version)
Expand Down
2 changes: 2 additions & 0 deletions pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,10 @@ func TestGetKetchupToNotify(t *testing.T) {
case "list error":
mockKetchupService.EXPECT().ListForRepositories(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("failed"))
case "empty":
mockKetchupService.EXPECT().ListSilentForRepositories(gomock.Any(), gomock.Any()).Return(nil, nil)
mockKetchupService.EXPECT().ListForRepositories(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
case "one release, n ketchups":
mockKetchupService.EXPECT().ListSilentForRepositories(gomock.Any(), gomock.Any()).Return(nil, nil)
mockKetchupService.EXPECT().ListForRepositories(gomock.Any(), gomock.Any(), gomock.Any()).Return([]model.Ketchup{
{
Pattern: model.DefaultPattern,
Expand Down
15 changes: 15 additions & 0 deletions pkg/service/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func (a App) ListForRepositories(ctx context.Context, repositories []model.Repos
return enrichKetchupsWithSemver(list), nil
}

// ListSilentForRepositories lists no notify but auto-update ketchups
func (a App) ListSilentForRepositories(ctx context.Context, repositories []model.Repository) ([]model.Ketchup, error) {
ids := make([]uint64, len(repositories))
for index, repo := range repositories {
ids[index] = repo.ID
}

list, err := a.ketchupStore.ListSilentForRepositories(ctx, ids)
if err != nil {
return nil, httpModel.WrapInternal(fmt.Errorf("unable to list silent by ids: %s", err))
}

return enrichKetchupsWithSemver(list), nil
}

// ListOutdatedByFrequency ketchups outdated
func (a App) ListOutdatedByFrequency(ctx context.Context, frequency model.KetchupFrequency, users ...model.User) ([]model.Ketchup, error) {
usersIds := make([]uint64, len(users))
Expand Down
44 changes: 44 additions & 0 deletions pkg/store/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,50 @@ func (a App) ListOutdatedByFrequency(ctx context.Context, frequency model.Ketchu
return list, a.db.List(ctx, scanner, query, params...)
}

const listSilentForRepositoriesQuery = `
SELECT
k.pattern,
k.version,
k.frequency,
k.update_when_notify,
k.repository_id,
k.user_id,
u.email
FROM
ketchup.ketchup k,
ketchup.user u
WHERE
repository_id = ANY ($1)
AND k.frequency = $2
AND k.update_when_notify IS TRUE
`

// ListSilentForRepositories retrieves ketchup with no notification and auto-update
func (a App) ListSilentForRepositories(ctx context.Context, ids []uint64) ([]model.Ketchup, error) {
var list []model.Ketchup

scanner := func(rows pgx.Rows) error {
var item model.Ketchup
item.Repository = model.NewRepository(0, 0, "", "")
var rawKetchupFrequency string

if err := rows.Scan(&item.Pattern, &item.Version, &rawKetchupFrequency, &item.UpdateWhenNotify, &item.Repository.ID, &item.User.ID, &item.User.Email); err != nil {
return err
}

ketchupFrequency, err := model.ParseKetchupFrequency(rawKetchupFrequency)
if err != nil {
return err
}
item.Frequency = ketchupFrequency

list = append(list, item)
return nil
}

return list, a.db.List(ctx, scanner, listByRepositoriesIDQuery, ids, model.None)
}

const getQuery = `
SELECT
k.pattern,
Expand Down

0 comments on commit ed61bf2

Please sign in to comment.