-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotify.go
More file actions
335 lines (304 loc) · 9.52 KB
/
Copy pathnotify.go
File metadata and controls
335 lines (304 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type NotifyConfig struct {
Enabled bool `yaml:"enabled"`
TargetRepo string `yaml:"target_repo"`
Label string `yaml:"label"`
}
type Issue struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
HTMLURL string `json:"html_url"`
UpdatedAt time.Time `json:"updated_at"`
}
type IssuesResponse []Issue
type Notifier struct {
token string
sourceRepo string
targetRepo string
label string
http *http.Client
}
// NewNotifier creates a new Notifier instance
func NewNotifier(token, sourceRepo string, cfg NotifyConfig) *Notifier {
targetRepo := cfg.TargetRepo
if targetRepo == "" {
targetRepo = sourceRepo
}
label := cfg.Label
if label == "" {
label = "ci-failure"
}
return &Notifier{
token: token,
sourceRepo: sourceRepo,
targetRepo: targetRepo,
label: label,
http: &http.Client{Timeout: 15 * time.Second},
}
}
func anyFailed(runs []Run) *Run {
for i := range runs {
if runs[i].Conclusion == "failure" {
return &runs[i]
}
}
return nil
}
// Process evaluates a WorkflowSummary and creates or updates a GitHub issue
// when a critical workflow has a failed run. It skips non-critical workflows
// and avoids duplicate comments if the issue was already updated within 24 hours.
func (n *Notifier) Process(summary WorkflowSummary) {
if !summary.Critical {
return
}
repr := anyFailed(summary.RecentRuns)
if repr == nil {
log.Printf("notify: %q — no failures in recent runs, skipping", summary.Name)
return
}
log.Printf("notify: %q — failure found (run #%d)", summary.Name, repr.RunNumber)
existingIssue := n.findOpenIssue(summary.Name)
if existingIssue == nil {
log.Printf("notify: opening issue for %q", summary.Name)
n.createIssue(summary, repr)
return
}
if time.Since(existingIssue.UpdatedAt) >= 24*time.Hour {
log.Printf("notify: adding update to issue #%d for %q", existingIssue.Number, summary.Name)
n.addComment(existingIssue.Number, summary, repr)
} else {
log.Printf("notify: issue #%d for %q updated within 24h — skipping comment",
existingIssue.Number, summary.Name)
}
}
func (n *Notifier) apiURL(path string) string {
return "https://api.github.com/repos/" + n.targetRepo + path
}
// do executes an authenticated HTTP request against the GitHub API
func (n *Notifier) do(method, rawURL string, body interface{}) (*http.Response, error) {
var buf *bytes.Buffer
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
buf = bytes.NewBuffer(b)
} else {
buf = bytes.NewBuffer(nil)
}
req, err := http.NewRequest(method, rawURL, buf)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+n.token)
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Content-Type", "application/json")
return n.http.Do(req)
}
// findOpenIssue searches paginated open issues on the target repo
func (n *Notifier) findOpenIssue(workflowName string) *Issue {
needle := issueTitle(workflowName)
page := 1
for {
rawURL := n.apiURL(fmt.Sprintf(
"/issues?state=open&labels=%s&per_page=100&page=%d",
url.QueryEscape(n.label), page,
))
resp, err := n.do("GET", rawURL, nil)
if err != nil {
log.Printf("notify: warn: could not list issues: %v", err)
return nil
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
log.Printf("notify: warn: issues list returned HTTP %d", resp.StatusCode)
return nil
}
var issues IssuesResponse
if err := json.NewDecoder(resp.Body).Decode(&issues); err != nil {
resp.Body.Close()
log.Printf("notify: warn: could not decode issues: %v", err)
return nil
}
resp.Body.Close()
for i := range issues {
title := issues[i].Title
if strings.EqualFold(title, needle) || strings.Contains(title, workflowName) {
log.Printf("notify: found existing issue #%d for %q",
issues[i].Number, workflowName)
return &issues[i]
}
}
if len(issues) < 100 {
break
}
page++
}
return nil
}
func (n *Notifier) createIssue(summary WorkflowSummary, repr *Run) {
resp, err := n.do("POST", n.apiURL("/issues"), map[string]interface{}{
"title": issueTitle(summary.Name),
"body": buildIssueBody(summary, repr, n.sourceRepo),
"labels": []string{n.label},
})
if err != nil {
log.Printf("notify: warn: could not create issue: %v", err)
return
}
defer resp.Body.Close()
var created Issue
json.NewDecoder(resp.Body).Decode(&created)
if created.Number > 0 {
log.Printf("notify: issue #%d created → %s", created.Number, created.HTMLURL)
}
}
// addComment posts a follow-up comment to an existing issue.
func (n *Notifier) addComment(issueNumber int, summary WorkflowSummary, repr *Run) {
rawURL := n.apiURL(fmt.Sprintf("/issues/%d/comments", issueNumber))
resp, err := n.do("POST", rawURL, map[string]string{
"body": buildCommentBody(summary, repr, n.sourceRepo),
})
if err != nil {
log.Printf("notify: warn: could not add comment: %v", err)
return
}
resp.Body.Close()
}
func issueTitle(workflowName string) string {
return fmt.Sprintf("CI Failure: %s", workflowName)
}
func weatherEmoji(c string) string {
switch c {
case "success":
return "✅"
case "failure":
return "❌"
case "skipped":
return "⏭️"
case "action_required":
return "⚠️"
default:
return "⬜"
}
}
func buildSparkline(history []string) string {
if len(history) == 0 {
return ""
}
var sb strings.Builder
for _, c := range history {
sb.WriteString(weatherEmoji(c))
sb.WriteString(" ")
}
return strings.TrimSpace(sb.String())
}
// buildRawLogBlock renders a collapsible <details> block containing the raw step
// log for a failed job. Returns an empty string if no usable log is available.
// Logs exceeding 30KB are truncated with a link
func buildRawLogBlock(job FailedJob) string {
raw := job.RawLog
if raw == "" ||
strings.HasPrefix(raw, "(log expired") ||
strings.HasPrefix(raw, "(log fetch") {
return ""
}
const maxBytes = 30000
truncated := false
if len(raw) > maxBytes {
raw = raw[:maxBytes]
truncated = true
}
var sb strings.Builder
sb.WriteString("<details>\n")
sb.WriteString(fmt.Sprintf("<summary>Full step log — %s</summary>\n\n", job.Name))
sb.WriteString("```\n")
sb.WriteString(raw)
if truncated {
sb.WriteString("\n\n[truncated at 30KB — see full log at: ")
sb.WriteString(job.HTMLURL)
sb.WriteString("]")
}
sb.WriteString("\n```\n")
sb.WriteString("</details>\n\n")
return sb.String()
}
// buildSnippetSection renders the log snippet and optional raw log block for a failed job
func buildSnippetSection(job FailedJob) string {
var sb strings.Builder
switch job.LogSnippet {
case "", "(no actionable failure signal found in log)", "(log fetch failed)":
if job.LogSnippet != "" {
sb.WriteString(fmt.Sprintf("> _%s_\n\n", job.LogSnippet))
}
default:
sb.WriteString("**Signal summary:**\n\n")
sb.WriteString("```\n")
sb.WriteString(strings.TrimSpace(job.LogSnippet))
sb.WriteString("\n```\n\n")
}
sb.WriteString(buildRawLogBlock(job))
return sb.String()
}
// buildFailedJobsSection renders a Markdown section listing each failed job with its log snippet
func buildFailedJobsSection(jobs []FailedJob) string {
if len(jobs) == 0 {
return "> No individual job failure data captured — check the run link above.\n\n"
}
var sb strings.Builder
for _, job := range jobs {
sb.WriteString(fmt.Sprintf("#### ❌ [%s](%s)\n\n", job.Name, job.HTMLURL))
sb.WriteString(buildSnippetSection(job))
}
return sb.String()
}
// buildIssueBody composes the full Markdown body for a newly opened CI failure issue
func buildIssueBody(summary WorkflowSummary, repr *Run, sourceRepo string) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("## ❌ Critical workflow failing: `%s`\n\n", summary.Name))
if summary.Description != "" {
sb.WriteString(fmt.Sprintf("> %s\n\n", summary.Description))
}
sb.WriteString("### Failed Run\n\n")
sb.WriteString("| Field | Value |\n|---|---|\n")
sb.WriteString(fmt.Sprintf("| Run | [#%d](%s) |\n", repr.RunNumber, repr.HTMLURL))
sb.WriteString(fmt.Sprintf("| Started | `%s` |\n", repr.CreatedAt.Format(time.RFC1123)))
sb.WriteString(fmt.Sprintf("| Conclusion | `%s` |\n", repr.Conclusion))
sb.WriteString(fmt.Sprintf("| Attempt | `%d` |\n", repr.RunAttempt))
sb.WriteString(fmt.Sprintf("| Repo | [%s](https://github.com/%s) |\n\n",
sourceRepo, sourceRepo))
if spark := buildSparkline(summary.WeatherHistory); spark != "" {
sb.WriteString("### Recent History (oldest → newest)\n\n")
sb.WriteString(spark + "\n\n")
}
sb.WriteString("### Failed Jobs\n\n")
sb.WriteString(buildFailedJobsSection(repr.FailedJobs))
sb.WriteString("---\n")
sb.WriteString("This issue was opened automatically by the CI dashboard.")
sb.WriteString("Please close it manually once the issue is resolved._\n")
return sb.String()
}
// buildCommentBody composes a shorter Markdown body for a follow-up comment on
// an existing issue, summarising the latest failing run and updated job details.
func buildCommentBody(summary WorkflowSummary, repr *Run, sourceRepo string) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("### ❌ Still failing — run [#%d](%s)\n\n",
repr.RunNumber, repr.HTMLURL))
sb.WriteString(fmt.Sprintf("Failed at `%s`.\n\n", repr.CreatedAt.Format(time.RFC1123)))
if spark := buildSparkline(summary.WeatherHistory); spark != "" {
sb.WriteString("**Recent history (oldest → newest):** " + spark + "\n\n")
}
sb.WriteString(buildFailedJobsSection(repr.FailedJobs))
return sb.String()
}