Skip to content

Commit e6b2794

Browse files
author
Gusted
committed
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`
1 parent 43bbc54 commit e6b2794

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ COMMA := ,
2727
XGO_VERSION := go-1.17.x
2828
MIN_GO_VERSION := 001016000
2929
MIN_NODE_VERSION := 012017000
30+
MIN_GOLANGCI_LINT_VERSION = 1.43.0
3031

3132
DOCKER_IMAGE ?= gitea/gitea
3233
DOCKER_TAG ?= latest
@@ -767,8 +768,13 @@ pr\#%: clean-all
767768
.PHONY: golangci-lint
768769
golangci-lint:
769770
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
771+
echo "Downloading golangci-lint"; \
770772
export BINARY="golangci-lint"; \
771-
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.37.0; \
773+
curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \
774+
elif $(GO) run contrib/golangci-lint/check-version.go $(MIN_GOLANGCI_LINT_VERSION) > /dev/null 2>&1; then \
775+
echo "Downloading newer version of golangci-lint"; \
776+
export BINARY="golangci-lint"; \
777+
curl -sfL "https://raw.githubusercontent.com/golangci/golangci-lint/${MIN_GOLANGCI_LINT_VERSION}" | sh -s -- -b $(GOPATH)/bin v$(MIN_GOLANGCI_LINT_VERSION); \
772778
fi
773779
golangci-lint run --timeout 10m
774780

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"log"
9+
"os"
10+
"os/exec"
11+
"strconv"
12+
"strings"
13+
)
14+
15+
// Expect that golangci-lint is installed.
16+
// Get the current version of golangci-lint and compare it with the minium
17+
// version. Exit -1 if it's equal or higher than the minium version.
18+
// exit 0 if it's lower than the minium version.
19+
20+
// validVersion, checks if the version only contains dots and digits.
21+
// also that it doesn't contain 2 dots after each other. Also cannot
22+
// end with a dot.
23+
func validVersion(version string) bool {
24+
if version == "" {
25+
return false
26+
}
27+
28+
wasPreviousDot := false
29+
for _, cRune := range version {
30+
switch cRune {
31+
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
32+
wasPreviousDot = false
33+
case '.':
34+
if wasPreviousDot {
35+
// Cannot have `..` within a version.
36+
return false
37+
}
38+
wasPreviousDot = true
39+
default:
40+
return false
41+
}
42+
}
43+
44+
// Simplified from if wasPreviousDot { return false } else { return true }
45+
return !wasPreviousDot
46+
}
47+
48+
// compareVersions compares 2 given versions.
49+
// It will return true if the second version is equal or higher than
50+
// the first version. It will return false if the second version is
51+
// lower than the first version.
52+
func compareVersions(firstVersion, secondVersion string) bool {
53+
firstVersionDigits := strings.Split(firstVersion, ".")
54+
secondVersionDigits := strings.Split(secondVersion, ".")
55+
56+
lenSecondVersionDigits := len(secondVersionDigits) - 1
57+
58+
for idx := range firstVersionDigits {
59+
if idx > lenSecondVersionDigits {
60+
return false
61+
}
62+
63+
firstNumber, _ := strconv.Atoi(firstVersionDigits[idx])
64+
secondNumber, _ := strconv.Atoi(secondVersionDigits[idx])
65+
if firstNumber != secondNumber {
66+
return firstNumber >= secondNumber
67+
}
68+
}
69+
70+
return true
71+
}
72+
73+
func main() {
74+
// The minium version should be given by the the first argument.
75+
if len(os.Args) != 2 {
76+
log.Fatal("Incorrect amount of arguments was passed, expected 1 argument")
77+
}
78+
79+
miniumVersion := os.Args[1]
80+
if !validVersion(miniumVersion) {
81+
log.Fatal("Given miniumVersion isn't a valid version")
82+
}
83+
84+
// Get the version from golangci-lint
85+
cmd := exec.Command("golangci-lint", "--version")
86+
87+
// Run the command and get the output.
88+
bytesOutput, err := cmd.Output()
89+
if err != nil {
90+
log.Fatalf("Running \"golangci-lint --version\" ran into a error: %v", err)
91+
}
92+
output := string(bytesOutput)
93+
94+
// Extract the version from output.
95+
// Assuming they won't change this in the future
96+
// We will assume the version starts from 27th character.
97+
// We shouldn't assume the length of the version, so get the first
98+
// index of a whitespace after the 27th character.
99+
if len(output) < 28 {
100+
log.Fatalf("Output of \"golangci-lint --version\" hasn't the correct length")
101+
}
102+
103+
whitespaceAfterVersion := strings.Index(output[27:], " ")
104+
if whitespaceAfterVersion == -1 {
105+
log.Fatalf("Couldn't get the whitespace after the version from the output of \"golangci-lint --version\"")
106+
}
107+
108+
// Get the version from the output at the correct indexes.
109+
installedVersion := string(output[27 : 27+whitespaceAfterVersion])
110+
111+
// Check if it's a valid version.
112+
if !validVersion(installedVersion) {
113+
log.Fatal("installedVersion isn't a valid version")
114+
}
115+
116+
// If the installedVersion is higher or equal to miniumVersion
117+
// than it's all fine and thus we will exit with a 1 code.
118+
// Such that the code will only exit with a 0 code when the
119+
// installedVerion is lower than miniumVersion.
120+
if compareVersions(miniumVersion, installedVersion) {
121+
os.Exit(-1)
122+
}
123+
}

0 commit comments

Comments
 (0)