Upgrade golangci-lint and fixes some linting errors. (#22909)

Example: https://play.golang.org/p/cfPIPG3BwjJ
This commit is contained in:
Carl Bergquist
2020-03-23 13:37:53 +01:00
committed by GitHub
parent c82a778c3d
commit 3798ac903d
17 changed files with 131 additions and 41 deletions
+17 -13
View File
@@ -2,6 +2,7 @@ package api
import (
"encoding/hex"
"errors"
"net/url"
"strings"
@@ -14,6 +15,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
"github.com/grafana/grafana/pkg/util/errutil"
)
const (
@@ -85,7 +87,12 @@ func (hs *HTTPServer) LoginView(c *models.ReqContext) {
if c.IsSignedIn {
// Assign login token to auth proxy users if enable_login_token = true
if setting.AuthProxyEnabled && setting.AuthProxyEnableLoginToken {
hs.loginAuthProxyUser(c)
user := &models.User{Id: c.SignedInUser.UserId, Email: c.SignedInUser.Email, Login: c.SignedInUser.Login}
err := hs.loginUserWithUser(user, c)
if err != nil {
c.Handle(500, "Failed to sign in user", err)
return
}
}
if redirectTo, _ := url.QueryUnescape(c.GetCookie("redirect_to")); len(redirectTo) > 0 {
@@ -107,14 +114,6 @@ func (hs *HTTPServer) LoginView(c *models.ReqContext) {
c.HTML(200, getViewIndex(), viewData)
}
func (hs *HTTPServer) loginAuthProxyUser(c *models.ReqContext) {
hs.loginUserWithUser(&models.User{
Id: c.SignedInUser.UserId,
Email: c.SignedInUser.Email,
Login: c.SignedInUser.Login,
}, c)
}
func tryOAuthAutoLogin(c *models.ReqContext) bool {
if !setting.OAuthAutoLogin {
return false
@@ -171,7 +170,10 @@ func (hs *HTTPServer) LoginPost(c *models.ReqContext, cmd dtos.LoginCommand) Res
user := authQuery.User
hs.loginUserWithUser(user, c)
err := hs.loginUserWithUser(user, c)
if err != nil {
return Error(500, "Error while signing in user", err)
}
result := map[string]interface{}{
"message": "Logged in",
@@ -190,17 +192,19 @@ func (hs *HTTPServer) LoginPost(c *models.ReqContext, cmd dtos.LoginCommand) Res
return JSON(200, result)
}
func (hs *HTTPServer) loginUserWithUser(user *models.User, c *models.ReqContext) {
func (hs *HTTPServer) loginUserWithUser(user *models.User, c *models.ReqContext) error {
if user == nil {
hs.log.Error("user login with nil user")
return errors.New("could not login user")
}
userToken, err := hs.AuthTokenService.CreateToken(c.Req.Context(), user.Id, c.RemoteAddr(), c.Req.UserAgent())
if err != nil {
hs.log.Error("failed to create auth token", "error", err)
return errutil.Wrap("failed to create auth token", err)
}
hs.log.Info("Successful Login", "User", user.Email)
middleware.WriteSessionCookie(c, userToken.UnhashedToken, hs.Cfg.LoginMaxLifetimeDays)
return nil
}
func (hs *HTTPServer) Logout(c *models.ReqContext) {
+5 -1
View File
@@ -218,7 +218,11 @@ func (hs *HTTPServer) OAuthLogin(ctx *models.ReqContext) {
}
// login
hs.loginUserWithUser(cmd.Result, ctx)
err = hs.loginUserWithUser(cmd.Result, ctx)
if err != nil {
hs.redirectWithError(ctx, err)
return
}
metrics.MApiLoginOAuth.Inc()
+4 -1
View File
@@ -197,7 +197,10 @@ func (hs *HTTPServer) CompleteInvite(c *models.ReqContext, completeInvite dtos.C
return rsp
}
hs.loginUserWithUser(user, c)
err := hs.loginUserWithUser(user, c)
if err != nil {
return Error(500, "failed to accept invite", err)
}
metrics.MApiUserSignUpCompleted.Inc()
metrics.MApiUserSignUpInvite.Inc()
+5 -1
View File
@@ -117,7 +117,11 @@ func (hs *HTTPServer) SignUpStep2(c *models.ReqContext, form dtos.SignUpStep2For
apiResponse["code"] = "redirect-to-select-org"
}
hs.loginUserWithUser(user, c)
err := hs.loginUserWithUser(user, c)
if err != nil {
return Error(500, "failed to login user", err)
}
metrics.MApiUserSignUpCompleted.Inc()
return JSON(200, apiResponse)
+7 -5
View File
@@ -36,7 +36,7 @@ func TestNotificationService(t *testing.T) {
require.Equalf(t, 1, scenarioCtx.renderCount, "expected render to be called, but wasn't")
require.Equalf(t, 1, scenarioCtx.imageUploadCount, "expected image to be uploaded, but wasn't")
require.Truef(t, evalCtx.Ctx.Value("notificationSent").(bool), "expected notification to be sent, but wasn't")
require.Truef(t, evalCtx.Ctx.Value(notificationSent{}).(bool), "expected notification to be sent, but wasn't")
})
notificationServiceScenario(t, "Given alert rule with upload image disabled should not render and upload image, but send notification", evalCtx, false, func(scenarioCtx *scenarioContext) {
@@ -45,7 +45,7 @@ func TestNotificationService(t *testing.T) {
require.Equalf(t, 0, scenarioCtx.renderCount, "expected render not to be called, but it was")
require.Equalf(t, 0, scenarioCtx.imageUploadCount, "expected image not to be uploaded, but it was")
require.Truef(t, evalCtx.Ctx.Value("notificationSent").(bool), "expected notification to be sent, but wasn't")
require.Truef(t, evalCtx.Ctx.Value(notificationSent{}).(bool), "expected notification to be sent, but wasn't")
})
notificationServiceScenario(t, "Given alert rule with upload image enabled and render times out should send notification", evalCtx, true, func(scenarioCtx *scenarioContext) {
@@ -74,7 +74,7 @@ func TestNotificationService(t *testing.T) {
require.Equalf(t, 0, scenarioCtx.renderCount, "expected render not to be called, but it was")
require.Equalf(t, 0, scenarioCtx.imageUploadCount, "expected image not to be uploaded, but it was")
require.Truef(t, evalCtx.Ctx.Value("notificationSent").(bool), "expected notification to be sent, but wasn't")
require.Truef(t, evalCtx.Ctx.Value(notificationSent{}).(bool), "expected notification to be sent, but wasn't")
})
notificationServiceScenario(t, "Given alert rule with upload image enabled and upload times out should send notification", evalCtx, true, func(scenarioCtx *scenarioContext) {
@@ -103,7 +103,7 @@ func TestNotificationService(t *testing.T) {
require.Equalf(t, 1, scenarioCtx.renderCount, "expected render to be called, but wasn't")
require.Equalf(t, 0, scenarioCtx.imageUploadCount, "expected image not to be uploaded, but it was")
require.Truef(t, evalCtx.Ctx.Value("notificationSent").(bool), "expected notification to be sent, but wasn't")
require.Truef(t, evalCtx.Ctx.Value(notificationSent{}).(bool), "expected notification to be sent, but wasn't")
})
}
@@ -244,8 +244,10 @@ func newTestNotifier(model *models.AlertNotification) (Notifier, error) {
}, nil
}
type notificationSent struct{}
func (n *testNotifier) Notify(evalCtx *EvalContext) error {
evalCtx.Ctx = context.WithValue(evalCtx.Ctx, "notificationSent", true)
evalCtx.Ctx = context.WithValue(evalCtx.Ctx, notificationSent{}, true)
return nil
}
+1 -1
View File
@@ -28,7 +28,7 @@ func newSession() *DBSession {
}
func startSession(ctx context.Context, engine *xorm.Engine, beginTran bool) (*DBSession, error) {
value := ctx.Value(ContextSessionName)
value := ctx.Value(ContextSessionKey{})
var sess *DBSession
sess, ok := value.(*DBSession)
+2 -1
View File
@@ -35,7 +35,8 @@ var (
sqlog log.Logger = log.New("sqlstore")
)
const ContextSessionName = "db-session"
// ContextSessionKey is used as key to save values in `context.Context`
type ContextSessionKey struct{}
func init() {
// This change will make xorm use an empty default schema for postgres and
+1 -1
View File
@@ -22,7 +22,7 @@ func (ss *SqlStore) InTransaction(ctx context.Context, fn func(ctx context.Conte
func (ss *SqlStore) inTransactionWithRetry(ctx context.Context, fn func(ctx context.Context) error, retry int) error {
return inTransactionWithRetryCtx(ctx, ss.engine, func(sess *DBSession) error {
withValue := context.WithValue(ctx, ContextSessionName, sess)
withValue := context.WithValue(ctx, ContextSessionKey{}, sess)
return fn(withValue)
}, retry)
}
+1 -1
View File
@@ -15,7 +15,7 @@ var ErrProvokedError = errors.New("testing error")
func TestTransaction(t *testing.T) {
ss := InitTestDB(t)
Convey("InTransaction asdf asdf", t, func() {
Convey("InTransaction", t, func() {
cmd := &models.AddApiKeyCommand{Key: "secret-key", Name: "key", OrgId: 1}
err := AddApiKey(cmd)
+2 -2
View File
@@ -455,8 +455,8 @@ func TestUserDataAccess(t *testing.T) {
var err error
createUserCmd := &models.CreateUserCommand{
Email: fmt.Sprint("admin", "@test.com"),
Name: fmt.Sprint("admin"),
Login: fmt.Sprint("admin"),
Name: "admin",
Login: "admin",
IsAdmin: true,
}
err = CreateUser(context.Background(), createUserCmd)
+1 -1
View File
@@ -507,7 +507,7 @@ func (cfg *Cfg) loadConfiguration(args *CommandLineArgs) (*ini.File, error) {
// load defaults
parsedFile, err := ini.Load(defaultConfigFile)
if err != nil {
fmt.Println(fmt.Sprintf("Failed to parse defaults.ini, %v", err))
fmt.Printf("Failed to parse defaults.ini, %v\n", err)
os.Exit(1)
return nil, err
}
+1 -2
View File
@@ -1,7 +1,6 @@
package cloudwatch
import (
"fmt"
"os"
"testing"
@@ -25,7 +24,7 @@ func TestECSCredProvider(t *testing.T) {
So(ecsProvider, ShouldNotBeNil)
So(ok, ShouldBeTrue)
So(ecsProvider.Client.Endpoint, ShouldEqual, fmt.Sprintf("http://169.254.170.2/abc/123"))
So(ecsProvider.Client.Endpoint, ShouldEqual, "http://169.254.170.2/abc/123")
})
}
+1 -5
View File
@@ -56,11 +56,7 @@ func (e *CloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo
responsesByRefID := make(map[string][]*cloudwatchResponse)
for _, res := range cloudwatchResponses {
if _, ok := responsesByRefID[res.RefId]; ok {
responsesByRefID[res.RefId] = append(responsesByRefID[res.RefId], res)
} else {
responsesByRefID[res.RefId] = []*cloudwatchResponse{res}
}
responsesByRefID[res.RefId] = append(responsesByRefID[res.RefId], res)
}
for refID, responses := range responsesByRefID {