Skip to content

Commit

Permalink
feat: implement gotify hook type
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Jan 30, 2024
1 parent 25924b6 commit e0ce655
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 38 deletions.
176 changes: 144 additions & 32 deletions gen/go/v1/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions internal/hook/gotifyhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package hook

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"

v1 "github.com/garethgeorge/backrest/gen/go/v1"
)

func (h *Hook) doGotify(cmd *v1.Hook_ActionGotify, vars HookVars, output io.Writer) error {
payload, err := h.renderTemplateOrDefault(cmd.ActionGotify.GetTemplate(), defaultTemplate, vars)
if err != nil {
return fmt.Errorf("template rendering: %w", err)
}

title, err := h.renderTemplateOrDefault(cmd.ActionGotify.GetTitleTemplate(), "Backrest Event", vars)
if err != nil {
return fmt.Errorf("title template rendering: %w", err)
}

message := struct {
Message string `json:"message"`
Title string `json:"title"`
Priority int `json:"priority"`
}{
Title: title,
Priority: 5,
Message: payload,
}

b, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("json marshal: %w", err)
}

baseUrl := strings.Trim(cmd.ActionGotify.GetBaseUrl(), "/")

postUrl := fmt.Sprintf(
"%s/message?token=%s",
baseUrl,
url.QueryEscape(cmd.ActionGotify.GetToken()))

output.Write([]byte(fmt.Sprintf("Sending gotify message to %s\n", postUrl)))

body, err := post(postUrl, "application/json", bytes.NewReader(b))

if err != nil {
return fmt.Errorf("send gotify message: %w", err)
}

if body != "" {
output.Write([]byte(body))
}

return nil
}
4 changes: 3 additions & 1 deletion internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

var (
defaultTemplate = `{{.Summary}}`
defaultTemplate = `{{ .Summary }}`
)

// ExecuteHooks schedules tasks for the hooks subscribed to the given event. The vars map is used to substitute variables
Expand Down Expand Up @@ -138,6 +138,8 @@ func (h *Hook) Do(event v1.Hook_Condition, vars HookVars, output io.Writer) erro
return h.doCommand(action, vars, output)
case *v1.Hook_ActionDiscord:
return h.doDiscord(action, vars, output)
case *v1.Hook_ActionGotify:
return h.doGotify(action, vars, output)
default:
return fmt.Errorf("unknown hook action: %v", action)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/hook/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func post(url string, contentType string, body io.Reader) (string, error) {
r, err := http.Post(url, contentType, body)
if err != nil {
return "", fmt.Errorf("send request: %w", url, err)
return "", fmt.Errorf("send request %v: %w", url, err)
}
if r.StatusCode == 204 {
return "", nil
Expand Down
2 changes: 1 addition & 1 deletion internal/orchestrator/taskcollectgarbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
// - it has no snapshot associated with it
// - it has a forgotten snapshot associated with it
gcHistoryAge = 30 * 24 * time.Hour
gcHistoryMaxCount = 200
gcHistoryMaxCount = 1000
)

type CollectGarbageTask struct {
Expand Down
10 changes: 9 additions & 1 deletion proto/v1/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ message Hook {
Command action_command = 100 [json_name="actionCommand"];
Webhook action_webhook = 101 [json_name="actionWebhook"];
Discord action_discord = 102 [json_name="actionDiscord"];
Gotify action_gotify = 103 [json_name="actionGotify"];
}

message Command {
Expand All @@ -90,7 +91,14 @@ message Hook {

message Discord {
string webhook_url = 1 [json_name="webhookUrl"];
string template = 2; // template for the webhook payload.
string template = 2 [json_name="template"]; // template for the webhook payload.
}

message Gotify {
string base_url = 1 [json_name="baseUrl"];
string token = 3 [json_name="token"];
string template = 100 [json_name="template"]; // template for the webhook payload.
string title_template = 101 [json_name="titleTemplate"]; // template for the webhook title.
}
}

Loading

0 comments on commit e0ce655

Please sign in to comment.