Skip to content

Commit

Permalink
feat: override mute instead of adding a new one if exists
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed May 16, 2024
1 parent 2146c5e commit c8dbc28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pkg/mutes/mutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (m *Mute) Matches(chain string, proposalID string) bool {
return match
}

func (m *Mute) LabelsEqual(another *Mute) bool {
return m.Chain == another.Chain && m.ProposalID == another.ProposalID
}

func (m *Mutes) IsMuted(chain string, proposalID string) bool {
for _, mute := range m.Mutes {
if mute.IsExpired() {
Expand All @@ -50,6 +54,18 @@ func (m *Mutes) IsMuted(chain string, proposalID string) bool {
}

func (m *Mutes) AddMute(mute *Mute) {
for _, existingMute := range m.Mutes {
if existingMute.LabelsEqual(mute) {
existingMute.Comment = mute.Comment
existingMute.Expires = mute.Expires

m.Mutes = utils.Filter(m.Mutes, func(m *Mute) bool {
return !m.IsExpired()
})
return
}
}

m.Mutes = append(m.Mutes, mute)
m.Mutes = utils.Filter(m.Mutes, func(m *Mute) bool {
return !m.IsExpired()
Expand Down
29 changes: 28 additions & 1 deletion pkg/mutes/mutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestMutesMatchesNotIgnoreActual(t *testing.T) {
assert.True(t, muted, "Mute should be muted!")
}

func TestMutesAddsMute(t *testing.T) {
func TestMutesAddsMuteNew(t *testing.T) {
t.Parallel()

mutes := Mutes{
Expand All @@ -84,3 +84,30 @@ func TestMutesAddsMute(t *testing.T) {
assert.Len(t, mutes.Mutes, 1, "There should be 1 mute!")
assert.Equal(t, "chain2", mutes.Mutes[0].Chain, "Chain name should match!")
}

func TestMutesAddsMuteOverride(t *testing.T) {
t.Parallel()

expireTime := time.Now().Add(time.Hour)

mutes := Mutes{
Mutes: []*Mute{
{Chain: "chain1", ProposalID: "proposal1", Expires: expireTime, Comment: "comment1"},
{Chain: "chain1", ProposalID: "proposal2", Expires: expireTime, Comment: "comment2"},
},
}

newExpireTime := time.Now().Add(3 * time.Hour)

mutes.AddMute(&Mute{
Chain: "chain1",
ProposalID: "proposal1",
Expires: newExpireTime,
Comment: "newcomment",
})
assert.Len(t, mutes.Mutes, 2)
assert.Equal(t, "chain1", mutes.Mutes[0].Chain)
assert.Equal(t, "proposal1", mutes.Mutes[0].ProposalID)
assert.Equal(t, "newcomment", mutes.Mutes[0].Comment)
assert.Equal(t, newExpireTime, mutes.Mutes[0].Expires)
}

0 comments on commit c8dbc28

Please sign in to comment.