Skip to content

Commit

Permalink
feat: Adding frequency option for limiting email notificaation
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed May 5, 2021
1 parent dc9718d commit ca956d7
Show file tree
Hide file tree
Showing 15 changed files with 269 additions and 93 deletions.
18 changes: 18 additions & 0 deletions cmd/ketchup/templates/ketchup.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ <h2 class="header">Create ketchup</h2>
<input id="create-pattern" type="text" name="pattern" placeholder="stable" class="full">
</p>

<p class="padding no-margin">
<label for="create-frequency" class="block">Frequency:</label>
<select id="create-frequency" name="frequency" class="full">
<option value="Weekly">Weekly</option>
<option value="Daily" selected>Daily</option>
<option value="None">None</option>
</select>
</p>

<p class="padding no-margin">
<label for="create-version" class="block">My version:</label>
<input id="create-version" type="text" name="version" placeholder="1.0.0" class="full">
Expand Down Expand Up @@ -96,6 +105,15 @@ <h2 class="header">Edit ketchup</h2>
<input id="edit-pattern-{{ .Repository.ID }}" name="pattern" type="text" value="{{ .Pattern }}">
</p>

<p class="padding no-margin">
<label for="edit-frequency" class="block">Frequency:</label>
<select id="edit-frequency" name="frequency" class="full">
<option value="Weekly" {{ if eq .Frequency.String "Weekly" }}selected{{ end }}>Weekly</option>
<option value="Daily" {{ if eq .Frequency.String "Daily" }}selected{{ end }}>Daily</option>
<option value="None" {{ if eq .Frequency.String "None" }}selected{{ end }}>None</option>
</select>
</p>

<p class="padding no-margin">
<label for="edit-version-{{ .Repository.ID }}" class="block">My version:</label>
<input id="edit-version-{{ .Repository.ID }}" name="version" type="text" value="{{ .Version }}">
Expand Down
18 changes: 15 additions & 3 deletions pkg/ketchup/ketchups.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func (a app) handleCreate(w http.ResponseWriter, r *http.Request) {
return
}

ketchupFrequency, err := model.ParseKetchupFrequency(r.FormValue("frequency"))
if err != nil {
a.rendererApp.Error(w, httpModel.WrapInvalid(err))
return
}

var repository model.Repository
name := r.FormValue("name")

Expand All @@ -57,7 +63,7 @@ func (a app) handleCreate(w http.ResponseWriter, r *http.Request) {
a.rendererApp.Error(w, httpModel.WrapInternal(fmt.Errorf("unhandled repository kind `%s`", repositoryKind)))
}

item := model.NewKetchup(r.FormValue("pattern"), r.FormValue("version"), repository)
item := model.NewKetchup(r.FormValue("pattern"), r.FormValue("version"), ketchupFrequency, repository)

