Skip to content

Commit

Permalink
fix(ketchup): Handling pattern update from UI
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <bob@vibioh.fr>
  • Loading branch information
ViBiOh committed Sep 13, 2021
1 parent 054c414 commit 60dd3e4
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 187 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ style:
.PHONY: mocks
mocks:
find . -name "mocks" -type d -exec rm -r "{}" \+
mockgen -destination pkg/mocks/model.go -mock_names Mailer=Mailer,AuthService=AuthService,UserService=UserService,UserStore=UserStore,HelmProvider=HelmProvider,RepositoryService=RepositoryService -package mocks github.com/ViBiOh/ketchup/pkg/model Mailer,AuthService,UserService,UserStore,HelmProvider,RepositoryService
mockgen -destination pkg/mocks/model.go -mock_names Mailer=Mailer,AuthService=AuthService,UserService=UserService,UserStore=UserStore,HelmProvider=HelmProvider,RepositoryService=RepositoryService,KetchupService=KetchupService -package mocks github.com/ViBiOh/ketchup/pkg/model Mailer,AuthService,UserService,UserStore,HelmProvider,RepositoryService,KetchupService

## test: Shortcut to launch all the test tasks (unit, functional and integration).
.PHONY: test
Expand Down
1 change: 1 addition & 0 deletions cmd/ketchup/templates/ketchup.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ <h2 class="header">Edit ketchup</h2>

<form method="POST" action="/app/ketchups/{{ .Repository.ID }}">
<input type="hidden" name="method" value="PUT">
<input type="hidden" name="old-pattern" value="{{ .Pattern }}">

<p class="padding no-margin">
<label for="edit-pattern-{{ .ID }}" class="block">Pattern: <img class="icon icon-small" title="'latest': latest version, beta included.
Expand Down
2 changes: 1 addition & 1 deletion pkg/ketchup/ketchups.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (a App) handleUpdate(w http.ResponseWriter, r *http.Request) {

item := model.NewKetchup(r.FormValue("pattern"), r.FormValue("version"), ketchupFrequency, model.NewGithubRepository(id, "")).WithID()

updated, err := a.ketchupService.Update(r.Context(), item)
updated, err := a.ketchupService.Update(r.Context(), r.FormValue("old-pattern"), item)
if err != nil {
a.rendererApp.Error(w, err)
return
Expand Down
129 changes: 128 additions & 1 deletion pkg/mocks/model.go

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

11 changes: 11 additions & 0 deletions pkg/model/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ type RepositoryService interface {
Clean(context.Context) error
LatestVersions(Repository) (map[string]semver.Version, error)
}

// KetchupService defines interaction with ketchup
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) ([]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
Delete(ctx context.Context, item Ketchup) error
}
5 changes: 2 additions & 3 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/ViBiOh/httputils/v4/pkg/logger"
"github.com/ViBiOh/ketchup/pkg/model"
"github.com/ViBiOh/ketchup/pkg/semver"
"github.com/ViBiOh/ketchup/pkg/service/ketchup"
"github.com/ViBiOh/ketchup/pkg/service/user"
mailerModel "github.com/ViBiOh/mailer/pkg/model"
"github.com/prometheus/client_golang/prometheus/push"
Expand All @@ -27,7 +26,7 @@ var (
// App of package
type App struct {
repositoryService model.RepositoryService
ketchupService ketchup.App
ketchupService model.KetchupService
userService user.App
mailerApp model.Mailer
helmApp model.HelmProvider
Expand All @@ -53,7 +52,7 @@ func Flags(fs *flag.FlagSet, prefix string) Config {
}

// New creates new App from Config
func New(config Config, repositoryService model.RepositoryService, ketchupService ketchup.App, userService user.App, mailerApp model.Mailer, helmApp model.HelmProvider) App {
func New(config Config, repositoryService model.RepositoryService, ketchupService model.KetchupService, userService user.App, mailerApp model.Mailer, helmApp model.HelmProvider) App {
return App{
loginID: uint64(*config.loginID),
pushURL: strings.TrimSpace(*config.pushURL),
Expand Down
99 changes: 58 additions & 41 deletions pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ViBiOh/ketchup/pkg/mocks"
"github.com/ViBiOh/ketchup/pkg/model"
"github.com/ViBiOh/ketchup/pkg/semver"
"github.com/ViBiOh/ketchup/pkg/service/ketchup/ketchuptest"
"github.com/golang/mock/gomock"
)

Expand Down Expand Up @@ -167,7 +166,7 @@ func TestGetKetchupToNotify(t *testing.T) {
}{
{
"list error",
App{ketchupService: ketchuptest.New().SetListForRepositories(nil, errors.New("failed"))},
App{},
args{
ctx: context.Background(),
},
Expand All @@ -176,7 +175,7 @@ func TestGetKetchupToNotify(t *testing.T) {
},
{
"empty",
App{ketchupService: ketchuptest.New()},
App{},
args{
ctx: context.Background(),
},
Expand All @@ -185,44 +184,7 @@ func TestGetKetchupToNotify(t *testing.T) {
},
{
"one release, n ketchups",
App{ketchupService: ketchuptest.New().SetListForRepositories([]model.Ketchup{
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(1, repositoryName),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(1, repositoryName),
User: model.NewUser(2, "guest@nowhere", authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(2, "vibioh/dotfiles"),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: "^1.1-0",
Repository: model.NewGithubRepository(2, "vibioh/dotfiles"),
User: model.NewUser(2, "guest@nowhere", authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(3, "vibioh/zzz"),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(3, "vibioh/zzz"),
User: model.NewUser(2, "guest@nowhere", authModel.NewUser(0, "")),
Version: "1.1.0",
},
}, nil)},
App{},
args{
ctx: context.Background(),
releases: []model.Release{
Expand Down Expand Up @@ -283,6 +245,61 @@ func TestGetKetchupToNotify(t *testing.T) {

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockKetchupService := mocks.NewKetchupService(ctrl)

tc.instance.ketchupService = mockKetchupService

switch tc.intention {
case "list error":
mockKetchupService.EXPECT().ListForRepositories(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("failed"))
case "empty":
mockKetchupService.EXPECT().ListForRepositories(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
mockKetchupService.EXPECT().ListOutdatedByFrequency(gomock.Any(), gomock.Any()).Return(nil, nil)
case "one release, n ketchups":
mockKetchupService.EXPECT().ListForRepositories(gomock.Any(), gomock.Any(), gomock.Any()).Return([]model.Ketchup{
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(1, repositoryName),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(1, repositoryName),
User: model.NewUser(2, "guest@nowhere", authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(2, "vibioh/dotfiles"),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: "^1.1-0",
Repository: model.NewGithubRepository(2, "vibioh/dotfiles"),
User: model.NewUser(2, "guest@nowhere", authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(3, "vibioh/zzz"),
User: model.NewUser(1, testEmail, authModel.NewUser(0, "")),
Version: repositoryVersion,
},
{
Pattern: model.DefaultPattern,
Repository: model.NewGithubRepository(3, "vibioh/zzz"),
User: model.NewUser(2, "guest@nowhere", authModel.NewUser(0, "")),
Version: "1.1.0",
},
}, nil)
mockKetchupService.EXPECT().ListOutdatedByFrequency(gomock.Any(), gomock.Any()).Return(nil, nil)
}

got, gotErr := tc.instance.getKetchupToNotify(tc.args.ctx, tc.args.releases)

failed := false
Expand Down
Loading

0 comments on commit 60dd3e4

Please sign in to comment.