Build: use golangci-lint as a make command (#17739)

* Build: use golangci-lint as a make command

* Since gometalinter was deprecated in favor of golangci-lint so it was
  replaced by it. Responsibilities held by the gometalinter was moved to
  golangci-lint

* There was some changes in implementation (that was also mentioned in
  the code comment) between the tools, which uncovered couple errors
  in the code. Those issues were either solved or disabled by
  the inline comments

* Introduce the golangci-lint config, to make their
  configuration more manageable

* Build: replace backend-lint.sh script with make
This commit is contained in:
Oleg Gaidarenko
2019-07-02 16:06:59 +03:00
committed by GitHub
parent 364f1f2e55
commit caa1314f44
20 changed files with 278 additions and 85 deletions
+4 -1
View File
@@ -5,7 +5,10 @@ Grafanas backend has been developed for a long time with a mix of code styles.
This style guide is a guide for how we want to write Go code in the future. Generally, we want to follow the style guides used in Go [Code Review Comments](https://code.google.com/p/go-wiki/wiki/CodeReviewComments) and Peter Bourgon's [Go: Best Practices for Production Environments](http://peter.bourgon.org/go-in-production/#formatting-and-style)
## Linting and formatting
We enforce strict `gofmt` formating and use some linters on our codebase. You can find the current list of linters at https://github.com/grafana/grafana/blob/master/scripts/backend-lint.sh
We enforce strict `gofmt` formating and use some linters on our codebase. You can lint the codebase with <akefile -
```bash
$ make lint-go
```
We use [revive](https://github.com/mgechev/revive) as a go linter, and do enforce our [custom config](https://github.com/grafana/grafana/blob/master/conf/revive.toml) for it.
+1 -1
View File
@@ -65,7 +65,7 @@ func TestDSRouteRule(t *testing.T) {
},
}
setting.SecretKey = "password"
setting.SecretKey = "password" //nolint:goconst
key, _ := util.Encrypt([]byte("123"), "password")
ds := &m.DataSource{
+9 -7
View File
@@ -28,6 +28,8 @@ func (assert *Assert) True(value bool, message string) {
func TestFirst(t *testing.T) {
anton := "anton"
street42 := "Street 42"
assert := NewAssert(t)
testJSON := `{
@@ -68,19 +70,19 @@ func TestFirst(t *testing.T) {
assert.True(err == nil, "failed to create json from string")
s, err := j.GetString("name")
assert.True(s == "anton" && err == nil, "name should be a string")
assert.True(s == anton && err == nil, "name should be a string")
s = j.MustGetString("name", "fallback")
assert.True(s == "anton", "must get string")
assert.True(s == anton, "must get string")
s = j.MustGetString("adsasdas", "fallback")
assert.True(s == "fallback", "must get string return fallback")
s, err = j.GetString("name")
assert.True(s == "anton" && err == nil, "name should match")
assert.True(s == anton && err == nil, "name should match")
s, err = j.GetString("address", "street")
assert.True(s == "Street 42" && err == nil, "street should match")
assert.True(s == street42 && err == nil, "street should match")
//log.Println("s: ", s.String())
_, err = j.GetNumber("age")
@@ -109,13 +111,13 @@ func TestFirst(t *testing.T) {
//log.Println("address: ", address)
s, err = address.GetString("street")
assert.True(s == "Street 42" && err == nil, "street mismatching")
assert.True(s == street42 && err == nil, "street mismatching")
addressAsString, err := j.GetString("address")
assert.True(addressAsString == "" && err != nil, "address should not be an string")
s, err = j.GetString("address", "street")
assert.True(s == "Street 42" && err == nil, "street mismatching")
assert.True(s == street42 && err == nil, "street mismatching")
s, err = j.GetString("address", "name2")
assert.True(s == "" && err != nil, "nonexistent string fail")
@@ -153,7 +155,7 @@ func TestFirst(t *testing.T) {
assert.True(err == nil, "create element fail")
s, err = element.GetString("street")
assert.True(s == "Street 42" && err == nil, "second fail")
assert.True(s == street42 && err == nil, "second fail")
}
obj, err := j.GetObject("country")
+2 -1
View File
@@ -90,7 +90,8 @@ func NewOAuthService() {
// handle the clients that do not properly support Basic auth headers and require passing client_id/client_secret via POST payload
if info.SendClientCredentialsViaPost {
oauth2.RegisterBrokenAuthHeaderProvider(info.TokenUrl)
// TODO: Fix the staticcheck error
oauth2.RegisterBrokenAuthHeaderProvider(info.TokenUrl) //nolint:staticcheck
}
if name == "grafananet" {
+7 -4
View File
@@ -10,6 +10,9 @@ import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/macaron.v1"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/remotecache"
@@ -18,11 +21,11 @@ import (
"github.com/grafana/grafana/pkg/services/login"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"gopkg.in/macaron.v1"
)
const errorTemplate = "error-template"
func mockGetTime() {
var timeSeed int64
getTime = func() time.Time {
@@ -37,7 +40,7 @@ func resetGetTime() {
}
func TestMiddleWareSecurityHeaders(t *testing.T) {
setting.ERR_TEMPLATE_NAME = "error-template"
setting.ERR_TEMPLATE_NAME = errorTemplate
Convey("Given the grafana middleware", t, func() {
@@ -70,7 +73,7 @@ func TestMiddleWareSecurityHeaders(t *testing.T) {
}
func TestMiddlewareContext(t *testing.T) {
setting.ERR_TEMPLATE_NAME = "error-template"
setting.ERR_TEMPLATE_NAME = errorTemplate
Convey("Given the grafana middleware", t, func() {
middlewareScenario(t, "middleware should add context to injector", func(sc *scenarioContext) {
+1
View File
@@ -11,6 +11,7 @@ import (
"github.com/grafana/grafana/pkg/util"
)
//nolint:goconst
func TestDataSourceCache(t *testing.T) {
Convey("When caching a datasource proxy", t, func() {
clearCache()
@@ -77,6 +77,7 @@ func TestWhenAlertManagerShouldNotify(t *testing.T) {
}
}
//nolint:goconst
func TestAlertmanagerNotifier(t *testing.T) {
Convey("Alertmanager notifier tests", t, func() {
+2 -1
View File
@@ -158,7 +158,6 @@ func (dn *DiscordNotifier) Notify(evalContext *alerting.EvalContext) error {
func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath string, existingJSONBody []byte) error {
f, err := os.Open(imagePath)
defer f.Close()
if err != nil {
if os.IsNotExist(err) {
cmd.Body = string(existingJSONBody)
@@ -169,6 +168,8 @@ func (dn *DiscordNotifier) embedImage(cmd *models.SendWebhookSync, imagePath str
}
}
defer f.Close()
var b bytes.Buffer
w := multipart.NewWriter(&b)
@@ -8,6 +8,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
)
//nolint:goconst
func TestHipChatNotifier(t *testing.T) {
Convey("HipChat notifier tests", t, func() {
@@ -29,9 +30,8 @@ func TestHipChatNotifier(t *testing.T) {
Convey("from settings", func() {
json := `
{
"url": "http://google.com"
"url": "http://google.com"
}`
settingsJSON, _ := simplejson.NewJson([]byte(json))
model := &models.AlertNotification{
Name: "ops",
@@ -26,6 +26,7 @@ func TestSlackNotifier(t *testing.T) {
So(err, ShouldNotBeNil)
})
//nolint:goconst
Convey("from settings", func() {
json := `
{
@@ -53,7 +53,9 @@ func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
// pollChanges periodically runs startWalkingDisk based on interval specified in the config.
func (fr *fileReader) pollChanges(ctx context.Context) {
ticker := time.Tick(time.Duration(int64(time.Second) * fr.Cfg.UpdateIntervalSeconds))
// TODO: Fix the staticcheck error
ticker := time.Tick(time.Duration(int64(time.Second) * fr.Cfg.UpdateIntervalSeconds)) //nolint:staticcheck
for {
select {
case <-ticker:
+1
View File
@@ -12,6 +12,7 @@ import (
m "github.com/grafana/grafana/pkg/models"
)
//nolint:goconst
func TestUserAuth(t *testing.T) {
InitTestDB(t)
@@ -17,6 +17,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
)
//nolint:goconst
func TestClient(t *testing.T) {
Convey("Test elasticsearch client", t, func() {
Convey("NewClient", func() {