From 635d07ae04a8f683d79cc070ec4f388cc3e5f140 Mon Sep 17 00:00:00 2001
From: Ramkumar Chinchani <45800463+rchincha@users.noreply.github.com>
Date: Thu, 27 Apr 2023 00:09:46 -0700
Subject: [PATCH] chore: update golang (to 1.20.x) and golangci-linter (#1388)

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
---
 .github/workflows/benchmark.yaml                | 2 +-
 .github/workflows/branch-cov.yml                | 2 +-
 .github/workflows/ci-cd.yml                     | 2 +-
 .github/workflows/cloc.yml                      | 2 +-
 .github/workflows/cluster.yaml                  | 6 +++---
 .github/workflows/codeql-analysis.yml           | 2 +-
 .github/workflows/ecosystem-tools.yaml          | 2 +-
 .github/workflows/golangci-lint.yaml            | 4 ++--
 .github/workflows/gqlgen.yaml                   | 2 +-
 .github/workflows/license.yaml                  | 2 +-
 .github/workflows/nightly.yaml                  | 2 +-
 .github/workflows/oci-conformance-action.yml    | 4 ++--
 .github/workflows/sync-3rdparty-images.yaml     | 2 --
 .github/workflows/tls.yaml                      | 2 +-
 .github/workflows/web-scan.yml                  | 2 +-
 Makefile                                        | 2 +-
 cmd/zb/helper.go                                | 5 +++--
 go.mod                                          | 2 +-
 golangcilint.yaml                               | 2 +-
 pkg/debug/swagger/swagger.go                    | 2 +-
 pkg/debug/swagger/swagger_disabled.go           | 5 ++---
 pkg/extensions/extension_userprefs_test.go      | 6 +++---
 pkg/extensions/search/cve/cve_test.go           | 3 ++-
 pkg/extensions/search/resolver.go               | 4 +++-
 pkg/meta/repodb/repodb.go                       | 2 +-
 pkg/meta/repodb/repodb_test.go                  | 4 ++--
 pkg/meta/repodb/repodbfactory/repodb_factory.go | 2 +-
 pkg/storage/common.go                           | 2 --
 pkg/storage/s3/s3.go                            | 2 +-
 pkg/test/common_test.go                         | 2 +-
 30 files changed, 41 insertions(+), 42 deletions(-)

diff --git a/.github/workflows/benchmark.yaml b/.github/workflows/benchmark.yaml
index 4ff4321e..6fd3751f 100644
--- a/.github/workflows/benchmark.yaml
+++ b/.github/workflows/benchmark.yaml
@@ -18,7 +18,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       # Run benchmark with `go test -bench` and stores the output to a file
       - name: Run benchmark
         run: make BENCH_OUTPUT=ci-cd run-bench
diff --git a/.github/workflows/branch-cov.yml b/.github/workflows/branch-cov.yml
index d3cb225e..e29f58c7 100644
--- a/.github/workflows/branch-cov.yml
+++ b/.github/workflows/branch-cov.yml
@@ -22,7 +22,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install dependencies
         run: |
           cd $GITHUB_WORKSPACE
diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml
index fb4a2632..c8d0e58d 100644
--- a/.github/workflows/ci-cd.yml
+++ b/.github/workflows/ci-cd.yml
@@ -39,7 +39,7 @@ jobs:
         uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Check out source code
         uses: actions/checkout@v3
 
diff --git a/.github/workflows/cloc.yml b/.github/workflows/cloc.yml
index 57ef8ae9..925121ce 100644
--- a/.github/workflows/cloc.yml
+++ b/.github/workflows/cloc.yml
@@ -21,7 +21,7 @@ jobs:
         uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Check out source code
         uses: actions/checkout@v3
       - name: Install dependencies
diff --git a/.github/workflows/cluster.yaml b/.github/workflows/cluster.yaml
index c4ae777e..2eb31893 100644
--- a/.github/workflows/cluster.yaml
+++ b/.github/workflows/cluster.yaml
@@ -31,7 +31,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install dependencies
         run: |
           cd $GITHUB_WORKSPACE
@@ -105,8 +105,8 @@ jobs:
             ./bin/zot-linux-amd64 serve test/cluster/config-minio3.json &
             sleep 10
             # run tests
-            skopeo --debug copy --format=oci --dest-tls-verify=false docker://ghcr.io/project-zot/golang:1.19 docker://localhost:8080/golang:1.19
-            skopeo --debug copy --src-tls-verify=false docker://localhost:8080/golang:1.19 oci:golang:1.19
+            skopeo --debug copy --format=oci --dest-tls-verify=false docker://ghcr.io/project-zot/golang:1.20 docker://localhost:8080/golang:1.20
+            skopeo --debug copy --src-tls-verify=false docker://localhost:8080/golang:1.20 oci:golang:1.20
             echo "{\"name\":\"foo\",\"value\":\"bar\"}" > config.json
             echo "hello world" > artifact.txt
             oras push --plain-http localhost:8080/hello-artifact:v2 \
diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml
index 47e0fb37..5b1d47c2 100644
--- a/.github/workflows/codeql-analysis.yml
+++ b/.github/workflows/codeql-analysis.yml
@@ -49,7 +49,7 @@ jobs:
       uses: actions/setup-go@v4
       with:
         cache: false
-        go-version: 1.19.x
+        go-version: 1.20.x
 
     - name: Install dependencies
       run: |
diff --git a/.github/workflows/ecosystem-tools.yaml b/.github/workflows/ecosystem-tools.yaml
index fe826148..892669c7 100644
--- a/.github/workflows/ecosystem-tools.yaml
+++ b/.github/workflows/ecosystem-tools.yaml
@@ -20,7 +20,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install dependencies
         run: |
           cd $GITHUB_WORKSPACE
diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml
index 25389287..2c9dc620 100644
--- a/.github/workflows/golangci-lint.yaml
+++ b/.github/workflows/golangci-lint.yaml
@@ -19,13 +19,13 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: '1.19'
+          go-version: '1.20'
       - uses: actions/checkout@v3
       - name: golangci-lint
         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.49.0
+          version: v1.52.2
 
           # Optional: working directory, useful for monorepos
           # working-directory: somedir
diff --git a/.github/workflows/gqlgen.yaml b/.github/workflows/gqlgen.yaml
index 19dab1cc..a0f9d50c 100644
--- a/.github/workflows/gqlgen.yaml
+++ b/.github/workflows/gqlgen.yaml
@@ -25,7 +25,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install dependencies
         run: |
           cd $GITHUB_WORKSPACE
diff --git a/.github/workflows/license.yaml b/.github/workflows/license.yaml
index 1f798d86..685d1319 100644
--- a/.github/workflows/license.yaml
+++ b/.github/workflows/license.yaml
@@ -22,7 +22,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install go-licenses
         run: go install github.com/google/go-licenses@latest
       - name: Check for forbidden licenses
diff --git a/.github/workflows/nightly.yaml b/.github/workflows/nightly.yaml
index b384335d..616fd354 100644
--- a/.github/workflows/nightly.yaml
+++ b/.github/workflows/nightly.yaml
@@ -19,7 +19,7 @@ jobs:
       - uses: actions/checkout@v3
       - uses: actions/setup-go@v3
         with:
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install dependencies
         run: |
           cd $GITHUB_WORKSPACE
diff --git a/.github/workflows/oci-conformance-action.yml b/.github/workflows/oci-conformance-action.yml
index f00eb83e..93a42deb 100644
--- a/.github/workflows/oci-conformance-action.yml
+++ b/.github/workflows/oci-conformance-action.yml
@@ -20,11 +20,11 @@ jobs:
     runs-on: ubuntu-latest
     # Steps represent a sequence of tasks that will be executed as part of the job
     steps:
-    - name: Install go 1.19
+    - name: Install go 1.20
       uses: actions/setup-go@v4
       with:
         cache: false
-        go-version: 1.19.x
+        go-version: 1.20.x
     - name: Checkout this PR 
       uses: actions/checkout@v3
     - name: Start zot server
diff --git a/.github/workflows/sync-3rdparty-images.yaml b/.github/workflows/sync-3rdparty-images.yaml
index a5121e30..870f3d4e 100644
--- a/.github/workflows/sync-3rdparty-images.yaml
+++ b/.github/workflows/sync-3rdparty-images.yaml
@@ -17,8 +17,6 @@ jobs:
     strategy:
       matrix:
         golang_version:
-          - "1.18"
-          - "1.19"
           - "1.20"
     runs-on: ubuntu-latest
     steps:
diff --git a/.github/workflows/tls.yaml b/.github/workflows/tls.yaml
index 5ff094d1..2ffec864 100644
--- a/.github/workflows/tls.yaml
+++ b/.github/workflows/tls.yaml
@@ -22,7 +22,7 @@ jobs:
       - uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Install dependencies
         run: |
           cd $GITHUB_WORKSPACE
diff --git a/.github/workflows/web-scan.yml b/.github/workflows/web-scan.yml
index e0216cef..add436da 100644
--- a/.github/workflows/web-scan.yml
+++ b/.github/workflows/web-scan.yml
@@ -25,7 +25,7 @@ jobs:
         uses: actions/setup-go@v4
         with:
           cache: false
-          go-version: 1.19.x
+          go-version: 1.20.x
       - name: Checkout
         uses: actions/checkout@v3
       - name: Build zot
diff --git a/Makefile b/Makefile
index 5d54d432..b7b44f80 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ TOOLSDIR := $(shell pwd)/hack/tools
 PATH := bin:$(TOOLSDIR)/bin:$(PATH)
 STACKER := $(shell which stacker)
 GOLINTER := $(TOOLSDIR)/bin/golangci-lint
-GOLINTER_VERSION := v1.49.0
+GOLINTER_VERSION := v1.52.2
 NOTATION := $(TOOLSDIR)/bin/notation
 NOTATION_VERSION := 1.0.0-rc.2
 COSIGN := $(TOOLSDIR)/bin/cosign
diff --git a/cmd/zb/helper.go b/cmd/zb/helper.go
index e63d820a..3f7df5bf 100644
--- a/cmd/zb/helper.go
+++ b/cmd/zb/helper.go
@@ -6,7 +6,7 @@ import (
 	"fmt"
 	"io"
 	"log"
-	mrand "math/rand"
+	"math/rand"
 	"net/http"
 	"os"
 	"path"
@@ -904,7 +904,8 @@ func getRandomSize(probabilityRange []float64) (int, int) {
 
 //nolint:gosec
 func flipFunc(probabilityRange []float64) int {
-	mrand.Seed(time.Now().UTC().UnixNano())
+	seed := time.Now().UTC().UnixNano()
+	mrand := rand.New(rand.NewSource(seed))
 	toss := mrand.Float64()
 
 	for idx, r := range probabilityRange {
diff --git a/go.mod b/go.mod
index 4dfabc08..2152288a 100644
--- a/go.mod
+++ b/go.mod
@@ -1,6 +1,6 @@
 module zotregistry.io/zot
 
-go 1.19
+go 1.20
 
 require (
 	github.com/99designs/gqlgen v0.17.29
diff --git a/golangcilint.yaml b/golangcilint.yaml
index 7f37edfd..e06ce573 100644
--- a/golangcilint.yaml
+++ b/golangcilint.yaml
@@ -5,7 +5,7 @@ run:
 
 linters:
   enable-all: true
-  disable: funlen,gocognit,exhaustivestruct,paralleltest,forbidigo,ireturn,wrapcheck,exhaustive,maintidx,exhaustruct,nosnakecase,interfacer,structcheck,varcheck,deadcode,ifshort,golint,scopelint,maligned,rowserrcheck,sqlclosecheck
+  disable: funlen,gocognit,exhaustivestruct,paralleltest,forbidigo,ireturn,wrapcheck,exhaustive,maintidx,exhaustruct,nosnakecase,interfacer,structcheck,varcheck,deadcode,ifshort,golint,scopelint,maligned,rowserrcheck,sqlclosecheck,revive,musttag
 
 linters-settings:
   dupl:
diff --git a/pkg/debug/swagger/swagger.go b/pkg/debug/swagger/swagger.go
index d8a1ffa9..d7530b25 100644
--- a/pkg/debug/swagger/swagger.go
+++ b/pkg/debug/swagger/swagger.go
@@ -21,7 +21,7 @@ func SetupSwaggerRoutes(conf *config.Config, router *mux.Router, authFunc mux.Mi
 	log log.Logger,
 ) {
 	log.Info().Msg("setting up swagger route")
-	// swagger swagger "/swagger/v2/index.html"
+	// swagger "/swagger/v2/index.html"
 	swgRouter := router.PathPrefix("/swagger/v2/").Subrouter()
 	swgRouter.Use(authFunc)
 	swgRouter.Methods("GET").Handler(httpSwagger.WrapHandler)
diff --git a/pkg/debug/swagger/swagger_disabled.go b/pkg/debug/swagger/swagger_disabled.go
index 7d5b5167..36206a96 100644
--- a/pkg/debug/swagger/swagger_disabled.go
+++ b/pkg/debug/swagger/swagger_disabled.go
@@ -11,15 +11,14 @@ import (
 	"github.com/gorilla/mux"
 
 	"zotregistry.io/zot/pkg/api/config"
-	"zotregistry.io/zot/pkg/log" //nolint:goimports
-	// as required by swaggo.
+	"zotregistry.io/zot/pkg/log"
 	_ "zotregistry.io/zot/swagger"
 )
 
 func SetupSwaggerRoutes(conf *config.Config, router *mux.Router, authFunc mux.MiddlewareFunc,
 	log log.Logger,
 ) {
-	// swagger swagger "/swagger/v2/index.html"
+	// swagger "/swagger/v2/index.html"
 	log.Warn().Msg("skipping enabling swagger because given zot binary " +
 		"doesn't include this feature, please build a binary that does so")
 }
diff --git a/pkg/extensions/extension_userprefs_test.go b/pkg/extensions/extension_userprefs_test.go
index 37b7d113..bbe97876 100644
--- a/pkg/extensions/extension_userprefs_test.go
+++ b/pkg/extensions/extension_userprefs_test.go
@@ -30,7 +30,7 @@ func TestHandlers(t *testing.T) {
 	mockrepoDB := mocks.RepoDBMock{}
 
 	Convey("No repo in request", t, func() {
-		request := httptest.NewRequest("GET", UserprefsBaseURL+"", strings.NewReader("My string"))
+		request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"", strings.NewReader("My string"))
 		response := httptest.NewRecorder()
 
 		extensions.PutStar(response, request, mockrepoDB, log)
@@ -45,7 +45,7 @@ func TestHandlers(t *testing.T) {
 	})
 
 	Convey("Empty repo in request", t, func() {
-		request := httptest.NewRequest("GET", UserprefsBaseURL+"?repo=", strings.NewReader("My string"))
+		request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"?repo=", strings.NewReader("My string"))
 		response := httptest.NewRecorder()
 
 		extensions.PutStar(response, request, mockrepoDB, log)
@@ -60,7 +60,7 @@ func TestHandlers(t *testing.T) {
 	})
 
 	Convey("ToggleStarRepo different errors", t, func() {
-		request := httptest.NewRequest("GET", UserprefsBaseURL+"?repo=test",
+		request := httptest.NewRequest(http.MethodGet, UserprefsBaseURL+"?repo=test",
 			strings.NewReader("My string"))
 
 		Convey("ErrRepoMetaNotFound", func() {
diff --git a/pkg/extensions/search/cve/cve_test.go b/pkg/extensions/search/cve/cve_test.go
index 8e4598f7..bee06ab8 100644
--- a/pkg/extensions/search/cve/cve_test.go
+++ b/pkg/extensions/search/cve/cve_test.go
@@ -111,7 +111,8 @@ func generateTestData(dbDir string) error { //nolint: gocyclo
 		return err
 	}
 
-	if err = os.WriteFile(path.Join(dbDir, "zot-nonreadable-test", "index.json"), buf, 0o111); err != nil {
+	if err = os.WriteFile(path.Join(dbDir, "zot-nonreadable-test", "index.json"), //nolint:gosec // test code
+		buf, 0o111); err != nil {
 		return err
 	}
 
diff --git a/pkg/extensions/search/resolver.go b/pkg/extensions/search/resolver.go
index 402c0fbe..2d3b0b70 100644
--- a/pkg/extensions/search/resolver.go
+++ b/pkg/extensions/search/resolver.go
@@ -27,7 +27,9 @@ import (
 	"zotregistry.io/zot/pkg/meta/repodb"
 	localCtx "zotregistry.io/zot/pkg/requestcontext"
 	"zotregistry.io/zot/pkg/storage"
-) // THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
+)
+
+// THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
 
 const (
 	querySizeLimit = 256
diff --git a/pkg/meta/repodb/repodb.go b/pkg/meta/repodb/repodb.go
index c74f5c5e..3bf4b872 100644
--- a/pkg/meta/repodb/repodb.go
+++ b/pkg/meta/repodb/repodb.go
@@ -14,7 +14,7 @@ const (
 	CosignType        = "cosign"
 )
 
-// Used to model changes to an object after a call to the the DB.
+// Used to model changes to an object after a call to the DB.
 type ToggleState int
 
 const (
diff --git a/pkg/meta/repodb/repodb_test.go b/pkg/meta/repodb/repodb_test.go
index efe94f69..6b856925 100644
--- a/pkg/meta/repodb/repodb_test.go
+++ b/pkg/meta/repodb/repodb_test.go
@@ -501,7 +501,7 @@ func RunRepoDBTests(repoDB repodb.RepoDB, preparationFuncs ...func() error) {
 			So(err, ShouldBeNil)
 			So(repoMeta.Stars, ShouldEqual, 0)
 
-			repoMeta, err = repoDB.GetRepoMeta("badRepo")
+			_, err = repoDB.GetRepoMeta("badRepo")
 			So(err, ShouldNotBeNil)
 		})
 
@@ -2663,7 +2663,7 @@ func generateTestImage() ([]byte, []byte, error) {
 	// init layers with random values
 	for i := range layers {
 		//nolint:gosec
-		_, err := rand.Read(layers[i])
+		_, err := rand.Read(layers[i]) //nolint:staticcheck
 		if err != nil {
 			return []byte{}, []byte{}, err
 		}
diff --git a/pkg/meta/repodb/repodbfactory/repodb_factory.go b/pkg/meta/repodb/repodbfactory/repodb_factory.go
index d7d6f093..311961dc 100644
--- a/pkg/meta/repodb/repodbfactory/repodb_factory.go
+++ b/pkg/meta/repodb/repodbfactory/repodb_factory.go
@@ -132,7 +132,7 @@ func toStringIfOk(cacheDriverConfig map[string]interface{}, param string, log lo
 	}
 
 	if str == "" {
-		log.Error().Msgf("parsing CacheDriver config failed, field '%s' is is empty", param)
+		log.Error().Msgf("parsing CacheDriver config failed, field '%s' is empty", param)
 
 		return "", false
 	}
diff --git a/pkg/storage/common.go b/pkg/storage/common.go
index 8f0174ae..4d3bbe09 100644
--- a/pkg/storage/common.go
+++ b/pkg/storage/common.go
@@ -203,7 +203,6 @@ func CheckIfIndexNeedsUpdate(index *ispec.Index, desc *ispec.Descriptor,
 		manifest := manifest
 		if reference == manifest.Digest.String() {
 			// nothing changed, so don't update
-			desc = &manifest
 			updateIndex = false
 
 			break
@@ -213,7 +212,6 @@ func CheckIfIndexNeedsUpdate(index *ispec.Index, desc *ispec.Descriptor,
 		if ok && v == reference {
 			if manifest.Digest.String() == desc.Digest.String() {
 				// nothing changed, so don't update
-				desc = &manifest
 				updateIndex = false
 
 				break
diff --git a/pkg/storage/s3/s3.go b/pkg/storage/s3/s3.go
index c26adbaa..f51588a1 100644
--- a/pkg/storage/s3/s3.go
+++ b/pkg/storage/s3/s3.go
@@ -1458,7 +1458,7 @@ func (is *ObjectStorage) getOriginalBlobFromDisk(duplicateBlobs []string) (strin
 }
 
 func (is *ObjectStorage) getOriginalBlob(digest godigest.Digest, duplicateBlobs []string) (string, error) {
-	originalBlob := ""
+	var originalBlob string
 
 	var err error
 
diff --git a/pkg/test/common_test.go b/pkg/test/common_test.go
index eb994265..122a6003 100644
--- a/pkg/test/common_test.go
+++ b/pkg/test/common_test.go
@@ -1229,7 +1229,7 @@ func TestGenerateNotationCerts(t *testing.T) {
 		So(err, ShouldBeNil)
 		signingKeysBuf, err := json.Marshal(notconfig.SigningKeys{})
 		So(err, ShouldBeNil)
-		err = os.WriteFile(filePath, signingKeysBuf, 0o555)
+		err = os.WriteFile(filePath, signingKeysBuf, 0o555) //nolint:gosec // test code
 		So(err, ShouldBeNil)
 		err = test.GenerateNotationCerts(t.TempDir(), "cert")
 		So(err, ShouldNotBeNil)