From 9d8e9158de07492241fd50c9d6d74bf4eeb45bf7 Mon Sep 17 00:00:00 2001 From: MinerYang Date: Thu, 26 May 2022 10:32:07 +0800 Subject: [PATCH] fix deadcode lint & update golangci-lint.yaml (#16896) * fix deadcode lint & update golangci-lint.yaml Signed-off-by: yminer mock.go Signed-off-by: yminer commentfmt Signed-off-by: yminer mock.go update Signed-off-by: yminer update makefile Signed-off-by: yminer * update /pkg/allowlist/validator.go Signed-off-by: yminer Co-authored-by: yminer --- Makefile | 2 +- src/.golangci.yaml | 10 +++++----- src/chartserver/reverse_proxy.go | 1 - src/chartserver/utils.go | 5 ----- src/common/http/tls.go | 1 - src/controller/replication/flow/mock.go | 7 ++++--- src/core/api/chart_repository.go | 6 ------ src/core/controllers/base.go | 6 ------ src/pkg/allowlist/validator.go | 7 ------- src/pkg/exporter/js_worker.go | 1 - src/pkg/reg/adapter/aliacr/types.go | 2 +- src/pkg/reg/adapter/artifacthub/consts.go | 13 ------------- src/pkg/reg/adapter/gitlab/client.go | 7 ------- src/pkg/reg/adapter/tencentcr/adapter.go | 2 +- src/pkg/scan/postprocessors/report_converters.go | 4 ---- src/pkg/scan/report/manager.go | 5 ----- src/pkg/systemartifact/dao/dao.go | 4 ++-- 17 files changed, 14 insertions(+), 69 deletions(-) diff --git a/Makefile b/Makefile index cf7d98bdb..86f04d893 100644 --- a/Makefile +++ b/Makefile @@ -537,7 +537,7 @@ GOLANGCI_LINT := $(shell go env GOPATH)/bin/golangci-lint lint: @echo checking lint @echo $(GOLANGCI_LINT) - @$(GOLANGCI_LINT) -v run ./src/... + @cd ./src/; $(GOLANGCI_LINT) -v run ./...; pushimage: @echo "pushing harbor images ..." diff --git a/src/.golangci.yaml b/src/.golangci.yaml index 51113646a..3a380668b 100644 --- a/src/.golangci.yaml +++ b/src/.golangci.yaml @@ -27,12 +27,12 @@ linters: # - nolintlint # - revive # - whitespace - # - bodyclose - # - deadcode + - bodyclose + - deadcode # - errcheck # - gosec # - gosimple - # - govet + - govet # - noctx # - rowserrcheck # - staticcheck @@ -47,8 +47,8 @@ run: skip-files: - ".*_test.go" - ".*test.go" - skip-dir: - - "./testing/" + skip-dirs: + - "testing/" timeout: 5m issue: diff --git a/src/chartserver/reverse_proxy.go b/src/chartserver/reverse_proxy.go index dce121975..020650747 100644 --- a/src/chartserver/reverse_proxy.go +++ b/src/chartserver/reverse_proxy.go @@ -29,7 +29,6 @@ const ( defaultRepo = "library" rootUploadingEndpoint = "/api/chartrepo/charts" - rootIndexEndpoint = "/chartrepo/index.yaml" chartRepoHealthEndpoint = "/api/chartrepo/health" ) diff --git a/src/chartserver/utils.go b/src/chartserver/utils.go index fdaa84793..d74646dc9 100644 --- a/src/chartserver/utils.go +++ b/src/chartserver/utils.go @@ -10,11 +10,6 @@ import ( "strings" ) -const ( - contentTypeHeader = "content-type" - contentTypeJSON = "application/json" -) - // Extract error object '{"error": "****---***"}' from the content if existing // nil error will be returned if it does exist func extractError(content []byte) (text string, err error) { diff --git a/src/common/http/tls.go b/src/common/http/tls.go index 1ac349bcd..063097fac 100644 --- a/src/common/http/tls.go +++ b/src/common/http/tls.go @@ -27,7 +27,6 @@ const ( internalVerifyClientCert = "INTERNAL_VERIFY_CLIENT_CERT" internalTLSKeyPath = "INTERNAL_TLS_KEY_PATH" internalTLSCertPath = "INTERNAL_TLS_CERT_PATH" - internalTrustCAPath = "INTERNAL_TLS_TRUST_CA_PATH" ) // InternalTLSEnabled returns true if internal TLS enabled diff --git a/src/controller/replication/flow/mock.go b/src/controller/replication/flow/mock.go index d7cc75091..90212aed8 100644 --- a/src/controller/replication/flow/mock.go +++ b/src/controller/replication/flow/mock.go @@ -14,11 +14,12 @@ package flow -import ( - "github.com/goharbor/harbor/src/pkg/reg/adapter" -) +import "github.com/goharbor/harbor/src/pkg/reg/adapter" // define a new interface to combine the two interfaces of adapter for mockery to generate the mocks + +// nolint:deadcode +// for make gen_mocks use type registryAdapter interface { adapter.Adapter adapter.ArtifactRegistry diff --git a/src/core/api/chart_repository.go b/src/core/api/chart_repository.go index 55898dabe..1d5431320 100755 --- a/src/core/api/chart_repository.go +++ b/src/core/api/chart_repository.go @@ -39,12 +39,6 @@ const ( rootIndexEndpoint = "/chartrepo/index.yaml" chartRepoHealthEndpoint = "/api/chartrepo/health" - accessLevelPublic = iota - accessLevelRead - accessLevelWrite - accessLevelAll - accessLevelSystem - formFieldNameForChart = "chart" formFiledNameForProv = "prov" headerContentType = "Content-Type" diff --git a/src/core/controllers/base.go b/src/core/controllers/base.go index 6c42d09cd..8c8c9da69 100644 --- a/src/core/controllers/base.go +++ b/src/core/controllers/base.go @@ -48,12 +48,6 @@ func (cc *CommonController) Render() error { // Prepare overwrites the Prepare func in api.BaseController to ignore unnecessary steps func (cc *CommonController) Prepare() {} -type messageDetail struct { - Hint string - URL string - UUID string -} - func redirectForOIDC(ctx context.Context, username string) bool { if lib.GetAuthMode(ctx) != common.OIDCAuth { return false diff --git a/src/pkg/allowlist/validator.go b/src/pkg/allowlist/validator.go index fc9f60eaa..e71712423 100644 --- a/src/pkg/allowlist/validator.go +++ b/src/pkg/allowlist/validator.go @@ -41,17 +41,10 @@ func IsInvalidErr(err error) bool { return ok } -const cveIDPattern = `^CVE-\d{4}-\d+$` - // Validate help validates the CVE allowlist, to ensure the CVE ID is valid and there's no duplication func Validate(wl models2.CVEAllowlist) error { m := map[string]struct{}{} - // re := regexp.MustCompile(cveIDPattern) for _, it := range wl.Items { - // Bypass the cve format checking - // if !re.MatchString(it.CVEID) { - // return &invalidErr{fmt.Sprintf("invalid CVE ID: %s", it.CVEID)} - // } if _, ok := m[it.CVEID]; ok { return &invalidErr{fmt.Sprintf("duplicate CVE ID in allowlist: %s", it.CVEID)} } diff --git a/src/pkg/exporter/js_worker.go b/src/pkg/exporter/js_worker.go index 43df9ca36..75b6debaa 100644 --- a/src/pkg/exporter/js_worker.go +++ b/src/pkg/exporter/js_worker.go @@ -13,7 +13,6 @@ const ( dialConnectionTimeout = 30 * time.Second dialReadTimeout = 10 * time.Second dialWriteTimeout = 10 * time.Second - pageItemNum = 20.0 ) var ( diff --git a/src/pkg/reg/adapter/aliacr/types.go b/src/pkg/reg/adapter/aliacr/types.go index a8869be49..0954d5a0b 100644 --- a/src/pkg/reg/adapter/aliacr/types.go +++ b/src/pkg/reg/adapter/aliacr/types.go @@ -3,7 +3,7 @@ package aliacr import "time" const ( - defaultTemporaryTokenExpiredTime = time.Hour * 1 + defaultTemporaryTokenExpiredTime = time.Hour * 1 // nolint:deadcode registryEndpointTpl = "https://registry.%s.aliyuncs.com" endpointTpl = "cr.%s.aliyuncs.com" ) diff --git a/src/pkg/reg/adapter/artifacthub/consts.go b/src/pkg/reg/adapter/artifacthub/consts.go index 3f18bfaa5..cc761a68c 100644 --- a/src/pkg/reg/adapter/artifacthub/consts.go +++ b/src/pkg/reg/adapter/artifacthub/consts.go @@ -24,19 +24,6 @@ const ( // ErrHTTPNotFound defines the return error when receiving 404 response code var ErrHTTPNotFound = errors.New("not found") -func searchPackages(kind, offset, limit int, queryString string) string { - if len(queryString) == 0 { - return fmt.Sprintf("/api/v1/packages/search?kind=%d&limit=%d&offset=%d", - kind, limit, offset) - } - return fmt.Sprintf("/api/v1/packages/search?kind=%d&limit=%d&offset=%d&ts_query_web=%s", - kind, limit, offset, queryString) -} - -func getHelmPackageDetail(fullName string) string { - return fmt.Sprintf("/api/v1/packages/helm/%s", fullName) -} - func getHelmVersion(fullName, version string) string { return fmt.Sprintf("/api/v1/packages/helm/%s/%s", fullName, version) } diff --git a/src/pkg/reg/adapter/gitlab/client.go b/src/pkg/reg/adapter/gitlab/client.go index dee1dd11a..c5e2bdce8 100644 --- a/src/pkg/reg/adapter/gitlab/client.go +++ b/src/pkg/reg/adapter/gitlab/client.go @@ -17,10 +17,6 @@ import ( common_http "github.com/goharbor/harbor/src/common/http" ) -const ( - scheme = "bearer" -) - // Client is a client to interact with GitLab type Client struct { client *common_http.Client @@ -55,9 +51,6 @@ func NewClient(registry *model.Registry) (*Client, error) { return client, nil } -func buildPingURL(endpoint string) string { - return fmt.Sprintf("%s/v2/", endpoint) -} func (c *Client) newRequest(method, url string, body io.Reader) (*http.Request, error) { req, err := http.NewRequest(method, url, body) if err != nil { diff --git a/src/pkg/reg/adapter/tencentcr/adapter.go b/src/pkg/reg/adapter/tencentcr/adapter.go index cae4832ae..e101ef7d5 100644 --- a/src/pkg/reg/adapter/tencentcr/adapter.go +++ b/src/pkg/reg/adapter/tencentcr/adapter.go @@ -25,7 +25,7 @@ import ( var ( errInvalidTcrEndpoint error = errors.New("[tencent-tcr.newAdapter] Invalid TCR instance endpoint") - errPingTcrEndpointFailed error = errors.New("[tencent-tcr.newAdapter] Ping TCR instance endpoint failed") + errPingTcrEndpointFailed error = errors.New("[tencent-tcr.newAdapter] Ping TCR instance endpoint failed") // nolint:deadcode ) func init() { diff --git a/src/pkg/scan/postprocessors/report_converters.go b/src/pkg/scan/postprocessors/report_converters.go index 777022c0d..c86ab6ff9 100644 --- a/src/pkg/scan/postprocessors/report_converters.go +++ b/src/pkg/scan/postprocessors/report_converters.go @@ -299,7 +299,3 @@ func toVulnerabilityItem(record *scan.VulnerabilityRecord, artifactDigest string return item } - -func convertingKey(reportUUID string) string { - return fmt.Sprintf("converting:%s", reportUUID) -} diff --git a/src/pkg/scan/report/manager.go b/src/pkg/scan/report/manager.go index 5baf0b74e..8fd6bc597 100644 --- a/src/pkg/scan/report/manager.go +++ b/src/pkg/scan/report/manager.go @@ -16,7 +16,6 @@ package report import ( "context" - "time" "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/q" @@ -103,10 +102,6 @@ type Manager interface { List(ctx context.Context, query *q.Query) ([]*scan.Report, error) } -const ( - reportTimeout = 1 * time.Hour -) - // basicManager is a default implementation of report manager. type basicManager struct { dao scan.DAO diff --git a/src/pkg/systemartifact/dao/dao.go b/src/pkg/systemartifact/dao/dao.go index f4b362b5a..d2be4229f 100644 --- a/src/pkg/systemartifact/dao/dao.go +++ b/src/pkg/systemartifact/dao/dao.go @@ -2,14 +2,14 @@ package dao import ( "context" + "github.com/goharbor/harbor/src/lib/orm" "github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/pkg/systemartifact/model" ) const ( - sizeQuery = "select sum(size) as total_size from system_artifact" - totalSizeColumn = "total_size" + sizeQuery = "select sum(size) as total_size from system_artifact" ) // DAO defines an data access interface for manging the CRUD and read of system