Skip to content

Commit

Permalink
refactor: Factorizing auto-update behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Oct 12, 2021
1 parent 4f281d4 commit a480d43
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
28 changes: 15 additions & 13 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,7 @@ func (a App) syncReleasesByUser(releases []model.Release, ketchups []model.Ketch
usersToNotify[current.User] = []model.Release{release}
}

if current.UpdateWhenNotify {
log := logger.WithField("repository", current.Repository.ID).WithField("user", current.User.ID).WithField("pattern", current.Pattern)

log.Info("Auto-updating ketchup to %s", release.Version.Name)
if err := a.ketchupService.UpdateVersion(context.Background(), current.Repository.ID, current.User.ID, current.Pattern, release.Version.Name); err != nil {
log.Error("unable to update ketchup version: %s", err)
}
}
a.handleKetchupNotification(current, release.Version.Name)
}
}
}
Expand All @@ -230,11 +223,20 @@ func (a App) addWeeklyKetchups(ketchups []model.Ketchup, usersToNotify map[model
usersToNotify[ketchup.User] = []model.Release{release}
}

if ketchup.UpdateWhenNotify {
if err := a.ketchupService.UpdateVersion(context.Background(), ketchup.Repository.ID, ketchup.User.ID, ketchup.Pattern, release.Version.Name); err != nil {
logger.Error("unable to update ketchup user=%d repository=%d: %s", ketchup.User.ID, ketchup.Repository.ID, err)
}
}
a.handleKetchupNotification(ketchup, release.Version.Name)
}
}

func (a App) handleKetchupNotification(ketchup model.Ketchup, version string) {
if !ketchup.UpdateWhenNotify {
return
}

log := logger.WithField("repository", ketchup.Repository.ID).WithField("user", ketchup.User.ID).WithField("pattern", ketchup.Pattern)

log.Info("Auto-updating ketchup to %s", version)
if err := a.ketchupService.UpdateVersion(context.Background(), ketchup.Repository.ID, ketchup.User.ID, ketchup.Pattern, version); err != nil {
logger.Error("unable to update ketchup user=%d repository=%d: %s", ketchup.User.ID, ketchup.Repository.ID, err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/store/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (a App) ListByRepositoriesID(ctx context.Context, ids []uint64, frequency m
return list, a.db.List(ctx, scanner, listByRepositoriesIDQuery, ids, strings.ToLower(frequency.String()))
}

const listOutdateByFrequencyQuery = `
const listOutdatedByFrequencyQuery = `
SELECT
rv.pattern,
rv.version,
Expand Down Expand Up @@ -226,7 +226,7 @@ func (a App) ListOutdatedByFrequency(ctx context.Context, frequency model.Ketchu
return nil
}

return list, a.db.List(ctx, scanner, listOutdateByFrequencyQuery, strings.ToLower(frequency.String()))
return list, a.db.List(ctx, scanner, listOutdatedByFrequencyQuery, strings.ToLower(frequency.String()))
}

const getQuery = `
Expand Down

0 comments on commit a480d43

Please sign in to comment.