Skip to content

Commit

Permalink
feat(notifier): Sorting release by kind and name for better formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Jun 22, 2021
1 parent 35473ff commit 4e10e52
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/model/ketchup.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,18 @@ func (a ReleaseByRepositoryIDAndPattern) Less(i, j int) bool {
}
return a[i].Repository.ID < a[j].Repository.ID
}

// ReleaseByKindAndName sort release by repository kind and repository name
type ReleaseByKindAndName []Release

func (a ReleaseByKindAndName) Len() int { return len(a) }
func (a ReleaseByKindAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ReleaseByKindAndName) Less(i, j int) bool {
if a[i].Repository.Kind == a[j].Repository.Kind {
if a[i].Repository.Name == a[j].Repository.Name {
return a[i].Repository.Part < a[j].Repository.Part
}
return a[i].Repository.Name < a[j].Repository.Name
}
return a[i].Repository.Kind < a[j].Repository.Kind
}
37 changes: 37 additions & 0 deletions pkg/model/ketchup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,40 @@ func TestReleaseByRepositoryID(t *testing.T) {
})
}
}

func TestReleaseByKindAndName(t *testing.T) {
type args struct {
array []Release
}

var cases = []struct {
intention string
args args
want []Release
}{
{
"simple",
args{
array: []Release{
NewRelease(NewHelmRepository(3, "http://chart", "app"), DefaultPattern, semver.Version{}),
NewRelease(NewGithubRepository(1, "vibioh/github"), DefaultPattern, semver.Version{}),
NewRelease(NewHelmRepository(2, "http://chart", "cron"), DefaultPattern, semver.Version{}),
},
},
[]Release{
NewRelease(NewGithubRepository(1, "vibioh/github"), DefaultPattern, semver.Version{}),
NewRelease(NewHelmRepository(3, "http://chart", "app"), DefaultPattern, semver.Version{}),
NewRelease(NewHelmRepository(2, "http://chart", "cron"), DefaultPattern, semver.Version{}),
},
},
}

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
sort.Sort(ReleaseByKindAndName(tc.args.array))
if got := tc.args.array; !reflect.DeepEqual(got, tc.want) {
t.Errorf("ReleaseByRepositoryID() = %+v, want %+v", got, tc.want)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ func (a app) sendNotification(ctx context.Context, ketchupToNotify map[model.Use
for user, releases := range ketchupToNotify {
logger.Info("Sending email to %s for %d releases", user.Email, len(releases))

sort.Sort(model.ReleaseByKindAndName(releases))

payload := map[string]interface{}{
"releases": releases,
}
Expand Down

0 comments on commit 4e10e52

Please sign in to comment.