From 8fd2c2b5dbf3f68cb77dba24882da6e96a9649d1 Mon Sep 17 00:00:00 2001 From: Vilen Topchii <32271530+vtopc@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:30:54 +0200 Subject: [PATCH] DE-1370 Add typecheck, goimports, govet, noctx linters (#358) --- .golangci.yml | 9 ++++----- bounces.go | 12 ++++++------ events.go | 37 +++++++++++++++++++------------------ recipients.go | 6 ++++-- routes.go | 5 +++-- tags.go | 3 ++- webhooks_test.go | 12 ++++++------ 7 files changed, 44 insertions(+), 40 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index e1d781db..ab8f7109 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,24 +4,23 @@ linters: # Full list of linters - https://golangci-lint.run/usage/linters disable-all: true enable: + - typecheck - errcheck # Mandatory. Do not disable. - ineffassign # Mandatory. Do not disable. - staticcheck # Mandatory. Do not disable. + - govet - gosimple - gosec - bodyclose # https://github.com/timakin/bodyclose + - goimports - gocyclo + - noctx - gomodguard - nolintlint # TODO: # - gocritic -# - goimports -# - gosimple -# - govet -# - noctx # - stylecheck -# - typecheck # - unused linters-settings: diff --git a/bounces.go b/bounces.go index ab7fd28f..0e576c75 100644 --- a/bounces.go +++ b/bounces.go @@ -159,16 +159,16 @@ func (mg *MailgunImpl) GetBounce(ctx context.Context, address string) (Bounce, e // AddBounce files a bounce report. // Address identifies the intended recipient of the message that bounced. // Code corresponds to the numeric response given by the e-mail server which rejected the message. -// Error provides the corresponding human readable reason for the problem. +// Error provides the corresponding human-readable reason for the problem. // For example, // here's how the these two fields relate. // Suppose the SMTP server responds with an error, as below. -// Then, . . . +// Then, ... // -// 550 Requested action not taken: mailbox unavailable -// \___/\_______________________________________________/ -// | | -// `-- Code `-- Error +// 550 Requested action not taken: mailbox unavailable +// \___/\_______________________________________________/ +// | | +// `-- Code `-- Error // // Note that both code and error exist as strings, even though // code will report as a number. diff --git a/events.go b/events.go index efd75b1d..4fe7e410 100644 --- a/events.go +++ b/events.go @@ -186,26 +186,27 @@ type EventPoller struct { err error } -// Poll the events api and return new events as they occur -// it = mg.PollEvents(&ListEventOptions{ -// // Only events with a timestamp after this date/time will be returned -// Begin: time.Now().Add(time.Second * -3), -// // How often we poll the api for new events -// PollInterval: time.Second * 4 -// }) +// PollEvents polls the events api and return new events as they occur // -// var events []Event -// ctx, cancel := context.WithCancel(context.Background()) +// it = mg.PollEvents(&ListEventOptions{ +// // Only events with a timestamp after this date/time will be returned +// Begin: time.Now().Add(time.Second * -3), +// // How often we poll the api for new events +// PollInterval: time.Second * 4 +// }) // -// // Blocks until new events appear or context is cancelled -// for it.Poll(ctx, &events) { -// for _, event := range(events) { -// fmt.Printf("Event %+v\n", event) -// } -// } -// if it.Err() != nil { -// log.Fatal(it.Err()) -// } +// var events []Event +// ctx, cancel := context.WithCancel(context.Background()) +// +// // Blocks until new events appear or context is cancelled +// for it.Poll(ctx, &events) { +// for _, event := range(events) { +// fmt.Printf("Event %+v\n", event) +// } +// } +// if it.Err() != nil { +// log.Fatal(it.Err()) +// } func (mg *MailgunImpl) PollEvents(opts *ListEventOptions) *EventPoller { now := time.Now() // ForceAscending must be set diff --git a/recipients.go b/recipients.go index 31788493..010697d1 100644 --- a/recipients.go +++ b/recipients.go @@ -1,7 +1,9 @@ package mailgun -import "fmt" -import "strings" +import ( + "fmt" + "strings" +) type Recipient struct { Name string `json:"-"` diff --git a/routes.go b/routes.go index c5e4b49d..97226c9f 100644 --- a/routes.go +++ b/routes.go @@ -49,14 +49,15 @@ type ForwardedMessage struct { // ExtractForwardedMessage extracts the forward route payload values from a parsed PostForm // Example usage: -// func Handler(w http.ResponseWriter, r *http.Request) { +// +// func Handler(w http.ResponseWriter, r *http.Request) { // err := r.ParseForm() // if err != nil { // log.Fatal(err) // } // forwardRoute := mailgun.ExtractForwardedMessage(r.PostForm) // fmt.Printf("Forwarded message: %#v", forwardRoute) -// } +// } func ExtractForwardedMessage(formValues url.Values) ForwardedMessage { forwardedMessage := ForwardedMessage{} forwardedMessage.BodyPlain = formValues.Get("body-plain") diff --git a/tags.go b/tags.go index a6ba9ea7..e8f477df 100644 --- a/tags.go +++ b/tags.go @@ -45,6 +45,7 @@ func (mg *MailgunImpl) GetTag(ctx context.Context, tag string) (Tag, error) { } // ListTags returns a cursor used to iterate through a list of tags +// // it := mg.ListTags(nil) // var page []mailgun.Tag // for it.Next(&page) { @@ -166,7 +167,7 @@ func canFetchPage(slug string) bool { if err != nil { return false } - params, _ := url.ParseQuery(parts.RawQuery) + params, err := url.ParseQuery(parts.RawQuery) if err != nil { return false } diff --git a/webhooks_test.go b/webhooks_test.go index aac3a596..73144063 100644 --- a/webhooks_test.go +++ b/webhooks_test.go @@ -105,7 +105,7 @@ func TestVerifyWebhookRequest_Form(t *testing.T) { for _, v := range signedTests { fields := getSignatureFields(mg.WebhookSigningKey(), v) - req := buildFormRequest(fields) + req := buildFormRequest(context.Background(), fields) verified, err := mg.VerifyWebhookRequest(req) require.NoError(t, err) @@ -122,7 +122,7 @@ func TestVerifyWebhookRequest_MultipartForm(t *testing.T) { for _, v := range signedTests { fields := getSignatureFields(mg.WebhookSigningKey(), v) - req := buildMultipartFormRequest(fields) + req := buildMultipartFormRequest(context.Background(), fields) verified, err := mg.VerifyWebhookRequest(req) require.NoError(t, err) @@ -133,7 +133,7 @@ func TestVerifyWebhookRequest_MultipartForm(t *testing.T) { } } -func buildFormRequest(fields map[string]string) *http.Request { +func buildFormRequest(ctx context.Context, fields map[string]string) *http.Request { values := url.Values{} for k, v := range fields { @@ -141,13 +141,13 @@ func buildFormRequest(fields map[string]string) *http.Request { } r := strings.NewReader(values.Encode()) - req, _ := http.NewRequest(http.MethodPost, "/", r) + req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/", r) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") return req } -func buildMultipartFormRequest(fields map[string]string) *http.Request { +func buildMultipartFormRequest(ctx context.Context, fields map[string]string) *http.Request { buf := &bytes.Buffer{} writer := multipart.NewWriter(buf) @@ -157,7 +157,7 @@ func buildMultipartFormRequest(fields map[string]string) *http.Request { writer.Close() - req, _ := http.NewRequest(http.MethodPost, "/", buf) + req, _ := http.NewRequestWithContext(ctx, http.MethodPost, "/", buf) req.Header.Set("Content-type", writer.FormDataContentType()) return req