Skip to content

Commit

Permalink
ci: add and configure golang-lint (#27)
Browse files Browse the repository at this point in the history
* ci: add and configure golang-lint in Github Actions
* chore: update code to comply with linter rules
  • Loading branch information
vorobeyme authored Apr 29, 2024
1 parent 3302a7f commit 0ffeebe
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 101 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: golangci-lint

on:
push:
branches:
- main
pull_request:

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.57.2
52 changes: 52 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
run:
timeout: 5m
issues-exit-code: 1
tests: true
modules-download-mode: readonly

linters:
disable-all: true
enable:
- bodyclose
- contextcheck
- gofmt
- errcheck
- errorlint
- gocritic
- godot
- gosec
- gosimple
- govet
- ineffassign
- misspell
- revive
- staticcheck
- typecheck
- unused
- nilerr
- tparallel
- unparam
- whitespace
- bidichk
- exportloopref
- goconst
- reassign
- goimports
- exhaustive
- gci
- gomodguard
- prealloc
- forbidigo
- dogsled
- nakedret
- stylecheck
- unconvert
- gocyclo
- gosec
- nolintlint
- forcetypeassert
- gochecknoglobals
- asciicheck
- goprintffuncname
- nestif
- noctx
5 changes: 4 additions & 1 deletion crowdin/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crowdin

import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -181,7 +182,9 @@ func TestBranchesService_GetByIDNotFound(t *testing.T) {
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Branches.Get expected status 404, got %v", resp.StatusCode)
}
if e, ok := err.(*model.ErrorResponse); !ok {

var e *model.ErrorResponse
if !errors.As(err, &e) {
t.Errorf("Branches.Get expected *model.ErrorResponse, got %+v", e)
}
}
Expand Down
4 changes: 1 addition & 3 deletions crowdin/crowdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (c *Client) newRequest(ctx context.Context, method, path string, body any,
return nil, err
}
}
req, err := http.NewRequest(method, u.String(), buf)
req, err := http.NewRequestWithContext(ctx, method, u.String(), buf)
if err != nil {
return nil, err
}
Expand All @@ -151,8 +151,6 @@ func (c *Client) newRequest(ctx context.Context, method, path string, body any,
}
}

req = req.WithContext(ctx)

return req, nil
}

