Skip to content

Commit

Permalink
DE-1370 Add typecheck, goimports, govet, noctx linters (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc authored Dec 4, 2024
1 parent 7574b73 commit 8fd2c2b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 40 deletions.
9 changes: 4 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions bounces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 19 additions & 18 deletions events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions recipients.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mailgun

import "fmt"
import "strings"
import (
"fmt"
"strings"
)

type Recipient struct {
Name string `json:"-"`
Expand Down
5 changes: 3 additions & 2 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -133,21 +133,21 @@ 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 {
values.Add(k, v)
}

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)

Expand All @@ -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
Expand Down

0 comments on commit 8fd2c2b

Please sign in to comment.