Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): add linters and upgrade versions #960

Merged
merged 8 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.54.2
version: v1.55.2
only-new-issues: true

5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ linters:
- asciicheck
- bidichk
- bodyclose
- decorder
- dogsled
- errcheck
- errorlint
- goconst
- gocyclo
- gofmt
Expand All @@ -38,9 +40,11 @@ linters:
- gosimple
- govet
- ineffassign
- loggercheck
- makezero
- misspell
- nilerr
- noctx
- prealloc
- promlinter
- staticcheck
Expand All @@ -55,6 +59,7 @@ issues:
- path: _test\.go
linters:
- gomnd
- errcheck

run:
timeout: 5m
Expand Down
7 changes: 4 additions & 3 deletions api/controller/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"bytes"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -96,7 +97,7 @@ func UpdateContact(dataBase moira.Database, contactDTO dto.Contact, contactData

// RemoveContact deletes notification contact for current user and remove contactID from all subscriptions
func RemoveContact(database moira.Database, contactID string, userLogin string, teamID string) *api.ErrorResponse { //nolint:gocyclo
subscriptionIDs := []string{}
subscriptionIDs := make([]string, 0)
Tetrergeru marked this conversation as resolved.
Show resolved Hide resolved
if userLogin != "" {
userSubscriptionIDs, err := database.GetUserSubscriptionIDs(userLogin)
if err != nil {
Expand Down Expand Up @@ -179,7 +180,7 @@ func SendTestContactNotification(dataBase moira.Database, contactID string) *api
func CheckUserPermissionsForContact(dataBase moira.Database, contactID string, userLogin string) (moira.ContactData, *api.ErrorResponse) {
contactData, err := dataBase.GetContact(contactID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return moira.ContactData{}, api.ErrorNotFound(fmt.Sprintf("contact with ID '%s' does not exists", contactID))
}
return moira.ContactData{}, api.ErrorInternalServer(err)
Expand All @@ -201,7 +202,7 @@ func CheckUserPermissionsForContact(dataBase moira.Database, contactID string, u

func isContactExists(dataBase moira.Database, contactID string) (bool, error) {
_, err := dataBase.GetContact(contactID)
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return false, nil
}
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions api/controller/subscription.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -107,7 +108,7 @@ func SendTestNotification(database moira.Database, subscriptionID string) *api.E
func CheckUserPermissionsForSubscription(dataBase moira.Database, subscriptionID string, userLogin string) (moira.SubscriptionData, *api.ErrorResponse) {
subscription, err := dataBase.GetSubscription(subscriptionID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return moira.SubscriptionData{}, api.ErrorNotFound(fmt.Sprintf("subscription with ID '%s' does not exists", subscriptionID))
}
return moira.SubscriptionData{}, api.ErrorInternalServer(err)
Expand All @@ -129,7 +130,7 @@ func CheckUserPermissionsForSubscription(dataBase moira.Database, subscriptionID

func isSubscriptionExists(dataBase moira.Database, subscriptionID string) (bool, error) {
_, err := dataBase.GetSubscription(subscriptionID)
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return false, nil
}
if err != nil {
Expand Down
29 changes: 15 additions & 14 deletions api/controller/team.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"errors"
"fmt"
"strings"

Expand All @@ -24,7 +25,7 @@ func CreateTeam(dataBase moira.Database, team dto.TeamModel, userID string) (dto
if err == nil {
return dto.SaveTeamResponse{}, api.ErrorInvalidRequest(fmt.Errorf("team with ID you specified already exists %s", teamID))
}
if err != nil && err != database.ErrNil {
if err != nil && !errors.Is(err, database.ErrNil) {
return dto.SaveTeamResponse{}, api.ErrorInternalServer(fmt.Errorf("cannot check id for team: %w", err))
}
} else { // on the other hand try to create an UUID for teamID
Expand All @@ -36,7 +37,7 @@ func CreateTeam(dataBase moira.Database, team dto.TeamModel, userID string) (dto
}
teamID = generatedUUID.String()
_, err = dataBase.GetTeam(teamID)
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
createdSuccessfully = true
break
}
Expand Down Expand Up @@ -71,7 +72,7 @@ func GetTeam(dataBase moira.Database, teamID string) (dto.TeamModel, *api.ErrorR
team, err := dataBase.GetTeam(teamID)

if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamModel{}, api.ErrorNotFound(fmt.Sprintf("cannot find team: %s", teamID))
}
return dto.TeamModel{}, api.ErrorInternalServer(fmt.Errorf("cannot get team from database: %w", err))
Expand All @@ -86,7 +87,7 @@ func GetUserTeams(dataBase moira.Database, userID string) (dto.UserTeams, *api.E
teams, err := dataBase.GetUserTeams(userID)

result := []dto.TeamModel{}
if err != nil && err != database.ErrNil {
if err != nil && !errors.Is(err, database.ErrNil) {
return dto.UserTeams{}, api.ErrorInternalServer(fmt.Errorf("cannot get user teams from database: %w", err))
}

Expand All @@ -107,7 +108,7 @@ func GetTeamUsers(dataBase moira.Database, teamID string) (dto.TeamMembers, *api
users, err := dataBase.GetTeamUsers(teamID)

if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamMembers{}, api.ErrorNotFound(fmt.Sprintf("cannot find team users: %s", teamID))
}
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get team users from database: %w", err))
Expand Down Expand Up @@ -151,8 +152,8 @@ func addTeamsForNewUsers(dataBase moira.Database, teamID string, newUsers map[st
if _, ok := teamsMap[userID]; ok {
continue
}
fetchedUserTeams, err := dataBase.GetUserTeams(userID) //nolint:govet
if err != nil && err != database.ErrNil {
fetchedUserTeams, err := dataBase.GetUserTeams(userID)
if err != nil && !errors.Is(err, database.ErrNil) {
return nil, api.ErrorInternalServer(fmt.Errorf("cannot get team users from database: %w", err))
}
fetchedUserTeams = append(fetchedUserTeams, teamID)
Expand All @@ -165,7 +166,7 @@ func addTeamsForNewUsers(dataBase moira.Database, teamID string, newUsers map[st
func SetTeamUsers(dataBase moira.Database, teamID string, allUsers []string) (dto.TeamMembers, *api.ErrorResponse) {
existingUsers, err := dataBase.GetTeamUsers(teamID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamMembers{}, api.ErrorNotFound(fmt.Sprintf("cannot find team users: %s", teamID))
}
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get team users from database: %w", err))
Expand Down Expand Up @@ -241,7 +242,7 @@ func addUserTeam(teamID string, teams []string) ([]string, error) {
func AddTeamUsers(dataBase moira.Database, teamID string, newUsers []string) (dto.TeamMembers, *api.ErrorResponse) {
existingUsers, err := dataBase.GetTeamUsers(teamID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamMembers{}, api.ErrorNotFound(fmt.Sprintf("cannot find team users: %s", teamID))
}
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get team users from database: %w", err))
Expand All @@ -253,7 +254,7 @@ func AddTeamUsers(dataBase moira.Database, teamID string, newUsers []string) (dt
for _, userID := range existingUsers {
userTeams, err := dataBase.GetUserTeams(userID) //nolint:govet
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamMembers{}, api.ErrorNotFound(fmt.Sprintf("cannot find user teams: %s", userID))
}
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get user teams from database: %w", err))
Expand All @@ -268,7 +269,7 @@ func AddTeamUsers(dataBase moira.Database, teamID string, newUsers []string) (dt
}

userTeams, err := dataBase.GetUserTeams(userID) //nolint:govet
if err != nil && err != redis.Nil {
if err != nil && !errors.Is(err, redis.Nil) {
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get user teams from database: %w", err))
}

Expand Down Expand Up @@ -335,7 +336,7 @@ func DeleteTeam(dataBase moira.Database, teamID, userLogin string) (dto.SaveTeam
func DeleteTeamUser(dataBase moira.Database, teamID string, removeUserID string) (dto.TeamMembers, *api.ErrorResponse) {
existingUsers, err := dataBase.GetTeamUsers(teamID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamMembers{}, api.ErrorNotFound(fmt.Sprintf("cannot find team users: %s", teamID))
}
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get team users from database: %w", err))
Expand All @@ -361,7 +362,7 @@ func DeleteTeamUser(dataBase moira.Database, teamID string, removeUserID string)
for _, userID := range existingUsers {
userTeams, err := dataBase.GetUserTeams(userID) //nolint:govet
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return dto.TeamMembers{}, api.ErrorNotFound(fmt.Sprintf("cannot find user teams: %s", userID))
}
return dto.TeamMembers{}, api.ErrorInternalServer(fmt.Errorf("cannot get user teams from database: %w", err))
Expand Down Expand Up @@ -402,7 +403,7 @@ func removeUserTeam(teams []string, teamID string) ([]string, error) {
func CheckUserPermissionsForTeam(dataBase moira.Database, teamID, userID string) *api.ErrorResponse {
_, err := dataBase.GetTeam(teamID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return api.ErrorNotFound(fmt.Sprintf("team with ID '%s' does not exists", teamID))
}
return api.ErrorInternalServer(err)
Expand Down
11 changes: 6 additions & 5 deletions api/controller/trigger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"errors"
"fmt"
"time"

Expand All @@ -17,7 +18,7 @@ const maxTriggerLockAttempts = 10
func UpdateTrigger(dataBase moira.Database, trigger *dto.TriggerModel, triggerID string, timeSeriesNames map[string]bool) (*dto.SaveTriggerResponse, *api.ErrorResponse) {
_, err := dataBase.GetTrigger(triggerID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return nil, api.ErrorNotFound(fmt.Sprintf("trigger with ID = '%s' does not exists", triggerID))
}
return nil, api.ErrorInternalServer(err)
Expand All @@ -32,11 +33,11 @@ func saveTrigger(dataBase moira.Database, trigger *moira.Trigger, triggerID stri
}
defer dataBase.DeleteTriggerCheckLock(triggerID) //nolint
lastCheck, err := dataBase.GetTriggerLastCheck(triggerID)
if err != nil && err != database.ErrNil {
if err != nil && !errors.Is(err, database.ErrNil) {
return nil, api.ErrorInternalServer(err)
}

if err != database.ErrNil {
if !errors.Is(err, database.ErrNil) {
for metric := range lastCheck.Metrics {
if _, ok := timeSeriesNames[metric]; !ok {
lastCheck.RemoveMetricState(metric)
Expand Down Expand Up @@ -74,7 +75,7 @@ func saveTrigger(dataBase moira.Database, trigger *moira.Trigger, triggerID stri
func GetTrigger(dataBase moira.Database, triggerID string) (*dto.Trigger, *api.ErrorResponse) {
trigger, err := dataBase.GetTrigger(triggerID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return nil, api.ErrorNotFound("trigger not found")
}
return nil, api.ErrorInternalServer(err)
Expand Down Expand Up @@ -119,7 +120,7 @@ func GetTriggerLastCheck(dataBase moira.Database, triggerID string) (*dto.Trigge

*lastCheck, err = dataBase.GetTriggerLastCheck(triggerID)
if err != nil {
if err != database.ErrNil {
if !errors.Is(err, database.ErrNil) {
return nil, api.ErrorInternalServer(err)
}
lastCheck = nil
Expand Down
7 changes: 4 additions & 3 deletions api/controller/trigger_metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"errors"
"fmt"

"github.com/moira-alert/moira"
Expand Down Expand Up @@ -51,7 +52,7 @@ func DeleteTriggerNodataMetrics(dataBase moira.Database, triggerID string) *api.
func GetTriggerMetrics(dataBase moira.Database, metricSourceProvider *metricSource.SourceProvider, from, to int64, triggerID string) (*dto.TriggerMetrics, *api.ErrorResponse) {
tts, _, err := GetTriggerEvaluationResult(dataBase, metricSourceProvider, from, to, triggerID, false)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return nil, api.ErrorInvalidRequest(fmt.Errorf("trigger not found"))
}
return nil, api.ErrorInternalServer(err)
Expand Down Expand Up @@ -79,7 +80,7 @@ func GetTriggerMetrics(dataBase moira.Database, metricSourceProvider *metricSour
func deleteTriggerMetrics(dataBase moira.Database, metricName string, triggerID string, removeAllNodataMetrics bool) *api.ErrorResponse {
trigger, err := dataBase.GetTrigger(triggerID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return api.ErrorInvalidRequest(fmt.Errorf("trigger not found"))
}
return api.ErrorInternalServer(err)
Expand All @@ -92,7 +93,7 @@ func deleteTriggerMetrics(dataBase moira.Database, metricName string, triggerID

lastCheck, err := dataBase.GetTriggerLastCheck(triggerID)
if err != nil {
if err == database.ErrNil {
if errors.Is(err, database.ErrNil) {
return api.ErrorInvalidRequest(fmt.Errorf("trigger check not found"))
}
return api.ErrorInternalServer(err)
Expand Down
3 changes: 2 additions & 1 deletion api/controller/triggers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controller

import (
"errors"
"fmt"
"math"
"regexp"
Expand Down Expand Up @@ -204,7 +205,7 @@ func getTriggerChecks(database moira.Database, triggerIDs []string) ([]moira.Tri

func triggerExists(database moira.Database, triggerID string) (bool, error) {
_, err := database.GetTrigger(triggerID)
if err == db.ErrNil {
if errors.Is(err, db.ErrNil) {
return false, nil
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/handler/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func subscriptionFilter(next http.Handler) http.Handler {
func updateSubscription(writer http.ResponseWriter, request *http.Request) {
subscription := &dto.Subscription{}
if err := render.Bind(request, subscription); err != nil {
switch err.(type) {
switch err.(type) { // nolint:errorlint
case dto.ErrProvidedContactsForbidden:
render.Render(writer, request, api.ErrorForbidden(err.Error())) //nolint
default:
Expand Down
4 changes: 2 additions & 2 deletions api/handler/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func createTrigger(writer http.ResponseWriter, request *http.Request) {
func getTriggerFromRequest(request *http.Request) (*dto.Trigger, *api.ErrorResponse) {
trigger := &dto.Trigger{}
if err := render.Bind(request, trigger); err != nil {
switch err.(type) {
switch err.(type) { // nolint:errorlint
case local.ErrParseExpr, local.ErrEvalExpr, local.ErrUnknownFunction:
return nil, api.ErrorInvalidRequest(fmt.Errorf("invalid graphite targets: %s", err.Error()))
case expression.ErrInvalidExpression:
Expand Down Expand Up @@ -211,7 +211,7 @@ func triggerCheck(writer http.ResponseWriter, request *http.Request) {
response := dto.TriggerCheckResponse{}

if err := render.Bind(request, trigger); err != nil {
switch err.(type) {
switch err.(type) { // nolint:errorlint
case expression.ErrInvalidExpression, local.ErrParseExpr, local.ErrEvalExpr, local.ErrUnknownFunction:
// TODO write comment, why errors are ignored, it is not obvious.
// In getTriggerFromRequest these types of errors lead to 400.
Expand Down
3 changes: 2 additions & 1 deletion api/handler/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -39,7 +40,7 @@ func TestGetSearchRequestString(t *testing.T) {
{"QueRy", "query"},
}
for _, testCase := range testCases {
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/trigger/search?onlyProblems=false&p=0&size=20&text=%s", testCase.text), nil)
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, fmt.Sprintf("/api/trigger/search?onlyProblems=false&p=0&size=20&text=%s", testCase.text), nil)
searchRequest := getSearchRequestString(req)
So(searchRequest, ShouldEqual, testCase.expectedSearchRequest)
}
Expand Down
6 changes: 3 additions & 3 deletions api/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import (

func TestGetLogin(t *testing.T) {
Convey("Request does not contain login, should get anonymous", t, func() {
req, err := http.NewRequest(http.MethodGet, "https://testurl.com", http.NoBody)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://testurl.com", http.NoBody)
So(err, ShouldBeNil)
So(anonymousUser, ShouldEqual, GetLogin(req))
})

Convey("Request contains login, but empty, should get anonymous", t, func() {
req, err := http.NewRequest(http.MethodGet, "https://testurl.com", http.NoBody)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://testurl.com", http.NoBody)
ctx := context.WithValue(req.Context(), loginKey, "")
req = req.WithContext(ctx)
So(err, ShouldBeNil)
So(anonymousUser, ShouldEqual, GetLogin(req))
})

Convey("Request contains login header, should get that", t, func() {
req, err := http.NewRequest(http.MethodGet, "https://testurl.com", http.NoBody)
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://testurl.com", http.NoBody)
ctx := context.WithValue(req.Context(), loginKey, "awesome_user")
req = req.WithContext(ctx)
So(err, ShouldBeNil)
Expand Down
Loading
Loading