diff --git a/pkg/mutes/mutes.go b/pkg/mutes/mutes.go index 956eba7..6c6d946 100644 --- a/pkg/mutes/mutes.go +++ b/pkg/mutes/mutes.go @@ -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() { @@ -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() diff --git a/pkg/mutes/mutes_test.go b/pkg/mutes/mutes_test.go index bc84c61..add7cfe 100644 --- a/pkg/mutes/mutes_test.go +++ b/pkg/mutes/mutes_test.go @@ -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{ @@ -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) +}