Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SMTP support to the announces (#2485)
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com> Co-authored-with: Erkan Zileli <erkan.zileli@trendyol.com> Co-authored-with: Furkan Turkal <furkan.turkal@trendyol.com>
- Loading branch information
1 parent
a9074a3
commit afad409
Showing
7 changed files
with
176 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package smtp | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
|
||
"github.com/apex/log" | ||
"github.com/caarlos0/env/v6" | ||
"github.com/goreleaser/goreleaser/internal/pipe" | ||
"github.com/goreleaser/goreleaser/internal/tmpl" | ||
"github.com/goreleaser/goreleaser/pkg/context" | ||
gomail "gopkg.in/mail.v2" | ||
) | ||
|
||
const ( | ||
defaultSubjectTemplate = `{{ .ProjectName }} {{ .Tag }} is out!` | ||
defaultBodyTemplate = `You can view details from: {{ .GitURL }}/releases/tag/{{ .Tag }}` | ||
) | ||
|
||
type Pipe struct{} | ||
|
||
func (Pipe) String() string { return "reddit" } | ||
|
||
type Config struct { | ||
Host string `env:"SMTP_HOST,notEmpty"` | ||
Port int `env:"SMTP_PORT,notEmpty"` | ||
Username string `env:"SMTP_USERNAME,notEmpty"` | ||
Password string `env:"SMTP_PASSWORD,notEmpty"` | ||
} | ||
|
||
func (Pipe) Default(ctx *context.Context) error { | ||
if ctx.Config.Announce.SMTP.SubjectTemplate == "" { | ||
ctx.Config.Announce.SMTP.SubjectTemplate = defaultSubjectTemplate | ||
} | ||
|
||
if ctx.Config.Announce.SMTP.BodyTemplate == "" { | ||
ctx.Config.Announce.SMTP.BodyTemplate = defaultBodyTemplate | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (Pipe) Announce(ctx *context.Context) error { | ||
if ctx.SkipAnnounce { | ||
return pipe.ErrSkipAnnounceEnabled | ||
} | ||
if !ctx.Config.Announce.SMTP.Enabled { | ||
return pipe.ErrSkipDisabledPipe | ||
} | ||
|
||
subject, err := tmpl.New(ctx).Apply(ctx.Config.Announce.SMTP.SubjectTemplate) | ||
if err != nil { | ||
return fmt.Errorf("announce: failed to announce to SMTP: %w", err) | ||
} | ||
|
||
body, err := tmpl.New(ctx).Apply(ctx.Config.Announce.SMTP.BodyTemplate) | ||
if err != nil { | ||
return fmt.Errorf("announce: failed to announce to SMTP: %w", err) | ||
} | ||
|
||
m := gomail.NewMessage() | ||
|
||
// Set E-Mail sender | ||
m.SetHeader("From", ctx.Config.Announce.SMTP.From) | ||
|
||
// Set E-Mail receivers | ||
receivers := ctx.Config.Announce.SMTP.To | ||
m.SetHeader("To", receivers...) | ||
|
||
// Set E-Mail subject | ||
m.SetHeader("Subject", subject) | ||
|
||
// Set E-Mail body. You can set plain text or html with text/html | ||
m.SetBody("text/plain", body) | ||
|
||
var cfg Config | ||
if err := env.Parse(&cfg); err != nil { | ||
return fmt.Errorf("announce: failed to announce to SMTP: %w", err) | ||
} | ||
|
||
// Settings for SMTP server | ||
d := gomail.NewDialer(cfg.Host, cfg.Port, cfg.Username, cfg.Password) | ||
|
||
// This is only needed when SSL/TLS certificate is not valid on server. | ||
// In production this should be set to false. | ||
d.TLSConfig = &tls.Config{InsecureSkipVerify: ctx.Config.Announce.SMTP.InsecureSkipVerify} | ||
|
||
// Now send E-Mail | ||
if err := d.DialAndSend(m); err != nil { | ||
return fmt.Errorf("announce: failed to announce to SMTP: %w", err) | ||
} | ||
|
||
log.Infof("announce: The mail has been send from %s to %s\n", ctx.Config.Announce.SMTP.From, receivers) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
afad409
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: