Skip to content

Commit

Permalink
apply changes per feedback review
Browse files Browse the repository at this point in the history
Signed-off-by: cpanato <[email protected]>
  • Loading branch information
cpanato committed May 30, 2024
1 parent 0218214 commit 1bff424
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
45 changes: 21 additions & 24 deletions cmd/patch-release-notification/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"embed"
"fmt"
"io"
"log"
"math"
"net/http"
"os"
Expand Down Expand Up @@ -91,7 +90,7 @@ func getConfig() (*Config, error) {
return &c, nil
}

func New(ctx context.Context) (*Options, error) {
func NewOptions(ctx context.Context) (*Options, error) {
config, err := getConfig()
if err != nil {
return nil, fmt.Errorf("failed to get config: %w", err)
Expand All @@ -102,7 +101,6 @@ func New(ctx context.Context) (*Options, error) {
Region: aws.String(config.AWSRegion),
})
if err != nil {
log.Println("Error occurred while creating aws session", err)
return nil, err
}

Expand All @@ -114,20 +112,21 @@ func New(ctx context.Context) (*Options, error) {
}

func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { //nolint: gocritic
o, err := New(ctx)
o, err := NewOptions(ctx)
if err != nil {
return events.APIGatewayProxyResponse{
Body: `{"status": "nok"}`,
StatusCode: http.StatusInternalServerError,
}, err
}

logrus.Infof("Will pull the path release schedule from: %s", o.Config.SchedulePath)
data, err := loadFileOrURL(o.Config.SchedulePath)
if err != nil {
return events.APIGatewayProxyResponse{
Body: `{"status": "nok"}`,
StatusCode: http.StatusInternalServerError,
}, fmt.Errorf("failed to read the file: %w", err)
}, fmt.Errorf("reading the file: %w", err)
}

patchSchedule := &model.PatchSchedule{}
Expand All @@ -138,7 +137,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
return events.APIGatewayProxyResponse{
Body: `{"status": "nok"}`,
StatusCode: http.StatusInternalServerError,
}, fmt.Errorf("failed to decode the file: %w", err)
}, fmt.Errorf("decoding the file: %w", err)
}

output := &Template{}
Expand All @@ -157,7 +156,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
currentTime := time.Now().UTC()
days := t.Sub(currentTime).Hours() / 24
intDay, _ := math.Modf(days)
logrus.Infof("cherry pick deadline: %d, days to alert: %d", int(intDay), o.Config.DaysToAlert)
logrus.Infof("Cherry pick deadline: %d, days to alert: %d", int(intDay), o.Config.DaysToAlert)
if int(intDay) == o.Config.DaysToAlert {
output.Releases = append(output.Releases, TemplateRelease{
Release: patch.Release,
Expand All @@ -167,6 +166,14 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
}
}

if !shouldSendEmail {
logrus.Info("No email is needed to send")
return events.APIGatewayProxyResponse{
Body: `{"status": "ok"}`,
StatusCode: http.StatusOK,
}, nil
}

tmpl, err := template.ParseFS(tpls, "templates/email.tmpl")
if err != nil {
return events.APIGatewayProxyResponse{
Expand All @@ -181,19 +188,11 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
return events.APIGatewayProxyResponse{
Body: `{"status": "nok"}`,
StatusCode: http.StatusInternalServerError,
}, fmt.Errorf("parsing values to the template: %w", err)
}

if !shouldSendEmail {
logrus.Info("No email is needed to send")
return events.APIGatewayProxyResponse{
Body: `{"status": "ok"}`,
StatusCode: http.StatusOK,
}, nil
}, fmt.Errorf("executing the template: %w", err)
}

logrus.Info("Sending mail")
subject := "[Please Read] Patch Releases cherry-pick deadline"
subject := "[Please Read] Upcoming Patch Releases Cherry-Pick Deadline for Kubernetes"
fromEmail := o.Config.FromEmail

recipient := Recipient{
Expand All @@ -212,7 +211,7 @@ func handler(ctx context.Context, request events.APIGatewayProxyRequest) (events
return events.APIGatewayProxyResponse{
Body: `{"status": "nok"}`,
StatusCode: http.StatusInternalServerError,
}, fmt.Errorf("parsing values to the template: %w", err)
}, fmt.Errorf("sending the email: %w", err)
}

return events.APIGatewayProxyResponse{
Expand Down Expand Up @@ -240,9 +239,6 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
recipients = append(recipients, &recipient)
}

// Set to emails
msg.SetHeader("To", recipient.toEmails...)

// cc mails mentioned
if len(recipient.ccEmails) != 0 {
// Need to add cc mail IDs also in recipient list
Expand Down Expand Up @@ -275,7 +271,7 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
var emailRaw bytes.Buffer
_, err := msg.WriteTo(&emailRaw)
if err != nil {
log.Printf("failed to write mail: %v\n", err)
logrus.Errorf("Failed to write mail: %v", err)
return err
}

Expand All @@ -287,11 +283,11 @@ func (o *Options) SendEmailRawSES(messageBody, subject, fromEmail string, recipi
// send raw email
_, err = svc.SendRawEmail(input)
if err != nil {
log.Println("Error sending mail - ", err)
logrus.Errorf("Error sending mail - %v", err)
return err
}

log.Println("Email sent successfully to: ", recipient.toEmails)
logrus.Infof("Email sent successfully to: %q", recipient.toEmails)
return nil
}

Expand All @@ -314,5 +310,6 @@ func loadFileOrURL(fileRef string) ([]byte, error) {
return nil, err
}
}

return raw, nil
}
4 changes: 2 additions & 2 deletions cmd/patch-release-notification/templates/email.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ensure they have:</p>
<p> - /priority</p>
<p> - /lgtm</p>
<p> - /approve</p>
<p> - passing tests</p>
<p> - passing tests and not on hold.</p>
<br>
<p>Details on the cherry-pick process can be found here:</p>
<p>https://git.k8s.io/community/contributors/devel/sig-release/cherry-picks.md</p>
Expand All @@ -27,6 +27,6 @@ ensure they have:</p>
<p>If you have any questions for the Release Managers, please feel free to
reach out to us at #release-management (Kubernetes Slack) or [email protected]</p><br>
<p>We wish everyone a happy and safe week!</p>
<p>SIG-Release Team</p>
<p>SIG Release Team</p>
</body>
</html>

0 comments on commit 1bff424

Please sign in to comment.