Skip to content

Commit f9ec2f8

Browse files
kolaentetechknowlogick
authored andcommitted
Add golangci (#6418)
1 parent 5832f8d commit f9ec2f8

File tree

147 files changed

+1046
-774
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

147 files changed

+1046
-774
lines changed

.drone.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,10 @@ pipeline:
7474
commands:
7575
- make clean
7676
- make generate
77-
- make vet
78-
- make lint
79-
- make fmt-check
77+
- make golangci-lint
78+
- make revive
8079
- make swagger-check
8180
- make swagger-validate
82-
- make misspell-check
8381
- make test-vendor
8482
- make build
8583
when:

.golangci.yml

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
linters:
2+
enable:
3+
- gosimple
4+
- deadcode
5+
- typecheck
6+
- govet
7+
- errcheck
8+
- staticcheck
9+
- unused
10+
- structcheck
11+
- varcheck
12+
- golint
13+
- dupl
14+
#- gocyclo # The cyclomatic complexety of a lot of functions is too high, we should refactor those another time.
15+
- gofmt
16+
- misspell
17+
- gocritic
18+
enable-all: false
19+
disable-all: true
20+
fast: false
21+
22+
linters-settings:
23+
gocritic:
24+
disabled-checks:
25+
- ifElseChain
26+
- singleCaseSwitch # Every time this occured in the code, there was no other way.
27+
28+
issues:
29+
exclude-rules:
30+
# Exclude some linters from running on tests files.
31+
- path: _test\.go
32+
linters:
33+
- gocyclo
34+
- errcheck
35+
- dupl
36+
- gosec
37+
- unparam
38+
- staticcheck
39+
- path: models/migrations/v
40+
linters:
41+
- gocyclo
42+
- errcheck
43+
- dupl
44+
- gosec
45+
- linters:
46+
- dupl
47+
text: "webhook"
48+
- linters:
49+
- gocritic
50+
text: "`ID' should not be capitalized"
51+
- path: modules/templates/helper.go
52+
linters:
53+
- gocritic
54+
- linters:
55+
- unused
56+
- deadcode
57+
text: "swagger"
58+
- path: contrib/pr/checkout.go
59+
linters:
60+
- errcheck
61+
- path: models/issue.go
62+
linters:
63+
- errcheck
64+
- path: models/migrations/
65+
linters:
66+
- errcheck
67+
- path: modules/log/
68+
linters:
69+
- errcheck
70+
- path: routers/routes/routes.go
71+
linters:
72+
- dupl
73+
- path: routers/repo/view.go
74+
linters:
75+
- dupl
76+
- path: models/migrations/
77+
linters:
78+
- unused
79+
- linters:
80+
- staticcheck
81+
text: "argument x is overwritten before first use"
82+
- path: modules/httplib/httplib.go
83+
linters:
84+
- staticcheck
85+
# Enabling this would require refactoring the methods and how they are called.
86+
- path: models/issue_comment_list.go
87+
linters:
88+
- dupl
89+
# "Destroy" is misspelled in github.com/go-macaron/session/session.go:213 so it's not our responsability to fix it
90+
- path: modules/session/virtual.go
91+
linters:
92+
- misspell
93+
text: '`Destory` is a misspelling of `Destroy`'
94+
- path: modules/session/memory.go
95+
linters:
96+
- misspell
97+
text: '`Destory` is a misspelling of `Destroy`'

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ errcheck:
135135

136136
.PHONY: lint
137137
lint:
138+
@echo 'make lint is depricated. Use "make revive" if you want to use the old lint tool, or "make golangci-lint" to run a complete code check.'
139+
140+
.PHONY: revive
141+
revive:
138142
@hash revive > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
139143
$(GO) get -u github.com/mgechev/revive; \
140144
fi
@@ -461,3 +465,10 @@ generate-images:
461465
.PHONY: pr
462466
pr:
463467
$(GO) run contrib/pr/checkout.go $(PR)
468+
469+
.PHONY: golangci-lint
470+
golangci-lint:
471+
@hash golangci-lint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
472+
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.16.0; \
473+
fi
474+
golangci-lint run

cmd/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ func runUpdateOauth(c *cli.Context) error {
481481
}
482482

483483
// update custom URL mapping
484-
var customURLMapping *oauth2.CustomURLMapping
484+
var customURLMapping = &oauth2.CustomURLMapping{}
485485

486486
if oAuth2Config.CustomURLMapping != nil {
487487
customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL

cmd/cert.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,28 @@ func runCert(c *cli.Context) error {
170170
if err != nil {
171171
log.Fatalf("Failed to open cert.pem for writing: %v", err)
172172
}
173-
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
174-
certOut.Close()
173+
err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
174+
if err != nil {
175+
log.Fatalf("Failed to encode certificate: %v", err)
176+
}
177+
err = certOut.Close()
178+
if err != nil {
179+
log.Fatalf("Failed to write cert: %v", err)
180+
}
175181
log.Println("Written cert.pem")
176182

177183
keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
178184
if err != nil {
179185
log.Fatalf("Failed to open key.pem for writing: %v", err)
180186
}
181-
pem.Encode(keyOut, pemBlockForKey(priv))
182-
keyOut.Close()
187+
err = pem.Encode(keyOut, pemBlockForKey(priv))
188+
if err != nil {
189+
log.Fatalf("Failed to encode key: %v", err)
190+
}
191+
err = keyOut.Close()
192+
if err != nil {
193+
log.Fatalf("Failed to write key: %v", err)
194+
}
183195
log.Println("Written key.pem")
184-
185196
return nil
186197
}

cmd/serv.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
)
3131

3232
const (
33-
accessDenied = "Repository does not exist or you do not have access"
3433
lfsAuthenticateVerb = "git-lfs-authenticate"
3534
)
3635

@@ -67,7 +66,7 @@ func checkLFSVersion() {
6766
}
6867

6968
func setup(logPath string) {
70-
log.DelLogger("console")
69+
_ = log.DelLogger("console")
7170
setting.NewContext()
7271
checkLFSVersion()
7372
}
@@ -112,7 +111,9 @@ func runServ(c *cli.Context) error {
112111
}
113112

114113
if len(c.Args()) < 1 {
115-
cli.ShowSubcommandHelp(c)
114+
if err := cli.ShowSubcommandHelp(c); err != nil {
115+
fmt.Printf("error showing subcommand help: %v\n", err)
116+
}
116117
return nil
117118
}
118119

cmd/web.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,16 @@ func runWeb(ctx *cli.Context) error {
178178
}
179179
err = runHTTPS(listenAddr, setting.CertFile, setting.KeyFile, context2.ClearHandler(m))
180180
case setting.FCGI:
181-
listener, err := net.Listen("tcp", listenAddr)
181+
var listener net.Listener
182+
listener, err = net.Listen("tcp", listenAddr)
182183
if err != nil {
183184
log.Fatal("Failed to bind %s: %v", listenAddr, err)
184185
}
185-
defer listener.Close()
186+
defer func() {
187+
if err := listener.Close(); err != nil {
188+
log.Fatal("Failed to stop server: %v", err)
189+
}
190+
}()
186191
err = fcgi.Serve(listener, context2.ClearHandler(m))
187192
case setting.UnixSocket:
188193
if err := os.Remove(listenAddr); err != nil && !os.IsNotExist(err) {

contrib/pr/checkout.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ func runPR() {
9191
routers.NewServices()
9292
//x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
9393

94-
var helper testfixtures.Helper
95-
helper = &testfixtures.SQLite{}
94+
var helper testfixtures.Helper = &testfixtures.SQLite{}
9695
models.NewEngine(func(_ *xorm.Engine) error {
9796
return nil
9897
})

integrations/branches_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func branchAction(t *testing.T, button string) (*HTMLDoc, string) {
6262
req = NewRequestWithValues(t, "POST", link, map[string]string{
6363
"_csrf": getCsrf(t, htmlDoc.doc),
6464
})
65-
resp = session.MakeRequest(t, req, http.StatusOK)
65+
session.MakeRequest(t, req, http.StatusOK)
6666

6767
url, err := url.Parse(link)
6868
assert.NoError(t, err)

integrations/editor_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestCreateFile(t *testing.T) {
3434
"content": "Content",
3535
"commit_choice": "direct",
3636
})
37-
resp = session.MakeRequest(t, req, http.StatusFound)
37+
session.MakeRequest(t, req, http.StatusFound)
3838
})
3939
}
4040

