Skip to content

Commit

Permalink
feat: hide buttons after deleting; allow deleting after creating sile…
Browse files Browse the repository at this point in the history
…nce (#41)

* feat: hide buttons after deleting; allow deleting after creating silence

* chore: bump golang version in CI
  • Loading branch information
freak12techno authored Oct 28, 2024
1 parent a19ed73 commit 16ba905
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- uses: actions/checkout@master
- uses: actions/setup-go@v2
with:
go-version: '^1.18.0'
go-version: '^1.23.0'
- run: go version
- run: go mod download
- run: go build cmd/grafana-interacter.go
Expand All @@ -17,7 +17,7 @@ jobs:
- uses: actions/checkout@master
- uses: actions/setup-go@v2
with:
go-version: '^1.18.0'
go-version: '^1.23.0'
- run: go version
- run: go mod download
- run: go vet ./...
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18
go-version: 1.23

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
15 changes: 12 additions & 3 deletions pkg/app/silences_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"fmt"
"main/pkg/constants"
"main/pkg/types"
"main/pkg/types/render"
"main/pkg/utils"
Expand All @@ -15,7 +16,7 @@ func (a *App) HandleGrafanaNewSilence(c tele.Context) error {
Str("text", c.Text()).
Msg("Got new silence query")

return a.HandleNewSilence(c, a.Grafana)
return a.HandleNewSilence(c, a.Grafana, constants.GrafanaUnsilencePrefix)
}

func (a *App) HandleAlertmanagerNewSilence(c tele.Context) error {
Expand All @@ -28,12 +29,13 @@ func (a *App) HandleAlertmanagerNewSilence(c tele.Context) error {
return c.Reply("Alertmanager is disabled.")
}

return a.HandleNewSilence(c, a.Alertmanager)
return a.HandleNewSilence(c, a.Alertmanager, constants.AlertmanagerUnsilencePrefix)
}

func (a *App) HandleNewSilence(
c tele.Context,
silenceManager types.SilenceManager,
unsilencePrefix string,
) error {
silenceInfo, err := utils.ParseSilenceOptions(c.Text(), c)
if err != "" {
Expand Down Expand Up @@ -69,5 +71,12 @@ func (a *App) HandleNewSilence(
return c.Reply(fmt.Sprintf("Error rendering template: %s", renderErr))
}

return a.BotReply(c, template)
menu := &tele.ReplyMarkup{ResizeKeyboard: true}
menu.Inline(menu.Row(menu.Data(
"❌Unsilence",
unsilencePrefix,
silence.ID,
)))

return a.BotReply(c, template, menu)
}
60 changes: 58 additions & 2 deletions pkg/app/silences_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,76 @@ func (a *App) HandleAlertmanagerDeleteSilence(c tele.Context) error {
func (a *App) HandleGrafanaCallbackDeleteSilence(c tele.Context) error {
a.Logger.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("Got new delete silence callback via button")

callback := c.Callback()

if callback.Message != nil && callback.Message.ReplyMarkup != nil {
for rowIndex, row := range callback.Message.ReplyMarkup.InlineKeyboard {
for itemIndex, item := range row {
split := strings.SplitN(item.Data, "|", 2)
if len(split) != 2 {
continue
}

if split[1] == callback.Data {
callback.Message.ReplyMarkup.InlineKeyboard[rowIndex] = append(
callback.Message.ReplyMarkup.InlineKeyboard[rowIndex][:itemIndex],
callback.Message.ReplyMarkup.InlineKeyboard[rowIndex][itemIndex+1:]...,
)
}
}
}

if _, err := a.Bot.EditReplyMarkup(
callback.Message,
callback.Message.ReplyMarkup,
); err != nil {
a.Logger.Error().
Str("sender", c.Sender().Username).
Err(err).
Msg("Error updating message when deleting a Grafana silence via callback")
}
}

return a.HandleDeleteSilence(c, a.Grafana, callback.Data)
}

func (a *App) HandleAlertmanagerCallbackDeleteSilence(c tele.Context) error {
a.Logger.Info().
Str("sender", c.Sender().Username).
Str("text", c.Text()).
Msg("Got new delete Alertmanager silence callback via button")

callback := c.Callback()

if callback.Message != nil && callback.Message.ReplyMarkup != nil {
for rowIndex, row := range callback.Message.ReplyMarkup.InlineKeyboard {
for itemIndex, item := range row {
split := strings.SplitN(item.Data, "|", 2)
if len(split) != 2 {
continue
}

if split[1] == callback.Data {
callback.Message.ReplyMarkup.InlineKeyboard[rowIndex] = append(
callback.Message.ReplyMarkup.InlineKeyboard[rowIndex][:itemIndex],
callback.Message.ReplyMarkup.InlineKeyboard[rowIndex][itemIndex+1:]...,
)
}
}
}

if _, err := a.Bot.EditReplyMarkup(
callback.Message,
callback.Message.ReplyMarkup,
); err != nil {
a.Logger.Error().
Str("sender", c.Sender().Username).
Err(err).
Msg("Error updating message when deleting an Alertmanager silence via callback")
}
}

return a.HandleDeleteSilence(c, a.Alertmanager, callback.Data)
}

Expand Down

0 comments on commit 16ba905

Please sign in to comment.