|
1 | 1 | package drift
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 |
| - "encoding/json" |
6 | 4 | "fmt"
|
7 |
| - "io" |
8 | 5 | "log/slog"
|
9 |
| - "net/http" |
10 | 6 | "regexp"
|
11 | 7 | "strings"
|
12 | 8 | )
|
@@ -40,48 +36,39 @@ func SplitCodeBlocks(message string) []string {
|
40 | 36 | return res
|
41 | 37 | }
|
42 | 38 |
|
43 |
| -func (slack SlackNotification) Send(projectName string, plan string) error { |
44 |
| - message := fmt.Sprintf(":bangbang: Drift detected in digger project %v details below: \n\n```\n%v\n```", projectName, plan) |
45 |
| - httpClient := &http.Client{} |
46 |
| - type SlackMessage struct { |
47 |
| - Text string `json:"text"` |
48 |
| - } |
| 39 | +func (slack *SlackNotification) SendNotificationForProject(projectName string, repoFullName string, plan string) error { |
| 40 | + message := fmt.Sprintf( |
| 41 | + ":warning: *Infrastructure Drift Detected* :warning:\n\n"+ |
| 42 | + ":file_folder: *Project:* `%s`\n"+ |
| 43 | + ":books: *Repository:* `%s`\n\n"+ |
| 44 | + ":memo: *Terraform Plan:*\n```\n%v\n```\n\n", |
| 45 | + projectName, repoFullName, plan, |
| 46 | + ) |
49 | 47 | parts := SplitCodeBlocks(message)
|
50 | 48 | for _, part := range parts {
|
51 |
| - slackMessage := SlackMessage{ |
52 |
| - Text: part, |
53 |
| - } |
54 |
| - |
55 |
| - jsonData, err := json.Marshal(slackMessage) |
56 |
| - if err != nil { |
57 |
| - slog.Error("failed to marshal slack message", "error", err) |
58 |
| - return err |
59 |
| - } |
60 |
| - |
61 |
| - request, err := http.NewRequest("POST", slack.Url, bytes.NewBuffer(jsonData)) |
62 |
| - if err != nil { |
63 |
| - slog.Error("failed to create slack drift request", "error", err) |
64 |
| - return err |
65 |
| - } |
66 |
| - |
67 |
| - request.Header.Set("Content-Type", "application/json") |
68 |
| - resp, err := httpClient.Do(request) |
| 49 | + err := SendSlackMessage(slack.Url, part) |
69 | 50 | if err != nil {
|
70 | 51 | slog.Error("failed to send slack drift request", "error", err)
|
71 | 52 | return err
|
72 | 53 | }
|
73 |
| - if resp.StatusCode != 200 { |
74 |
| - body, err := io.ReadAll(resp.Body) |
75 |
| - if err != nil { |
76 |
| - slog.Error("failed to read response body", "error", err) |
77 |
| - return err |
78 |
| - } |
79 |
| - slog.Error("failed to send slack drift request", "status code", resp.Status, "body", body) |
80 |
| - msg := fmt.Sprintf("failed to send slack drift request. %v. Message: %v", resp.Status, body) |
81 |
| - return fmt.Errorf("%s", msg) |
82 |
| - } |
83 |
| - resp.Body.Close() |
84 | 54 | }
|
85 | 55 |
|
86 | 56 | return nil
|
87 | 57 | }
|
| 58 | + |
| 59 | +func (slack *SlackNotification) SendErrorNotificationForProject(projectName string, repoFullName string, err error) error { |
| 60 | + message := fmt.Sprintf( |
| 61 | + ":rotating_light: *Error While Drift Processing* :rotating_light:\n\n"+ |
| 62 | + ":file_folder: *Project:* `%s`\n"+ |
| 63 | + ":books: *Repository:* `%s`\n\n"+ |
| 64 | + ":warning: *Error Details:*\n```\n%v\n```\n\n"+ |
| 65 | + "_Please check the workflow logs for more information._", |
| 66 | + projectName, repoFullName, err, |
| 67 | + ) |
| 68 | + |
| 69 | + return SendSlackMessage(slack.Url, message) |
| 70 | +} |
| 71 | + |
| 72 | +func (slack *SlackNotification) Flush() error { |
| 73 | + return nil |
| 74 | +} |
0 commit comments