|
| 1 | +package pkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log/slog" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +type Details struct { |
| 16 | + Label string |
| 17 | + Message string |
| 18 | +} |
| 19 | + |
| 20 | +type teamsCard struct { |
| 21 | + Type string `json:"type"` |
| 22 | + Attachments []teamsAttachment `json:"attachments"` |
| 23 | +} |
| 24 | + |
| 25 | +type teamsAttachment struct { |
| 26 | + ContentType string `json:"contentType"` |
| 27 | + ContentURL *string `json:"contentUrl"` |
| 28 | + Content teamsCardContent `json:"content"` |
| 29 | +} |
| 30 | + |
| 31 | +type teamsCardContent struct { |
| 32 | + Schema string `json:"$schema"` |
| 33 | + Type string `json:"type"` |
| 34 | + Version string `json:"version"` |
| 35 | + AccentColor string `json:"accentColor"` |
| 36 | + Body []interface{} `json:"body"` |
| 37 | + Actions []teamsAction `json:"actions,omitempty"` |
| 38 | + MSTeams teamsMSTeams `json:"msteams"` |
| 39 | +} |
| 40 | + |
| 41 | +type teamsTextBlock struct { |
| 42 | + Type string `json:"type"` |
| 43 | + Text string `json:"text"` |
| 44 | + ID string `json:"id,omitempty"` |
| 45 | + Size string `json:"size,omitempty"` |
| 46 | + Weight string `json:"weight,omitempty"` |
| 47 | + Color string `json:"color,omitempty"` |
| 48 | +} |
| 49 | + |
| 50 | +type teamsFact struct { |
| 51 | + Title string `json:"title"` |
| 52 | + Value string `json:"value"` |
| 53 | +} |
| 54 | + |
| 55 | +type teamsFactSet struct { |
| 56 | + Type string `json:"type"` |
| 57 | + Facts []teamsFact `json:"facts"` |
| 58 | + ID string `json:"id"` |
| 59 | +} |
| 60 | + |
| 61 | +type teamsAction struct { |
| 62 | + Type string `json:"type"` |
| 63 | + Title string `json:"title"` |
| 64 | + URL string `json:"url"` |
| 65 | +} |
| 66 | + |
| 67 | +type teamsMSTeams struct { |
| 68 | + Width string `json:"width"` |
| 69 | +} |
| 70 | + |
| 71 | +func normalizeGitURL(rawURL string) string { |
| 72 | + u := strings.TrimSpace(rawURL) |
| 73 | + u = strings.TrimRight(u, "/") |
| 74 | + if strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") { |
| 75 | + return u |
| 76 | + } |
| 77 | + return "https://" + u |
| 78 | +} |
| 79 | + |
| 80 | +func actionButton(title string, details []Details, gitURL string) []teamsAction { |
| 81 | + if gitURL == "" { |
| 82 | + return nil |
| 83 | + } |
| 84 | + var filePath, match, ignore, lines string |
| 85 | + for _, d := range details { |
| 86 | + switch d.Label { |
| 87 | + case "File": |
| 88 | + filePath = d.Message |
| 89 | + case "Match": |
| 90 | + match = d.Message |
| 91 | + case "Ignore": |
| 92 | + ignore = d.Message |
| 93 | + case "Lines": |
| 94 | + lines = d.Message |
| 95 | + } |
| 96 | + } |
| 97 | + issueBody := fmt.Sprintf("**File:** %s\n**Match:** %s\n**Ignore:** %s\n\n**Lines:**\n```\n%s\n```", filePath, match, ignore, lines) |
| 98 | + q := url.Values{} |
| 99 | + q.Set("title", title) |
| 100 | + q.Set("body", issueBody) |
| 101 | + q.Set("labels", "go-watch-logs") |
| 102 | + normalizedURL := normalizeGitURL(gitURL) |
| 103 | + issueURL := normalizedURL + "/issues/new?" + q.Encode() |
| 104 | + buttonTitle := "Create issue" |
| 105 | + if parsed, err := url.Parse(normalizedURL); err == nil { |
| 106 | + if orgRepo := strings.TrimLeft(parsed.Path, "/"); orgRepo != "" { |
| 107 | + buttonTitle = "Create Issue on " + orgRepo |
| 108 | + } |
| 109 | + } |
| 110 | + return []teamsAction{{ |
| 111 | + Type: "Action.OpenUrl", |
| 112 | + Title: buttonTitle, |
| 113 | + URL: issueURL, |
| 114 | + }} |
| 115 | +} |
| 116 | + |
| 117 | +func sendToTeams(title string, details []Details, gitURL, hookURL string, httpClient *http.Client) error { |
| 118 | + facts := make([]teamsFact, len(details)) |
| 119 | + for i, d := range details { |
| 120 | + facts[i] = teamsFact{Title: d.Label, Value: d.Message} |
| 121 | + } |
| 122 | + |
| 123 | + actions := actionButton(title, details, gitURL) |
| 124 | + |
| 125 | + card := teamsCard{ |
| 126 | + Type: "message", |
| 127 | + Attachments: []teamsAttachment{ |
| 128 | + { |
| 129 | + ContentType: "application/vnd.microsoft.card.adaptive", |
| 130 | + ContentURL: nil, |
| 131 | + Content: teamsCardContent{ |
| 132 | + Schema: "http://adaptivecards.io/schemas/adaptive-card.json", |
| 133 | + Type: "AdaptiveCard", |
| 134 | + Version: "1.4", |
| 135 | + AccentColor: "bf0000", |
| 136 | + Body: []interface{}{ |
| 137 | + teamsTextBlock{ |
| 138 | + Type: "TextBlock", |
| 139 | + Text: title, |
| 140 | + ID: "title", |
| 141 | + Size: "large", |
| 142 | + Weight: "bolder", |
| 143 | + Color: "accent", |
| 144 | + }, |
| 145 | + teamsFactSet{ |
| 146 | + Type: "FactSet", |
| 147 | + Facts: facts, |
| 148 | + ID: "acFactSet", |
| 149 | + }, |
| 150 | + }, |
| 151 | + Actions: actions, |
| 152 | + MSTeams: teamsMSTeams{Width: "Full"}, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + } |
| 157 | + |
| 158 | + requestBody, err := json.Marshal(card) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + req, err := http.NewRequest("POST", hookURL, bytes.NewBuffer(requestBody)) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + req.Header.Set("Content-type", "application/json") |
| 168 | + |
| 169 | + resp, err := httpClient.Do(req) //nolint:gosec // hookURL is user-configured webhook, not attacker-controlled |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + defer resp.Body.Close() |
| 174 | + _, err = io.ReadAll(resp.Body) |
| 175 | + return err |
| 176 | +} |
| 177 | + |
| 178 | +func NotifyOwnErrorToTeams(e error, r slog.Record, msTeamsHook string, httpClient *http.Client) { |
| 179 | + hostname, _ := os.Hostname() |
| 180 | + slog.Info("Sending own error to MS Teams") |
| 181 | + |
| 182 | + details := []Details{ |
| 183 | + { |
| 184 | + Label: "Hostname", |
| 185 | + Message: hostname, |
| 186 | + }, |
| 187 | + { |
| 188 | + Label: "Error", |
| 189 | + Message: e.Error(), |
| 190 | + }, |
| 191 | + } |
| 192 | + r.Attrs(func(attr slog.Attr) bool { |
| 193 | + details = append(details, Details{ |
| 194 | + Label: attr.Key, |
| 195 | + Message: fmt.Sprintf("%v", attr.Value), |
| 196 | + }) |
| 197 | + return true |
| 198 | + }) |
| 199 | + |
| 200 | + err := sendToTeams(hostname, details, "", msTeamsHook, httpClient) |
| 201 | + if err != nil { |
| 202 | + // keep it warn to prevent infinite loop from the global handler of slog |
| 203 | + slog.Warn("Error sending to Teams", "error", err.Error()) |
| 204 | + return |
| 205 | + } |
| 206 | + slog.Info("Successfully sent own error to MS Teams") |
| 207 | +} |
0 commit comments