mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-18 10:41:21 +00:00
chore: enable forcetypeassert linter, fix issues (#38804)
Enable [`forcetypeassert`](https://github.com/gostaticanalysis/forcetypeassert) linter to prevent unchecked type assertions. ~650 issues fixed, most fixes were clean, some use `setting.PanicInDevOrTesting`. The only behaviour changes are where code would previously send a 500 error or panic, a 4xx error is now emitted. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
@@ -58,11 +58,9 @@ func (t *Task) IsEnabled() bool {
|
||||
// GetConfig will return a copy of the task's config
|
||||
func (t *Task) GetConfig() Config {
|
||||
if reflect.TypeOf(t.config).Kind() == reflect.Pointer {
|
||||
// Pointer:
|
||||
return reflect.New(reflect.ValueOf(t.config).Elem().Type()).Interface().(Config)
|
||||
return reflect.New(reflect.ValueOf(t.config).Elem().Type()).Interface().(Config) //nolint:forcetypeassert // pointer
|
||||
}
|
||||
// Not pointer:
|
||||
return reflect.New(reflect.TypeOf(t.config)).Elem().Interface().(Config)
|
||||
return reflect.New(reflect.TypeOf(t.config)).Elem().Interface().(Config) //nolint:forcetypeassert // not pointer
|
||||
}
|
||||
|
||||
// Run will run the task incrementing the cron counter with no user defined
|
||||
@@ -124,9 +122,9 @@ func (t *Task) RunWithUser(doer *user_model.User, config Config) {
|
||||
if err := t.fun(ctx, doer, config); err != nil {
|
||||
var message string
|
||||
var status string
|
||||
if db.IsErrCancelled(err) {
|
||||
if errCancelled, ok := err.(db.ErrCancelled); ok {
|
||||
status = "cancelled"
|
||||
message = err.(db.ErrCancelled).Message
|
||||
message = errCancelled.Message
|
||||
} else {
|
||||
status = "error"
|
||||
message = err.Error()
|
||||
@@ -168,7 +166,7 @@ func GetTask(name string) *Task {
|
||||
}
|
||||
|
||||
// RegisterTask allows a task to be registered with the cron service
|
||||
func RegisterTask(name string, config Config, fun func(context.Context, *user_model.User, Config) error) error {
|
||||
func RegisterTask[T Config](name string, config T, fun func(context.Context, *user_model.User, T) error) error {
|
||||
log.Debug("Registering task: %s", name)
|
||||
|
||||
i18nKey := "admin.dashboard." + name
|
||||
@@ -185,7 +183,9 @@ func RegisterTask(name string, config Config, fun func(context.Context, *user_mo
|
||||
task := &Task{
|
||||
Name: name,
|
||||
config: config,
|
||||
fun: fun,
|
||||
fun: func(ctx context.Context, doer *user_model.User, runConfig Config) error {
|
||||
return fun(ctx, doer, runConfig.(T)) //nolint:forcetypeassert // must be valid
|
||||
},
|
||||
}
|
||||
lock.Lock()
|
||||
locked := true
|
||||
@@ -218,7 +218,7 @@ func RegisterTask(name string, config Config, fun func(context.Context, *user_mo
|
||||
}
|
||||
|
||||
// RegisterTaskFatal will register a task but if there is an error log.Fatal
|
||||
func RegisterTaskFatal(name string, config Config, fun func(context.Context, *user_model.User, Config) error) {
|
||||
func RegisterTaskFatal[T Config](name string, config T, fun func(context.Context, *user_model.User, T) error) {
|
||||
if err := RegisterTask(name, config, fun); err != nil {
|
||||
log.Fatal("Unable to register cron task %s Error: %v", name, err)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func registerStopZombieTasks() {
|
||||
Enabled: true,
|
||||
RunAtStart: true,
|
||||
Schedule: "@every 5m",
|
||||
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return actions_service.StopZombieTasks(ctx)
|
||||
})
|
||||
}
|
||||
@@ -37,7 +37,7 @@ func registerStopEndlessTasks() {
|
||||
Enabled: true,
|
||||
RunAtStart: true,
|
||||
Schedule: "@every 30m",
|
||||
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return actions_service.StopEndlessTasks(ctx)
|
||||
})
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func registerCancelAbandonedJobs() {
|
||||
Enabled: true,
|
||||
RunAtStart: true,
|
||||
Schedule: "@every 6h",
|
||||
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return actions_service.CancelAbandonedJobs(ctx)
|
||||
})
|
||||
}
|
||||
@@ -59,7 +59,7 @@ func registerScheduleTasks() {
|
||||
Enabled: true,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 1m",
|
||||
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
// Call the function to start schedule tasks and pass the context.
|
||||
return actions_service.StartScheduleTasks(ctx)
|
||||
})
|
||||
@@ -70,7 +70,7 @@ func registerActionsCleanup() {
|
||||
Enabled: true,
|
||||
RunAtStart: false,
|
||||
Schedule: "@midnight",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return actions_service.Cleanup(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -36,9 +36,8 @@ func registerUpdateMirrorTask() {
|
||||
},
|
||||
PullLimit: 50,
|
||||
PushLimit: 50,
|
||||
}, func(ctx context.Context, _ *user_model.User, cfg Config) error {
|
||||
umtc := cfg.(*UpdateMirrorTaskConfig)
|
||||
return mirror_service.Update(ctx, umtc.PullLimit, umtc.PushLimit)
|
||||
}, func(ctx context.Context, _ *user_model.User, cfg *UpdateMirrorTaskConfig) error {
|
||||
return mirror_service.Update(ctx, cfg.PullLimit, cfg.PushLimit)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -56,10 +55,9 @@ func registerRepoHealthCheck() {
|
||||
},
|
||||
Timeout: time.Duration(setting.Git.Timeout.GC) * time.Second,
|
||||
Args: []string{},
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
rhcConfig := config.(*RepoHealthCheckConfig)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *RepoHealthCheckConfig) error {
|
||||
// the git args are set by config, they can be safe to be trusted
|
||||
return repo_service.GitFsckRepos(ctx, rhcConfig.Timeout, gitcmd.ToTrustedCmdArgs(rhcConfig.Args))
|
||||
return repo_service.GitFsckRepos(ctx, config.Timeout, gitcmd.ToTrustedCmdArgs(config.Args))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -68,7 +66,7 @@ func registerCheckRepoStats() {
|
||||
Enabled: true,
|
||||
RunAtStart: true,
|
||||
Schedule: "@midnight",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return repostats.CheckRepoStats(ctx)
|
||||
})
|
||||
}
|
||||
@@ -81,9 +79,8 @@ func registerArchiveCleanup() {
|
||||
Schedule: "@midnight",
|
||||
},
|
||||
OlderThan: 24 * time.Hour,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
acConfig := config.(*OlderThanConfig)
|
||||
return archiver_service.DeleteOldRepositoryArchives(ctx, acConfig.OlderThan)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *OlderThanConfig) error {
|
||||
return archiver_service.DeleteOldRepositoryArchives(ctx, config.OlderThan)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -95,9 +92,8 @@ func registerSyncExternalUsers() {
|
||||
Schedule: "@midnight",
|
||||
},
|
||||
UpdateExisting: true,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
realConfig := config.(*UpdateExistingConfig)
|
||||
return auth.SyncExternalUsers(ctx, realConfig.UpdateExisting)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *UpdateExistingConfig) error {
|
||||
return auth.SyncExternalUsers(ctx, config.UpdateExisting)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -109,9 +105,8 @@ func registerDeletedBranchesCleanup() {
|
||||
Schedule: "@midnight",
|
||||
},
|
||||
OlderThan: 24 * time.Hour,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
realConfig := config.(*OlderThanConfig)
|
||||
git_model.RemoveOldDeletedBranches(ctx, realConfig.OlderThan)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *OlderThanConfig) error {
|
||||
git_model.RemoveOldDeletedBranches(ctx, config.OlderThan)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -121,7 +116,7 @@ func registerUpdateMigrationPosterID() {
|
||||
Enabled: true,
|
||||
RunAtStart: true,
|
||||
Schedule: "@midnight",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return migrations.UpdateMigrationPosterID(ctx)
|
||||
})
|
||||
}
|
||||
@@ -136,9 +131,8 @@ func registerCleanupHookTaskTable() {
|
||||
CleanupType: "OlderThan",
|
||||
OlderThan: 168 * time.Hour,
|
||||
NumberToKeep: 10,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
realConfig := config.(*CleanupHookTaskConfig)
|
||||
return webhook.CleanupHookTaskTable(ctx, webhook.ToHookTaskCleanupType(realConfig.CleanupType), realConfig.OlderThan, realConfig.NumberToKeep)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *CleanupHookTaskConfig) error {
|
||||
return webhook.CleanupHookTaskTable(ctx, webhook.ToHookTaskCleanupType(config.CleanupType), config.OlderThan, config.NumberToKeep)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -150,9 +144,8 @@ func registerCleanupPackages() {
|
||||
Schedule: "@midnight",
|
||||
},
|
||||
OlderThan: 24 * time.Hour,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
realConfig := config.(*OlderThanConfig)
|
||||
return packages_cleanup_service.CleanupTask(ctx, realConfig.OlderThan)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *OlderThanConfig) error {
|
||||
return packages_cleanup_service.CleanupTask(ctx, config.OlderThan)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -161,7 +154,7 @@ func registerSyncRepoLicenses() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@annually",
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return repo_service.SyncRepoLicenses(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -28,9 +28,8 @@ func registerDeleteInactiveUsers() {
|
||||
Schedule: "@annually",
|
||||
},
|
||||
OlderThan: time.Minute * time.Duration(setting.Service.ActiveCodeLives),
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
olderThanConfig := config.(*OlderThanConfig)
|
||||
return user_service.DeleteInactiveUsers(ctx, olderThanConfig.OlderThan)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *OlderThanConfig) error {
|
||||
return user_service.DeleteInactiveUsers(ctx, config.OlderThan)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -39,7 +38,7 @@ func registerDeleteRepositoryArchives() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@annually",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return archiver_service.DeleteRepositoryArchives(ctx)
|
||||
})
|
||||
}
|
||||
@@ -58,10 +57,9 @@ func registerGarbageCollectRepositories() {
|
||||
},
|
||||
Timeout: time.Duration(setting.Git.Timeout.GC) * time.Second,
|
||||
Args: setting.Git.GCArgs,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
rhcConfig := config.(*RepoHealthCheckConfig)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *RepoHealthCheckConfig) error {
|
||||
// the git args are set by config, they can be safe to be trusted
|
||||
return repo_service.GitGcRepos(ctx, rhcConfig.Timeout, gitcmd.ToTrustedCmdArgs(rhcConfig.Args))
|
||||
return repo_service.GitGcRepos(ctx, config.Timeout, gitcmd.ToTrustedCmdArgs(config.Args))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -70,7 +68,7 @@ func registerRewriteAllPublicKeys() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 72h",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return asymkey_service.RewriteAllPublicKeys(ctx)
|
||||
})
|
||||
}
|
||||
@@ -80,7 +78,7 @@ func registerRewriteAllPrincipalKeys() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 72h",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return asymkey_service.RewriteAllPrincipalKeys(ctx)
|
||||
})
|
||||
}
|
||||
@@ -90,7 +88,7 @@ func registerRepositoryUpdateHook() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 72h",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return repo_service.SyncRepositoryHooks(ctx)
|
||||
})
|
||||
}
|
||||
@@ -100,7 +98,7 @@ func registerReinitMissingRepositories() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 72h",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return repo_service.ReinitMissingRepositories(ctx)
|
||||
})
|
||||
}
|
||||
@@ -110,7 +108,7 @@ func registerDeleteMissingRepositories() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 72h",
|
||||
}, func(ctx context.Context, user *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, user *user_model.User, _ *BaseConfig) error {
|
||||
return repo_service.DeleteMissingRepositories(ctx, user)
|
||||
})
|
||||
}
|
||||
@@ -120,7 +118,7 @@ func registerRemoveRandomAvatars() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@every 72h",
|
||||
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return repo_service.RemoveRandomAvatars(ctx)
|
||||
})
|
||||
}
|
||||
@@ -133,9 +131,8 @@ func registerDeleteOldActions() {
|
||||
Schedule: "@every 168h",
|
||||
},
|
||||
OlderThan: 365 * 24 * time.Hour,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
olderThanConfig := config.(*OlderThanConfig)
|
||||
return activities_model.DeleteOldActions(ctx, olderThanConfig.OlderThan)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *OlderThanConfig) error {
|
||||
return activities_model.DeleteOldActions(ctx, config.OlderThan)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -151,9 +148,8 @@ func registerUpdateGiteaChecker() {
|
||||
Schedule: "@every 168h",
|
||||
},
|
||||
HTTPEndpoint: "https://dl.gitea.com/gitea/version.json",
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
updateCheckerConfig := config.(*UpdateCheckerConfig)
|
||||
return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *UpdateCheckerConfig) error {
|
||||
return updatechecker.GiteaUpdateChecker(config.HTTPEndpoint)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -165,9 +161,8 @@ func registerDeleteOldSystemNotices() {
|
||||
Schedule: "@every 168h",
|
||||
},
|
||||
OlderThan: 365 * 24 * time.Hour,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
olderThanConfig := config.(*OlderThanConfig)
|
||||
return system.DeleteOldSystemNotices(ctx, olderThanConfig.OlderThan)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *OlderThanConfig) error {
|
||||
return system.DeleteOldSystemNotices(ctx, config.OlderThan)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -204,12 +199,11 @@ func registerGCLFS() {
|
||||
LastUpdatedMoreThanAgo: 24 * time.Hour * 3,
|
||||
NumberToCheckPerRepo: 100,
|
||||
ProportionToCheckPerRepo: 0.6,
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
gcLFSConfig := config.(*GCLFSConfig)
|
||||
}, func(ctx context.Context, _ *user_model.User, config *GCLFSConfig) error {
|
||||
return repo_service.GarbageCollectLFSMetaObjects(ctx, repo_service.GarbageCollectLFSMetaObjectsOptions{
|
||||
AutoFix: true,
|
||||
OlderThan: time.Now().Add(-gcLFSConfig.OlderThan),
|
||||
UpdatedLessRecentlyThan: time.Now().Add(-gcLFSConfig.LastUpdatedMoreThanAgo),
|
||||
OlderThan: time.Now().Add(-config.OlderThan),
|
||||
UpdatedLessRecentlyThan: time.Now().Add(-config.LastUpdatedMoreThanAgo),
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -219,7 +213,7 @@ func registerRebuildIssueIndexer() {
|
||||
Enabled: false,
|
||||
RunAtStart: false,
|
||||
Schedule: "@annually",
|
||||
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
||||
}, func(ctx context.Context, _ *user_model.User, _ *BaseConfig) error {
|
||||
return issue_indexer.PopulateIssueIndexer(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user