| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "html" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "gitlab.com/Timfa/bluegit/internal/ci" |
| 12 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 13 | "gitlab.com/Timfa/bluegit/internal/store" |
| 14 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 15 | ) |
| 16 | |
| 17 | func (s *Server) queueFailureMail(repo store.Repo, ev webhook.Event) { |
| 18 | if s.Mail == nil || ev.Type != webhook.EventPipelineFailure { |
| 19 | return |
| 20 | } |
| 21 | go func() { |
| 22 | ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| 23 | defer cancel() |
| 24 | if err := s.sendFailureMail(ctx, repo, ev); err != nil && s.Log != nil { |
| 25 | s.Log.Error("pipeline-mail", "err", err) |
| 26 | } |
| 27 | }() |
| 28 | } |
| 29 | |
| 30 | // sendFailureMail mailt de veroorzaker in diens taal. Een fout hier stopt de pipeline niet. |
| 31 | func (s *Server) sendFailureMail(ctx context.Context, repo store.Repo, ev webhook.Event) error { |
| 32 | if s.Mail == nil { |
| 33 | return nil |
| 34 | } |
| 35 | id, err := strconv.ParseInt(ev.PipelineID, 10, 64) |
| 36 | if err != nil || id <= 0 { |
| 37 | return nil |
| 38 | } |
| 39 | p, err := s.Store.PipelineByID(ctx, repo.ID, id) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | actor := strings.TrimSpace(p.Actor) |
| 44 | if actor == "" { |
| 45 | return nil |
| 46 | } |
| 47 | lang := i18n.EN |
| 48 | if u, err := s.Store.UserByUsername(ctx, actor); err == nil { |
| 49 | lang = i18n.Parse(u.Language) |
| 50 | } |
| 51 | jobs, err := s.Store.JobsByPipeline(ctx, p.ID) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | refName, _ := ci.SplitRef(p.Ref) |
| 56 | if refName == "" { |
| 57 | refName = p.Ref |
| 58 | } |
| 59 | pipeURL := strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(p.ID, 10) |
| 60 | subject := clipRunes(i18n.T(lang, "mail.fail.subject", p.ID, repo.FullPath()), 200) |
| 61 | var b strings.Builder |
| 62 | fmt.Fprintf(&b, "<p>%s</p>", html.EscapeString(i18n.T(lang, "mail.fail.intro", p.ID, repo.FullPath(), refName))) |
| 63 | var hard []store.Job |
| 64 | for _, j := range jobs { |
| 65 | if j.Status == "failed" && !j.AllowFailure { |
| 66 | hard = append(hard, j) |
| 67 | } |
| 68 | } |
| 69 | if len(hard) > 0 { |
| 70 | fmt.Fprintf(&b, "<p>%s</p><ul>", html.EscapeString(i18n.T(lang, "mail.fail.jobs"))) |
| 71 | for _, j := range hard { |
| 72 | jobURL := pipeURL + "/jobs/" + strconv.FormatInt(j.ID, 10) |
| 73 | fmt.Fprintf(&b, `<li><a href="%s">%s</a></li>`, html.EscapeString(jobURL), html.EscapeString(j.Name)) |
| 74 | } |
| 75 | b.WriteString("</ul>") |
| 76 | } |
| 77 | fmt.Fprintf(&b, `<p><a href="%s">%s</a></p>`, html.EscapeString(pipeURL), html.EscapeString(i18n.T(lang, "mail.fail.open"))) |
| 78 | return s.Mail.SendEmail(ctx, actor, subject, b.String()) |
| 79 | } |
| 80 | |
| 81 | func clipRunes(s string, n int) string { |
| 82 | r := []rune(s) |
| 83 | if len(r) <= n { |
| 84 | return s |
| 85 | } |
| 86 | return string(r[:n]) |
| 87 | } |