Skip to content

Commit

Permalink
chore: upgrade and fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jun 17, 2024
1 parent 33e79c2 commit 4217699
Show file tree
Hide file tree
Showing 47 changed files with 105 additions and 99 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: 1.22.3

- name: Check out code into the Go module directory
uses: actions/checkout@v3
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: 1.22.3

- name: Check out code into the Go module directory
uses: actions/checkout@v3
Expand All @@ -53,7 +53,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.2
version: v1.59.1
skip-pkg-cache: true
skip-build-cache: true
args: --timeout 5m
Expand Down
36 changes: 29 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ linters-settings:
- github.com/satisfactorymodding/smr-api/*

govet:
check-shadowing: true
enable-all: true
disable:
- shadow

Expand All @@ -31,14 +29,31 @@ linters-settings:
- blank
- dot

run:
skip-files:
sloglint:
attr-only: true
context: scope
forbidden-keys:
- time
- level
- msg
- source

spancheck:
checks:
- end
- record-error
- set-status
extra-start-span-signatures:
- github.com/satisfactorymodding/smr-api/gql.WrapQueryTrace:opentelemetry
- github.com/satisfactorymodding/smr-api/gql.WrapMutationTrace:opentelemetry


issues:
exclude-files:
- ./generated/generated.go
- ./generated/models_gen.go
skip-dirs:
exclude-dirs:
- ./generated/

issues:
exclude:
- should pass the context parameter

Expand Down Expand Up @@ -66,3 +81,10 @@ linters:
- gocritic
- gofumpt
- nonamedreturns
- inamedparam
- sloglint
- spancheck
- intrange
- copyloopvar
- fatcontext
- canonicalheader
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func installExportPipeline(ctx context.Context) func() {
client := otlptracehttp.NewClient()
exporter, err := otlptrace.New(ctx, client)
if err != nil {
slog.Error("creating OTLP trace exporter", slog.Any("err", err))
slog.ErrorContext(ctx, "creating OTLP trace exporter", slog.Any("err", err))
os.Exit(1)
}

Expand All @@ -331,7 +331,7 @@ func installExportPipeline(ctx context.Context) func() {

return func() {
if err := tracerProvider.Shutdown(ctx); err != nil {
slog.Error("stopping tracer provider", slog.Any("err", err))
slog.ErrorContext(ctx, "stopping tracer provider", slog.Any("err", err))
os.Exit(1)
}
}
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func InitializeConfig(baseCtx context.Context) context.Context {
Level: slog.LevelDebug,
AddSource: true,
TimeFormat: time.RFC3339Nano,
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.LevelKey {
level := attr.Value.Any().(slog.Level)
if level == slog.LevelDebug {
Expand All @@ -69,7 +69,7 @@ func InitializeConfig(baseCtx context.Context) context.Context {
}

if err != nil {
slog.Warn("config initialized using defaults and environment only!", slog.Any("err", err))
slog.WarnContext(baseCtx, "config initialized using defaults and environment only!", slog.Any("err", err))
}

slox.Info(baseCtx, "Config initialized")
Expand Down
2 changes: 1 addition & 1 deletion db/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func Tx(ctx context.Context, f func(newCtx context.Context, tx *ent.Tx) error, o

newCtx := context.WithValue(ctx, txKey{}, tx)

if err := f(newCtx, tx); err != nil {
if err = f(newCtx, tx); err != nil {
finalError := err

func() {
Expand Down
4 changes: 2 additions & 2 deletions db/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
func ConvertModFilter(query *ent.ModQuery, filter *models.ModFilter, count bool, unapproved bool) *ent.ModQuery {
query = query.WithTags()

if len(filter.Ids) > 0 {
query = query.Where(mod.IDIn(filter.Ids...))
if len(filter.IDs) > 0 {
query = query.Where(mod.IDIn(filter.IDs...))
} else if len(filter.References) > 0 {
query = query.Where(mod.ModReferenceIn(filter.References...))
} else if filter != nil {
Expand Down
3 changes: 1 addition & 2 deletions db/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func CompleteOAuthFlow(ctx context.Context, u *oauth.UserData, userAgent string)

newUser := false
if ent.IsNotFound(err) {
var err error
create := From(ctx).User.
Create().
SetEmail(u.Email).
Expand Down Expand Up @@ -67,7 +66,7 @@ func CompleteOAuthFlow(ctx context.Context, u *oauth.UserData, userAgent string)
}

if update != nil {
if err := update.Exec(ctx); err != nil {
if err = update.Exec(ctx); err != nil {
return nil, err
}
}
Expand Down
3 changes: 3 additions & 0 deletions db/pgxpooldriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -88,6 +89,8 @@ func (e *EntPgxpoolDriver) BeginTx(ctx context.Context, opts *sql.TxOptions) (di

pgxOpts, err := getPgxTxOptions(opts)
if err != nil {
span.SetStatus(codes.Error, err.Error())
span.RecordError(err)
return nil, err
}
tx, err := e.pool.BeginTx(ctx, *pgxOpts)
Expand Down
11 changes: 7 additions & 4 deletions db/schema/mixin_softdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
return next.Mutate(ctx, m) // nolint
}
mx, ok := m.(interface {
SetOp(ent.Op)
SetOp(operation ent.Op)
Client() *gen.Client
SetDeletedAt(time.Time)
WhereP(...func(*sql.Selector))
SetDeletedAt(deleteTime time.Time)
WhereP(selectors ...func(*sql.Selector))
})
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
Expand All @@ -87,7 +87,10 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
}

// P adds a storage-level predicate to the queries and mutations.
func (d SoftDeleteMixin) P(w interface{ WhereP(...func(*sql.Selector)) }) {
func (d SoftDeleteMixin) P(w interface {
WhereP(selectors ...func(*sql.Selector))
},
) {
w.WhereP(
sql.FieldIsNull(d.Fields()[0].Descriptor().Name),
)
Expand Down
6 changes: 3 additions & 3 deletions db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import (

func UserHas(ctx context.Context, role *auth.Role, usr *ent.User) bool {
groups := auth.GetRoleGroups(role)
groupIds := make([]string, len(groups))
groupIDs := make([]string, len(groups))
for i, group := range groups {
groupIds[i] = group.ID
groupIDs[i] = group.ID
}

exist, err := usr.QueryGroups().Where(usergroup.GroupIDIn(groupIds...)).Exist(ctx)
exist, err := usr.QueryGroups().Where(usergroup.GroupIDIn(groupIDs...)).Exist(ctx)
if err != nil {
slox.Error(ctx, "failed retrieving user groups", slog.Any("err", err))
return false
Expand Down
1 change: 1 addition & 0 deletions gql/gql_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func WrapMutationTrace(ctx context.Context, action string) (TraceWrapper, contex
}

func wrapTrace(ctx context.Context, action string, actionType string) (TraceWrapper, context.Context) {
//nolint:spancheck
spanCtx, span := otel.Tracer("gql").Start(ctx, "GraphQL "+action, trace.WithAttributes(
attribute.String("action_type", "API.graphql."+actionType),
))
Expand Down
2 changes: 1 addition & 1 deletion gql/resolver_announcements.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *mutationResolver) DeleteAnnouncement(ctx context.Context, announcementI
wrapper, ctx := WrapMutationTrace(ctx, "deleteAnnouncement")
defer wrapper.end()

slog.Info("deleting announcement", slog.String("id", announcementID))
slog.InfoContext(ctx, "deleting announcement", slog.String("id", announcementID))

if err := db.From(ctx).Announcement.DeleteOneID(announcementID).Exec(ctx); err != nil {
return false, err
Expand Down
4 changes: 2 additions & 2 deletions gql/resolver_guides.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func (r *guideResolver) User(ctx context.Context, obj *generated.Guide) (*genera
}

func convertGuideFilter(query *ent.GuideQuery, filter *models.GuideFilter) *ent.GuideQuery {
if len(filter.Ids) > 0 {
query = query.Where(guide.IDIn(filter.Ids...))
if len(filter.IDs) > 0 {
query = query.Where(guide.IDIn(filter.IDs...))
} else if filter != nil {
query = query.
Limit(*filter.Limit).
Expand Down
1 change: 0 additions & 1 deletion gql/resolver_mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (r *mutationResolver) CreateMod(ctx context.Context, newMod generated.NewMo
}

logoData, err = converter.ConvertAnyImageToWebp(ctx, file)

if err != nil {
return nil, fmt.Errorf("failed to convert logo file: %w", err)
}
Expand Down
11 changes: 4 additions & 7 deletions gql/resolver_sml_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (r *mutationResolver) CreateSMLVersion(ctx context.Context, smlVersion gene

var result *ent.SmlVersion
if err := db.Tx(ctx, func(ctx context.Context, tx *ent.Tx) error {
result, err = db.From(ctx).SmlVersion.
result, err = tx.SmlVersion.
Create().
SetVersion(smlVersion.Version).
SetSatisfactoryVersion(smlVersion.SatisfactoryVersion).
Expand All @@ -51,16 +51,13 @@ func (r *mutationResolver) CreateSMLVersion(ctx context.Context, smlVersion gene
return err
}

println(result.ID)

for _, smlVersionTarget := range smlVersion.Targets {
if _, err := db.From(ctx).SmlVersionTarget.
if _, err := tx.SmlVersionTarget.
Create().
SetVersionID(result.ID).
SetTargetName(string(smlVersionTarget.TargetName)).
SetLink(smlVersionTarget.Link).
Save(ctx); err != nil {
println("HERE")
return err
}
}
Expand Down Expand Up @@ -243,8 +240,8 @@ func (r *getSMLVersionsResolver) Count(ctx context.Context, _ *generated.GetSMLV
}

func convertSMLVersionFilter(query *ent.SmlVersionQuery, filter *models.SMLVersionFilter) *ent.SmlVersionQuery {
if len(filter.Ids) > 0 {
query = query.Where(smlversion.IDIn(filter.Ids...))
if len(filter.IDs) > 0 {
query = query.Where(smlversion.IDIn(filter.IDs...))
} else if filter != nil {
query = query.
Limit(*filter.Limit).
Expand Down
4 changes: 2 additions & 2 deletions gql/resolver_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func (r *queryResolver) GetUser(ctx context.Context, userID string) (*generated.
return (*conv.UserImpl)(nil).Convert(result), nil
}

func (r *queryResolver) GetUsers(ctx context.Context, userIds []string) ([]*generated.User, error) {
func (r *queryResolver) GetUsers(ctx context.Context, userIDs []string) ([]*generated.User, error) {
wrapper, ctx := WrapQueryTrace(ctx, "getUsers")
defer wrapper.end()

result, err := db.From(ctx).User.Query().Where(user.IDIn(userIds...)).All(ctx)
result, err := db.From(ctx).User.Query().Where(user.IDIn(userIDs...)).All(ctx)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions gql/resolver_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ func (r *versionResolver) Dependencies(ctx context.Context, obj *generated.Versi
if dependencies == nil {
var err error
dependencies, err = dataloader.For(ctx).VersionDependenciesByVersionID.Load(ctx, obj.ID)()

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -523,8 +522,8 @@ func (r *getMyVersionsResolver) Count(ctx context.Context, _ *generated.GetMyVer
}

func convertVersionFilter(query *ent.VersionQuery, filter *models.VersionFilter, unapproved bool) *ent.VersionQuery {
if len(filter.Ids) > 0 {
query = query.Where(version.IDIn(filter.Ids...))
if len(filter.IDs) > 0 {
query = query.Where(version.IDIn(filter.IDs...))
} else if filter != nil {
query = query.
Limit(*filter.Limit).
Expand Down
4 changes: 2 additions & 2 deletions integrations/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewMod(ctx context.Context, mod *ent.Mod) {
req, _ := http.NewRequest("POST", viper.GetString("discord.webhook_url"), bytes.NewReader(payloadJSON))

req.Header.Add("Content-Type", "application/json")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Cache-Control", "no-cache")

res, _ := http.DefaultClient.Do(req)

Expand Down Expand Up @@ -156,7 +156,7 @@ func NewVersion(ctx context.Context, version *ent.Version) {
req, _ := http.NewRequest("POST", viper.GetString("discord.webhook_url"), bytes.NewReader(payloadJSON))

req.Header.Add("Content-Type", "application/json")
req.Header.Add("cache-control", "no-cache")
req.Header.Add("Cache-Control", "no-cache")

res, _ := http.DefaultClient.Do(req)

Expand Down
2 changes: 1 addition & 1 deletion migrations/code/20200426221900_test_new_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func init() {
migration.NewCodeMigration(
func(executionContext interface{}) error {
func(_ interface{}) error {
ctx, err := db.WithDB(context.Background())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion migrations/code/20200501224200_parse_paks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func init() {
migration.NewCodeMigration(
func(executionContext interface{}) error {
func(_ interface{}) error {
ctx, err := db.WithDB(context.Background())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion migrations/code/20200524203800_after_reference_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func init() {
migration.NewCodeMigration(
func(executionContext interface{}) error {
func(_ interface{}) error {
ctx, err := db.WithDB(context.Background())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion migrations/code/20200621195500_after_id_length_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func init() {
migration.NewCodeMigration(
func(executionContext interface{}) error {
func(_ interface{}) error {
ctx, err := db.WithDB(context.Background())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion migrations/code/20200622003600_after_validation_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func init() {
migration.NewCodeMigration(
func(executionContext interface{}) error {
func(_ interface{}) error {
ctx, err := db.WithDB(context.Background())
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion migrations/code/20200629093800_copy_to_new_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func init() {
migration.NewCodeMigration(
func(executionContext interface{}) error {
func(_ interface{}) error {
ctx, err := db.WithDB(context.Background())
if err != nil {
return err
Expand Down
Loading

0 comments on commit 4217699

Please sign in to comment.