Skip to content

Commit

Permalink
feat: revamp (#9)
Browse files Browse the repository at this point in the history
Fixes #4, #6

- adds revote notifications for telegram
- adds proposals error and vote query error notifications for telegram
- adds tests infrastructure and codecov
  • Loading branch information
freak12techno committed Oct 2, 2022
1 parent b40b1f7 commit 9725ebc
Show file tree
Hide file tree
Showing 16 changed files with 990 additions and 203 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
args: --timeout 300s
test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2
with:
go-version: '1.18.2'
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Run tests
run: go test -coverprofile coverage.txt -v *.go
- name: Download uploader
run: curl -Os https://uploader.codecov.io/latest/linux/codecov
- name: Make uploader executable
run: chmod +x codecov
- name: Run uploader
run: ./codecov
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
config.toml
config2.toml
main
*.json
.idea
cover.out
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ func (c *Chain) GetExplorerProposalsLinks(proposalID string) []ExplorerLink {
return links
}

type Chains []Chain

func (c Chains) FindByName(name string) *Chain {
for _, chain := range c {
if chain.Name == name {
return &chain
}
}

return nil
}

type Config struct {
PagerDutyConfig PagerDutyConfig `toml:"pagerduty"`
TelegramConfig TelegramConfig `toml:"telegram"`
Expand Down
160 changes: 160 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

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

chain := Chain{
Name: "",
}

err := chain.Validate()
assert.NotEqual(t, err, nil, "Error should be presented!")
}

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

chain := Chain{
Name: "chain",
LCDEndpoints: []string{},
}

err := chain.Validate()
assert.NotEqual(t, err, nil, "Error should be presented!")
}

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

chain := Chain{
Name: "chain",
LCDEndpoints: []string{"endpoint"},
Wallets: []string{},
}

err := chain.Validate()
assert.NotEqual(t, err, nil, "Error should be presented!")
}

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

chain := Chain{
Name: "chain",
LCDEndpoints: []string{"endpoint"},
Wallets: []string{"wallet"},
}

err := chain.Validate()
assert.Equal(t, err, nil, "Error should not be presented!")
}

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

chain := Chain{
Name: "chain",
PrettyName: "",
}

err := chain.GetName()
assert.Equal(t, err, "chain", "Chain name should match!")
}

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

chain := Chain{
Name: "chain",
PrettyName: "chain-pretty",
}

err := chain.GetName()
assert.Equal(t, err, "chain-pretty", "Chain name should match!")
}

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

config := Config{
Chains: []Chain{},
}
err := config.Validate()
assert.NotEqual(t, err, nil, "Error should be presented!")
}

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

config := Config{
Chains: []Chain{
{
Name: "",
},
},
}
err := config.Validate()
assert.NotEqual(t, err, nil, "Error should be presented!")
}

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

config := Config{
Chains: []Chain{
{
Name: "chain",
LCDEndpoints: []string{"endpoint"},
Wallets: []string{"wallet"},
},
},
}
err := config.Validate()
assert.Equal(t, err, nil, "Error should not be presented!")
}

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

chains := Chains{
{Name: "chain1"},
{Name: "chain2"},
}

chain := chains.FindByName("chain2")
assert.NotEqual(t, chain, nil, "Chain should be presented!")
}

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

chains := Chains{
{Name: "chain1"},
{Name: "chain2"},
}

chain := chains.FindByName("chain3")
assert.Nil(t, chain, "Chain should not be presented!")
}

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

chain := Chain{
KeplrName: "chain",
}

link := chain.GetKeplrLink("proposal")
assert.Equal(
t,
link,
"https://wallet.keplr.app/#/chain/governance?detailId=proposal",
"Chain Keplr link is wrong!",
)
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ require (
github.com/mcuadros/go-defaults v1.2.0
github.com/rs/zerolog v1.26.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.0
gopkg.in/telebot.v3 v3.0.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Execute(configPath string) {
mutesManager.Load()

reportGenerator := NewReportGenerator(stateManager, log, config.Chains)
stateGenerator := NewStateGenerator(log, config.Chains)

reporters := []Reporter{
NewPagerDutyReporter(config.PagerDutyConfig, log),
Expand All @@ -41,7 +42,8 @@ func Execute(configPath string) {
}

for {
report := reportGenerator.GenerateReport()
newState := stateGenerator.GetState(stateManager.State)
report := reportGenerator.GenerateReport(stateManager.State, newState)

if report.Empty() {
log.Debug().Msg("Empty report, not sending.")
Expand Down
4 changes: 4 additions & 0 deletions pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (r PagerDutyReporter) SendReport(report Report) error {
var err error

for _, entry := range report.Entries {
if !entry.IsVoteOrNotVoted() {
continue
}

alert := r.NewPagerDutyAlertFromReportEntry(entry)

if alertErr := r.SendAlert(alert); alertErr != nil {
Expand Down
Loading

0 comments on commit 9725ebc

Please sign in to comment.