Skip to content

Commit

Permalink
refactor: Don't return interface, act II
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 8, 2021
1 parent c8260f0 commit 786e05b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
29 changes: 13 additions & 16 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,7 @@ type Mailer interface {
}

// App of package
type App interface {
Notify(context.Context) error
}

// Config of package
type Config struct {
loginID *uint
pushURL *string
}

type app struct {
type App struct {
repositoryService repository.App
ketchupService ketchup.App
userService user.App
Expand All @@ -55,6 +45,12 @@ type app struct {
loginID uint64
}

// Config of package
type Config struct {
loginID *uint
pushURL *string
}

// Flags adds flags for configuring package
func Flags(fs *flag.FlagSet, prefix string) Config {
return Config{
Expand All @@ -65,7 +61,7 @@ func Flags(fs *flag.FlagSet, prefix string) Config {

// New creates new App from Config
func New(config Config, repositoryService repository.App, ketchupService ketchup.App, userService user.App, mailerApp Mailer, helmApp helm.App) App {
return app{
return App{
loginID: uint64(*config.loginID),
pushURL: strings.TrimSpace(*config.pushURL),

Expand All @@ -77,7 +73,8 @@ func New(config Config, repositoryService repository.App, ketchupService ketchup
}
}

func (a app) Notify(ctx context.Context) error {
// Notify users for new ketchup
func (a App) Notify(ctx context.Context) error {
userCtx := authModel.StoreUser(ctx, authModel.NewUser(a.loginID, "scheduler"))

if err := a.repositoryService.Clean(userCtx); err != nil {
Expand Down Expand Up @@ -125,7 +122,7 @@ func (a app) Notify(ctx context.Context) error {
return nil
}

func (a app) updateRepositories(ctx context.Context, releases []model.Release) error {
func (a App) updateRepositories(ctx context.Context, releases []model.Release) error {
if len(releases) == 0 {
return nil
}
Expand All @@ -151,7 +148,7 @@ func (a app) updateRepositories(ctx context.Context, releases []model.Release) e
return nil
}

func (a app) getKetchupToNotify(ctx context.Context, releases []model.Release) (map[model.User][]model.Release, error) {
func (a App) getKetchupToNotify(ctx context.Context, releases []model.Release) (map[model.User][]model.Release, error) {
repositories := make([]model.Repository, len(releases))
for index, release := range releases {
repositories[index] = release.Repository
Expand Down Expand Up @@ -234,7 +231,7 @@ func addWeeklyKetchups(ketchups []model.Ketchup, usersToNotify map[model.User][]
}
}

func (a app) sendNotification(ctx context.Context, ketchupToNotify map[model.User][]model.Release) error {
func (a App) sendNotification(ctx context.Context, ketchupToNotify map[model.User][]model.Release) error {
if len(ketchupToNotify) == 0 {
return nil
}
Expand Down
32 changes: 16 additions & 16 deletions pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ func TestGetNewRepositoryReleases(t *testing.T) {

var cases = []struct {
intention string
instance app
instance App
args args
want []model.Release
}{
{
"empty",
app{
App{
repositoryService: repositorytest.New().SetLatestVersions(nil, nil),
},
args{
Expand All @@ -84,7 +84,7 @@ func TestGetNewRepositoryReleases(t *testing.T) {
},
{
"no new",
app{
App{
repositoryService: repositorytest.New().SetLatestVersions(map[string]semver.Version{
model.DefaultPattern: safeParse(repositoryVersion),
}, nil),
Expand All @@ -96,7 +96,7 @@ func TestGetNewRepositoryReleases(t *testing.T) {
},
{
"invalid version",
app{
App{
repositoryService: repositorytest.New().SetLatestVersions(map[string]semver.Version{
model.DefaultPattern: safeParse(repositoryVersion),
}, nil),
Expand All @@ -108,7 +108,7 @@ func TestGetNewRepositoryReleases(t *testing.T) {
},
{
"not greater",
app{
App{
repositoryService: repositorytest.New().SetLatestVersions(map[string]semver.Version{
model.DefaultPattern: safeParse(repositoryVersion),
}, nil),
Expand All @@ -120,7 +120,7 @@ func TestGetNewRepositoryReleases(t *testing.T) {
},
{
"greater",
app{
App{
repositoryService: repositorytest.New().SetLatestVersions(map[string]semver.Version{
model.DefaultPattern: safeParse("1.1.0"),
}, nil),
Expand Down Expand Up @@ -151,14 +151,14 @@ func TestGetKetchupToNotify(t *testing.T) {

var cases = []struct {
intention string
instance app
instance App
args args
want map[model.User][]model.Release
wantErr error
}{
{
"list error",
app{ketchupService: ketchuptest.New().SetListForRepositories(nil, errors.New("failed"))},
App{ketchupService: ketchuptest.New().SetListForRepositories(nil, errors.New("failed"))},
args{
ctx: context.Background(),
},
Expand All @@ -167,7 +167,7 @@ func TestGetKetchupToNotify(t *testing.T) {
},
{
"empty",
app{ketchupService: ketchuptest.New()},
App{ketchupService: ketchuptest.New()},
args{
ctx: context.Background(),
},
Expand All @@ -176,7 +176,7 @@ func TestGetKetchupToNotify(t *testing.T) {
},
{
"one release, n ketchups",
app{ketchupService: ketchuptest.New().SetListForRepositories([]model.Ketchup{
App{ketchupService: ketchuptest.New().SetListForRepositories([]model.Ketchup{
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(1, repositoryName),
Expand Down Expand Up @@ -303,13 +303,13 @@ func TestSendNotification(t *testing.T) {

var cases = []struct {
intention string
instance app
instance App
args args
wantErr error
}{
{
"empty",
app{},
App{},
args{
ctx: context.Background(),
ketchupToNotify: nil,
Expand All @@ -318,7 +318,7 @@ func TestSendNotification(t *testing.T) {
},
{
"no mailer",
app{},
App{},
args{
ctx: context.Background(),
ketchupToNotify: map[model.User][]model.Release{
Expand All @@ -339,7 +339,7 @@ func TestSendNotification(t *testing.T) {
},
{
"mailer disabled",
app{},
App{},
args{
ctx: context.Background(),
ketchupToNotify: map[model.User][]model.Release{
Expand All @@ -360,7 +360,7 @@ func TestSendNotification(t *testing.T) {
},
{
"mailer error",
app{},
App{},
args{
ctx: context.TODO(),
ketchupToNotify: map[model.User][]model.Release{
Expand All @@ -381,7 +381,7 @@ func TestSendNotification(t *testing.T) {
},
{
"multiple releases",
app{},
App{},
args{
ctx: context.Background(),
ketchupToNotify: map[model.User][]model.Release{
Expand Down
10 changes: 5 additions & 5 deletions pkg/notifier/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ViBiOh/ketchup/pkg/semver"
)

func (a app) getNewReleases(ctx context.Context) ([]model.Release, uint64, error) {
func (a App) getNewReleases(ctx context.Context) ([]model.Release, uint64, error) {
releases, releasesCount, err := a.getNewStandardReleases(ctx)
if err != nil {
return nil, 0, err
Expand All @@ -23,7 +23,7 @@ func (a app) getNewReleases(ctx context.Context) ([]model.Release, uint64, error
return append(releases, helmReleases...), releasesCount + helmCount, nil
}

func (a app) getNewStandardReleases(ctx context.Context) ([]model.Release, uint64, error) {
func (a App) getNewStandardReleases(ctx context.Context) ([]model.Release, uint64, error) {
var newReleases []model.Release
var count uint64
var last string
Expand Down Expand Up @@ -56,7 +56,7 @@ func (a app) getNewStandardReleases(ctx context.Context) ([]model.Release, uint6
return newReleases, count, nil
}

func (a app) getNewRepositoryReleases(repo model.Repository) []model.Release {
func (a App) getNewRepositoryReleases(repo model.Repository) []model.Release {
versions, err := a.repositoryService.LatestVersions(repo)
if err != nil {
logger.Error("unable to get latest versions of %s `%s`: %s", repo.Kind, repo.Name, err)
Expand All @@ -72,7 +72,7 @@ func (a app) getNewRepositoryReleases(repo model.Repository) []model.Release {
return releases
}

func (a app) getNewHelmReleases(ctx context.Context) ([]model.Release, uint64, error) {
func (a App) getNewHelmReleases(ctx context.Context) ([]model.Release, uint64, error) {
var newReleases []model.Release
var count uint64
var last string
Expand Down Expand Up @@ -118,7 +118,7 @@ func (a app) getNewHelmReleases(ctx context.Context) ([]model.Release, uint64, e
return newReleases, count, nil
}

func (a app) getFetchHelmSources(repos map[string]model.Repository) []model.Release {
func (a App) getFetchHelmSources(repos map[string]model.Repository) []model.Release {
if len(repos) == 0 {
return nil
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/notifier/releases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func TestGetNewStandardReleases(t *testing.T) {

var cases = []struct {
intention string
instance app
instance App
args args
want []model.Release
wantErr error
}{
{
"list error",
app{
App{
repositoryService: repositorytest.New().SetListByKinds(nil, 0, errors.New("failed")),
},
args{
Expand All @@ -39,7 +39,7 @@ func TestGetNewStandardReleases(t *testing.T) {
},
{
"github error",
app{
App{
repositoryService: repositorytest.New().SetListByKinds([]model.Repository{
model.NewGithubRepository(1, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion)}, 1, nil).SetLatestVersions(nil, errors.New("failed")),
},
Expand All @@ -51,7 +51,7 @@ func TestGetNewStandardReleases(t *testing.T) {
},
{
"same version",
app{
App{
repositoryService: repositorytest.New().SetListByKinds([]model.Repository{
model.NewGithubRepository(1, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion),
}, 1, nil).SetLatestVersions(map[string]semver.Version{
Expand All @@ -68,7 +68,7 @@ func TestGetNewStandardReleases(t *testing.T) {
},
{
"success",
app{
App{
repositoryService: repositorytest.New().SetListByKinds([]model.Repository{
model.NewGithubRepository(1, repositoryName).AddVersion(model.DefaultPattern, repositoryVersion),
}, 1, nil).SetLatestVersions(map[string]semver.Version{
Expand Down Expand Up @@ -124,15 +124,15 @@ func TestGetNewHelmReleases(t *testing.T) {

var cases = []struct {
intention string
instance app
instance App
args args
want []model.Release
wantCount uint64
wantErr error
}{
{
"fetch error",
app{
App{
repositoryService: repositorytest.New().SetListByKinds(nil, 0, errors.New("db error")),
helmApp: helmtest.New(),
},
Expand All @@ -145,7 +145,7 @@ func TestGetNewHelmReleases(t *testing.T) {
},
{
"no repository",
app{
App{
repositoryService: repositorytest.New().SetListByKinds(nil, 0, nil),
helmApp: helmtest.New(),
},
Expand All @@ -158,7 +158,7 @@ func TestGetNewHelmReleases(t *testing.T) {
},
{
"helm error",
app{
App{
repositoryService: repositorytest.New().SetListByKinds([]model.Repository{
model.NewHelmRepository(1, "https://charts.vibioh.fr", "app"),
model.NewHelmRepository(1, "https://charts.vibioh.fr", "cron"),
Expand All @@ -176,7 +176,7 @@ func TestGetNewHelmReleases(t *testing.T) {
},
{
"helm",
app{
App{
repositoryService: repositorytest.New().SetListByKinds([]model.Repository{
postgresRepo,
appRepo,
Expand Down

0 comments on commit 786e05b

Please sign in to comment.