created, err := a.ketchupService.Create(r.Context(), item)
if err != nil {
Expand All @@ -75,7 +81,13 @@ func (a app) handleUpdate(w http.ResponseWriter, r *http.Request) {
return
}

item := model.NewKetchup(r.FormValue("pattern"), r.FormValue("version"), model.NewGithubRepository(id, ""))
ketchupFrequency, err := model.ParseKetchupFrequency(r.FormValue("frequency"))
if err != nil {
a.rendererApp.Error(w, httpModel.WrapInvalid(err))
return
}

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

updated, err := a.ketchupService.Update(r.Context(), item)
if err != nil {
Expand All @@ -93,7 +105,7 @@ func (a app) handleDelete(w http.ResponseWriter, r *http.Request) {
return
}

item := model.NewKetchup("", "", model.NewGithubRepository(id, ""))
item := model.NewKetchup("", "", model.Daily, model.NewGithubRepository(id, ""))

if err := a.ketchupService.Delete(r.Context(), item); err != nil {
a.rendererApp.Error(w, err)
Expand Down
37 changes: 36 additions & 1 deletion pkg/model/ketchup.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,63 @@
package model

import (
"fmt"
"strings"

"github.com/ViBiOh/ketchup/pkg/semver"
)

// KetchupFrequency defines constant for ketchup frequency
type KetchupFrequency int

const (
// None frequency
None KetchupFrequency = iota
// Daily frequency
Daily
// Weekly frequency (on Monday)
Weekly
)

var (
// KetchupFrequencyValues string values
KetchupFrequencyValues = []string{"None", "Daily", "Weekly"}

// NoneKetchup is an undefined ketchup
NoneKetchup = Ketchup{}
)

// ParseKetchupFrequency parse raw string into a KetchupFrequency
func ParseKetchupFrequency(value string) (KetchupFrequency, error) {
for i, short := range KetchupFrequencyValues {
if strings.EqualFold(short, value) {
return KetchupFrequency(i), nil
}
}

return Daily, fmt.Errorf("invalid value `%s` for ketchup frequency", value)
}

func (r KetchupFrequency) String() string {
return KetchupFrequencyValues[r]
}

// Ketchup of app
type Ketchup struct {
Semver string
Pattern string
Version string
User User
Repository Repository
Frequency KetchupFrequency
}

// NewKetchup creates new instance
func NewKetchup(pattern, version string, repo Repository) Ketchup {
func NewKetchup(pattern, version string, frequency KetchupFrequency, repo Repository) Ketchup {
return Ketchup{
Pattern: pattern,
Version: version,
Frequency: frequency,
Repository: repo,
}
}
Expand Down
90 changes: 76 additions & 14 deletions pkg/model/ketchup_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
package model

import (
"errors"
"reflect"
"sort"
"strings"
"testing"

"github.com/ViBiOh/ketchup/pkg/semver"
)

func TestParseKetchupFrequency(t *testing.T) {
type args struct {
value string
}

var cases = []struct {
intention string
args args
want KetchupFrequency
wantErr error
}{
{
"UpperCase",
args{
value: "NONE",
},
None,
nil,
},
{
"equal",
args{
value: "Weekly",
},
Weekly,
nil,
},
{
"not found",
args{
value: "wrong",
},
Daily,
errors.New("invalid value `wrong` for ketchup frequency"),
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
got, gotErr := ParseKetchupFrequency(tc.args.value)

failed := false

if tc.wantErr == nil && gotErr != nil {
failed = true
} else if tc.wantErr != nil && gotErr == nil {
failed = true
} else if tc.wantErr != nil && !strings.Contains(gotErr.Error(), tc.wantErr.Error()) {
failed = true
} else if got != tc.want {
failed = true
}

if failed {
t.Errorf("ParseRepositoryKind() = (`%s`, `%s`), want (`%s`, `%s`)", got, gotErr, tc.want, tc.wantErr)
}
})
}
}

func TestKetchupByRepositoryID(t *testing.T) {
type args struct {
array []Ketchup
Expand All @@ -22,15 +84,15 @@ func TestKetchupByRepositoryID(t *testing.T) {
"simple",
args{
array: []Ketchup{
NewKetchup(DefaultPattern, "", NewGithubRepository(10, "")),
NewKetchup(DefaultPattern, "", NewGithubRepository(1, "")),
NewKetchup("latest", "", NewGithubRepository(1, "")),
NewKetchup(DefaultPattern, "", Daily, NewGithubRepository(10, "")),
NewKetchup(DefaultPattern, "", Daily, NewGithubRepository(1, "")),
NewKetchup("latest", "", Daily, NewGithubRepository(1, "")),
},
},
[]Ketchup{
NewKetchup("latest", "", NewGithubRepository(1, "")),
NewKetchup(DefaultPattern, "", NewGithubRepository(1, "")),
NewKetchup(DefaultPattern, "", NewGithubRepository(10, "")),
NewKetchup("latest", "", Daily, NewGithubRepository(1, "")),
NewKetchup(DefaultPattern, "", Daily, NewGithubRepository(1, "")),
NewKetchup(DefaultPattern, "", Daily, NewGithubRepository(10, "")),
},
},
}
Expand Down Expand Up @@ -59,17 +121,17 @@ func TestKetchupByPriority(t *testing.T) {
"alphabetic",
args{
array: []Ketchup{
NewKetchup("", "", NewGithubRepository(0, "abc")),
NewKetchup("", "", NewGithubRepository(0, "ghi")),
NewKetchup("", "", NewGithubRepository(0, "jkl")),
NewKetchup("", "", NewGithubRepository(0, "def")),
NewKetchup("", "", Daily, NewGithubRepository(0, "abc")),
NewKetchup("", "", Daily, NewGithubRepository(0, "ghi")),
NewKetchup("", "", Daily, NewGithubRepository(0, "jkl")),
NewKetchup("", "", Daily, NewGithubRepository(0, "def")),
},
},
[]Ketchup{
NewKetchup("", "", NewGithubRepository(0, "abc")),
NewKetchup("", "", NewGithubRepository(0, "def")),
NewKetchup("", "", NewGithubRepository(0, "ghi")),
NewKetchup("", "", NewGithubRepository(0, "jkl")),
NewKetchup("", "", Daily, NewGithubRepository(0, "abc")),
NewKetchup("", "", Daily, NewGithubRepository(0, "def")),
NewKetchup("", "", Daily, NewGithubRepository(0, "ghi")),
NewKetchup("", "", Daily, NewGithubRepository(0, "jkl")),
},
},
{
Expand Down
30 changes: 14 additions & 16 deletions pkg/model/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"strings"
)

// RepositoryKind defines constant for repository types
type RepositoryKind int

const (
// DefaultPattern is the latest but non-beta version
DefaultPattern = "stable"

githubURL = "https://github.com"
)

// RepositoryKind defines constant for repository types
type RepositoryKind int

const (
// Github repository kind
Github RepositoryKind = iota
Expand All @@ -25,13 +25,22 @@ const (
var (
// RepositoryKindValues string values
RepositoryKindValues = []string{"github", "helm"}
)

var (
// NoneRepository is an undefined repository
NoneRepository = Repository{}
)

// ParseRepositoryKind parse raw string into a RepositoryKind
func ParseRepositoryKind(value string) (RepositoryKind, error) {
for i, short := range RepositoryKindValues {
if strings.EqualFold(short, value) {
return RepositoryKind(i), nil
}
}

return Github, fmt.Errorf("invalid value `%s` for repository kind", value)
}

func (r RepositoryKind) String() string {
return RepositoryKindValues[r]
}
Expand Down Expand Up @@ -95,17 +104,6 @@ func (r Repository) CompareURL(version string, pattern string) string {
return fmt.Sprintf("%s/%s/compare/%s...%s", githubURL, r.Name, r.Versions[pattern], version)
}

// ParseRepositoryKind parse raw string into a RepositoryKind
func ParseRepositoryKind(value string) (RepositoryKind, error) {
for i, short := range RepositoryKindValues {
if strings.EqualFold(short, value) {
return RepositoryKind(i), nil
}
}

return Github, fmt.Errorf("invalid value `%s` for repository kind", value)
}

// RepositoryByID sort repository by ID
type RepositoryByID []Repository

Expand Down
3 changes: 0 additions & 3 deletions pkg/semver/pattern_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package semver

import (
"fmt"
"testing"
)

Expand Down Expand Up @@ -159,8 +158,6 @@ func TestCheck(t *testing.T) {
},
}

fmt.Println(safeParsePattern("^1.0").Check(safeParse("2.0.0")))

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
if got := tc.instance.Check(tc.args.version); got != tc.want {
Expand Down
1 change: 1 addition & 0 deletions pkg/service/ketchup/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (a app) Update(ctx context.Context, item model.Ketchup) (model.Ketchup, err
current := model.Ketchup{
Pattern: item.Pattern,
Version: item.Version,
Frequency: item.Frequency,
Repository: old.Repository,
User: old.User,
}
Expand Down
Loading

0 comments on commit ca956d7

Please sign in to comment.