Expand Down
1 change: 0 additions & 1 deletion crowdin/crowdin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ func TestNewEnterpriseClient(t *testing.T) {

func TestWithCustomHTTPClient(t *testing.T) {
c, err := NewClient("token", WithHTTPClient(http.DefaultClient))

if err != nil {
t.Errorf("NewClient error: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions crowdin/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ func TestGroupService_Delete(t *testing.T) {
mux.HandleFunc("/api/v2/groups/2", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testURL(t, r, "/api/v2/groups/2")
w.WriteHeader(http.StatusNoContent)
})

_, err := client.Groups.Delete(context.Background(), 2)
Expand Down
3 changes: 2 additions & 1 deletion crowdin/languages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func TestLanguagesService_GetByLanguageIDNotFound(t *testing.T) {
client, mux, teardown := setupClient()
defer teardown()

mux.HandleFunc("/api/v2/languages/xx", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc("/api/v2/languages/xx", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
})

Expand Down Expand Up @@ -381,6 +381,7 @@ func TestLanguagesService_Delete(t *testing.T) {
mux.HandleFunc("/api/v2/languages/uk", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
testURL(t, r, "/api/v2/languages/uk")
w.WriteHeader(http.StatusNoContent)
})

_, err := client.Languages.Delete(context.Background(), "uk")
Expand Down
8 changes: 4 additions & 4 deletions crowdin/model/branches.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package model

import (
"fmt"
"errors"
"net/url"
)

Expand Down Expand Up @@ -80,7 +80,7 @@ func (r *BranchesAddRequest) Validate() error {
return ErrNilRequest
}
if r.Name == "" {
return fmt.Errorf("name is required")
return errors.New("name is required")
}
return nil
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (r *BranchesMergeRequest) Validate() error {
return ErrNilRequest
}
if r.SourceBranchID == 0 {
return fmt.Errorf("sourceBranchId is required")
return errors.New("sourceBranchId is required")
}
return nil
}
Expand All @@ -157,7 +157,7 @@ func (r *BranchesCloneRequest) Validate() error {
return ErrNilRequest
}
if r.Name == "" {
return fmt.Errorf("name is required")
return errors.New("name is required")
}
return nil
}
23 changes: 12 additions & 11 deletions crowdin/model/source_files.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"errors"
"fmt"
"net/url"
)
Expand Down Expand Up @@ -77,8 +78,8 @@ func (o *DirectoryListOptions) Values() (url.Values, bool) {
if o.Filter != "" {
v.Add("filter", o.Filter)
}
if o.Recursion != nil {
v.Add("recursion", o.Recursion.(string))
if recursion, ok := o.Recursion.(string); ok {
v.Add("recursion", recursion)
}

return v, len(v) > 0
Expand Down Expand Up @@ -115,10 +116,10 @@ func (r *DirectoryAddRequest) Validate() error {
return ErrNilRequest
}
if r.Name == "" {
return fmt.Errorf("name is required")
return errors.New("name is required")
}
if r.BranchID != 0 && r.DirectoryID != 0 {
return fmt.Errorf("branchId and directoryId cannot be used in the same request")
return errors.New("branchId and directoryId cannot be used in the same request")
}
return nil
}
Expand Down Expand Up @@ -204,8 +205,8 @@ func (o *FileListOptions) Values() (url.Values, bool) {
if o.Filter != "" {
v.Add("filter", o.Filter)
}
if o.Recursion != nil {
v.Add("recursion", o.Recursion.(string))
if recursion, ok := o.Recursion.(string); ok {
v.Add("recursion", recursion)
}

return v, len(v) > 0
Expand Down Expand Up @@ -261,13 +262,13 @@ func (r *FileAddRequest) Validate() error {
return ErrNilRequest
}
if r.StorageID == 0 {
return fmt.Errorf("storageId is required")
return errors.New("storageId is required")
}
if r.Name == "" {
return fmt.Errorf("name is required")
return errors.New("name is required")
}
if r.BranchID > 0 && r.DirectoryID > 0 {
return fmt.Errorf("branchId and directoryId cannot be used in the same request")
return errors.New("branchId and directoryId cannot be used in the same request")
}
return nil
}
Expand Down Expand Up @@ -490,10 +491,10 @@ func (r *FileUpdateRestoreRequest) Validate() error {
return ErrNilRequest
}
if r.RevisionID == 0 && r.StorageID == 0 {
return fmt.Errorf("one of revisionId or storageId is required")
return errors.New("one of revisionId or storageId is required")
}
if r.RevisionID != 0 && r.StorageID != 0 {
return fmt.Errorf("use only one of revisionId or storageId")
return errors.New("use only one of revisionId or storageId")
}
return nil
}
Expand Down
27 changes: 14 additions & 13 deletions crowdin/model/string_translations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"errors"
"fmt"
"net/url"
"strings"
Expand Down Expand Up @@ -139,16 +140,16 @@ type TranslationAlignmentRequest struct {
// It implements the crowdin.RequestValidator interface.
func (r *TranslationAlignmentRequest) Validate() error {
if r == nil {
return fmt.Errorf("request cannot be nil")
return errors.New("request cannot be nil")
}
if r.SourceLanguageID == "" {
return fmt.Errorf("source language ID is required")
return errors.New("source language ID is required")
}
if r.TargetLanguageID == "" {
return fmt.Errorf("target language ID is required")
return errors.New("target language ID is required")
}
if r.Text == "" {
return fmt.Errorf("text is required")
return errors.New("text is required")
}
return nil
}
Expand Down Expand Up @@ -369,16 +370,16 @@ type TranslationAddRequest struct {
// It implements the crowdin.RequestValidator interface.
func (r *TranslationAddRequest) Validate() error {
if r == nil {
return fmt.Errorf("request cannot be nil")
return errors.New("request cannot be nil")
}
if r.StringID == 0 {
return fmt.Errorf("string ID is required")
return errors.New("string ID is required")
}
if r.LanguageID == "" {
return fmt.Errorf("language ID is required")
return errors.New("language ID is required")
}
if r.Text == "" {
return fmt.Errorf("text is required")
return errors.New("text is required")
}
return nil
}
Expand Down Expand Up @@ -481,21 +482,21 @@ type VoteAddRequest struct {
// It implements the crowdin.RequestValidator interface.
func (r *VoteAddRequest) Validate() error {
if r == nil {
return fmt.Errorf("request cannot be nil")
return errors.New("request cannot be nil")
}
if r.Mark != VoteTypeUp && r.Mark != VoteTypeDown {
return fmt.Errorf("invalid vote type: %q", r.Mark)
}
if r.TranslationID == 0 {
return fmt.Errorf("translation ID is required")
return errors.New("translation ID is required")
}
return nil
}

func joinIntSlice(s []int) string {
var res []string
for _, v := range s {
res = append(res, fmt.Sprintf("%d", v))
res := make([]string, len(s))
for i, v := range s {
res[i] = fmt.Sprintf("%d", v)
}
return strings.Join(res, ",")
}
Loading

0 comments on commit 0ffeebe

Please sign in to comment.