@@ -48,15 +48,15 @@ func TestCreateFileOnProtectedBranch(t *testing.T) {
4848
"_csrf": csrf,
4949
"protected": "on",
5050
})
51-
resp := session.MakeRequest(t, req, http.StatusFound)
51+
session.MakeRequest(t, req, http.StatusFound)
5252
// Check if master branch has been locked successfully
5353
flashCookie := session.GetCookie("macaron_flash")
5454
assert.NotNil(t, flashCookie)
5555
assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Bbranch%2B%2527master%2527%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value)
5656

5757
// Request editor page
5858
req = NewRequest(t, "GET", "/user2/repo1/_new/master/")
59-
resp = session.MakeRequest(t, req, http.StatusOK)
59+
resp := session.MakeRequest(t, req, http.StatusOK)
6060

6161
doc := NewHTMLParser(t, resp.Body)
6262
lastCommit := doc.GetInputValueByName("last_commit")

integrations/integration_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type NilResponseRecorder struct {
4242
}
4343

4444
func (n *NilResponseRecorder) Write(b []byte) (int, error) {
45-
n.Length = n.Length + len(b)
45+
n.Length += len(b)
4646
return len(b), nil
4747
}
4848

@@ -141,8 +141,7 @@ func initIntegrationTest() {
141141
if err != nil {
142142
log.Fatalf("sql.Open: %v", err)
143143
}
144-
rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'",
145-
models.DbCfg.Name))
144+
rows, err := db.Query(fmt.Sprintf("SELECT 1 FROM pg_database WHERE datname = '%s'", models.DbCfg.Name))
146145
if err != nil {
147146
log.Fatalf("db.Query: %v", err)
148147
}
@@ -210,7 +209,7 @@ func (s *TestSession) MakeRequest(t testing.TB, req *http.Request, expectedStatu
210209
resp := MakeRequest(t, req, expectedStatus)
211210

212211
ch := http.Header{}
213-
ch.Add("Cookie", strings.Join(resp.HeaderMap["Set-Cookie"], ";"))
212+
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
214213
cr := http.Request{Header: ch}
215214
s.jar.SetCookies(baseURL, cr.Cookies())
216215

@@ -226,7 +225,7 @@ func (s *TestSession) MakeRequestNilResponseRecorder(t testing.TB, req *http.Req
226225
resp := MakeRequestNilResponseRecorder(t, req, expectedStatus)
227226

228227
ch := http.Header{}
229-
ch.Add("Cookie", strings.Join(resp.HeaderMap["Set-Cookie"], ";"))
228+
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
230229
cr := http.Request{Header: ch}
231230
s.jar.SetCookies(baseURL, cr.Cookies())
232231

@@ -266,7 +265,7 @@ func loginUserWithPassword(t testing.TB, userName, password string) *TestSession
266265
resp = MakeRequest(t, req, http.StatusFound)
267266

268267
ch := http.Header{}
269-
ch.Add("Cookie", strings.Join(resp.HeaderMap["Set-Cookie"], ";"))
268+
ch.Add("Cookie", strings.Join(resp.Header()["Set-Cookie"], ";"))
270269
cr := http.Request{Header: ch}
271270

272271
session := emptyTestSession(t)

integrations/lfs_getobject_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string
4545
lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
4646
}
4747

48-
lfsID = lfsID + 1
48+
lfsID++
4949
lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
5050
assert.NoError(t, err)
5151
contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}

