diff --git a/docs/content/3.cookbooks/2.slack-hook-build-kit-examples.md b/docs/content/3.cookbooks/2.slack-hook-build-kit-examples.md new file mode 100644 index 00000000..607c9eb3 --- /dev/null +++ b/docs/content/3.cookbooks/2.slack-hook-build-kit-examples.md @@ -0,0 +1,73 @@ +# Slack Hook Build Kit Examples + +## Overview + +When using the Slack Hook you can provide a simple message or a [Slack Block Kit](https://api.slack.com/block-kit) message. You can leverage the [Block Kit Builder](https://app.slack.com/block-kit-builder) to Preview the message. + +## Clean Job Summary + +
+Settings View +
+ +``` +{ + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "Backup: *{{ .Plan.Id }}* ➝ *{{ .Repo.Id }}* at *{{ .FormatTime .CurTime }}*" + } + }, + { + "type": "divider" + }, + {{ if .Error }} + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ":x: *Snapshot failed:*\n```{{ .Error }}```" + } + } + {{ else }} + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ":white_check_mark: *Snapshot `{{ .SnapshotId }}` created*" + } + }, + { + "type": "section", + "fields": [ + { + "type": "mrkdwn", + "text": "*Total Added:*\n`{{ .FormatSizeBytes .SnapshotStats.DataAdded }}`" + }, + { + "type": "mrkdwn", + "text": "*Files Processed:*\n`{{ .SnapshotStats.TotalFilesProcessed }}`" + }, + { + "type": "mrkdwn", + "text": "*Bytes Processed:*\n`{{ .FormatSizeBytes .SnapshotStats.TotalBytesProcessed }}`" + }, + { + "type": "mrkdwn", + "text": "*Time:*\n`{{ .SnapshotStats.TotalDuration }}s`" + } + ] + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "*Backup Stats:*\n• *Files:* `{{ .SnapshotStats.FilesNew }} new`, `{{ .SnapshotStats.FilesChanged }} changed`, `{{ .SnapshotStats.FilesUnmodified }} unmodified`\n• *Dirs:* `{{ .SnapshotStats.DirsNew }} new`, `{{ .SnapshotStats.DirsChanged }} changed`, `{{ .SnapshotStats.DirsUnmodified }} unmodified`" + } + } + {{ end }} + ] +} +``` \ No newline at end of file diff --git a/docs/content/3.cookbooks/2.reverse-proxy-examples.md b/docs/content/3.cookbooks/3.reverse-proxy-examples.md similarity index 100% rename from docs/content/3.cookbooks/2.reverse-proxy-examples.md rename to docs/content/3.cookbooks/3.reverse-proxy-examples.md diff --git a/docs/content/3.cookbooks/3.ssh-remote.md b/docs/content/3.cookbooks/4.ssh-remote.md similarity index 100% rename from docs/content/3.cookbooks/3.ssh-remote.md rename to docs/content/3.cookbooks/4.ssh-remote.md diff --git a/docs/public/screenshots/slack-clean-job.png b/docs/public/screenshots/slack-clean-job.png new file mode 100644 index 00000000..e221255b Binary files /dev/null and b/docs/public/screenshots/slack-clean-job.png differ diff --git a/internal/hook/types/slack.go b/internal/hook/types/slack.go index f3da893d..cd7f4a32 100644 --- a/internal/hook/types/slack.go +++ b/internal/hook/types/slack.go @@ -20,25 +20,30 @@ func (slackHandler) Name() string { } func (slackHandler) Execute(ctx context.Context, cmd *v1.Hook, vars interface{}, runner tasks.TaskRunner, event v1.Hook_Condition) error { + payload, err := hookutil.RenderTemplateOrDefault(cmd.GetActionSlack().GetTemplate(), hookutil.DefaultTemplate, vars) if err != nil { return fmt.Errorf("template rendering: %w", err) } l := runner.Logger(ctx) - l.Sugar().Infof("Sending slack message to %s", cmd.GetActionSlack().GetWebhookUrl()) - l.Debug("Sending slack message", zap.String("payload", payload)) - type Message struct { - Text string `json:"text"` + var requestBytes []byte + if json.Valid([]byte(payload)) { + l.Sugar().Infof("Sending advanced slack message to %s", cmd.GetActionSlack().GetWebhookUrl()) + l.Sugar().Debugf("Sending advanced slack message: %s", payload) + requestBytes = []byte(payload) + } else { + l.Sugar().Infof("Sending slack message to %s", cmd.GetActionSlack().GetWebhookUrl()) + l.Debug("Sending slack message", zap.String("payload", payload)) + request := struct { + Text string `json:"text"` + }{ + Text: "Backrest Notification\n" + payload, + } + requestBytes, _ = json.Marshal(request) } - request := Message{ - Text: "Backrest Notification\n" + payload, // leading newline looks better in discord. - } - - requestBytes, _ := json.Marshal(request) - body, err := hookutil.PostRequest(cmd.GetActionSlack().GetWebhookUrl(), "application/json", bytes.NewReader(requestBytes)) if err != nil { return fmt.Errorf("sending slack message to %q: %w", cmd.GetActionSlack().GetWebhookUrl(), err)