Skip to content

Commit

Permalink
Custom bug fixes and enhancement (#815)
Browse files Browse the repository at this point in the history
* Update .deepsource.toml

* Use `strings.EqualFold` for `string` comparison

* Use `strings.EqualFold` for `string` comparison

* Simplify returning of boolean expression

* Multiple `append` combined into a single call

* Simplify complex boolean expression

* Replace `for` loop with `append`

* Replace `time.Now().Sub(t)` with `time.Since(t)`

* Omit redundant control flow

* Merge variable declaration and assignment

* Omit redundant `nil` check on slices

* Fix check for empty string

* Format code with gofmt

* Update .deepsource.toml

Co-authored-by: DeepSource Bot <[email protected]>
Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 28, 2021
1 parent d9aa7c0 commit 35db62b
Show file tree
Hide file tree
Showing 57 changed files with 356 additions and 339 deletions.
5 changes: 5 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ enabled = true

[analyzers.meta]
import_paths = ["github.com/devtron-labs/devtron"]
import_root = " github.com/devtron-labs/devtron"

[[transformers]]
name = "gofmt"
enabled = true
15 changes: 7 additions & 8 deletions api/restHandler/AppListingRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"

application2 "github.com/argoproj/argo-cd/pkg/apiclient/application"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/devtron-labs/devtron/api/bean"
Expand All @@ -35,10 +40,6 @@ import (
"github.com/devtron-labs/devtron/util/rbac"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net/http"
"strconv"
"strings"
"time"
)

type AppListingRestHandler interface {
Expand Down Expand Up @@ -153,9 +154,7 @@ func (handler AppListingRestHandlerImpl) FetchAppsByEnvironment(w http.ResponseW
}
appEnvContainers := make([]*bean.AppEnvironmentContainer, 0)
if isActionUserSuperAdmin {
for _, envContainer := range envContainers {
appEnvContainers = append(appEnvContainers, envContainer)
}
appEnvContainers = append(appEnvContainers, envContainers...)
} else {
uniqueTeams := make(map[int]string)
authorizedTeams := make(map[int]bool)
Expand Down Expand Up @@ -402,7 +401,7 @@ func (handler AppListingRestHandlerImpl) FetchAppTriggerView(w http.ResponseWrit
return
}
response <- AppStatus{name: pipelineName, status: "", message: "", err: fmt.Errorf("Connection Closed by Client"), conditions: make([]v1alpha1.ApplicationCondition, 0)}
return

}(acdAppName)
}
rCount := 0
Expand Down
9 changes: 5 additions & 4 deletions api/restHandler/ClusterRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"

cluster3 "github.com/argoproj/argo-cd/pkg/apiclient/cluster"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
cluster2 "github.com/devtron-labs/devtron/client/argocdServer/cluster"
Expand All @@ -34,9 +38,6 @@ import (
"github.com/gorilla/mux"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
"strings"
)

type ClusterRestHandler interface {
Expand Down Expand Up @@ -514,7 +515,7 @@ func (impl ClusterRestHandlerImpl) FindAllForAutoComplete(w http.ResponseWriter,
}
//RBAC enforcer Ends

if result == nil || len(result) == 0 {
if len(result) == 0 {
result = make([]cluster.ClusterBean, 0)
}
writeJsonResp(w, err, result, http.StatusOK)
Expand Down
2 changes: 1 addition & 1 deletion api/restHandler/DockerRegRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (impl DockerRegRestHandlerImpl) IsDockerRegConfigured(w http.ResponseWriter
writeJsonResp(w, err, nil, http.StatusInternalServerError)
return
}
if res != nil && len(res) > 0 {
if len(res) > 0 {
isConfigured = true
}

Expand Down
13 changes: 7 additions & 6 deletions api/restHandler/NotificationRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,24 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/pkg/cluster"
"github.com/devtron-labs/devtron/pkg/notifier"
"github.com/devtron-labs/devtron/pkg/pipeline"
"github.com/devtron-labs/devtron/pkg/team"
"github.com/devtron-labs/devtron/pkg/user"
"github.com/devtron-labs/devtron/util/event"
util "github.com/devtron-labs/devtron/util/event"
"github.com/devtron-labs/devtron/util/rbac"
"github.com/devtron-labs/devtron/util/response"
"github.com/go-pg/pg"
"github.com/gorilla/mux"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"io/ioutil"
"net/http"
"strconv"
"strings"
)

type NotificationRestHandler interface {
Expand Down Expand Up @@ -532,7 +533,7 @@ func (impl NotificationRestHandlerImpl) FindAllNotificationConfig(w http.Respons

//RBAC
pass := true
if slackConfigs != nil && len(slackConfigs) > 0 {
if len(slackConfigs) > 0 {
var teamIds []*int
for _, item := range slackConfigs {
teamIds = append(teamIds, &item.TeamId)
Expand Down
11 changes: 6 additions & 5 deletions api/restHandler/PipelineConfigRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"

"github.com/argoproj/argo/pkg/apis/workflow/v1alpha1"
bean2 "github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/client/argocdServer/application"
Expand All @@ -48,10 +53,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gopkg.in/go-playground/validator.v9"
"io"
"net/http"
"strconv"
"strings"
)

type PipelineConfigRestHandler interface {
Expand Down Expand Up @@ -1205,7 +1206,7 @@ func (handler PipelineConfigRestHandlerImpl) GetArtifactsByCDPipeline(w http.Res
if err != nil {
handler.Logger.Errorw("service err, GetArtifactsByCDPipeline", "err", err, "cdPipelineId", cdPipelineId, "stage", stage)
}
if blockCveList != nil && len(blockCveList) > 0 {
if len(blockCveList) > 0 {
vulnerableMap[digest] = true
}
}
Expand Down
6 changes: 3 additions & 3 deletions api/restHandler/UserAuthHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ package restHandler
import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/client/pubsub"
"github.com/devtron-labs/devtron/internal/casbin"
Expand All @@ -30,8 +33,6 @@ import (
"github.com/nats-io/stan.go"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strings"
)

type UserAuthHandler interface {
Expand Down Expand Up @@ -252,7 +253,6 @@ func (handler UserAuthHandlerImpl) AddDefaultPolicyAndRoles(w http.ResponseWrite
return
}

return
}

func (handler UserAuthHandlerImpl) AuthVerification(w http.ResponseWriter, r *http.Request) {
Expand Down
11 changes: 6 additions & 5 deletions api/restHandler/UserRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ package restHandler
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"

"github.com/devtron-labs/devtron/api/bean"
"github.com/devtron-labs/devtron/client/pubsub"
"github.com/devtron-labs/devtron/internal/util"
Expand All @@ -30,9 +34,6 @@ import (
"github.com/gorilla/mux"
"go.uber.org/zap"
"gopkg.in/go-playground/validator.v9"
"net/http"
"strconv"
"strings"
)

type UserRestHandler interface {
Expand Down Expand Up @@ -117,7 +118,7 @@ func (handler UserRestHandlerImpl) CreateUser(w http.ResponseWriter, r *http.Req
return
}

if groupRoles != nil && len(groupRoles) > 0 {
if len(groupRoles) > 0 {
for _, groupRole := range groupRoles {
if len(groupRole.Team) > 0 {
if ok := handler.enforcer.Enforce(token, rbac.ResourceUser, rbac.ActionCreate, strings.ToLower(groupRole.Team)); !ok {
Expand Down Expand Up @@ -202,7 +203,7 @@ func (handler UserRestHandlerImpl) UpdateUser(w http.ResponseWriter, r *http.Req
return
}

if groupRoles != nil && len(groupRoles) > 0 {
if len(groupRoles) > 0 {
for _, groupRole := range groupRoles {
if len(groupRole.Team) > 0 {
if ok := handler.enforcer.Enforce(token, rbac.ResourceUser, rbac.ActionUpdate, strings.ToLower(groupRole.Team)); !ok {
Expand Down
6 changes: 3 additions & 3 deletions client/argocdServer/Connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ package argocdServer

import (
"fmt"
"log"

"github.com/argoproj/argo-cd/util/settings"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
"log"
)

func init() {
Expand All @@ -40,8 +41,7 @@ func GetConnection(token string, settings *settings.ArgoCDSettings) *grpc.Client
if len(token) > 0 {
option = append(option, grpc.WithPerRPCCredentials(TokenAuth{token: token}))
}
option = append(option, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor))
option = append(option, grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor))
option = append(option, grpc.WithUnaryInterceptor(grpc_prometheus.UnaryClientInterceptor), grpc.WithStreamInterceptor(grpc_prometheus.StreamClientInterceptor))

//if conf.Environment=="DEV"{
// option=append(option,grpc.WithInsecure())
Expand Down
7 changes: 4 additions & 3 deletions client/argocdServer/application/Application.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/argoproj/argo-cd/pkg/apiclient/application"
"github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/reposerver/apiclient"
Expand All @@ -32,7 +34,6 @@ import (
"google.golang.org/grpc"
v12 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"time"
)

const (
Expand Down Expand Up @@ -585,9 +586,9 @@ func parseResult(resp *v1alpha1.ApplicationTree, query *application.ResourcesQue
startTime := time.Now()
res, err := asc.GetResource(ctx, &request)
if err != nil {
c.logger.Errorw("GRPC_GET_RESOURCE", "data", request, "timeTaken", time.Now().Sub(startTime), "err", err)
c.logger.Errorw("GRPC_GET_RESOURCE", "data", request, "timeTaken", time.Since(startTime), "err", err)
} else {
c.logger.Debugw("GRPC_GET_RESOURCE", "data", request, "timeTaken", time.Now().Sub(startTime))
c.logger.Debugw("GRPC_GET_RESOURCE", "data", request, "timeTaken", time.Since(startTime))
}
if res != nil || err != nil {
response <- Result{Response: res, Error: err, Request: &request}
Expand Down
19 changes: 10 additions & 9 deletions client/grafana/GrafanaClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/caarlos0/env"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/attributes"
"go.uber.org/zap"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/caarlos0/env"
"github.com/devtron-labs/devtron/internal/util"
"github.com/devtron-labs/devtron/pkg/attributes"
"go.uber.org/zap"
)

type GrafanaClientConfig struct {
Expand Down Expand Up @@ -163,7 +164,7 @@ func NewGrafanaClientImpl(logger *zap.SugaredLogger, client *http.Client, config
}

func (impl *GrafanaClientImpl) GetAllDatasource() ([]*GetPrometheusDatasourceResponse, error) {
if len(impl.config.DestinationURL) == 0 {
if impl.config.DestinationURL == "" {
hostUrl, err := impl.attributesService.GetByKey(attributes.HostUrlKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -208,7 +209,7 @@ func (impl *GrafanaClientImpl) GetAllDatasource() ([]*GetPrometheusDatasourceRes
}

func (impl *GrafanaClientImpl) GetDatasource(datasourceId int) (*GetPrometheusDatasourceResponse, error) {
if len(impl.config.DestinationURL) == 0 {
if impl.config.DestinationURL == "" {
hostUrl, err := impl.attributesService.GetByKey(attributes.HostUrlKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -252,7 +253,7 @@ func (impl *GrafanaClientImpl) GetDatasource(datasourceId int) (*GetPrometheusDa
}

func (impl *GrafanaClientImpl) UpdateDatasource(updateDatasourceRequest UpdateDatasourceRequest, datasourceId int) (*DatasourceResponse, error) {
if len(impl.config.DestinationURL) == 0 {
if impl.config.DestinationURL == "" {
hostUrl, err := impl.attributesService.GetByKey(attributes.HostUrlKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -305,7 +306,7 @@ func (impl *GrafanaClientImpl) UpdateDatasource(updateDatasourceRequest UpdateDa
}

func (impl *GrafanaClientImpl) deleteDatasource(updateDatasourceRequest CreateDatasourceRequest, datasourceId int) (*DatasourceResponse, error) {
if len(impl.config.DestinationURL) == 0 {
if impl.config.DestinationURL == "" {
hostUrl, err := impl.attributesService.GetByKey(attributes.HostUrlKey)
if err != nil {
return nil, err
Expand Down Expand Up @@ -357,7 +358,7 @@ func (impl *GrafanaClientImpl) deleteDatasource(updateDatasourceRequest CreateDa
}

func (impl *GrafanaClientImpl) CreateDatasource(createDatasourceRequest CreateDatasourceRequest) (*DatasourceResponse, error) {
if len(impl.config.DestinationURL) == 0 {
if impl.config.DestinationURL == "" {
hostUrl, err := impl.attributesService.GetByKey(attributes.HostUrlKey)
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions client/telemetry/PosthogClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package telemetry

import (
"encoding/base64"
"time"

"github.com/devtron-labs/devtron/util"
"github.com/patrickmn/go-cache"
"github.com/posthog/posthog-go"
"go.uber.org/zap"
"time"
)

type PosthogClient struct {
Expand All @@ -47,7 +48,7 @@ const (
)

func NewPosthogClient(logger *zap.SugaredLogger) (*PosthogClient, error) {
if len(PosthogApiKey) == 0 {
if PosthogApiKey == "" {
encodedApiKey, apiKey, err := getPosthogApiKey(TelemetryApiKeyEndpoint, logger)
if err != nil {
logger.Errorw("exception caught while getting api key", "err", err)
Expand Down
Loading

0 comments on commit 35db62b

Please sign in to comment.