integrations/migration-test/migration_test.go

-15
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,6 @@ func initMigrationTest(t *testing.T) {
5757
setting.NewLogServices(true)
5858
}
5959

60-
func getDialect() string {
61-
dialect := "sqlite"
62-
switch {
63-
case setting.UseSQLite3:
64-
dialect = "sqlite"
65-
case setting.UseMySQL:
66-
dialect = "mysql"
67-
case setting.UsePostgreSQL:
68-
dialect = "pgsql"
69-
case setting.UseMSSQL:
70-
dialect = "mssql"
71-
}
72-
return dialect
73-
}
74-
7560
func availableVersions() ([]string, error) {
7661
migrationsDir, err := os.Open("integrations/migration-test")
7762
if err != nil {

integrations/testlogger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func PrintCurrentTest(t testing.TB, skip ...int) {
7373
_, filename, line, _ := runtime.Caller(actualSkip)
7474

7575
if log.CanColorStdout {
76-
fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", log.NewColoredValue(t.Name()), strings.TrimPrefix(filename, prefix), line)
76+
fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", fmt.Formatter(log.NewColoredValue(t.Name())), strings.TrimPrefix(filename, prefix), line)
7777
} else {
7878
fmt.Fprintf(os.Stdout, "=== %s (%s:%d)\n", t.Name(), strings.TrimPrefix(filename, prefix), line)
7979
}

main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var (
4242

4343
func init() {
4444
setting.AppVer = Version
45-
setting.AppBuiltWith = formatBuiltWith(Tags)
45+
setting.AppBuiltWith = formatBuiltWith()
4646

4747
// Grab the original help templates
4848
originalAppHelpTemplate = cli.AppHelpTemplate
@@ -56,7 +56,7 @@ func main() {
5656
app.Usage = "A painless self-hosted Git service"
5757
app.Description = `By default, gitea will start serving using the webserver with no
5858
arguments - which can alternatively be run by running the subcommand web.`
59-
app.Version = Version + formatBuiltWith(Tags)
59+
app.Version = Version + formatBuiltWith()
6060
app.Commands = []cli.Command{
6161
cmd.CmdWeb,
6262
cmd.CmdServ,
@@ -179,7 +179,7 @@ DEFAULT CONFIGURATION:
179179
`, originalTemplate, setting.CustomPath, overrided, setting.CustomConf, setting.AppPath, setting.AppWorkPath)
180180
}
181181

182-
func formatBuiltWith(makeTags string) string {
182+
func formatBuiltWith() string {
183183
var version = runtime.Version()
184184
if len(MakeVersion) > 0 {
185185
version = MakeVersion + ", " + runtime.Version()

models/access_test.go

-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13-
var accessModes = []AccessMode{
14-
AccessModeRead,
15-
AccessModeWrite,
16-
AccessModeAdmin,
17-
AccessModeOwner,
18-
}
19-
2013
func TestAccessLevel(t *testing.T) {
2114
assert.NoError(t, PrepareTestDatabase())
2215

0 commit comments

Comments
 (0)