Skip to content

Commit

Permalink
Fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hlubek committed Jul 18, 2024
1 parent dbb234f commit a9f9460
Show file tree
Hide file tree
Showing 19 changed files with 442 additions and 352 deletions.
607 changes: 290 additions & 317 deletions backend/.golangci.yml

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions backend/cli/ctl/cmd_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ func newAccountCmd() *cli.Command {
return err
}

organisationIdStr := c.String("organisationId")
if organisationIdStr != "" {
organisationId, err := uuid.FromString(organisationIdStr)
organisationIDStr := c.String("organisationId")
if organisationIDStr != "" {
organisationID, err := uuid.FromString(organisationIDStr)
if err != nil {
return errors.Wrap(err, "parsing organisationId")
return errors.Wrap(err, "parsing organisation id")
}
cmd.OrganisationID = uuid.NullUUID{Valid: true, UUID: organisationId}
cmd.OrganisationID = uuid.NullUUID{Valid: true, UUID: organisationID}
}

db, err := connectDatabase(c)
Expand Down
9 changes: 5 additions & 4 deletions backend/cli/ctl/cmd_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func serve(c *cli.Context, handler http.Handler, onShutdown func(c *cli.Context)
func setupCancelOnSignal(c *cli.Context) {
log := logger.FromContext(c.Context)

ch := make(chan os.Signal, 1)
signal.Notify(ch,
signals := make(chan os.Signal, 1)
signal.Notify(signals,
// kill -SIGINT XXXX or Ctrl+c
syscall.SIGINT,
// kill -SIGTERM XXXX
Expand All @@ -247,13 +247,14 @@ func setupCancelOnSignal(c *cli.Context) {
var cancel context.CancelFunc
c.Context, cancel = context.WithCancel(c.Context)
go func() {
sig := <-ch
sig := <-signals
log.Infof("Received signal: %v", sig)
cancel()
}()
}

func startCronJobs(c *cli.Context, db *sql.DB) (func(), error) {
//nolint:unparam // Adding jobs needs to return errors
func startCronJobs(c *cli.Context, _ *sql.DB) (func(), error) {
log := logger.FromContext(c.Context)

cronJobs := cron.New()
Expand Down
5 changes: 3 additions & 2 deletions backend/cli/ctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,11 @@ func buildMailer(c *cli.Context) (*mail.Mailer, error) {
return mailer, nil
}

//nolint:unparam // parsing other flags could return an error
func getConfig(c *cli.Context) (domain.Config, error) {
config := domain.DefaultConfig()
config.AppBaseUrl = c.String("app-base-url")
config.AppBaseURL = c.String("app-base-url")
config.HashCost = c.Int("hash-cost")
// Add more config options here (parsing these could return an error)
// Add more config options here
return config, nil
}
2 changes: 1 addition & 1 deletion backend/cli/ctl/time_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (cts currentTimeSource) Now() time.Time {
return time.Now()
}

func newCurrentTimeSource(c *cli.Context) (domain.TimeSource, error) {
func newCurrentTimeSource(_ *cli.Context) (domain.TimeSource, error) {
// TODO Get location from CLI context and store in time source

return currentTimeSource{}, nil
Expand Down
2 changes: 1 addition & 1 deletion backend/domain/cmd_account_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewAccountCreateCmd(emailAddress string, role Role, password string) (cmd A
}, nil
}

func (c AccountCreateCmd) Validate(config Config) error {
func (c AccountCreateCmd) Validate(_ Config) error {
if IsBlank(c.EmailAddress) {
return FieldError{
Field: "emailAddress",
Expand Down
2 changes: 1 addition & 1 deletion backend/domain/cmd_account_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewAccountUpdateCmd(config Config, currentOrganisationID uuid.NullUUID, acc
return cmd, nil
}

func (c AccountUpdateCmd) Validate(config Config) error {
func (c AccountUpdateCmd) Validate(_ Config) error {
if IsBlank(c.EmailAddress) {
return FieldError{
Field: "emailAddress",
Expand Down
4 changes: 2 additions & 2 deletions backend/domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultHashCost = 12
type Config struct {
AppName string
// Base URL of the app (for sending mails and linking back)
AppBaseUrl string
AppBaseURL string
// Bcrypt hash cost for passwords
HashCost int
// Location for date / time calculations, defaults to Europe/Berlin
Expand All @@ -32,7 +32,7 @@ func DefaultConfig() Config {
}
func (c Config) BuildURL(path string) string {
// Strip slashes to prevent mixup of double slashes or no slashes at all
baseURL := strings.TrimRight(c.AppBaseUrl, "/")
baseURL := strings.TrimRight(c.AppBaseURL, "/")
p := strings.TrimLeft(path, "/")
return baseURL + "/" + p
}
15 changes: 10 additions & 5 deletions backend/domain/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ func (d Date) IsValid() bool {
//
// In is always consistent with time.Date, even when time.Date returns a time
// on a different day. For example, if loc is America/Indiana/Vincennes, then both
// time.Date(1955, time.May, 1, 0, 0, 0, 0, loc)
//
// time.Date(1955, time.May, 1, 0, 0, 0, 0, loc)
//
// and
// civil.Date{Year: 1955, Month: time.May, Day: 1}.In(loc)
//
// civil.Date{Year: 1955, Month: time.May, Day: 1}.In(loc)
//
// return 23:00:00 on April 30, 1955.
//
// In panics if loc is nil.
Expand Down Expand Up @@ -144,16 +148,17 @@ func (d Date) Value() (driver.Value, error) {
}

func (d *Date) Scan(src interface{}) error {
switch v := src.(type) {
switch value := src.(type) {
case string:
parsedDate, err := ParseDate(v)
parsedDate, err := ParseDate(value)
if err != nil {
return err
}
*d = parsedDate
case time.Time:
*d = DateOf(v)
*d = DateOf(value)
default:
//nolint:goerr113
return fmt.Errorf("unhandled type: %T", src)
}

Expand Down
5 changes: 5 additions & 0 deletions backend/domain/enums.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package domain

import "errors"

var ErrEnumsMustBeStrings = errors.New("enums must be strings")
2 changes: 1 addition & 1 deletion backend/domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ func (n FieldError) Extensions() map[string]interface{} {
}
}

var _ FieldResolvableError = new(FieldError)
var _ FieldResolvableError = new(FieldError) //nolint:errcheck
2 changes: 1 addition & 1 deletion backend/domain/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (r Role) IsValid() bool {
func (r *Role) UnmarshalGQL(v interface{}) error {
str, ok := v.(string)
if !ok {
return errors.New("enums must be strings")
return ErrEnumsMustBeStrings
}

domainRole, err := RoleByIdentifier(str)
Expand Down
3 changes: 2 additions & 1 deletion backend/handler/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func (c *cronJob) init() {
c.sentryHub.Scope().SetTag("section", "cron")
}

func (c cronJob) context() context.Context {
//nolint:unused // This should be used by jobs
func (c *cronJob) context() context.Context {
ctx := context.Background()
ctx = sentry.SetHubOnContext(ctx, c.sentryHub)
return ctx
Expand Down
3 changes: 1 addition & 2 deletions backend/mail/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
//go:embed templates/*.txt
var templatesFS embed.FS

func executeTemplate(name string, data interface{}) (subject, body string, err error) {

func executeTemplate(name string, data any) (subject, body string, err error) {
templates, err := template.
New("").
Funcs(sprig.TxtFuncMap()).
Expand Down
5 changes: 2 additions & 3 deletions backend/security/authentication/generate_auth_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func GenerateAuthToken(account AuthTokenDataProvider, timeSource domain.TimeSour
}

now := timeSource.Now()

cl := jwt.Claims{
claims := jwt.Claims{
Subject: account.GetAccountID().String(),
IssuedAt: jwt.NewNumericDate(now),
Expiry: jwt.NewNumericDate(now.Add(opts.Expiry)),
Expand All @@ -59,7 +58,7 @@ func GenerateAuthToken(account AuthTokenDataProvider, timeSource domain.TimeSour
OrganisationID: organisationIDValue,
}

raw, err := jwt.Signed(sig).Claims(cl).Claims(privateCl).Serialize()
raw, err := jwt.Signed(sig).Claims(claims).Claims(privateCl).Serialize()
if err != nil {
return "", errors.Wrap(err, "signing and serializing JWT")
}
Expand Down
7 changes: 6 additions & 1 deletion backend/test/auth/fixed_login_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import (
"myvendor.mytld/myproject/backend/security/authentication"
)

//nolint:gochecknoglobals
var (
fixedSystemAdminAccountID = uuid.Must(uuid.FromString("d7037ad0-d4bb-4dcc-8759-d82fbb3354e8"))

fixedOrganisationAdminAccountID = uuid.Must(uuid.FromString("3ad082c7-cbda-49e1-a707-c53e1962be65"))
fixedOrganisationID = uuid.Must(uuid.FromString("6330de58-2761-411e-a243-bec6d0c53876"))

fixedTokenSecret = "f71ab8929ad747915e135b8e9a5e01403329cc6b202c8e540e74920a78394e36f6266e4a505bf9cd362206bfd39665c69330e038f96ba72bbbc1f4a522564410"
fixedTokenSecret = "f71ab8929ad747915e135b8e9a5e0140" //nolint:gosec
)

type ApplyAuthValuesFunc func(t *testing.T, timeSource domain.TimeSource, req *http.Request) FixedAuthTokenData

func ApplyFixedAuthValuesOrganisationAdministrator(t *testing.T, timeSource domain.TimeSource, req *http.Request) FixedAuthTokenData {
t.Helper()

authTokenData := FixedAuthTokenData{
TokenSecret: mustHexDecode(fixedTokenSecret),
AccountID: fixedOrganisationAdminAccountID,
Expand All @@ -39,6 +42,8 @@ func ApplyFixedAuthValuesOrganisationAdministrator(t *testing.T, timeSource doma
var _ ApplyAuthValuesFunc = ApplyFixedAuthValuesOrganisationAdministrator

func ApplyFixedAuthValuesSystemAdministrator(t *testing.T, timeSource domain.TimeSource, req *http.Request) FixedAuthTokenData {
t.Helper()

authTokenData := FixedAuthTokenData{
TokenSecret: mustHexDecode(fixedTokenSecret),
AccountID: fixedSystemAdminAccountID,
Expand Down
11 changes: 7 additions & 4 deletions ci/gitlab/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ backend:lint:
stage: test
needs: []
dependencies: []
image: registry.networkteam.com/networkteam/docker/golangci-lint:1.44.2
image: registry.networkteam.com/networkteam/docker/golangci-lint:1.55.2
script:
- cd backend
# Write the code coverage report to gl-code-quality-report.json
# and print linting issues to stdout in the format: path/to/file:line description
# remove `--issues-exit-code 0` or set to non-zero to fail the job if linting issues are detected
- golangci-lint run --issues-exit-code 0 --out-format code-climate | tee gl-code-quality-report.json | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"'
# and print linting issues to stdout in the format: path/to/file:line description.
- golangci-lint run --out-format code-climate | tee gl-code-quality-report.json | jq -r '.[] | "\(.severity)\t\(.location.path):\(.location.lines.begin) \(.description)"'
# Fail if any issue with severity "blocker" is found
- test -z "$(jq -r '.[] | select(.severity == "blocker") | .description' gl-code-quality-report.json)"
artifacts:
reports:
codequality: backend/gl-code-quality-report.json
paths:
- backend/gl-code-quality-report.json
interruptible: true

backend:gosec-sast:
extends: .backend-job
Expand Down
4 changes: 3 additions & 1 deletion devbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"nodejs@20",
"postgresql_16",
"[email protected]",
"python310Packages.pip"
"python310Packages.pip",
"mailhog@latest",
"[email protected]"
],
"include": ["github:networkteam/devbox-plugins?dir=postgresql"],
"shell": {
Expand Down
96 changes: 96 additions & 0 deletions devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,102 @@
}
}
},
"[email protected]": {
"last_modified": "2024-02-10T18:15:24Z",
"resolved": "github:NixOS/nixpkgs/10b813040df67c4039086db0f6eaf65c536886c6#golangci-lint",
"source": "devbox-search",
"version": "1.55.2",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/2hiyqyisiqrrp4g3xqa2c35w63ylp928-golangci-lint-1.55.2",
"default": true
}
],
"store_path": "/nix/store/2hiyqyisiqrrp4g3xqa2c35w63ylp928-golangci-lint-1.55.2"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/sxprhbskfahbqrfp14agsr4fj51n0fjd-golangci-lint-1.55.2",
"default": true
}
],
"store_path": "/nix/store/sxprhbskfahbqrfp14agsr4fj51n0fjd-golangci-lint-1.55.2"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/41bra9w6hn5vc8j28rfz1djwab637g5s-golangci-lint-1.55.2",
"default": true
}
],
"store_path": "/nix/store/41bra9w6hn5vc8j28rfz1djwab637g5s-golangci-lint-1.55.2"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/6gz1dqhjq45bz1icl8gxry7j0hrib99w-golangci-lint-1.55.2",
"default": true
}
],
"store_path": "/nix/store/6gz1dqhjq45bz1icl8gxry7j0hrib99w-golangci-lint-1.55.2"
}
}
},
"mailhog@latest": {
"last_modified": "2024-07-07T07:43:47Z",
"resolved": "github:NixOS/nixpkgs/b60793b86201040d9dee019a05089a9150d08b5b#mailhog",
"source": "devbox-search",
"version": "1.0.1",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/hp7l6gnk6vxfgj00vsfgdn5aqsib8lf6-MailHog-1.0.1",
"default": true
}
],
"store_path": "/nix/store/hp7l6gnk6vxfgj00vsfgdn5aqsib8lf6-MailHog-1.0.1"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/22kix69kg7w9xg5jq03fz3lfddcacmf7-MailHog-1.0.1",
"default": true
}
],
"store_path": "/nix/store/22kix69kg7w9xg5jq03fz3lfddcacmf7-MailHog-1.0.1"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/9h3nl95hxqxaabgx9br3nk9wamzr75nz-MailHog-1.0.1",
"default": true
}
],
"store_path": "/nix/store/9h3nl95hxqxaabgx9br3nk9wamzr75nz-MailHog-1.0.1"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/wjmcr55ni4yfbaz4b83wwl657hc7mrn8-MailHog-1.0.1",
"default": true
}
],
"store_path": "/nix/store/wjmcr55ni4yfbaz4b83wwl657hc7mrn8-MailHog-1.0.1"
}
}
},
"nodejs@20": {
"last_modified": "2024-07-10T06:55:44Z",
"plugin_version": "0.0.2",
Expand Down

0 comments on commit a9f9460

Please sign in to comment.