Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
slotheth committed Jun 5, 2024
2 parents 026b69e + ae62aa2 commit d0cc100
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- main
- develop
- sonarcloud
- refactor
pull_request:
types: [opened, synchronize, reopened]

Expand Down Expand Up @@ -32,8 +33,6 @@ jobs:
- name: SonarCloud Scan
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_PROJECT_KEY: Contribution-DAO_tendermint-proposal-monitor
SONAR_ORGANIZATION: cdao
run: |
sonar-scanner \
-Dsonar.projectKey=Contribution-DAO_tendermint-proposal-monitor \
Expand Down
47 changes: 39 additions & 8 deletions monitor/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,42 @@ import (
"tendermint_proposal_monitor/utils"
)

type AlertDetails struct {
ProposalDetail string
TimeLeft string
Description string
FormattedVotingStartTime string
}

func SendDiscordAlert(cfg *config.Configurations, chain config.ChainConfig, chainName string, proposal proposals.Proposal, globalDiscordNotifier *notifiers.DiscordNotifier, alertType string) error {
var discordNotifier *notifiers.DiscordNotifier
discordNotifier, err := getDiscordNotifier(cfg, chain, chainName, globalDiscordNotifier)
if err != nil {
return err
}

alertDetails, err := generateAlertDetails(cfg, chain, chainName, proposal)
if err != nil {
return err
}

messageContent := fmt.Sprintf("**%s %s**: %s\n\n**Proposal title:** %s\n\n**Short text description:** %s\n\n**Vote start:** %s\n\n**Time left: %s**\n\n**Read full proposal details:**\n%s",
alertType, chainName, proposal.ProposalID, proposal.Title, alertDetails.Description, alertDetails.FormattedVotingStartTime, alertDetails.TimeLeft, alertDetails.ProposalDetail)

return sendDiscordMessage(discordNotifier, messageContent)
}

func getDiscordNotifier(cfg *config.Configurations, chain config.ChainConfig, chainName string, globalDiscordNotifier *notifiers.DiscordNotifier) (*notifiers.DiscordNotifier, error) {
if chain.Alerts.Discord.Enabled && chain.Alerts.Discord.Webhook != "" {
discordNotifier = &notifiers.DiscordNotifier{WebhookURL: chain.Alerts.Discord.Webhook}
return &notifiers.DiscordNotifier{WebhookURL: chain.Alerts.Discord.Webhook}, nil
} else if chain.Alerts.Discord.Enabled && (cfg.Discord.Enabled && cfg.Discord.Webhook != "") {
discordNotifier = globalDiscordNotifier
return globalDiscordNotifier, nil
} else {
log.Printf("No valid Discord webhook URL available for chain %s", chainName)
return fmt.Errorf("no valid Discord webhook URL available for chain %s", chainName)
return nil, fmt.Errorf("no valid Discord webhook URL available for chain %s", chainName)
}
}

func generateAlertDetails(cfg *config.Configurations, chain config.ChainConfig, chainName string, proposal proposals.Proposal) (*AlertDetails, error) {
proposalDetail := utils.GenerateProposalDetailURL(cfg.ProposalDetailDomain, chainName, proposal.ProposalID)
if chain.ExplorerURL != "" {
if chain.ExplorerURL == "-" {
Expand All @@ -36,7 +60,7 @@ func SendDiscordAlert(cfg *config.Configurations, chain config.ChainConfig, chai

endTime, err := time.Parse(time.RFC3339Nano, proposal.VotingEndTime)
if err != nil {
return fmt.Errorf("error parsing voting end time: %v", err)
return nil, fmt.Errorf("error parsing voting end time: %v", err)
}
timeLeft := utils.FormatTimeLeft(endTime)

Expand All @@ -47,13 +71,20 @@ func SendDiscordAlert(cfg *config.Configurations, chain config.ChainConfig, chai

votingStartTime, err := time.Parse(time.RFC3339, proposal.VotingStartTime)
if err != nil {
return fmt.Errorf("error parsing VotingStartTime: %v", err)
return nil, fmt.Errorf("error parsing VotingStartTime: %v", err)
}

formattedVotingStartTime := votingStartTime.Format("2006-01-02 15:04")

messageContent := fmt.Sprintf("**%s %s**: %s\n\n**Proposal title:** %s\n\n**Short text description:** %s\n\n**Vote start:** %s\n\n**Time left: %s**\n\n**Read full proposal details:**\n%s",
alertType, chainName, proposal.ProposalID, proposal.Title, description, formattedVotingStartTime, timeLeft, proposalDetail)
return &AlertDetails{
ProposalDetail: proposalDetail,
TimeLeft: timeLeft,
Description: description,
FormattedVotingStartTime: formattedVotingStartTime,
}, nil
}

func sendDiscordMessage(discordNotifier *notifiers.DiscordNotifier, messageContent string) error {
embed := notifiers.DiscordEmbed{
Color: notifiers.MessageBoxColor,
Description: messageContent,
Expand Down
26 changes: 20 additions & 6 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,13 @@ func (h *Handler) Run(cfg *config.Configurations, useMock bool) error {
log.Printf("Checking for new proposals...")

for chainName, chain := range cfg.Chains {
propList, err := proposals.Fetch(chain, chain.APIVersion, useMock)
propList, err := fetchProposals(chain, useMock, chainName)
if err != nil {
log.Printf("Error fetching proposals for chain %s: %v", chainName, err)
continue
}

for _, proposal := range propList {
if statusValue, exists := proposals.ProposalStatusValue[proposal.Status]; exists &&
(statusValue == proposals.ProposalStatusValue["PROPOSAL_STATUS_PASSED"] ||
statusValue == proposals.ProposalStatusValue["PROPOSAL_STATUS_REJECTED"] ||
statusValue == proposals.ProposalStatusValue["PROPOSAL_STATUS_FAILED"]) {
if shouldSkipProposal(proposal) {
continue
}

Expand Down Expand Up @@ -137,3 +133,21 @@ func (h *Handler) Run(cfg *config.Configurations, useMock bool) error {

return nil
}

func fetchProposals(chain config.ChainConfig, useMock bool, chainName string) ([]proposals.Proposal, error) {
propList, err := proposals.Fetch(chain, chain.APIVersion, useMock)
if err != nil {
log.Printf("Error fetching proposals for chain %s: %v", chainName, err)
}
return propList, err
}

func shouldSkipProposal(proposal proposals.Proposal) bool {
if statusValue, exists := proposals.ProposalStatusValue[proposal.Status]; exists &&
(statusValue == proposals.ProposalStatusValue["PROPOSAL_STATUS_PASSED"] ||
statusValue == proposals.ProposalStatusValue["PROPOSAL_STATUS_REJECTED"] ||
statusValue == proposals.ProposalStatusValue["PROPOSAL_STATUS_FAILED"]) {
return true
}
return false
}

0 comments on commit d0cc100

Please sign in to comment.