From 59a4edd7a5259f311916f69c7be20b25ca856075 Mon Sep 17 00:00:00 2001 From: Jonathan Martz Date: Sun, 12 Jan 2025 19:03:44 +0100 Subject: [PATCH] send mail when failed is bigger then 0 --- mailer.go | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/mailer.go b/mailer.go index 5fb2ecd..2a53261 100644 --- a/mailer.go +++ b/mailer.go @@ -68,27 +68,37 @@ func main() { log.Fatal("Counts field not found in PHPUnit output.") } - // Serialize the counts field to JSON for the email body - countsJSON, err := json.MarshalIndent(counts, "", " ") - if err != nil { - log.Fatalf("Failed to serialize counts to JSON: %v", err) + // Check if 'fails' is greater than 0 + fails, ok := counts["failed"].(float64) + if !ok { + log.Fatal("'fails' field not found in counts.") } - // Prepare the email body - emailBody := fmt.Sprintf("Subject: %s\r\n\r\n%s", subject, string(countsJSON)) + if fails > 0 { + // Serialize the counts field to JSON for the email body + countsJSON, err := json.MarshalIndent(counts, "", " ") + if err != nil { + log.Fatalf("Failed to serialize counts to JSON: %v", err) + } - // Connect to the SMTP server - auth := smtp.PlainAuth("", *smtpUser, *smtpPass, smtpHost) - err = smtp.SendMail( - smtpHost+":"+smtpPort, - auth, - *sender, - []string{recipient}, - []byte(emailBody), - ) - if err != nil { - log.Fatalf("Failed to send email: %v", err) + // Prepare the email body + emailBody := fmt.Sprintf("Subject: %s\r\n\r\n%s", subject, string(countsJSON)) + + // Connect to the SMTP server + auth := smtp.PlainAuth("", *smtpUser, *smtpPass, smtpHost) + err = smtp.SendMail( + smtpHost+":"+smtpPort, + auth, + *sender, + []string{recipient}, + []byte(emailBody), + ) + if err != nil { + log.Fatalf("Failed to send email: %v", err) + } + + log.Println("Test results sent to", recipient) + } else { + log.Println("No failures detected. No email sent.") } - - log.Println("Test results sent to", recipient) } \ No newline at end of file