From 699d3d60607537b96eaf0e6e74cb5c7699d12451 Mon Sep 17 00:00:00 2001 From: konrad Date: Sun, 11 Oct 2020 20:10:03 +0000 Subject: [PATCH] Add Golangci Lint (#676) Increase golangci timeout Fix installing golangci-lint in ci Remove mage targets replaced by golangci Run golint in ci Add goheader linter Enable & fix more linters Fix lint issues Add mage target to automagically fix issues found by golangci golangci-lint run --fix Add golangci config Add golangci mage target Co-authored-by: kolaente Reviewed-on: https://kolaente.dev/vikunja/api/pulls/676 Co-Authored-By: konrad Co-Committed-By: konrad --- .drone1.yml | 11 +-- .golangci.yml | 73 +++++++++++++++ code-header-template.txt | 15 ++++ go.mod | 1 + magefile.go | 89 +++---------------- pkg/caldav/caldav.go | 7 +- pkg/caldav/caldav_test.go | 5 +- pkg/cmd/cmd.go | 3 +- pkg/cmd/dump.go | 3 +- pkg/cmd/user.go | 13 +-- pkg/cmd/version.go | 5 +- pkg/cmd/web.go | 9 +- pkg/db/db.go | 16 ++-- pkg/db/test.go | 7 +- pkg/db/test_fixtures.go | 7 +- pkg/files/error.go | 6 +- pkg/files/filehandling.go | 7 +- pkg/files/files.go | 7 +- pkg/files/files_test.go | 3 +- pkg/integrations/archived_test.go | 5 +- pkg/integrations/integrations.go | 13 +-- pkg/integrations/kanban_test.go | 3 +- pkg/integrations/link_sharing_test.go | 5 +- pkg/integrations/list_test.go | 5 +- pkg/integrations/login_test.go | 5 +- pkg/integrations/register_test.go | 5 +- pkg/integrations/task_collection_test.go | 5 +- pkg/integrations/task_comment_test.go | 3 +- pkg/integrations/task_test.go | 3 +- pkg/integrations/token_test.go | 5 +- pkg/integrations/user_change_password_test.go | 5 +- pkg/integrations/user_confirm_email_test.go | 5 +- pkg/integrations/user_list_test.go | 5 +- .../user_password_request_token_test.go | 5 +- pkg/integrations/user_password_reset_test.go | 5 +- pkg/integrations/user_show_test.go | 5 +- pkg/log/logging.go | 8 +- pkg/log/xorm_logger.go | 5 +- pkg/mail/mail.go | 5 +- pkg/mail/send_mail.go | 3 +- pkg/metrics/active_users.go | 5 +- pkg/migration/20200420215928.go | 1 + pkg/migration/20200516123847.go | 5 +- pkg/migration/20200621214452.go | 5 +- pkg/migration/20200906184746.go | 3 +- pkg/migration/migration.go | 5 +- pkg/models/bulk_task_test.go | 3 +- pkg/models/error.go | 5 +- pkg/models/kanban.go | 3 +- pkg/models/kanban_test.go | 3 +- pkg/models/label.go | 3 +- pkg/models/label_task.go | 3 +- pkg/models/label_task_test.go | 7 +- pkg/models/label_test.go | 7 +- pkg/models/link_sharing.go | 3 +- pkg/models/list.go | 7 +- pkg/models/list_duplicate.go | 1 + pkg/models/list_duplicate_test.go | 3 +- pkg/models/list_team.go | 3 +- pkg/models/list_team_test.go | 9 +- pkg/models/list_test.go | 5 +- pkg/models/list_users.go | 3 +- pkg/models/list_users_rights_test.go | 5 +- pkg/models/list_users_test.go | 7 +- pkg/models/main_test.go | 7 +- pkg/models/models.go | 3 +- pkg/models/namespace.go | 5 +- pkg/models/namespace_team.go | 3 +- pkg/models/namespace_team_rights_test.go | 5 +- pkg/models/namespace_team_test.go | 9 +- pkg/models/namespace_test.go | 3 +- pkg/models/namespace_users.go | 3 +- pkg/models/namespace_users_rights_test.go | 5 +- pkg/models/namespace_users_test.go | 9 +- pkg/models/saved_filters.go | 3 +- pkg/models/saved_filters_test.go | 3 +- pkg/models/task_assignees.go | 3 +- pkg/models/task_attachment.go | 5 +- pkg/models/task_attachment_test.go | 9 +- pkg/models/task_collection_filter.go | 7 +- pkg/models/task_collection_sort_test.go | 3 +- pkg/models/task_collection_test.go | 5 +- pkg/models/task_comments.go | 3 +- pkg/models/task_comments_test.go | 3 +- pkg/models/task_relation.go | 5 +- pkg/models/task_relation_test.go | 3 +- pkg/models/tasks.go | 21 +++-- pkg/models/tasks_rights.go | 1 - pkg/models/tasks_test.go | 5 +- pkg/models/team_members_test.go | 3 +- pkg/models/teams.go | 3 +- pkg/models/teams_rights_test.go | 5 +- pkg/models/teams_test.go | 5 +- pkg/models/users_list_test.go | 3 +- pkg/modules/avatar/gravatar/gravatar.go | 17 ++-- pkg/modules/avatar/initials/initials.go | 31 +++---- pkg/modules/avatar/upload/upload.go | 9 +- pkg/modules/background/handler/background.go | 9 +- pkg/modules/background/unsplash/proxy.go | 13 ++- pkg/modules/background/unsplash/unsplash.go | 31 ++++--- pkg/modules/background/upload/upload.go | 3 +- pkg/modules/dump/dump.go | 9 +- pkg/modules/dump/restore.go | 13 +-- pkg/modules/keyvalue/memory/memory.go | 3 +- pkg/modules/keyvalue/redis/redis.go | 7 +- .../migration/create_from_structure.go | 3 +- .../migration/create_from_structure_test.go | 3 +- pkg/modules/migration/handler/handler.go | 3 +- pkg/modules/migration/main_test.go | 5 +- pkg/modules/migration/migration_status.go | 3 +- pkg/modules/migration/todoist/todoist.go | 25 ++++-- pkg/modules/migration/todoist/todoist_test.go | 10 +-- .../migration/wunderlist/wunderlist.go | 36 +++++--- .../migration/wunderlist/wunderlist_test.go | 9 +- pkg/routes/api/v1/auth.go | 5 +- pkg/routes/api/v1/avatar.go | 7 +- pkg/routes/api/v1/docs.go | 3 +- pkg/routes/api/v1/info.go | 3 +- pkg/routes/api/v1/link_sharing_auth.go | 3 +- pkg/routes/api/v1/list_by_namespace.go | 5 +- pkg/routes/api/v1/login.go | 5 +- pkg/routes/api/v1/task_attachment.go | 3 +- pkg/routes/api/v1/token_check.go | 5 +- pkg/routes/api/v1/user_confirm_email.go | 5 +- pkg/routes/api/v1/user_list.go | 5 +- pkg/routes/api/v1/user_password_reset.go | 7 +- pkg/routes/api/v1/user_register.go | 7 +- pkg/routes/api/v1/user_settings.go | 3 +- pkg/routes/api/v1/user_show.go | 3 +- pkg/routes/api/v1/user_totp.go | 7 +- pkg/routes/api/v1/user_update_email.go | 5 +- pkg/routes/api/v1/user_update_password.go | 5 +- pkg/routes/caldav/handler.go | 15 ++-- pkg/routes/caldav/listStorageProvider.go | 19 ++-- pkg/routes/caldav/parsing.go | 5 +- pkg/routes/rate_limit.go | 7 +- pkg/routes/routes.go | 5 +- pkg/user/error.go | 3 +- pkg/user/totp.go | 5 +- pkg/user/user.go | 11 +-- pkg/user/user_email_confirm_test.go | 3 +- pkg/user/user_test.go | 3 +- pkg/utils/random_string_test.go | 3 +- 143 files changed, 630 insertions(+), 426 deletions(-) create mode 100644 .golangci.yml create mode 100644 code-header-template.txt diff --git a/.drone1.yml b/.drone1.yml index 2d71aff18..7c70d9757 100644 --- a/.drone1.yml +++ b/.drone1.yml @@ -83,16 +83,9 @@ steps: depends_on: [ build ] commands: - ./mage-static build:generate - - ./mage-static check:lint - - ./mage-static check:fmt - ./mage-static check:got-swag - - ./mage-static check:ineffassign - - ./mage-static check:misspell - - ./mage-static check:goconst - - ./mage-static check:gocyclo - - ./mage-static check:static - - wget -O - -q https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $GOPATH/bin v2.2.0 # Need to manually install as it does not support being installed via go modules like the rest. - - ./mage-static check:gosec + - wget -O - -q https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.31.0 + - ./mage-static check:golangci when: event: [ push, tag, pull_request ] diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..636f47c79 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,73 @@ +run: + timeout: 5m + tests: true + +linters: + enable: + - megacheck + - govet + - goconst + - gocritic + - gocyclo + - goerr113 + - goheader + - gofmt + - goimports + - golint + - misspell + disable: + - scopelint # Obsolete, using exportloopref instead + presets: + - bugs + - unused + fast: false + +linter-settings: + nestif: + min-complexity: 6 + goheader: + template-path: code-hesader-template.txt + +issues: + exclude-rules: + # Exclude some linters from running on tests files. + - path: _test\.go + linters: + - gocyclo + - deadcode + - path: pkg/integrations/* + linters: + - gocyclo + - deadcode + - varcheck + - unparam + - bodyclose + - path: pkg/integrations/* + text: "unlambda" + linters: + - gocritic + - path: pkg/modules/background/unsplash/unsplash\.go + linters: + - bodyclose + - path: pkg/migration/* + linters: + - exhaustive + - goconst + - goerr113 + - path: pkg/models/task_collection_filter\.go + linters: + - exhaustive + - path: pkg/utils/random_string\.go + text: "G404:" # We don't care about cryptographically secure randomness when we're using that utility function. + linters: + - gosec + - path: pkg/modules/dump/* + linters: + - goerr113 + - path: pkg/ + text: "err113: do not define dynamic errors, use wrapped static errors instead:" + linters: + - goerr113 + - text: "commentFormatting: put a space between `//` and comment text" + linters: + - gocritic diff --git a/code-header-template.txt b/code-header-template.txt new file mode 100644 index 000000000..0f4a873b1 --- /dev/null +++ b/code-header-template.txt @@ -0,0 +1,15 @@ +Vikunja is a to-do list application to facilitate your life. +Copyright 2018-2020 Vikunja and contributors. All rights reserved. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . diff --git a/go.mod b/go.mod index 2d83975b7..981989457 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/fzipp/gocyclo v0.0.0-20150627053110-6acd4345c835 github.com/gabriel-vasile/mimetype v1.1.1 github.com/getsentry/sentry-go v0.7.0 + github.com/go-errors/errors v1.0.1 github.com/go-redis/redis/v7 v7.4.0 github.com/go-sql-driver/mysql v1.5.0 github.com/go-testfixtures/testfixtures/v3 v3.4.0 diff --git a/magefile.go b/magefile.go index effc1e37a..4401988a7 100644 --- a/magefile.go +++ b/magefile.go @@ -59,7 +59,6 @@ var ( Aliases = map[string]interface{}{ "build": Build.Build, "do-the-swag": DoTheSwag, - "check:go-sec": Check.GoSec, "check:got-swag": Check.GotSwag, "release:os-package": Release.OsPackage, "dev:create-migration": Dev.CreateMigration, @@ -330,34 +329,6 @@ func (Test) Integration() { type Check mg.Namespace -// Checks if the code is properly formatted with go fmt -func (Check) Fmt() error { - mg.Deps(initVars) - args := append([]string{"-s", "-d"}, GoFiles...) - c := exec.Command("gofmt", args...) - out, err := c.Output() - if err != nil { - return err - } - - if len(out) > 0 { - fmt.Println("Code is not properly gofmt'ed.") - fmt.Println("Please run 'mage fmt' and commit the result:") - fmt.Print(string(out)) - os.Exit(1) - } - - return nil -} - -// Runs golint on all packages -func (Check) Lint() { - mg.Deps(initVars) - checkAndInstallGoTool("golint", "golang.org/x/lint/golint") - args := append([]string{"-set_exit_status"}, ApiPackages...) - runAndStreamOutput("golint", args...) -} - // Checks if the swagger docs need to be re-generated from the code annotations func (Check) GotSwag() { mg.Deps(initVars) @@ -388,65 +359,31 @@ func (Check) GotSwag() { } } -// Checks the source code for misspellings -func (Check) Misspell() { +func checkGolangCiLintInstalled() { mg.Deps(initVars) - checkAndInstallGoTool("misspell", "github.com/client9/misspell/cmd/misspell") - runAndStreamOutput("misspell", append([]string{"-error"}, GoFiles...)...) -} - -// Checks the source code for ineffectual assigns -func (Check) Ineffassign() { - mg.Deps(initVars) - checkAndInstallGoTool("ineffassign", "github.com/gordonklaus/ineffassign") - runAndStreamOutput("ineffassign", GoFiles...) -} - -// Checks for the cyclomatic complexity of the source code -func (Check) Gocyclo() { - mg.Deps(initVars) - checkAndInstallGoTool("gocyclo", "github.com/fzipp/gocyclo") - runAndStreamOutput("gocyclo", append([]string{"-over", "49"}, GoFiles...)...) -} - -// Statically analyzes the source code about a range of different problems -func (Check) Static() { - mg.Deps(initVars) - checkAndInstallGoTool("staticcheck", "honnef.co/go/tools/cmd/staticcheck") - runAndStreamOutput("staticcheck", ApiPackages...) -} - -// Checks the source code for potential security issues -func (Check) GoSec() { - mg.Deps(initVars) - if err := exec.Command("gosec").Run(); err != nil && strings.Contains(err.Error(), "executable file not found") { - fmt.Println("Please manually install gosec by running") - fmt.Println("curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | bash -s -- -b $GOPATH/bin v2.2.0") + if err := exec.Command("golangci-lint").Run(); err != nil && strings.Contains(err.Error(), "executable file not found") { + fmt.Println("Please manually install golangci-lint by running") + fmt.Println("curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.31.0") os.Exit(1) } - runAndStreamOutput("gosec", "./...") } -// Checks for repeated strings that could be replaced by a constant -func (Check) Goconst() { - mg.Deps(initVars) - checkAndInstallGoTool("goconst", "github.com/jgautheron/goconst/cmd/goconst") - runAndStreamOutput("goconst", ApiPackages...) +func (Check) Golangci() { + checkGolangCiLintInstalled() + runAndStreamOutput("golangci-lint", "run") +} + +func (Check) GolangciFix() { + checkGolangCiLintInstalled() + runAndStreamOutput("golangci-lint", "run", "--fix") } // Runs fmt-check, lint, got-swag, misspell-check, ineffasign-check, gocyclo-check, static-check, gosec-check, goconst-check all in parallel func (Check) All() { mg.Deps(initVars) mg.Deps( - Check.Fmt, - Check.Lint, + Check.Golangci, Check.GotSwag, - Check.Misspell, - Check.Ineffassign, - Check.Gocyclo, - Check.Static, - Check.GoSec, - Check.Goconst, ) } diff --git a/pkg/caldav/caldav.go b/pkg/caldav/caldav.go index 76577b91a..f58bb9c57 100644 --- a/pkg/caldav/caldav.go +++ b/pkg/caldav/caldav.go @@ -17,13 +17,14 @@ package caldav import ( - "code.vikunja.io/api/pkg/config" - "code.vikunja.io/api/pkg/user" - "code.vikunja.io/api/pkg/utils" "fmt" "strconv" "strings" "time" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/api/pkg/utils" ) // DateFormat ist the caldav date format diff --git a/pkg/caldav/caldav_test.go b/pkg/caldav/caldav_test.go index ffefd79dc..30bd5e4dc 100644 --- a/pkg/caldav/caldav_test.go +++ b/pkg/caldav/caldav_test.go @@ -17,10 +17,11 @@ package caldav import ( - "code.vikunja.io/api/pkg/config" - "github.com/stretchr/testify/assert" "testing" "time" + + "code.vikunja.io/api/pkg/config" + "github.com/stretchr/testify/assert" ) func TestParseEvents(t *testing.T) { diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index 0b742583d..eeccf2b57 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -18,8 +18,9 @@ package cmd import ( "fmt" - "github.com/spf13/cobra" "os" + + "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ diff --git a/pkg/cmd/dump.go b/pkg/cmd/dump.go index 235698bd5..c6fd6dcec 100644 --- a/pkg/cmd/dump.go +++ b/pkg/cmd/dump.go @@ -17,11 +17,12 @@ package cmd import ( + "time" + "code.vikunja.io/api/pkg/initialize" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/dump" "github.com/spf13/cobra" - "time" ) func init() { diff --git a/pkg/cmd/user.go b/pkg/cmd/user.go index db315295c..0c2c13a44 100644 --- a/pkg/cmd/user.go +++ b/pkg/cmd/user.go @@ -18,17 +18,18 @@ package cmd import ( - "code.vikunja.io/api/pkg/initialize" - "code.vikunja.io/api/pkg/log" - "code.vikunja.io/api/pkg/user" "fmt" - "github.com/olekukonko/tablewriter" - "github.com/spf13/cobra" - "golang.org/x/crypto/ssh/terminal" "os" "strconv" "strings" "time" + + "code.vikunja.io/api/pkg/initialize" + "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/user" + "github.com/olekukonko/tablewriter" + "github.com/spf13/cobra" + "golang.org/x/crypto/ssh/terminal" ) var ( diff --git a/pkg/cmd/version.go b/pkg/cmd/version.go index 9b691ebc6..ae7599509 100644 --- a/pkg/cmd/version.go +++ b/pkg/cmd/version.go @@ -17,10 +17,11 @@ package cmd import ( - "code.vikunja.io/api/pkg/version" "fmt" - "github.com/spf13/cobra" "runtime" + + "code.vikunja.io/api/pkg/version" + "github.com/spf13/cobra" ) func init() { diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index a727cebd8..194241555 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -17,17 +17,18 @@ package cmd import ( + "context" + "os" + "os/signal" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/initialize" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/routes" "code.vikunja.io/api/pkg/swagger" "code.vikunja.io/api/pkg/version" - "context" "github.com/spf13/cobra" - "os" - "os/signal" - "time" ) func init() { diff --git a/pkg/db/db.go b/pkg/db/db.go index 5224232a6..542e0cb6b 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -17,16 +17,17 @@ package db import ( - "code.vikunja.io/api/pkg/config" - "code.vikunja.io/api/pkg/log" "encoding/gob" "fmt" - xrc "gitea.com/xorm/xorm-redis-cache" "net/url" "os" "strconv" "strings" "time" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/log" + xrc "gitea.com/xorm/xorm-redis-cache" "xorm.io/core" "xorm.io/xorm" "xorm.io/xorm/caches" @@ -52,23 +53,24 @@ func CreateDBEngine() (engine *xorm.Engine, err error) { } // Use Mysql if set - if config.DatabaseType.GetString() == "mysql" { + switch config.DatabaseType.GetString() { + case "mysql": engine, err = initMysqlEngine() if err != nil { return } - } else if config.DatabaseType.GetString() == "postgres" { + case "postgres": engine, err = initPostgresEngine() if err != nil { return } - } else if config.DatabaseType.GetString() == "sqlite" { + case "sqlite": // Otherwise use sqlite engine, err = initSqliteEngine() if err != nil { return } - } else { + default: log.Fatalf("Unknown database type %s", config.DatabaseType.GetString()) } diff --git a/pkg/db/test.go b/pkg/db/test.go index 7ef1303e4..eb82c051c 100644 --- a/pkg/db/test.go +++ b/pkg/db/test.go @@ -18,12 +18,13 @@ package db import ( - "code.vikunja.io/api/pkg/config" - "code.vikunja.io/api/pkg/log" "fmt" - "github.com/stretchr/testify/assert" "os" "testing" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/log" + "github.com/stretchr/testify/assert" "xorm.io/core" "xorm.io/xorm" ) diff --git a/pkg/db/test_fixtures.go b/pkg/db/test_fixtures.go index eeccecedb..ea90aa339 100644 --- a/pkg/db/test_fixtures.go +++ b/pkg/db/test_fixtures.go @@ -18,12 +18,13 @@ package db import ( - "code.vikunja.io/api/pkg/config" "fmt" - "github.com/go-testfixtures/testfixtures/v3" - "github.com/stretchr/testify/assert" "path/filepath" "testing" + + "code.vikunja.io/api/pkg/config" + "github.com/go-testfixtures/testfixtures/v3" + "github.com/stretchr/testify/assert" "xorm.io/xorm/schemas" ) diff --git a/pkg/files/error.go b/pkg/files/error.go index 5dafe0d77..548fd057b 100644 --- a/pkg/files/error.go +++ b/pkg/files/error.go @@ -29,7 +29,7 @@ func (err ErrFileDoesNotExist) Error() string { return fmt.Sprintf("file %d does not exist", err.FileID) } -//IsErrFileDoesNotExist checks if an error is ErrFileDoesNotExist +// IsErrFileDoesNotExist checks if an error is ErrFileDoesNotExist func IsErrFileDoesNotExist(err error) bool { _, ok := err.(ErrFileDoesNotExist) return ok @@ -45,7 +45,7 @@ func (err ErrFileIsTooLarge) Error() string { return fmt.Sprintf("file is too large [Size: %d]", err.Size) } -//IsErrFileIsTooLarge checks if an error is ErrFileIsTooLarge +// IsErrFileIsTooLarge checks if an error is ErrFileIsTooLarge func IsErrFileIsTooLarge(err error) bool { _, ok := err.(ErrFileIsTooLarge) return ok @@ -62,7 +62,7 @@ func (err ErrFileIsNotUnsplashFile) Error() string { return fmt.Sprintf("file was not downloaded from unsplash [FileID: %d]", err.FileID) } -//IsErrFileIsNotUnsplashFile checks if an error is ErrFileIsNotUnsplashFile +// IsErrFileIsNotUnsplashFile checks if an error is ErrFileIsNotUnsplashFile func IsErrFileIsNotUnsplashFile(err error) bool { _, ok := err.(ErrFileIsNotUnsplashFile) return ok diff --git a/pkg/files/filehandling.go b/pkg/files/filehandling.go index 7d7158c00..5749211a6 100644 --- a/pkg/files/filehandling.go +++ b/pkg/files/filehandling.go @@ -17,13 +17,14 @@ package files import ( + "os" + "testing" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/log" "github.com/spf13/afero" "github.com/stretchr/testify/assert" - "os" - "testing" ) // This file handles storing and retrieving a file for different backends @@ -49,7 +50,7 @@ func initFixtures(t *testing.T) { InitTestFileFixtures(t) } -//InitTestFileFixtures initializes file fixtures +// InitTestFileFixtures initializes file fixtures func InitTestFileFixtures(t *testing.T) { // Init fixture files filename := config.FilesBasePath.GetString() + "/1" diff --git a/pkg/files/files.go b/pkg/files/files.go index 90763508a..9145f4c99 100644 --- a/pkg/files/files.go +++ b/pkg/files/files.go @@ -17,13 +17,14 @@ package files import ( + "io" + "strconv" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/web" "github.com/c2h5oh/datasize" "github.com/spf13/afero" - "io" - "strconv" - "time" ) // File holds all information about a file diff --git a/pkg/files/files_test.go b/pkg/files/files_test.go index 51adaa3d1..ea1bc474f 100644 --- a/pkg/files/files_test.go +++ b/pkg/files/files_test.go @@ -18,10 +18,11 @@ package files import ( - "github.com/stretchr/testify/assert" "io" "os" "testing" + + "github.com/stretchr/testify/assert" ) type testfile struct { diff --git a/pkg/integrations/archived_test.go b/pkg/integrations/archived_test.go index f1716dd14..9aab92f06 100644 --- a/pkg/integrations/archived_test.go +++ b/pkg/integrations/archived_test.go @@ -18,11 +18,12 @@ package integrations import ( + "net/url" + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/stretchr/testify/assert" - "net/url" - "testing" ) // This tests the following behaviour: diff --git a/pkg/integrations/integrations.go b/pkg/integrations/integrations.go index 3b0b31a78..a625c6f90 100644 --- a/pkg/integrations/integrations.go +++ b/pkg/integrations/integrations.go @@ -17,6 +17,13 @@ package integrations import ( + "net/http" + "net/http/httptest" + "net/url" + "os" + "strings" + "testing" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" @@ -29,12 +36,6 @@ import ( "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "net/http" - "net/http/httptest" - "net/url" - "os" - "strings" - "testing" ) // These are the test users, the same way they are in the test database diff --git a/pkg/integrations/kanban_test.go b/pkg/integrations/kanban_test.go index 3e6f912b5..abcd0b7d9 100644 --- a/pkg/integrations/kanban_test.go +++ b/pkg/integrations/kanban_test.go @@ -17,11 +17,12 @@ package integrations import ( + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "testing" ) func TestBucket(t *testing.T) { diff --git a/pkg/integrations/link_sharing_test.go b/pkg/integrations/link_sharing_test.go index 2cb613122..d36dbbbd6 100644 --- a/pkg/integrations/link_sharing_test.go +++ b/pkg/integrations/link_sharing_test.go @@ -17,12 +17,13 @@ package integrations import ( + "net/url" + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "net/url" - "testing" ) func TestLinkSharing(t *testing.T) { diff --git a/pkg/integrations/list_test.go b/pkg/integrations/list_test.go index a4486257d..066a5c6ca 100644 --- a/pkg/integrations/list_test.go +++ b/pkg/integrations/list_test.go @@ -17,12 +17,13 @@ package integrations import ( + "net/url" + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "net/url" - "testing" ) func TestList(t *testing.T) { diff --git a/pkg/integrations/login_test.go b/pkg/integrations/login_test.go index 83ec5dd63..11b3e7073 100644 --- a/pkg/integrations/login_test.go +++ b/pkg/integrations/login_test.go @@ -17,11 +17,12 @@ package integrations import ( + "net/http" + "testing" + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestLogin(t *testing.T) { diff --git a/pkg/integrations/register_test.go b/pkg/integrations/register_test.go index 10322ec72..c898982b0 100644 --- a/pkg/integrations/register_test.go +++ b/pkg/integrations/register_test.go @@ -17,11 +17,12 @@ package integrations import ( + "net/http" + "testing" + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestRegister(t *testing.T) { diff --git a/pkg/integrations/task_collection_test.go b/pkg/integrations/task_collection_test.go index 8a8234332..73b15a131 100644 --- a/pkg/integrations/task_collection_test.go +++ b/pkg/integrations/task_collection_test.go @@ -18,11 +18,12 @@ package integrations import ( + "net/url" + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/stretchr/testify/assert" - "net/url" - "testing" ) func TestTaskCollection(t *testing.T) { diff --git a/pkg/integrations/task_comment_test.go b/pkg/integrations/task_comment_test.go index e1a8bc5a1..11148086a 100644 --- a/pkg/integrations/task_comment_test.go +++ b/pkg/integrations/task_comment_test.go @@ -17,11 +17,12 @@ package integrations import ( + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "testing" ) func TestTaskComments(t *testing.T) { diff --git a/pkg/integrations/task_test.go b/pkg/integrations/task_test.go index 62dacf974..f9f88f29f 100644 --- a/pkg/integrations/task_test.go +++ b/pkg/integrations/task_test.go @@ -17,11 +17,12 @@ package integrations import ( + "testing" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "testing" ) func TestTask(t *testing.T) { diff --git a/pkg/integrations/token_test.go b/pkg/integrations/token_test.go index 34985e583..8ea0c797d 100644 --- a/pkg/integrations/token_test.go +++ b/pkg/integrations/token_test.go @@ -17,10 +17,11 @@ package integrations import ( - apiv1 "code.vikunja.io/api/pkg/routes/api/v1" - "github.com/stretchr/testify/assert" "net/http" "testing" + + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" + "github.com/stretchr/testify/assert" ) func TestCheckToken(t *testing.T) { diff --git a/pkg/integrations/user_change_password_test.go b/pkg/integrations/user_change_password_test.go index 7e3e1c4f3..e5dc34604 100644 --- a/pkg/integrations/user_change_password_test.go +++ b/pkg/integrations/user_change_password_test.go @@ -17,11 +17,12 @@ package integrations import ( + "net/http" + "testing" + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestUserChangePassword(t *testing.T) { diff --git a/pkg/integrations/user_confirm_email_test.go b/pkg/integrations/user_confirm_email_test.go index bd2396bfc..6f43dde38 100644 --- a/pkg/integrations/user_confirm_email_test.go +++ b/pkg/integrations/user_confirm_email_test.go @@ -17,12 +17,13 @@ package integrations import ( + "net/http" + "testing" + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" "code.vikunja.io/api/pkg/user" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestUserConfirmEmail(t *testing.T) { diff --git a/pkg/integrations/user_list_test.go b/pkg/integrations/user_list_test.go index d15134343..f83f3cc1c 100644 --- a/pkg/integrations/user_list_test.go +++ b/pkg/integrations/user_list_test.go @@ -17,10 +17,11 @@ package integrations import ( - apiv1 "code.vikunja.io/api/pkg/routes/api/v1" - "github.com/stretchr/testify/assert" "net/http" "testing" + + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" + "github.com/stretchr/testify/assert" ) func TestUserList(t *testing.T) { diff --git a/pkg/integrations/user_password_request_token_test.go b/pkg/integrations/user_password_request_token_test.go index 5eb00d304..a4498e45c 100644 --- a/pkg/integrations/user_password_request_token_test.go +++ b/pkg/integrations/user_password_request_token_test.go @@ -17,12 +17,13 @@ package integrations import ( + "net/http" + "testing" + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" "code.vikunja.io/api/pkg/user" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestUserRequestResetPasswordToken(t *testing.T) { diff --git a/pkg/integrations/user_password_reset_test.go b/pkg/integrations/user_password_reset_test.go index d183f3b91..db62acbba 100644 --- a/pkg/integrations/user_password_reset_test.go +++ b/pkg/integrations/user_password_reset_test.go @@ -17,12 +17,13 @@ package integrations import ( + "net/http" + "testing" + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" "code.vikunja.io/api/pkg/user" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" - "net/http" - "testing" ) func TestUserPasswordReset(t *testing.T) { diff --git a/pkg/integrations/user_show_test.go b/pkg/integrations/user_show_test.go index 7d1cb2355..799ac65d2 100644 --- a/pkg/integrations/user_show_test.go +++ b/pkg/integrations/user_show_test.go @@ -17,10 +17,11 @@ package integrations import ( - apiv1 "code.vikunja.io/api/pkg/routes/api/v1" - "github.com/stretchr/testify/assert" "net/http" "testing" + + apiv1 "code.vikunja.io/api/pkg/routes/api/v1" + "github.com/stretchr/testify/assert" ) func TestUserShow(t *testing.T) { diff --git a/pkg/log/logging.go b/pkg/log/logging.go index ee07d8525..5fd2598c8 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -17,13 +17,14 @@ package log import ( - "code.vikunja.io/api/pkg/config" - "github.com/op/go-logging" - "github.com/spf13/viper" "io" "os" "strings" "time" + + "code.vikunja.io/api/pkg/config" + "github.com/op/go-logging" + "github.com/spf13/viper" ) // ErrFmt holds the format for all the console logging @@ -105,7 +106,6 @@ func GetLogger() *logging.Logger { return logInstance } -///// // The following functions are to be used as an "eye-candy", so one can just write log.Error() instead of log.Log.Error() // Debug is for debug messages diff --git a/pkg/log/xorm_logger.go b/pkg/log/xorm_logger.go index f948118dd..e3eed5622 100644 --- a/pkg/log/xorm_logger.go +++ b/pkg/log/xorm_logger.go @@ -17,10 +17,11 @@ package log import ( - "code.vikunja.io/api/pkg/config" - "github.com/op/go-logging" "strings" "time" + + "code.vikunja.io/api/pkg/config" + "github.com/op/go-logging" "xorm.io/xorm/log" ) diff --git a/pkg/mail/mail.go b/pkg/mail/mail.go index 3cef9ba5d..4cbd0693b 100644 --- a/pkg/mail/mail.go +++ b/pkg/mail/mail.go @@ -17,11 +17,12 @@ package mail import ( + "crypto/tls" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" - "crypto/tls" "gopkg.in/gomail.v2" - "time" ) // Queue is the mail queue diff --git a/pkg/mail/send_mail.go b/pkg/mail/send_mail.go index 536c97d13..f0357547f 100644 --- a/pkg/mail/send_mail.go +++ b/pkg/mail/send_mail.go @@ -18,13 +18,14 @@ package mail import ( "bytes" + "html/template" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/static" "code.vikunja.io/api/pkg/utils" "github.com/shurcooL/httpfs/html/vfstemplate" "gopkg.in/gomail.v2" - "html/template" ) // Opts holds infos for a mail diff --git a/pkg/metrics/active_users.go b/pkg/metrics/active_users.go index 328be356d..4aeddae4f 100644 --- a/pkg/metrics/active_users.go +++ b/pkg/metrics/active_users.go @@ -17,13 +17,14 @@ package metrics import ( + "sync" + "time" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/keyvalue" "code.vikunja.io/web" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - "sync" - "time" ) // SecondsUntilInactive defines the seconds until a user is considered inactive diff --git a/pkg/migration/20200420215928.go b/pkg/migration/20200420215928.go index 9cf85a605..ffc05c954 100644 --- a/pkg/migration/20200420215928.go +++ b/pkg/migration/20200420215928.go @@ -18,6 +18,7 @@ package migration import ( "math" + "src.techknowlogick.com/xormigrate" "xorm.io/xorm" ) diff --git a/pkg/migration/20200516123847.go b/pkg/migration/20200516123847.go index e9eefb700..f9b9c5c88 100644 --- a/pkg/migration/20200516123847.go +++ b/pkg/migration/20200516123847.go @@ -17,8 +17,9 @@ package migration import ( - "src.techknowlogick.com/xormigrate" "strings" + + "src.techknowlogick.com/xormigrate" "xorm.io/xorm" ) @@ -55,7 +56,7 @@ func init() { // The general idea here is to take the title and slice it into pieces, until we found a unique piece. var exists = true - titleSlug := []rune(strings.Replace(strings.ToUpper(l.Title), " ", "", -1)) + titleSlug := []rune(strings.ReplaceAll(strings.ToUpper(l.Title), " ", "")) // We can save at most 10 characters in the db, so we need to ensure it has at most 10 characters if len(titleSlug) > 10 { diff --git a/pkg/migration/20200621214452.go b/pkg/migration/20200621214452.go index b0dc7f4c4..0e6666cdc 100644 --- a/pkg/migration/20200621214452.go +++ b/pkg/migration/20200621214452.go @@ -18,8 +18,9 @@ package migration import ( "fmt" - "src.techknowlogick.com/xormigrate" "strings" + + "src.techknowlogick.com/xormigrate" "xorm.io/xorm" "xorm.io/xorm/schemas" ) @@ -654,7 +655,7 @@ create unique index UQE_users_namespace_id // The statement is probably useless anyway since its only purpose is to clean up old tables // which may be leftovers from a previously failed migration. However, since the whole thing // is wrapped in sessions, this is extremely unlikely to happen anyway. - //"ALTER TABLE " + table + " DROP COLUMN IF EXISTS " + colTmp + ";", + // "ALTER TABLE " + table + " DROP COLUMN IF EXISTS " + colTmp + ";", "ALTER TABLE " + table + " ADD COLUMN " + colTmp + " DATETIME NULL;", // #nosec "UPDATE " + table + " SET " + colTmp + " = IF(" + colOld + " = 0, NULL, FROM_UNIXTIME(" + colOld + "));", diff --git a/pkg/migration/20200906184746.go b/pkg/migration/20200906184746.go index dfce801ea..a8317f1b6 100644 --- a/pkg/migration/20200906184746.go +++ b/pkg/migration/20200906184746.go @@ -17,9 +17,10 @@ package migration import ( + "time" + "code.vikunja.io/api/pkg/models" "src.techknowlogick.com/xormigrate" - "time" "xorm.io/xorm" ) diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index 6d268f5aa..3cb0c9f62 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -17,6 +17,9 @@ package migration import ( + "os" + "sort" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" @@ -25,8 +28,6 @@ import ( "code.vikunja.io/api/pkg/modules/migration" "code.vikunja.io/api/pkg/user" "github.com/olekukonko/tablewriter" - "os" - "sort" "src.techknowlogick.com/xormigrate" "xorm.io/xorm" ) diff --git a/pkg/models/bulk_task_test.go b/pkg/models/bulk_task_test.go index 58fc5bc97..1a02ed061 100644 --- a/pkg/models/bulk_task_test.go +++ b/pkg/models/bulk_task_test.go @@ -1,9 +1,10 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" - "testing" ) func TestBulkTask_Update(t *testing.T) { diff --git a/pkg/models/error.go b/pkg/models/error.go index f6786b57e..7e9ac9f96 100644 --- a/pkg/models/error.go +++ b/pkg/models/error.go @@ -17,10 +17,11 @@ package models import ( - "code.vikunja.io/api/pkg/config" - "code.vikunja.io/web" "fmt" "net/http" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/web" ) // Generic diff --git a/pkg/models/kanban.go b/pkg/models/kanban.go index 1f15be630..3242b8f8f 100644 --- a/pkg/models/kanban.go +++ b/pkg/models/kanban.go @@ -17,10 +17,11 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" "xorm.io/xorm" ) diff --git a/pkg/models/kanban_test.go b/pkg/models/kanban_test.go index cd1dc513b..a6ca3e645 100644 --- a/pkg/models/kanban_test.go +++ b/pkg/models/kanban_test.go @@ -17,10 +17,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestBucket_ReadAll(t *testing.T) { diff --git a/pkg/models/label.go b/pkg/models/label.go index a9a296931..414b4a2d8 100644 --- a/pkg/models/label.go +++ b/pkg/models/label.go @@ -17,9 +17,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" ) // Label represents a label diff --git a/pkg/models/label_task.go b/pkg/models/label_task.go index 86d2cb6e2..7d0614d66 100644 --- a/pkg/models/label_task.go +++ b/pkg/models/label_task.go @@ -17,9 +17,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" "xorm.io/builder" ) diff --git a/pkg/models/label_task_test.go b/pkg/models/label_task_test.go index 27f0348b8..3bcdf50ee 100644 --- a/pkg/models/label_task_test.go +++ b/pkg/models/label_task_test.go @@ -1,14 +1,15 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" - "gopkg.in/d4l3k/messagediff.v1" "reflect" "runtime" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "gopkg.in/d4l3k/messagediff.v1" + "code.vikunja.io/web" ) diff --git a/pkg/models/label_test.go b/pkg/models/label_test.go index c5d3dc38e..dc96b65e1 100644 --- a/pkg/models/label_test.go +++ b/pkg/models/label_test.go @@ -17,14 +17,15 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" - "gopkg.in/d4l3k/messagediff.v1" "reflect" "runtime" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "gopkg.in/d4l3k/messagediff.v1" + "code.vikunja.io/web" ) diff --git a/pkg/models/link_sharing.go b/pkg/models/link_sharing.go index 2849fb012..7fc85c7dc 100644 --- a/pkg/models/link_sharing.go +++ b/pkg/models/link_sharing.go @@ -18,11 +18,12 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/utils" "code.vikunja.io/web" "github.com/dgrijalva/jwt-go" - "time" ) // SharingType holds the sharing type diff --git a/pkg/models/list.go b/pkg/models/list.go index 52fb995ac..8c5b13cda 100644 --- a/pkg/models/list.go +++ b/pkg/models/list.go @@ -17,12 +17,13 @@ package models import ( + "strings" + "time" + "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "strings" - "time" "xorm.io/builder" "xorm.io/xorm" ) @@ -450,7 +451,7 @@ func GenerateListIdentifier(l *List, sess *xorm.Engine) (err error) { // The general idea here is to take the title and slice it into pieces, until we found a unique piece. var exists = true - titleSlug := []rune(strings.Replace(strings.ToUpper(l.Title), " ", "", -1)) + titleSlug := []rune(strings.ReplaceAll(strings.ToUpper(l.Title), " ", "")) // We can save at most 10 characters in the db, so we need to ensure it has at most 10 characters if len(titleSlug) > 10 { diff --git a/pkg/models/list_duplicate.go b/pkg/models/list_duplicate.go index 916ad8795..e723f3339 100644 --- a/pkg/models/list_duplicate.go +++ b/pkg/models/list_duplicate.go @@ -65,6 +65,7 @@ func (ld *ListDuplicate) CanCreate(a web.Auth) (canCreate bool, err error) { // @Failure 403 {object} web.HTTPError "The user does not have access to the list or namespace" // @Failure 500 {object} models.Message "Internal error" // @Router /lists/{listID}/duplicate [put] +//nolint:gocyclo func (ld *ListDuplicate) Create(a web.Auth) (err error) { log.Debugf("Duplicating list %d", ld.ListID) diff --git a/pkg/models/list_duplicate_test.go b/pkg/models/list_duplicate_test.go index 90176b127..17385bf65 100644 --- a/pkg/models/list_duplicate_test.go +++ b/pkg/models/list_duplicate_test.go @@ -17,11 +17,12 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestListDuplicate(t *testing.T) { diff --git a/pkg/models/list_team.go b/pkg/models/list_team.go index 47816b6d2..db36d9e0e 100644 --- a/pkg/models/list_team.go +++ b/pkg/models/list_team.go @@ -17,8 +17,9 @@ package models import ( - "code.vikunja.io/web" "time" + + "code.vikunja.io/web" ) // TeamList defines the relation between a team and a list diff --git a/pkg/models/list_team_test.go b/pkg/models/list_team_test.go index 94454bfcf..6ff5c6956 100644 --- a/pkg/models/list_team_test.go +++ b/pkg/models/list_team_test.go @@ -17,14 +17,15 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" - "code.vikunja.io/web" - "github.com/stretchr/testify/assert" "reflect" "runtime" "testing" "time" + + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" + "github.com/stretchr/testify/assert" ) func TestTeamList_ReadAll(t *testing.T) { diff --git a/pkg/models/list_test.go b/pkg/models/list_test.go index 2146a7c71..ab763dd90 100644 --- a/pkg/models/list_test.go +++ b/pkg/models/list_test.go @@ -17,11 +17,12 @@ package models import ( + "reflect" + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "reflect" - "testing" ) func TestList_CreateOrUpdate(t *testing.T) { diff --git a/pkg/models/list_users.go b/pkg/models/list_users.go index 4c2f8afa0..ce139e1dc 100644 --- a/pkg/models/list_users.go +++ b/pkg/models/list_users.go @@ -17,9 +17,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" ) // ListUser represents a list <-> user relation diff --git a/pkg/models/list_users_rights_test.go b/pkg/models/list_users_rights_test.go index b5cae0618..0ddb712d8 100644 --- a/pkg/models/list_users_rights_test.go +++ b/pkg/models/list_users_rights_test.go @@ -17,11 +17,12 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" ) diff --git a/pkg/models/list_users_test.go b/pkg/models/list_users_test.go index dce067bbe..a94569111 100644 --- a/pkg/models/list_users_test.go +++ b/pkg/models/list_users_test.go @@ -17,14 +17,15 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" - "gopkg.in/d4l3k/messagediff.v1" "reflect" "runtime" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "gopkg.in/d4l3k/messagediff.v1" + "code.vikunja.io/web" ) diff --git a/pkg/models/main_test.go b/pkg/models/main_test.go index d30acd47c..3aae44d64 100644 --- a/pkg/models/main_test.go +++ b/pkg/models/main_test.go @@ -17,13 +17,14 @@ package models import ( - "code.vikunja.io/api/pkg/config" - "code.vikunja.io/api/pkg/files" - "code.vikunja.io/api/pkg/user" "fmt" "os" "testing" "time" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/files" + "code.vikunja.io/api/pkg/user" ) func setupTime() { diff --git a/pkg/models/models.go b/pkg/models/models.go index c8a73bdbe..c990e0a86 100644 --- a/pkg/models/models.go +++ b/pkg/models/models.go @@ -17,12 +17,13 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/log" _ "github.com/go-sql-driver/mysql" // Because. _ "github.com/lib/pq" // Because. - "time" "xorm.io/xorm" _ "github.com/mattn/go-sqlite3" // Because. diff --git a/pkg/models/namespace.go b/pkg/models/namespace.go index c753d5ca3..e3b77b8ef 100644 --- a/pkg/models/namespace.go +++ b/pkg/models/namespace.go @@ -17,12 +17,13 @@ package models import ( + "sort" + "time" + "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" "github.com/imdario/mergo" - "sort" - "time" "xorm.io/builder" ) diff --git a/pkg/models/namespace_team.go b/pkg/models/namespace_team.go index cb69d92c0..417e27e14 100644 --- a/pkg/models/namespace_team.go +++ b/pkg/models/namespace_team.go @@ -17,8 +17,9 @@ package models import ( - "code.vikunja.io/web" "time" + + "code.vikunja.io/web" ) // TeamNamespace defines the relationship between a Team and a Namespace diff --git a/pkg/models/namespace_team_rights_test.go b/pkg/models/namespace_team_rights_test.go index 814504772..f50946e81 100644 --- a/pkg/models/namespace_team_rights_test.go +++ b/pkg/models/namespace_team_rights_test.go @@ -17,11 +17,12 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" ) diff --git a/pkg/models/namespace_team_test.go b/pkg/models/namespace_team_test.go index 8e92473f9..b0d595753 100644 --- a/pkg/models/namespace_team_test.go +++ b/pkg/models/namespace_team_test.go @@ -17,14 +17,15 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" - "code.vikunja.io/web" - "github.com/stretchr/testify/assert" "reflect" "runtime" "testing" "time" + + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" + "github.com/stretchr/testify/assert" ) func TestTeamNamespace_ReadAll(t *testing.T) { diff --git a/pkg/models/namespace_test.go b/pkg/models/namespace_test.go index 27819b3ce..bc1f4502d 100644 --- a/pkg/models/namespace_test.go +++ b/pkg/models/namespace_test.go @@ -17,10 +17,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestNamespace_Create(t *testing.T) { diff --git a/pkg/models/namespace_users.go b/pkg/models/namespace_users.go index 3e5c4ffe3..8b69630f4 100644 --- a/pkg/models/namespace_users.go +++ b/pkg/models/namespace_users.go @@ -17,9 +17,10 @@ package models import ( + "time" + user2 "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" ) // NamespaceUser represents a namespace <-> user relation diff --git a/pkg/models/namespace_users_rights_test.go b/pkg/models/namespace_users_rights_test.go index c5af4ef5c..44c31a944 100644 --- a/pkg/models/namespace_users_rights_test.go +++ b/pkg/models/namespace_users_rights_test.go @@ -17,11 +17,12 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" ) diff --git a/pkg/models/namespace_users_test.go b/pkg/models/namespace_users_test.go index 2950c2e22..69dc80919 100644 --- a/pkg/models/namespace_users_test.go +++ b/pkg/models/namespace_users_test.go @@ -17,14 +17,15 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" - "code.vikunja.io/web" - "gopkg.in/d4l3k/messagediff.v1" "reflect" "runtime" "testing" "time" + + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" + "gopkg.in/d4l3k/messagediff.v1" ) func TestNamespaceUser_Create(t *testing.T) { diff --git a/pkg/models/saved_filters.go b/pkg/models/saved_filters.go index 18a0bbce5..d3d081da6 100644 --- a/pkg/models/saved_filters.go +++ b/pkg/models/saved_filters.go @@ -17,9 +17,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" ) // SavedFilter represents a saved bunch of filters diff --git a/pkg/models/saved_filters_test.go b/pkg/models/saved_filters_test.go index 5838d7110..7c6fb4413 100644 --- a/pkg/models/saved_filters_test.go +++ b/pkg/models/saved_filters_test.go @@ -17,10 +17,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" "xorm.io/xorm/schemas" ) diff --git a/pkg/models/task_assignees.go b/pkg/models/task_assignees.go index f74f50dab..bb103322b 100644 --- a/pkg/models/task_assignees.go +++ b/pkg/models/task_assignees.go @@ -17,9 +17,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" "xorm.io/xorm" ) diff --git a/pkg/models/task_attachment.go b/pkg/models/task_attachment.go index ada1aadf6..ac0d72927 100644 --- a/pkg/models/task_attachment.go +++ b/pkg/models/task_attachment.go @@ -17,11 +17,12 @@ package models import ( + "io" + "time" + "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "io" - "time" ) // TaskAttachment is the definition of a task attachment diff --git a/pkg/models/task_attachment_test.go b/pkg/models/task_attachment_test.go index 1edad9c32..8f1985351 100644 --- a/pkg/models/task_attachment_test.go +++ b/pkg/models/task_attachment_test.go @@ -18,15 +18,16 @@ package models import ( + "io" + "os" + "strconv" + "testing" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "io" - "os" - "strconv" - "testing" ) func TestTaskAttachment_ReadOne(t *testing.T) { diff --git a/pkg/models/task_collection_filter.go b/pkg/models/task_collection_filter.go index b456ead08..1284e8069 100644 --- a/pkg/models/task_collection_filter.go +++ b/pkg/models/task_collection_filter.go @@ -18,12 +18,13 @@ package models import ( - "code.vikunja.io/api/pkg/config" "fmt" - "github.com/iancoleman/strcase" "reflect" "strconv" "time" + + "code.vikunja.io/api/pkg/config" + "github.com/iancoleman/strcase" "xorm.io/xorm/schemas" ) @@ -112,6 +113,8 @@ func validateTaskFieldComparator(comparator taskFilterComparator) error { taskFilterComparatorLessEquals, taskFilterComparatorNotEquals: return nil + case taskFilterComparatorInvalid: + fallthrough default: return ErrInvalidTaskFilterComparator{Comparator: comparator} } diff --git a/pkg/models/task_collection_sort_test.go b/pkg/models/task_collection_sort_test.go index 3c41e00fc..9677bc1c9 100644 --- a/pkg/models/task_collection_sort_test.go +++ b/pkg/models/task_collection_sort_test.go @@ -17,8 +17,9 @@ package models import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestSortParamValidation(t *testing.T) { diff --git a/pkg/models/task_collection_test.go b/pkg/models/task_collection_test.go index ddd3ddd79..af999503a 100644 --- a/pkg/models/task_collection_test.go +++ b/pkg/models/task_collection_test.go @@ -17,14 +17,15 @@ package models import ( + "testing" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" "gopkg.in/d4l3k/messagediff.v1" - "testing" - "time" ) func TestTaskCollection_ReadAll(t *testing.T) { diff --git a/pkg/models/task_comments.go b/pkg/models/task_comments.go index 568a5fe59..a0c7ae3a6 100644 --- a/pkg/models/task_comments.go +++ b/pkg/models/task_comments.go @@ -18,9 +18,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" ) // TaskComment represents a task comment diff --git a/pkg/models/task_comments_test.go b/pkg/models/task_comments_test.go index f89632f4f..41839deee 100644 --- a/pkg/models/task_comments_test.go +++ b/pkg/models/task_comments_test.go @@ -17,10 +17,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestTaskComment_Create(t *testing.T) { diff --git a/pkg/models/task_relation.go b/pkg/models/task_relation.go index 640e85670..4372e2eaf 100644 --- a/pkg/models/task_relation.go +++ b/pkg/models/task_relation.go @@ -18,9 +18,10 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" ) // RelationKind represents a kind of relation between to tasks @@ -174,6 +175,8 @@ func (rel *TaskRelation) Create(a web.Auth) error { otherRelation.RelationKind = RelationKindCopiedTo case RelationKindCopiedTo: otherRelation.RelationKind = RelationKindCopiedFrom + case RelationKindUnknown: + // Nothing to do } // Finally insert everything diff --git a/pkg/models/task_relation_test.go b/pkg/models/task_relation_test.go index cbb3387a5..6987c5cbe 100644 --- a/pkg/models/task_relation_test.go +++ b/pkg/models/task_relation_test.go @@ -18,10 +18,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestTaskRelation_Create(t *testing.T) { diff --git a/pkg/models/tasks.go b/pkg/models/tasks.go index 2d1d4533b..a779f52a3 100644 --- a/pkg/models/tasks.go +++ b/pkg/models/tasks.go @@ -17,16 +17,17 @@ package models import ( + "math" + "sort" + "strconv" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/utils" "code.vikunja.io/web" "github.com/imdario/mergo" - "math" - "sort" - "strconv" - "time" "xorm.io/builder" "xorm.io/xorm" "xorm.io/xorm/schemas" @@ -168,6 +169,7 @@ func (t *Task) ReadAll(a web.Auth, search string, page int, perPage int) (result return nil, 0, 0, nil } +//nolint:gocyclo func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks []*Task, resultCount int, totalItems int64, err error) { // If the user does not have any lists, don't try to get any tasks @@ -258,6 +260,8 @@ func getRawTasksForLists(lists []*List, a web.Auth, opts *taskOptions) (tasks [] } else { filters = append(filters, &builder.Lte{f.field: f.value}) } + case taskFilterComparatorInvalid: + // Nothing to do } } @@ -728,6 +732,7 @@ func createTask(s *xorm.Session, t *Task, a web.Auth, updateAssignees bool) (err // @Failure 403 {object} web.HTTPError "The user does not have access to the task (aka its list)" // @Failure 500 {object} models.Message "Internal error" // @Router /tasks/{id} [post] +//nolint:gocyclo func (t *Task) Update() (err error) { s := x.NewSession() @@ -836,12 +841,12 @@ func (t *Task) Update() (err error) { // a lot of existing code which we'll then need to refactor. // This is why. // - //if err := ot.updateTaskLabels(t.Labels); err != nil { - // return err - //} + // if err := ot.updateTaskLabels(t.Labels); err != nil { + // return err + // } // set the labels to ot.Labels because our updateTaskLabels function puts the full label objects in it pretty nicely // We also set this here to prevent it being overwritten later on. - //t.Labels = ot.Labels + // t.Labels = ot.Labels // For whatever reason, xorm dont detect if done is updated, so we need to update this every time by hand // Which is why we merge the actual task struct with the one we got from the db diff --git a/pkg/models/tasks_rights.go b/pkg/models/tasks_rights.go index 49ee1ceaf..efc0f4b8a 100644 --- a/pkg/models/tasks_rights.go +++ b/pkg/models/tasks_rights.go @@ -39,7 +39,6 @@ func (t *Task) CanCreate(a web.Auth) (bool, error) { // CanRead determines if a user can read a task func (t *Task) CanRead(a web.Auth) (canRead bool, maxRight int, err error) { - //return t.canDoTask(a) // Get the task, error out if it doesn't exist *t, err = GetTaskByIDSimple(t.ID) if err != nil { diff --git a/pkg/models/tasks_test.go b/pkg/models/tasks_test.go index 3cd02820f..b350f73f8 100644 --- a/pkg/models/tasks_test.go +++ b/pkg/models/tasks_test.go @@ -17,11 +17,12 @@ package models import ( + "testing" + "time" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" - "time" ) func TestTask_Create(t *testing.T) { diff --git a/pkg/models/team_members_test.go b/pkg/models/team_members_test.go index 2bda54a40..51b2bc832 100644 --- a/pkg/models/team_members_test.go +++ b/pkg/models/team_members_test.go @@ -17,10 +17,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestTeamMember_Create(t *testing.T) { diff --git a/pkg/models/teams.go b/pkg/models/teams.go index 36817a1fa..dbf188170 100644 --- a/pkg/models/teams.go +++ b/pkg/models/teams.go @@ -17,10 +17,11 @@ package models import ( + "time" + "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" - "time" "xorm.io/builder" ) diff --git a/pkg/models/teams_rights_test.go b/pkg/models/teams_rights_test.go index 700ebcc10..72bb48861 100644 --- a/pkg/models/teams_rights_test.go +++ b/pkg/models/teams_rights_test.go @@ -17,11 +17,12 @@ package models import ( - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/user" "testing" "time" + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web" ) diff --git a/pkg/models/teams_test.go b/pkg/models/teams_test.go index 02409bce1..4d40575bd 100644 --- a/pkg/models/teams_test.go +++ b/pkg/models/teams_test.go @@ -17,11 +17,12 @@ package models import ( + "reflect" + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "reflect" - "testing" ) func TestTeam_Create(t *testing.T) { diff --git a/pkg/models/users_list_test.go b/pkg/models/users_list_test.go index 6f1020cab..955a7261f 100644 --- a/pkg/models/users_list_test.go +++ b/pkg/models/users_list_test.go @@ -18,10 +18,11 @@ package models import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/user" "gopkg.in/d4l3k/messagediff.v1" - "testing" ) func TestListUsersFromList(t *testing.T) { diff --git a/pkg/modules/avatar/gravatar/gravatar.go b/pkg/modules/avatar/gravatar/gravatar.go index 075df0a03..8dffe5d21 100644 --- a/pkg/modules/avatar/gravatar/gravatar.go +++ b/pkg/modules/avatar/gravatar/gravatar.go @@ -17,14 +17,16 @@ package gravatar import ( - "code.vikunja.io/api/pkg/config" - "code.vikunja.io/api/pkg/log" - "code.vikunja.io/api/pkg/user" - "code.vikunja.io/api/pkg/utils" + "context" "io/ioutil" "net/http" "strconv" "time" + + "code.vikunja.io/api/pkg/config" + "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/api/pkg/utils" ) type avatar struct { @@ -62,10 +64,15 @@ func (g *Provider) GetAvatar(user *user.User, size int64) ([]byte, string, error } if !exists || needsRefetch { log.Debugf("Gravatar for user %d with size %d not cached, requesting from gravatar...", user.ID, size) - resp, err := http.Get("https://www.gravatar.com/avatar/" + utils.Md5String(user.Email) + "?s=" + sizeString + "&d=mp") + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://www.gravatar.com/avatar/"+utils.Md5String(user.Email)+"?s="+sizeString+"&d=mp", nil) if err != nil { return nil, "", err } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, "", err + } + defer resp.Body.Close() avatarContent, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, "", err diff --git a/pkg/modules/avatar/initials/initials.go b/pkg/modules/avatar/initials/initials.go index 0defdc66a..aba5a5192 100644 --- a/pkg/modules/avatar/initials/initials.go +++ b/pkg/modules/avatar/initials/initials.go @@ -18,6 +18,13 @@ package initials import ( "bytes" + "image" + "image/color" + "image/draw" + "image/png" + "strconv" + "strings" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/keyvalue" e "code.vikunja.io/api/pkg/modules/keyvalue/error" @@ -27,12 +34,6 @@ import ( "golang.org/x/image/font" "golang.org/x/image/font/gofont/goregular" "golang.org/x/image/math/fixed" - "image" - "image/color" - "image/draw" - "image/png" - "strconv" - "strings" ) // Provider represents the provider implementation of the initials provider @@ -41,15 +42,15 @@ type Provider struct { var ( avatarBgColors = []*color.RGBA{ - {69, 189, 243, 255}, - {224, 143, 112, 255}, - {77, 182, 172, 255}, - {149, 117, 205, 255}, - {176, 133, 94, 255}, - {240, 98, 146, 255}, - {163, 211, 108, 255}, - {121, 134, 203, 255}, - {241, 185, 29, 255}, + {R: 69, G: 189, B: 243, A: 255}, + {R: 224, G: 143, B: 112, A: 255}, + {R: 77, G: 182, B: 172, A: 255}, + {R: 149, G: 117, B: 205, A: 255}, + {R: 176, G: 133, B: 94, A: 255}, + {R: 240, G: 98, B: 146, A: 255}, + {R: 163, G: 211, B: 108, A: 255}, + {R: 121, G: 134, B: 203, A: 255}, + {R: 241, G: 185, B: 29, A: 255}, } ) diff --git a/pkg/modules/avatar/upload/upload.go b/pkg/modules/avatar/upload/upload.go index 357bc287d..0145f6b89 100644 --- a/pkg/modules/avatar/upload/upload.go +++ b/pkg/modules/avatar/upload/upload.go @@ -18,16 +18,17 @@ package upload import ( "bytes" + "image" + "image/png" + "io/ioutil" + "strconv" + "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/modules/keyvalue" e "code.vikunja.io/api/pkg/modules/keyvalue/error" "code.vikunja.io/api/pkg/user" "github.com/disintegration/imaging" - "image" - "image/png" - "io/ioutil" - "strconv" ) // Provider represents the upload avatar provider diff --git a/pkg/modules/background/handler/background.go b/pkg/modules/background/handler/background.go index 9c482ab1d..2fecef566 100644 --- a/pkg/modules/background/handler/background.go +++ b/pkg/modules/background/handler/background.go @@ -17,6 +17,11 @@ package handler import ( + "io" + "net/http" + "strconv" + "strings" + "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" @@ -27,10 +32,6 @@ import ( "code.vikunja.io/web/handler" "github.com/gabriel-vasile/mimetype" "github.com/labstack/echo/v4" - "io" - "net/http" - "strconv" - "strings" ) // BackgroundProvider represents a thing which holds a background provider diff --git a/pkg/modules/background/unsplash/proxy.go b/pkg/modules/background/unsplash/proxy.go index 8561d9dea..d1b524567 100644 --- a/pkg/modules/background/unsplash/proxy.go +++ b/pkg/modules/background/unsplash/proxy.go @@ -17,18 +17,25 @@ package unsplash import ( - "code.vikunja.io/web/handler" - "github.com/labstack/echo/v4" + "context" "net/http" "strings" + + "code.vikunja.io/web/handler" + "github.com/labstack/echo/v4" ) func unsplashImage(url string, c echo.Context) error { // Replacing and appending the url for security reasons - resp, err := http.Get("https://images.unsplash.com/" + strings.Replace(url, "https://images.unsplash.com/", "", 1)) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://images.unsplash.com/"+strings.Replace(url, "https://images.unsplash.com/", "", 1), nil) if err != nil { return err } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() if resp.StatusCode > 399 { return echo.ErrNotFound } diff --git a/pkg/modules/background/unsplash/unsplash.go b/pkg/modules/background/unsplash/unsplash.go index cacd404b2..d7952e1bd 100644 --- a/pkg/modules/background/unsplash/unsplash.go +++ b/pkg/modules/background/unsplash/unsplash.go @@ -18,6 +18,14 @@ package unsplash import ( "bytes" + "context" + "encoding/json" + "net/http" + "net/url" + "strconv" + "strings" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" @@ -26,12 +34,6 @@ import ( "code.vikunja.io/api/pkg/modules/keyvalue" e "code.vikunja.io/api/pkg/modules/keyvalue/error" "code.vikunja.io/web" - "encoding/json" - "net/http" - "net/url" - "strconv" - "strings" - "time" ) const ( @@ -89,7 +91,7 @@ type initialCollection struct { var emptySearchResult *initialCollection func doGet(url string, result ...interface{}) (err error) { - req, err := http.NewRequest("GET", unsplashAPIURL+url, nil) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, unsplashAPIURL+url, nil) if err != nil { return } @@ -100,6 +102,7 @@ func doGet(url string, result ...interface{}) (err error) { if err != nil { return } + defer resp.Body.Close() if len(result) > 0 { return json.NewDecoder(resp.Body).Decode(result[0]) @@ -250,10 +253,14 @@ func (p *Provider) Set(image *background.Image, list *models.List, auth web.Auth // Download the photo from unsplash // The parameters crop the image to a max width of 2560 and a max height of 2048 to save bandwidth and storage. - resp, err := http.Get(photo.Urls.Raw + "&w=2560&h=2048&q=90") + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, photo.Urls.Raw+"&w=2560&h=2048&q=90", nil) if err != nil { return } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } defer resp.Body.Close() if resp.StatusCode > 399 { @@ -327,9 +334,13 @@ func Pingback(f *files.File) { } func pingbackByPhotoID(photoID string) { - if _, err := http.Get("https://views.unsplash.com/v?app_id=" + config.BackgroundsUnsplashApplicationID.GetString() + "&photo_id=" + photoID); err != nil { + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://views.unsplash.com/v?app_id="+config.BackgroundsUnsplashApplicationID.GetString()+"&photo_id="+photoID, nil) + if err != nil { + log.Errorf("Unsplash Pingback Failed: %s", err.Error()) + } + _, err = http.DefaultClient.Do(req) + if err != nil { log.Errorf("Unsplash Pingback Failed: %s", err.Error()) } log.Debugf("Pinged unsplash for photo %s", photoID) - } diff --git a/pkg/modules/background/upload/upload.go b/pkg/modules/background/upload/upload.go index c580e3b73..9f39bee32 100644 --- a/pkg/modules/background/upload/upload.go +++ b/pkg/modules/background/upload/upload.go @@ -17,11 +17,12 @@ package upload import ( + "strconv" + "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/modules/background" "code.vikunja.io/web" - "strconv" ) // Provider represents an upload provider diff --git a/pkg/modules/dump/dump.go b/pkg/modules/dump/dump.go index e0647dc6f..d99425ee7 100644 --- a/pkg/modules/dump/dump.go +++ b/pkg/modules/dump/dump.go @@ -18,15 +18,16 @@ package dump import ( "archive/zip" + "fmt" + "io" + "os" + "strconv" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/version" - "fmt" "github.com/spf13/viper" - "io" - "os" - "strconv" ) // Change to deflate to gain better compression diff --git a/pkg/modules/dump/restore.go b/pkg/modules/dump/restore.go index 504458dbb..78c4dd805 100644 --- a/pkg/modules/dump/restore.go +++ b/pkg/modules/dump/restore.go @@ -20,19 +20,20 @@ import ( "archive/zip" "bufio" "bytes" - "code.vikunja.io/api/pkg/db" - "code.vikunja.io/api/pkg/files" - "code.vikunja.io/api/pkg/initialize" - "code.vikunja.io/api/pkg/log" - "code.vikunja.io/api/pkg/migration" "encoding/json" "fmt" "io" "os" "sort" - "src.techknowlogick.com/xormigrate" "strconv" "strings" + + "code.vikunja.io/api/pkg/db" + "code.vikunja.io/api/pkg/files" + "code.vikunja.io/api/pkg/initialize" + "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/migration" + "src.techknowlogick.com/xormigrate" ) const maxConfigSize = 5 * 1024 * 1024 // 5 MB, should be largely enough diff --git a/pkg/modules/keyvalue/memory/memory.go b/pkg/modules/keyvalue/memory/memory.go index 043573f7d..7a2e83b9e 100644 --- a/pkg/modules/keyvalue/memory/memory.go +++ b/pkg/modules/keyvalue/memory/memory.go @@ -18,8 +18,9 @@ package memory import ( - e "code.vikunja.io/api/pkg/modules/keyvalue/error" "sync" + + e "code.vikunja.io/api/pkg/modules/keyvalue/error" ) // Storage is the memory implementation of a storage backend diff --git a/pkg/modules/keyvalue/redis/redis.go b/pkg/modules/keyvalue/redis/redis.go index 1422ceb15..8a01c3ae4 100644 --- a/pkg/modules/keyvalue/redis/redis.go +++ b/pkg/modules/keyvalue/redis/redis.go @@ -18,9 +18,12 @@ package redis import ( + "encoding/json" + + "github.com/go-errors/errors" + e "code.vikunja.io/api/pkg/modules/keyvalue/error" "code.vikunja.io/api/pkg/red" - "encoding/json" "github.com/go-redis/redis/v7" ) @@ -52,7 +55,7 @@ func (s *Storage) Put(key string, value interface{}) (err error) { func (s *Storage) Get(key string) (value interface{}, err error) { b, err := s.client.Get(key).Bytes() if err != nil { - if err == redis.Nil { + if errors.Is(err, redis.Nil) { return nil, &e.ErrValueNotFoundForKey{Key: key} } return nil, err diff --git a/pkg/modules/migration/create_from_structure.go b/pkg/modules/migration/create_from_structure.go index 571e4bfcb..993ff68ca 100644 --- a/pkg/modules/migration/create_from_structure.go +++ b/pkg/modules/migration/create_from_structure.go @@ -18,10 +18,11 @@ package migration import ( "bytes" + "io/ioutil" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" - "io/ioutil" ) // InsertFromStructure takes a fully nested Vikunja data structure and a user and then creates everything for this user diff --git a/pkg/modules/migration/create_from_structure_test.go b/pkg/modules/migration/create_from_structure_test.go index 75fea4fbe..a94939cb7 100644 --- a/pkg/modules/migration/create_from_structure_test.go +++ b/pkg/modules/migration/create_from_structure_test.go @@ -17,12 +17,13 @@ package migration import ( + "testing" + "code.vikunja.io/api/pkg/db" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "github.com/stretchr/testify/assert" - "testing" ) func TestInsertFromStructure(t *testing.T) { diff --git a/pkg/modules/migration/handler/handler.go b/pkg/modules/migration/handler/handler.go index bf67a3965..fdba2547e 100644 --- a/pkg/modules/migration/handler/handler.go +++ b/pkg/modules/migration/handler/handler.go @@ -17,12 +17,13 @@ package handler import ( + "net/http" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/modules/migration" user2 "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // MigrationWeb holds the web migration handler diff --git a/pkg/modules/migration/main_test.go b/pkg/modules/migration/main_test.go index 75e6462ae..91da70018 100644 --- a/pkg/modules/migration/main_test.go +++ b/pkg/modules/migration/main_test.go @@ -17,12 +17,13 @@ package migration import ( + "os" + "testing" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" - "os" - "testing" ) // TestMain is the main test function used to bootstrap the test env diff --git a/pkg/modules/migration/migration_status.go b/pkg/modules/migration/migration_status.go index 79485faf9..a319ee7aa 100644 --- a/pkg/modules/migration/migration_status.go +++ b/pkg/modules/migration/migration_status.go @@ -17,8 +17,9 @@ package migration import ( - "code.vikunja.io/api/pkg/user" "time" + + "code.vikunja.io/api/pkg/user" ) // Status represents this migration status diff --git a/pkg/modules/migration/todoist/todoist.go b/pkg/modules/migration/todoist/todoist.go index 111ac5438..462925c69 100644 --- a/pkg/modules/migration/todoist/todoist.go +++ b/pkg/modules/migration/todoist/todoist.go @@ -18,6 +18,14 @@ package todoist import ( "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" @@ -25,12 +33,6 @@ import ( "code.vikunja.io/api/pkg/modules/migration" "code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/utils" - "encoding/json" - "fmt" - "net/http" - "net/url" - "strings" - "time" ) // Migration is the todoist migration struct @@ -217,7 +219,7 @@ func (m *Migration) AuthURL() string { } func doPost(url string, form url.Values) (resp *http.Response, err error) { - req, err := http.NewRequest("POST", url, strings.NewReader(form.Encode())) + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, strings.NewReader(form.Encode())) if err != nil { return } @@ -342,7 +344,12 @@ func convertTodoistToVikunja(sync *sync) (fullVikunjaHierachie []*models.Namespa // Only add the attachment if there's something to download if len(n.FileAttachment.FileURL) > 0 { // Download the attachment and put it in the file - resp, err := http.Get(n.FileAttachment.FileURL) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, n.FileAttachment.FileURL, nil) + if err != nil { + return nil, err + } + hc := http.Client{} + resp, err := hc.Do(req) if err != nil { return nil, err } @@ -417,6 +424,7 @@ func getAccessTokenFromAuthToken(authToken string) (accessToken string, err erro if err != nil { return } + defer resp.Body.Close() if resp.StatusCode > 399 { buf := &bytes.Buffer{} @@ -468,6 +476,7 @@ func (m *Migration) Migrate(u *user.User) (err error) { if err != nil { return } + defer resp.Body.Close() syncResponse := &sync{} err = json.NewDecoder(resp.Body).Decode(syncResponse) diff --git a/pkg/modules/migration/todoist/todoist_test.go b/pkg/modules/migration/todoist/todoist_test.go index a242b7b0f..e94e280ed 100644 --- a/pkg/modules/migration/todoist/todoist_test.go +++ b/pkg/modules/migration/todoist/todoist_test.go @@ -17,15 +17,16 @@ package todoist import ( + "io/ioutil" + "strconv" + "testing" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/models" "github.com/stretchr/testify/assert" "gopkg.in/d4l3k/messagediff.v1" - "io/ioutil" - "strconv" - "testing" - "time" ) func TestConvertTodoistToVikunja(t *testing.T) { @@ -43,7 +44,6 @@ func TestConvertTodoistToVikunja(t *testing.T) { dueTime = dueTime.In(config.GetTimeZone()) nilTime, err := time.Parse(time.RFC3339Nano, "0001-01-01T00:00:00Z") assert.NoError(t, err) - //nilTime = nilTime.In(config.GetTimeZone()) exampleFile, err := ioutil.ReadFile(config.ServiceRootpath.GetString() + "/pkg/modules/migration/wunderlist/testimage.jpg") assert.NoError(t, err) diff --git a/pkg/modules/migration/wunderlist/wunderlist.go b/pkg/modules/migration/wunderlist/wunderlist.go index 6c2644f3a..12f6a9e42 100644 --- a/pkg/modules/migration/wunderlist/wunderlist.go +++ b/pkg/modules/migration/wunderlist/wunderlist.go @@ -18,6 +18,14 @@ package wunderlist import ( "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strconv" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/log" @@ -25,12 +33,6 @@ import ( "code.vikunja.io/api/pkg/modules/migration" "code.vikunja.io/api/pkg/user" "code.vikunja.io/api/pkg/utils" - "encoding/json" - "fmt" - "net/http" - "net/url" - "strconv" - "time" ) // Migration represents the implementation of the migration for wunderlist @@ -51,17 +53,17 @@ type wunderlistAuthToken struct { } type task struct { - ID int `json:"id"` AssigneeID int `json:"assignee_id"` CreatedAt time.Time `json:"created_at"` CreatedByID int `json:"created_by_id"` + Completed bool `json:"completed"` + CompletedAt time.Time `json:"completed_at"` DueDate string `json:"due_date"` + ID int `json:"id"` ListID int `json:"list_id"` Revision int `json:"revision"` Starred bool `json:"starred"` Title string `json:"title"` - Completed bool `json:"completed"` - CompletedAt time.Time `json:"completed_at"` } type list struct { @@ -181,7 +183,11 @@ func convertListForFolder(listID int, list *list, content *wunderlistContents) ( for _, f := range content.files { if f.TaskID == t.ID { // Download the attachment and put it in the file - resp, err := http.Get(f.URL) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, f.URL, nil) + if err != nil { + return nil, err + } + resp, err := http.DefaultClient.Do(req) if err != nil { return nil, err } @@ -296,7 +302,7 @@ func convertWunderlistToVikunja(content *wunderlistContents) (fullVikunjaHierach } func makeAuthGetRequest(token *wunderlistAuthToken, urlPart string, v interface{}, urlParams url.Values) error { - req, err := http.NewRequest(http.MethodGet, "https://a.wunderlist.com/api/v1/"+urlPart, nil) + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://a.wunderlist.com/api/v1/"+urlPart, nil) if err != nil { return err } @@ -366,10 +372,16 @@ func (w *Migration) Migrate(user *user.User) (err error) { if err != nil { return } - resp, err := http.Post("https://www.wunderlist.com/oauth/access_token", "application/json", bytes.NewBuffer(jsonAuth)) + req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "https://www.wunderlist.com/oauth/access_token", bytes.NewBuffer(jsonAuth)) + if err != nil { + return err + } + req.Header.Add("Content-Type", "application/json") + resp, err := http.DefaultClient.Do(req) if err != nil { return } + defer resp.Body.Close() authToken := &wunderlistAuthToken{} err = json.NewDecoder(resp.Body).Decode(authToken) diff --git a/pkg/modules/migration/wunderlist/wunderlist_test.go b/pkg/modules/migration/wunderlist/wunderlist_test.go index 565fbbb7c..fcc8ab6c2 100644 --- a/pkg/modules/migration/wunderlist/wunderlist_test.go +++ b/pkg/modules/migration/wunderlist/wunderlist_test.go @@ -17,15 +17,16 @@ package wunderlist import ( + "io/ioutil" + "strconv" + "testing" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/files" "code.vikunja.io/api/pkg/models" "github.com/stretchr/testify/assert" "gopkg.in/d4l3k/messagediff.v1" - "io/ioutil" - "strconv" - "testing" - "time" ) func TestWunderlistParsing(t *testing.T) { diff --git a/pkg/routes/api/v1/auth.go b/pkg/routes/api/v1/auth.go index 4441ad419..2367488df 100644 --- a/pkg/routes/api/v1/auth.go +++ b/pkg/routes/api/v1/auth.go @@ -17,14 +17,15 @@ package v1 import ( + "net/http" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web" "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" - "net/http" - "time" ) // These are all valid auth types diff --git a/pkg/routes/api/v1/avatar.go b/pkg/routes/api/v1/avatar.go index b2789ef03..8e8d4a1eb 100644 --- a/pkg/routes/api/v1/avatar.go +++ b/pkg/routes/api/v1/avatar.go @@ -29,15 +29,16 @@ import ( "code.vikunja.io/web/handler" "bytes" - "github.com/disintegration/imaging" - "github.com/gabriel-vasile/mimetype" - "github.com/labstack/echo/v4" "image" "image/png" "io" "net/http" "strconv" "strings" + + "github.com/disintegration/imaging" + "github.com/gabriel-vasile/mimetype" + "github.com/labstack/echo/v4" ) // GetAvatar returns a user's avatar diff --git a/pkg/routes/api/v1/docs.go b/pkg/routes/api/v1/docs.go index 506996bb1..2ce5c61c5 100644 --- a/pkg/routes/api/v1/docs.go +++ b/pkg/routes/api/v1/docs.go @@ -19,10 +19,11 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/log" "github.com/labstack/echo/v4" "github.com/swaggo/swag" - "net/http" ) // DocsJSON serves swagger doc json specs diff --git a/pkg/routes/api/v1/info.go b/pkg/routes/api/v1/info.go index f81d9363a..646d43f5c 100644 --- a/pkg/routes/api/v1/info.go +++ b/pkg/routes/api/v1/info.go @@ -17,12 +17,13 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/modules/migration/todoist" "code.vikunja.io/api/pkg/modules/migration/wunderlist" "code.vikunja.io/api/pkg/version" "github.com/labstack/echo/v4" - "net/http" ) type vikunjaInfos struct { diff --git a/pkg/routes/api/v1/link_sharing_auth.go b/pkg/routes/api/v1/link_sharing_auth.go index 914b3cebb..ba62c855f 100644 --- a/pkg/routes/api/v1/link_sharing_auth.go +++ b/pkg/routes/api/v1/link_sharing_auth.go @@ -17,10 +17,11 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // LinkShareToken represents a link share auth token with extra infos about the actual link share diff --git a/pkg/routes/api/v1/list_by_namespace.go b/pkg/routes/api/v1/list_by_namespace.go index a09b3110c..1d6f021b8 100644 --- a/pkg/routes/api/v1/list_by_namespace.go +++ b/pkg/routes/api/v1/list_by_namespace.go @@ -17,12 +17,13 @@ package v1 import ( + "net/http" + "strconv" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" - "strconv" ) // GetListsByNamespaceID is the web handler to delete a namespace diff --git a/pkg/routes/api/v1/login.go b/pkg/routes/api/v1/login.go index 8a8b49dfe..e9733724e 100644 --- a/pkg/routes/api/v1/login.go +++ b/pkg/routes/api/v1/login.go @@ -17,12 +17,13 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" user2 "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" - "net/http" ) // Token represents an authentification token @@ -45,7 +46,7 @@ type Token struct { func Login(c echo.Context) error { u := user2.Login{} if err := c.Bind(&u); err != nil { - return c.JSON(http.StatusBadRequest, models.Message{"Please provide a username and password."}) + return c.JSON(http.StatusBadRequest, models.Message{Message: "Please provide a username and password."}) } // Check user diff --git a/pkg/routes/api/v1/task_attachment.go b/pkg/routes/api/v1/task_attachment.go index b524408ee..5a995d130 100644 --- a/pkg/routes/api/v1/task_attachment.go +++ b/pkg/routes/api/v1/task_attachment.go @@ -17,10 +17,11 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // UploadTaskAttachment handles everything needed for the upload of a task attachment diff --git a/pkg/routes/api/v1/token_check.go b/pkg/routes/api/v1/token_check.go index ab93bdda8..1a70b4af4 100644 --- a/pkg/routes/api/v1/token_check.go +++ b/pkg/routes/api/v1/token_check.go @@ -17,8 +17,9 @@ package v1 import ( - "code.vikunja.io/api/pkg/models" "fmt" + + "code.vikunja.io/api/pkg/models" "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" ) @@ -30,5 +31,5 @@ func CheckToken(c echo.Context) error { fmt.Println(user.Valid) - return c.JSON(418, models.Message{"🍵"}) + return c.JSON(418, models.Message{Message: "🍵"}) } diff --git a/pkg/routes/api/v1/user_confirm_email.go b/pkg/routes/api/v1/user_confirm_email.go index 994c0418a..cde7db7b7 100644 --- a/pkg/routes/api/v1/user_confirm_email.go +++ b/pkg/routes/api/v1/user_confirm_email.go @@ -17,11 +17,12 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // UserConfirmEmail is the handler to confirm a user email @@ -47,5 +48,5 @@ func UserConfirmEmail(c echo.Context) error { return handler.HandleHTTPError(err, c) } - return c.JSON(http.StatusOK, models.Message{"The email was confirmed successfully."}) + return c.JSON(http.StatusOK, models.Message{Message: "The email was confirmed successfully."}) } diff --git a/pkg/routes/api/v1/user_list.go b/pkg/routes/api/v1/user_list.go index 1f9061deb..2b5053fe4 100644 --- a/pkg/routes/api/v1/user_list.go +++ b/pkg/routes/api/v1/user_list.go @@ -17,12 +17,13 @@ package v1 import ( + "net/http" + "strconv" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" - "strconv" ) // UserList gets all information about a user diff --git a/pkg/routes/api/v1/user_password_reset.go b/pkg/routes/api/v1/user_password_reset.go index 10b1af0ff..383332af4 100644 --- a/pkg/routes/api/v1/user_password_reset.go +++ b/pkg/routes/api/v1/user_password_reset.go @@ -17,11 +17,12 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // UserResetPassword is the handler to change a users password @@ -47,7 +48,7 @@ func UserResetPassword(c echo.Context) error { return handler.HandleHTTPError(err, c) } - return c.JSON(http.StatusOK, models.Message{"The password was updated successfully."}) + return c.JSON(http.StatusOK, models.Message{Message: "The password was updated successfully."}) } // UserRequestResetPasswordToken is the handler to change a users password @@ -77,5 +78,5 @@ func UserRequestResetPasswordToken(c echo.Context) error { return handler.HandleHTTPError(err, c) } - return c.JSON(http.StatusOK, models.Message{"Token was sent."}) + return c.JSON(http.StatusOK, models.Message{Message: "Token was sent."}) } diff --git a/pkg/routes/api/v1/user_register.go b/pkg/routes/api/v1/user_register.go index 03724518f..3130d731d 100644 --- a/pkg/routes/api/v1/user_register.go +++ b/pkg/routes/api/v1/user_register.go @@ -17,12 +17,13 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // RegisterUser is the register handler @@ -43,10 +44,10 @@ func RegisterUser(c echo.Context) error { // Check for Request Content var datUser *user.APIUserPassword if err := c.Bind(&datUser); err != nil { - return c.JSON(http.StatusBadRequest, models.Message{"No or invalid user model provided."}) + return c.JSON(http.StatusBadRequest, models.Message{Message: "No or invalid user model provided."}) } if datUser == nil { - return c.JSON(http.StatusBadRequest, models.Message{"No or invalid user model provided."}) + return c.JSON(http.StatusBadRequest, models.Message{Message: "No or invalid user model provided."}) } // Insert the user diff --git a/pkg/routes/api/v1/user_settings.go b/pkg/routes/api/v1/user_settings.go index b7d29413e..e0e19b1da 100644 --- a/pkg/routes/api/v1/user_settings.go +++ b/pkg/routes/api/v1/user_settings.go @@ -17,11 +17,12 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" user2 "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // UserAvatarProvider holds the user avatar provider type diff --git a/pkg/routes/api/v1/user_show.go b/pkg/routes/api/v1/user_show.go index a48a8199e..7118e97ed 100644 --- a/pkg/routes/api/v1/user_show.go +++ b/pkg/routes/api/v1/user_show.go @@ -17,10 +17,11 @@ package v1 import ( + "net/http" + user2 "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // UserShow gets all informations about the current user diff --git a/pkg/routes/api/v1/user_totp.go b/pkg/routes/api/v1/user_totp.go index b218ae771..c61a069fc 100644 --- a/pkg/routes/api/v1/user_totp.go +++ b/pkg/routes/api/v1/user_totp.go @@ -18,14 +18,15 @@ package v1 import ( "bytes" + "fmt" + "image/jpeg" + "net/http" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" - "fmt" "github.com/labstack/echo/v4" - "image/jpeg" - "net/http" ) // UserTOTPEnroll is the handler to enroll a user into totp diff --git a/pkg/routes/api/v1/user_update_email.go b/pkg/routes/api/v1/user_update_email.go index 6e8cb2f96..3aca2d023 100644 --- a/pkg/routes/api/v1/user_update_email.go +++ b/pkg/routes/api/v1/user_update_email.go @@ -17,13 +17,14 @@ package v1 import ( + "fmt" + "net/http" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" - "fmt" "github.com/labstack/echo/v4" - "net/http" ) // UpdateUserEmail is the handler to let a user update their email address. diff --git a/pkg/routes/api/v1/user_update_password.go b/pkg/routes/api/v1/user_update_password.go index 3fafb9fd2..8a65fae7d 100644 --- a/pkg/routes/api/v1/user_update_password.go +++ b/pkg/routes/api/v1/user_update_password.go @@ -17,11 +17,12 @@ package v1 import ( + "net/http" + "code.vikunja.io/api/pkg/models" "code.vikunja.io/api/pkg/user" "code.vikunja.io/web/handler" "github.com/labstack/echo/v4" - "net/http" ) // UserPassword holds a user password. Used to update it. @@ -70,5 +71,5 @@ func UserChangePassword(c echo.Context) error { return handler.HandleHTTPError(err, c) } - return c.JSON(http.StatusOK, models.Message{"The password was updated successfully."}) + return c.JSON(http.StatusOK, models.Message{Message: "The password was updated successfully."}) } diff --git a/pkg/routes/caldav/handler.go b/pkg/routes/caldav/handler.go index cce861e53..d3b3535b7 100644 --- a/pkg/routes/caldav/handler.go +++ b/pkg/routes/caldav/handler.go @@ -18,18 +18,19 @@ package caldav import ( "bytes" - "code.vikunja.io/api/pkg/log" - "code.vikunja.io/api/pkg/models" - "code.vikunja.io/api/pkg/user" - "code.vikunja.io/web/handler" "fmt" - "github.com/labstack/echo/v4" - "github.com/samedi/caldav-go" - "github.com/samedi/caldav-go/lib" "io/ioutil" "reflect" "strconv" "strings" + + "code.vikunja.io/api/pkg/log" + "code.vikunja.io/api/pkg/models" + "code.vikunja.io/api/pkg/user" + "code.vikunja.io/web/handler" + "github.com/labstack/echo/v4" + "github.com/samedi/caldav-go" + "github.com/samedi/caldav-go/lib" ) func getBasicAuthUserFromContext(c echo.Context) (*user.User, error) { diff --git a/pkg/routes/caldav/listStorageProvider.go b/pkg/routes/caldav/listStorageProvider.go index 1abc683d0..5b3680d82 100644 --- a/pkg/routes/caldav/listStorageProvider.go +++ b/pkg/routes/caldav/listStorageProvider.go @@ -17,14 +17,15 @@ package caldav import ( + "strconv" + "strings" + "time" + "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" user2 "code.vikunja.io/api/pkg/user" "github.com/samedi/caldav-go/data" "github.com/samedi/caldav-go/errs" - "strconv" - "strings" - "time" ) // DavBasePath is the base url path @@ -119,8 +120,8 @@ func (vcls *VikunjaCaldavListStorage) GetResourcesByList(rpaths []string) ([]dat var uids []string for _, path := range rpaths { parts := strings.Split(path, "/") - uid := []rune(parts[4]) // The 4th part is the id with ".ics" suffix - endlen := len(uid) - 4 // ".ics" are 4 bytes + uid := []rune(parts[4]) // The 4th part is the id with ".ics" suffix + endlen := len(uid) - len(".ics") // ".ics" are 4 bytes uids = append(uids, string(uid[:endlen])) } @@ -174,7 +175,7 @@ func (vcls *VikunjaCaldavListStorage) GetResourcesByFilters(rpath string, filter r.Name = vcls.list.Title return []data.Resource{r}, nil // For now, filtering is disabled. - //return vcls.GetResources(rpath, false) + // return vcls.GetResources(rpath, false) } func getTaskURL(task *models.Task) string { @@ -336,9 +337,9 @@ func (vlra *VikunjaListResourceAdapter) CalculateEtag() string { // And therefore, updating the task fails since these etags don't match. // To fix that, we use this extra field to determine if we're currently updating a task and return the // etag of the list instead. - //if vlra.list != nil { - // return `"` + strconv.FormatInt(vlra.list.ID, 10) + `-` + strconv.FormatInt(vlra.list.Updated, 10) + `"` - //} + // if vlra.list != nil { + // return `"` + strconv.FormatInt(vlra.list.ID, 10) + `-` + strconv.FormatInt(vlra.list.Updated, 10) + `"` + // } // Return the etag of a task if we have one if vlra.task != nil { diff --git a/pkg/routes/caldav/parsing.go b/pkg/routes/caldav/parsing.go index 0439adef2..c6568c6e7 100644 --- a/pkg/routes/caldav/parsing.go +++ b/pkg/routes/caldav/parsing.go @@ -17,12 +17,13 @@ package caldav import ( + "strconv" + "time" + "code.vikunja.io/api/pkg/caldav" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" "github.com/laurent22/ical-go" - "strconv" - "time" ) func getCaldavTodosForTasks(list *models.List) string { diff --git a/pkg/routes/rate_limit.go b/pkg/routes/rate_limit.go index 8809f5715..a038081e8 100644 --- a/pkg/routes/rate_limit.go +++ b/pkg/routes/rate_limit.go @@ -18,6 +18,10 @@ package routes import ( + "net/http" + "strconv" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/red" @@ -26,9 +30,6 @@ import ( "github.com/ulule/limiter/v3" "github.com/ulule/limiter/v3/drivers/store/memory" "github.com/ulule/limiter/v3/drivers/store/redis" - "net/http" - "strconv" - "time" ) // RateLimit is the rate limit middleware diff --git a/pkg/routes/routes.go b/pkg/routes/routes.go index ffbb848db..dfdb4844f 100644 --- a/pkg/routes/routes.go +++ b/pkg/routes/routes.go @@ -47,6 +47,9 @@ package routes import ( + "strings" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/log" "code.vikunja.io/api/pkg/models" @@ -71,8 +74,6 @@ import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" elog "github.com/labstack/gommon/log" - "strings" - "time" ) // CustomValidator is a dummy struct to use govalidator with echo diff --git a/pkg/user/error.go b/pkg/user/error.go index c40e6b06f..f97227d67 100644 --- a/pkg/user/error.go +++ b/pkg/user/error.go @@ -18,9 +18,10 @@ package user import ( - "code.vikunja.io/web" "fmt" "net/http" + + "code.vikunja.io/web" ) // ===================== diff --git a/pkg/user/totp.go b/pkg/user/totp.go index d6ea149dd..b5c29c2e7 100644 --- a/pkg/user/totp.go +++ b/pkg/user/totp.go @@ -17,10 +17,11 @@ package user import ( + "image" + "code.vikunja.io/api/pkg/config" "github.com/pquerna/otp" "github.com/pquerna/otp/totp" - "image" ) // TOTP holds a user's totp setting in the database. @@ -35,7 +36,7 @@ type TOTP struct { } // TableName holds the table name for totp secrets -func (T *TOTP) TableName() string { +func (t *TOTP) TableName() string { return "totp" } diff --git a/pkg/user/user.go b/pkg/user/user.go index 190cf7d7c..ecc3f3b2e 100644 --- a/pkg/user/user.go +++ b/pkg/user/user.go @@ -18,17 +18,19 @@ package user import ( + "errors" + "fmt" + "reflect" + "time" + "code.vikunja.io/api/pkg/config" "code.vikunja.io/api/pkg/mail" "code.vikunja.io/api/pkg/metrics" "code.vikunja.io/api/pkg/utils" "code.vikunja.io/web" - "fmt" "github.com/dgrijalva/jwt-go" "github.com/labstack/echo/v4" "golang.org/x/crypto/bcrypt" - "reflect" - "time" ) // Login Object to recive user credentials in JSON format @@ -189,7 +191,7 @@ func CheckUserCredentials(u *Login) (*User, error) { func CheckUserPassword(user *User, password string) error { err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) if err != nil { - if err == bcrypt.ErrMismatchedHashAndPassword { + if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { return ErrWrongUsernameOrPassword{} } return err @@ -322,7 +324,6 @@ func UpdateUser(user *User) (updatedUser *User, err error) { // Check if we have at least a username if user.Username == "" { - //return User{}, ErrNoUsername{user.ID} user.Username = theUser.Username // Dont change the username if we dont have one } else { // Check if the new username already exists diff --git a/pkg/user/user_email_confirm_test.go b/pkg/user/user_email_confirm_test.go index bfb38138e..4964c3ca6 100644 --- a/pkg/user/user_email_confirm_test.go +++ b/pkg/user/user_email_confirm_test.go @@ -18,8 +18,9 @@ package user import ( - "code.vikunja.io/api/pkg/db" "testing" + + "code.vikunja.io/api/pkg/db" ) func TestUserEmailConfirm(t *testing.T) { diff --git a/pkg/user/user_test.go b/pkg/user/user_test.go index a00b31ab5..d4bfce152 100644 --- a/pkg/user/user_test.go +++ b/pkg/user/user_test.go @@ -18,9 +18,10 @@ package user import ( + "testing" + "code.vikunja.io/api/pkg/db" "github.com/stretchr/testify/assert" - "testing" ) func TestCreateUser(t *testing.T) { diff --git a/pkg/utils/random_string_test.go b/pkg/utils/random_string_test.go index cfc2fe7fb..894cc6012 100644 --- a/pkg/utils/random_string_test.go +++ b/pkg/utils/random_string_test.go @@ -17,8 +17,9 @@ package utils import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestMakeRandomString(t *testing.T) {