diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d171922f..a4c89a02 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -37,7 +37,7 @@ jobs: run: make nilaway - name: lint - run: make lint-new + run: make lint - name: Test run: go test -v -race -p=1 -count=1 diff --git a/.golangci.yml b/.golangci.yml index 495c683d..cb4026d7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -59,6 +59,15 @@ linters-settings: checks: ["all"] issues: + # Which files to exclude: they will be analyzed, but issues from them won't be reported. + # There is no need to include all autogenerated files, + # we confidently recognize autogenerated files. + # If it's not, please let us know. + # "/" will be replaced by current OS file path separator to properly work on Windows. + # Default: [] + exclude-files: + - "^mock_*" + # Maximum issues count per one linter. Set to 0 to disable. Default is 50. max-issues-per-linter: 0 @@ -67,7 +76,8 @@ issues: run: # include test files or not, default is true - tests: true + # TODO(DE-1139): Enable + tests: false # Timeout for analysis, e.g. 30s, 5m. # Default: 1m diff --git a/Makefile b/Makefile index 06490610..8756b1ee 100644 --- a/Makefile +++ b/Makefile @@ -33,10 +33,6 @@ GOLINT = $(GOPATH)/bin/golangci-lint $(GOLINT): curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin v1.61.0 -.PHONY: lint-new -lint-new: $(GOLINT) - $(GOLINT) run -new-from-rev=master - .PHONY: lint lint: $(GOLINT) $(GOLINT) run diff --git a/events.go b/events.go index a9ce2af2..ac2dc8fb 100644 --- a/events.go +++ b/events.go @@ -267,7 +267,6 @@ func (ep *EventPoller) Poll(ctx context.Context, events *[]Event) bool { // If we have events to return if len(results) != 0 { *events = results - results = nil return true } diff --git a/httphelpers.go b/httphelpers.go index 6aa3ab3a..70775f32 100644 --- a/httphelpers.go +++ b/httphelpers.go @@ -39,7 +39,7 @@ type httpResponse struct { type payload interface { getPayloadBuffer() (*bytes.Buffer, error) - getContentType() string + getContentType() (string, error) getValues() []keyValuePair } @@ -109,8 +109,8 @@ func (j *jsonEncodedPayload) getPayloadBuffer() (*bytes.Buffer, error) { return bytes.NewBuffer(b), nil } -func (j *jsonEncodedPayload) getContentType() string { - return "application/json" +func (j *jsonEncodedPayload) getContentType() (string, error) { + return "application/json", nil } func (j *jsonEncodedPayload) getValues() []keyValuePair { @@ -134,8 +134,8 @@ func (f *urlEncodedPayload) getPayloadBuffer() (*bytes.Buffer, error) { return bytes.NewBufferString(data.Encode()), nil } -func (f *urlEncodedPayload) getContentType() string { - return "application/x-www-form-urlencoded" +func (f *urlEncodedPayload) getContentType() (string, error) { + return "application/x-www-form-urlencoded", nil } func (f *urlEncodedPayload) getValues() []keyValuePair { @@ -177,24 +177,31 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, keyVal := range f.Values { if tmp, err := writer.CreateFormField(keyVal.key); err == nil { - // TODO(DE-1139): handle error: - tmp.Write([]byte(keyVal.value)) + _, err := tmp.Write([]byte(keyVal.value)) + if err != nil { + return nil, err + } } else { return nil, err } } for _, file := range f.Files { - if tmp, err := writer.CreateFormFile(file.key, path.Base(file.value)); err == nil { - if fp, err := os.Open(file.value); err == nil { - // TODO(DE-1139): defer in a loop: - defer fp.Close() - // TODO(DE-1139): handle error: - io.Copy(tmp, fp) - } else { - return nil, err - } - } else { + tmp, err := writer.CreateFormFile(file.key, path.Base(file.value)) + if err != nil { + return nil, err + } + + fp, err := os.Open(file.value) + if err != nil { + return nil, err + } + + // TODO(DE-1139): defer in a loop: + defer fp.Close() + + _, err = io.Copy(tmp, fp) + if err != nil { return nil, err } } @@ -203,8 +210,11 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { if tmp, err := writer.CreateFormFile(file.key, file.name); err == nil { // TODO(DE-1139): defer in a loop: defer file.value.Close() - // TODO(DE-1139): handle error: - io.Copy(tmp, file.value) + + _, err := io.Copy(tmp, file.value) + if err != nil { + return nil, err + } } else { return nil, err } @@ -213,25 +223,31 @@ func (f *formDataPayload) getPayloadBuffer() (*bytes.Buffer, error) { for _, buff := range f.Buffers { if tmp, err := writer.CreateFormFile(buff.key, buff.name); err == nil { r := bytes.NewReader(buff.value) - // TODO(DE-1139): handle error: - io.Copy(tmp, r) + + _, err := io.Copy(tmp, r) + if err != nil { + return nil, err + } } else { return nil, err } } + // TODO(vtopc): getPayloadBuffer is not just a getter, it also sets the content type f.contentType = writer.FormDataContentType() return data, nil } -func (f *formDataPayload) getContentType() string { +func (f *formDataPayload) getContentType() (string, error) { if f.contentType == "" { - // TODO(DE-1139): handle error: - f.getPayloadBuffer() + _, err := f.getPayloadBuffer() + if err != nil { + return "", err + } } - return f.contentType + return f.contentType, nil } func (r *httpRequest) addHeader(name, value string) { @@ -277,8 +293,13 @@ func (r *httpRequest) NewRequest(ctx context.Context, method string, payload pay return nil, err } - if payload != nil && payload.getContentType() != "" { - req.Header.Add("Content-Type", payload.getContentType()) + if payload != nil { + contentType, err := payload.getContentType() + if err != nil { + return nil, err + } + + req.Header.Add("Content-Type", contentType) } if r.BasicAuthUser != "" && r.BasicAuthPassword != "" { @@ -381,7 +402,8 @@ func (r *httpRequest) curlString(req *http.Request, p payload) string { // parts = append(parts, fmt.Sprintf(" --user '%s:%s'", r.BasicAuthUser, r.BasicAuthPassword)) if p != nil { - if p.getContentType() == "application/json" { + contentType, _ := p.getContentType() + if contentType == "application/json" { b, err := p.getPayloadBuffer() if err != nil { return "Unable to get payload buffer: " + err.Error() diff --git a/webhooks.go b/webhooks.go index c82ee4b8..175df421 100644 --- a/webhooks.go +++ b/webhooks.go @@ -124,8 +124,15 @@ type WebhookPayload struct { // Use this method to parse the webhook signature given as JSON in the webhook response func (mg *MailgunImpl) VerifyWebhookSignature(sig Signature) (verified bool, err error) { h := hmac.New(sha256.New, []byte(mg.APIKey())) - io.WriteString(h, sig.TimeStamp) - io.WriteString(h, sig.Token) + + _, err = io.WriteString(h, sig.TimeStamp) + if err != nil { + return false, err + } + _, err = io.WriteString(h, sig.Token) + if err != nil { + return false, err + } calculatedSignature := h.Sum(nil) signature, err := hex.DecodeString(sig.Signature) @@ -143,8 +150,16 @@ func (mg *MailgunImpl) VerifyWebhookSignature(sig Signature) (verified bool, err // version of WebHooks from mailgun func (mg *MailgunImpl) VerifyWebhookRequest(req *http.Request) (verified bool, err error) { h := hmac.New(sha256.New, []byte(mg.APIKey())) - io.WriteString(h, req.FormValue("timestamp")) - io.WriteString(h, req.FormValue("token")) + + _, err = io.WriteString(h, req.FormValue("timestamp")) + if err != nil { + return false, err + } + + _, err = io.WriteString(h, req.FormValue("token")) + if err != nil { + return false, err + } calculatedSignature := h.Sum(nil) signature, err := hex.DecodeString(req.FormValue("signature"))