From e6b279449491f046b923cb1e1e8a6f60f290f1a2 Mon Sep 17 00:00:00 2001 From: Gusted Date: Wed, 10 Nov 2021 18:56:25 +0100 Subject: [PATCH 1/5] Update golangci-lint in Makefile - Partially resolvess #17596 - Download specific version(v1.43.0) by default. - If current installed version is older than the minium version, it will download the mininium required version. - Update the install script to avoid deprecated error `golangci/golangci-lint err this script is deprecated, please do not use it anymore. check https://github.com/goreleaser/godownloader/issues/207` --- Makefile | 8 +- contrib/golangci-lint/check-version.go | 123 +++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 contrib/golangci-lint/check-version.go diff --git a/Makefile b/Makefile index aa47cea53477b..625d23b44f6eb 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,7 @@ COMMA := , XGO_VERSION := go-1.17.x MIN_GO_VERSION := 001016000 MIN_NODE_VERSION := 012017000 +MIN_GOLANGCI_LINT_VERSION = 1.43.0 DOCKER_IMAGE ?= gitea/gitea DOCKER_TAG ?= latest @@ -767,8 +768,13 @@ pr\#%: clean-all .PHONY: golangci-lint golangci-lint: @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + echo "Downloading golangci-lint"; \ export BINARY="golangci-lint"; \ - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.37.0; \ + curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ + elif $(GO) run contrib/golangci-lint/check-version.go $(MIN_GOLANGCI_LINT_VERSION) > /dev/null 2>&1; then \ + echo "Downloading newer version of golangci-lint"; \ + export BINARY="golangci-lint"; \ + curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ fi golangci-lint run --timeout 10m diff --git a/contrib/golangci-lint/check-version.go b/contrib/golangci-lint/check-version.go new file mode 100644 index 0000000000000..2ec8fa0ef48b6 --- /dev/null +++ b/contrib/golangci-lint/check-version.go @@ -0,0 +1,123 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package main + +import ( + "log" + "os" + "os/exec" + "strconv" + "strings" +) + +// Expect that golangci-lint is installed. +// Get the current version of golangci-lint and compare it with the minium +// version. Exit -1 if it's equal or higher than the minium version. +// exit 0 if it's lower than the minium version. + +// validVersion, checks if the version only contains dots and digits. +// also that it doesn't contain 2 dots after each other. Also cannot +// end with a dot. +func validVersion(version string) bool { + if version == "" { + return false + } + + wasPreviousDot := false + for _, cRune := range version { + switch cRune { + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': + wasPreviousDot = false + case '.': + if wasPreviousDot { + // Cannot have `..` within a version. + return false + } + wasPreviousDot = true + default: + return false + } + } + + // Simplified from if wasPreviousDot { return false } else { return true } + return !wasPreviousDot +} + +// compareVersions compares 2 given versions. +// It will return true if the second version is equal or higher than +// the first version. It will return false if the second version is +// lower than the first version. +func compareVersions(firstVersion, secondVersion string) bool { + firstVersionDigits := strings.Split(firstVersion, ".") + secondVersionDigits := strings.Split(secondVersion, ".") + + lenSecondVersionDigits := len(secondVersionDigits) - 1 + + for idx := range firstVersionDigits { + if idx > lenSecondVersionDigits { + return false + } + + firstNumber, _ := strconv.Atoi(firstVersionDigits[idx]) + secondNumber, _ := strconv.Atoi(secondVersionDigits[idx]) + if firstNumber != secondNumber { + return firstNumber >= secondNumber + } + } + + return true +} + +func main() { + // The minium version should be given by the the first argument. + if len(os.Args) != 2 { + log.Fatal("Incorrect amount of arguments was passed, expected 1 argument") + } + + miniumVersion := os.Args[1] + if !validVersion(miniumVersion) { + log.Fatal("Given miniumVersion isn't a valid version") + } + + // Get the version from golangci-lint + cmd := exec.Command("golangci-lint", "--version") + + // Run the command and get the output. + bytesOutput, err := cmd.Output() + if err != nil { + log.Fatalf("Running \"golangci-lint --version\" ran into a error: %v", err) + } + output := string(bytesOutput) + + // Extract the version from output. + // Assuming they won't change this in the future + // We will assume the version starts from 27th character. + // We shouldn't assume the length of the version, so get the first + // index of a whitespace after the 27th character. + if len(output) < 28 { + log.Fatalf("Output of \"golangci-lint --version\" hasn't the correct length") + } + + whitespaceAfterVersion := strings.Index(output[27:], " ") + if whitespaceAfterVersion == -1 { + log.Fatalf("Couldn't get the whitespace after the version from the output of \"golangci-lint --version\"") + } + + // Get the version from the output at the correct indexes. + installedVersion := string(output[27 : 27+whitespaceAfterVersion]) + + // Check if it's a valid version. + if !validVersion(installedVersion) { + log.Fatal("installedVersion isn't a valid version") + } + + // If the installedVersion is higher or equal to miniumVersion + // than it's all fine and thus we will exit with a 1 code. + // Such that the code will only exit with a 0 code when the + // installedVerion is lower than miniumVersion. + if compareVersions(miniumVersion, installedVersion) { + os.Exit(-1) + } +} From 572dc3f4b7c3548f49d82bc7362049590fc2b36d Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 15 Nov 2021 16:21:32 +0100 Subject: [PATCH 2/5] Simplify golangci-lint version check --- Makefile | 12 ++- contrib/golangci-lint/check-version.go | 123 ------------------------- 2 files changed, 8 insertions(+), 127 deletions(-) delete mode 100644 contrib/golangci-lint/check-version.go diff --git a/Makefile b/Makefile index 625d23b44f6eb..4addfc39452ac 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ COMMA := , XGO_VERSION := go-1.17.x MIN_GO_VERSION := 001016000 MIN_NODE_VERSION := 012017000 -MIN_GOLANGCI_LINT_VERSION = 1.43.0 +MIN_GOLANGCI_LINT_VERSION := 001043000 DOCKER_IMAGE ?= gitea/gitea DOCKER_TAG ?= latest @@ -766,17 +766,21 @@ pr\#%: clean-all $(GO) run contrib/pr/checkout.go $* .PHONY: golangci-lint -golangci-lint: +golangci-lint: golangci-lint-check + golangci-lint run --timeout 10m + +.PHONY: golangci-lint-check +golangci-lint-check: + $(eval GOLANGCI_LINT_VERSION := $(shell printf "%03d%03d%03d" $(shell golangci-lint --version | grep -Eo '[0-9]+\.[0-9.]+' | tr '.' ' ');)) @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ echo "Downloading golangci-lint"; \ export BINARY="golangci-lint"; \ curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ - elif $(GO) run contrib/golangci-lint/check-version.go $(MIN_GOLANGCI_LINT_VERSION) > /dev/null 2>&1; then \ + elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(MIN_GOLANGCI_LINT_VERSION)" ]; then \ echo "Downloading newer version of golangci-lint"; \ export BINARY="golangci-lint"; \ curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ fi - golangci-lint run --timeout 10m .PHONY: docker docker: diff --git a/contrib/golangci-lint/check-version.go b/contrib/golangci-lint/check-version.go deleted file mode 100644 index 2ec8fa0ef48b6..0000000000000 --- a/contrib/golangci-lint/check-version.go +++ /dev/null @@ -1,123 +0,0 @@ -// Copyright 2020 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package main - -import ( - "log" - "os" - "os/exec" - "strconv" - "strings" -) - -// Expect that golangci-lint is installed. -// Get the current version of golangci-lint and compare it with the minium -// version. Exit -1 if it's equal or higher than the minium version. -// exit 0 if it's lower than the minium version. - -// validVersion, checks if the version only contains dots and digits. -// also that it doesn't contain 2 dots after each other. Also cannot -// end with a dot. -func validVersion(version string) bool { - if version == "" { - return false - } - - wasPreviousDot := false - for _, cRune := range version { - switch cRune { - case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': - wasPreviousDot = false - case '.': - if wasPreviousDot { - // Cannot have `..` within a version. - return false - } - wasPreviousDot = true - default: - return false - } - } - - // Simplified from if wasPreviousDot { return false } else { return true } - return !wasPreviousDot -} - -// compareVersions compares 2 given versions. -// It will return true if the second version is equal or higher than -// the first version. It will return false if the second version is -// lower than the first version. -func compareVersions(firstVersion, secondVersion string) bool { - firstVersionDigits := strings.Split(firstVersion, ".") - secondVersionDigits := strings.Split(secondVersion, ".") - - lenSecondVersionDigits := len(secondVersionDigits) - 1 - - for idx := range firstVersionDigits { - if idx > lenSecondVersionDigits { - return false - } - - firstNumber, _ := strconv.Atoi(firstVersionDigits[idx]) - secondNumber, _ := strconv.Atoi(secondVersionDigits[idx]) - if firstNumber != secondNumber { - return firstNumber >= secondNumber - } - } - - return true -} - -func main() { - // The minium version should be given by the the first argument. - if len(os.Args) != 2 { - log.Fatal("Incorrect amount of arguments was passed, expected 1 argument") - } - - miniumVersion := os.Args[1] - if !validVersion(miniumVersion) { - log.Fatal("Given miniumVersion isn't a valid version") - } - - // Get the version from golangci-lint - cmd := exec.Command("golangci-lint", "--version") - - // Run the command and get the output. - bytesOutput, err := cmd.Output() - if err != nil { - log.Fatalf("Running \"golangci-lint --version\" ran into a error: %v", err) - } - output := string(bytesOutput) - - // Extract the version from output. - // Assuming they won't change this in the future - // We will assume the version starts from 27th character. - // We shouldn't assume the length of the version, so get the first - // index of a whitespace after the 27th character. - if len(output) < 28 { - log.Fatalf("Output of \"golangci-lint --version\" hasn't the correct length") - } - - whitespaceAfterVersion := strings.Index(output[27:], " ") - if whitespaceAfterVersion == -1 { - log.Fatalf("Couldn't get the whitespace after the version from the output of \"golangci-lint --version\"") - } - - // Get the version from the output at the correct indexes. - installedVersion := string(output[27 : 27+whitespaceAfterVersion]) - - // Check if it's a valid version. - if !validVersion(installedVersion) { - log.Fatal("installedVersion isn't a valid version") - } - - // If the installedVersion is higher or equal to miniumVersion - // than it's all fine and thus we will exit with a 1 code. - // Such that the code will only exit with a 0 code when the - // installedVerion is lower than miniumVersion. - if compareVersions(miniumVersion, installedVersion) { - os.Exit(-1) - } -} From 5453261e1141c90596092ffe5dcab49d779f6c37 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 15 Nov 2021 23:42:39 +0100 Subject: [PATCH 3/5] Fix version conversion --- Makefile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 4addfc39452ac..44916f63cba62 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ COMMA := , XGO_VERSION := go-1.17.x MIN_GO_VERSION := 001016000 MIN_NODE_VERSION := 012017000 -MIN_GOLANGCI_LINT_VERSION := 001043000 +MIN_GOLANGCI_LINT_VERSION := 1.43.0 DOCKER_IMAGE ?= gitea/gitea DOCKER_TAG ?= latest @@ -775,11 +775,12 @@ golangci-lint-check: @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ echo "Downloading golangci-lint"; \ export BINARY="golangci-lint"; \ - curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ - elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(MIN_GOLANGCI_LINT_VERSION)" ]; then \ + curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ + elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(shell printf "%03d%03d%03d" $(shell echo $(MIN_GOLANGCI_LINT_VERSION) | tr '.' ' '))" ]; then \ echo "Downloading newer version of golangci-lint"; \ export BINARY="golangci-lint"; \ - curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ + echo "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh"; \ + curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ fi .PHONY: docker From b508833f1721b38fc376de8b23ec883ee0c1754e Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 16 Nov 2021 20:58:18 +0000 Subject: [PATCH 4/5] Add version that's downloading Co-authored-by: zeripath --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 44916f63cba62..1a6bec88baaa8 100644 --- a/Makefile +++ b/Makefile @@ -773,7 +773,7 @@ golangci-lint: golangci-lint-check golangci-lint-check: $(eval GOLANGCI_LINT_VERSION := $(shell printf "%03d%03d%03d" $(shell golangci-lint --version | grep -Eo '[0-9]+\.[0-9.]+' | tr '.' ' ');)) @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - echo "Downloading golangci-lint"; \ + echo "Downloading golangci-lint v${MIN_GOLANGCI_LINT_VERSION}"; \ export BINARY="golangci-lint"; \ curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(shell printf "%03d%03d%03d" $(shell echo $(MIN_GOLANGCI_LINT_VERSION) | tr '.' ' '))" ]; then \ From ac412852a81fa7e754a44a5dffdbaa81c389e616 Mon Sep 17 00:00:00 2001 From: Gusted Date: Tue, 16 Nov 2021 23:37:51 +0100 Subject: [PATCH 5/5] Consistency --- Makefile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 1a6bec88baaa8..13df1775c6715 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ COMMA := , XGO_VERSION := go-1.17.x MIN_GO_VERSION := 001016000 MIN_NODE_VERSION := 012017000 -MIN_GOLANGCI_LINT_VERSION := 1.43.0 +MIN_GOLANGCI_LINT_VERSION := 001043000 DOCKER_IMAGE ?= gitea/gitea DOCKER_TAG ?= latest @@ -772,15 +772,15 @@ golangci-lint: golangci-lint-check .PHONY: golangci-lint-check golangci-lint-check: $(eval GOLANGCI_LINT_VERSION := $(shell printf "%03d%03d%03d" $(shell golangci-lint --version | grep -Eo '[0-9]+\.[0-9.]+' | tr '.' ' ');)) + $(eval MIN_GOLANGCI_LINT_VER_FMT := $(shell printf "%g.%g.%g" $(shell echo $(MIN_GOLANGCI_LINT_VERSION) | grep -o ...))) @hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ - echo "Downloading golangci-lint v${MIN_GOLANGCI_LINT_VERSION}"; \ + echo "Downloading golangci-lint v${MIN_GOLANGCI_LINT_VER_FMT}"; \ export BINARY="golangci-lint"; \ - curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ - elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(shell printf "%03d%03d%03d" $(shell echo $(MIN_GOLANGCI_LINT_VERSION) | tr '.' ' '))" ]; then \ - echo "Downloading newer version of golangci-lint"; \ + curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VER_FMT}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VER_FMT); \ + elif [ "$(GOLANGCI_LINT_VERSION)" -lt "$(MIN_GOLANGCI_LINT_VERSION)" ]; then \ + echo "Downloading newer version of golangci-lint v${MIN_GOLANGCI_LINT_VER_FMT}"; \ export BINARY="golangci-lint"; \ - echo "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh"; \ - curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VERSION}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \ + curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${MIN_GOLANGCI_LINT_VER_FMT}/install.sh" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VER_FMT); \ fi .PHONY: docker