Skip to content

Commit d5ffa90

Browse files
committed
refactor: move Slack types to their own file
Signed-off-by: Santiago Fernández Núñez <santiago.nunez@nubank.com.br>
1 parent 685a2a1 commit d5ffa90

4 files changed

Lines changed: 75 additions & 48 deletions

File tree

.cursor/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
plans/

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/amtool
66
/data/
77
/vendor
8+
.idea

notify/slack/slack.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,6 @@ import (
3636
// https://api.slack.com/reference/messaging/attachments#legacy_fields - 1024, no units given, assuming runes or characters.
3737
const maxTitleLenRunes = 1024
3838

39-
// Notifier implements a Notifier for Slack notifications.
40-
type Notifier struct {
41-
conf *config.SlackConfig
42-
tmpl *template.Template
43-
logger *slog.Logger
44-
client *http.Client
45-
retrier *notify.Retrier
46-
47-
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
48-
}
49-
5039
// New returns a new Slack notification handler.
5140
func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
5241
client, err := notify.NewClientWithTracing(*c.HTTPConfig, "slack", httpOpts...)
@@ -64,43 +53,6 @@ func New(c *config.SlackConfig, t *template.Template, l *slog.Logger, httpOpts .
6453
}, nil
6554
}
6655

67-
// request is the request for sending a slack notification.
68-
type request struct {
69-
Channel string `json:"channel,omitempty"`
70-
Timestamp string `json:"ts,omitempty"`
71-
Username string `json:"username,omitempty"`
72-
IconEmoji string `json:"icon_emoji,omitempty"`
73-
IconURL string `json:"icon_url,omitempty"`
74-
LinkNames bool `json:"link_names,omitempty"`
75-
Text string `json:"text,omitempty"`
76-
Attachments []attachment `json:"attachments"`
77-
}
78-
79-
// attachment is used to display a richly-formatted message block.
80-
type attachment struct {
81-
Title string `json:"title,omitempty"`
82-
TitleLink string `json:"title_link,omitempty"`
83-
Pretext string `json:"pretext,omitempty"`
84-
Text string `json:"text"`
85-
Fallback string `json:"fallback"`
86-
CallbackID string `json:"callback_id"`
87-
Fields []config.SlackField `json:"fields,omitempty"`
88-
Actions []config.SlackAction `json:"actions,omitempty"`
89-
ImageURL string `json:"image_url,omitempty"`
90-
ThumbURL string `json:"thumb_url,omitempty"`
91-
Footer string `json:"footer"`
92-
Color string `json:"color,omitempty"`
93-
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
94-
}
95-
96-
// slackResponse represents the response from Slack API.
97-
type slackResponse struct {
98-
OK bool `json:"ok"`
99-
Error string `json:"error,omitempty"`
100-
Channel string `json:"channel,omitempty"`
101-
Timestamp string `json:"ts,omitempty"`
102-
}
103-
10456
// Notify implements the Notifier interface.
10557
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
10658
var err error

notify/slack/types.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2019 Prometheus Team
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package slack
15+
16+
import (
17+
"context"
18+
"io"
19+
"log/slog"
20+
"net/http"
21+
22+
"github.com/prometheus/alertmanager/config"
23+
"github.com/prometheus/alertmanager/notify"
24+
"github.com/prometheus/alertmanager/template"
25+
)
26+
27+
// Notifier implements a Notifier for Slack notifications.
28+
type Notifier struct {
29+
conf *config.SlackConfig
30+
tmpl *template.Template
31+
logger *slog.Logger
32+
client *http.Client
33+
retrier *notify.Retrier
34+
35+
postJSONFunc func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error)
36+
}
37+
38+
// request is the request for sending a Slack notification.
39+
type request struct {
40+
Channel string `json:"channel,omitempty"`
41+
Timestamp string `json:"ts,omitempty"`
42+
Username string `json:"username,omitempty"`
43+
IconEmoji string `json:"icon_emoji,omitempty"`
44+
IconURL string `json:"icon_url,omitempty"`
45+
LinkNames bool `json:"link_names,omitempty"`
46+
Text string `json:"text,omitempty"`
47+
Attachments []attachment `json:"attachments"`
48+
}
49+
50+
// attachment is used to display a richly formatted message block.
51+
type attachment struct {
52+
Title string `json:"title,omitempty"`
53+
TitleLink string `json:"title_link,omitempty"`
54+
Pretext string `json:"pretext,omitempty"`
55+
Text string `json:"text"`
56+
Fallback string `json:"fallback"`
57+
CallbackID string `json:"callback_id"`
58+
Fields []config.SlackField `json:"fields,omitempty"`
59+
Actions []config.SlackAction `json:"actions,omitempty"`
60+
ImageURL string `json:"image_url,omitempty"`
61+
ThumbURL string `json:"thumb_url,omitempty"`
62+
Footer string `json:"footer"`
63+
Color string `json:"color,omitempty"`
64+
MrkdwnIn []string `json:"mrkdwn_in,omitempty"`
65+
}
66+
67+
// slackResponse represents the response from Slack API.
68+
type slackResponse struct {
69+
OK bool `json:"ok"`
70+
Error string `json:"error,omitempty"`
71+
Channel string `json:"channel,omitempty"`
72+
Timestamp string `json:"ts,omitempty"`
73+
}

0 commit comments

Comments
 (0)