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:
silverwind
2026-08-09 10:25:06 +00:00
committed by GitHub
co-authored by wxiaoguang
parent fac8bf2eca
commit 76a81b24f9
248 changed files with 1110 additions and 995 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ func UpdateAvatarSetting(ctx *context.Context, form forms.AvatarForm) error {
// SettingsAvatar save new POSTed repository avatar
func SettingsAvatar(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AvatarForm)
form := web.GetForm[*forms.AvatarForm](ctx)
form.Source = forms.AvatarLocal
if err := UpdateAvatarSetting(ctx, *form); err != nil {
ctx.Flash.Error(err.Error())
+1 -1
View File
@@ -34,7 +34,7 @@ func DeployKeys(ctx *context.Context) {
// DeployKeysPost response for adding a deploy key of a repository
func DeployKeysPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.AddKeyForm)
form := web.GetForm[*forms.AddKeyForm](ctx)
ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
ctx.Data["PageIsSettingsKeys"] = true
ctx.Data["DisableSSH"] = setting.SSH.Disabled
+2 -2
View File
@@ -109,7 +109,7 @@ func SettingsProtectedBranch(c *context.Context) {
// SettingsProtectedBranchPost updates the protected branch settings
func SettingsProtectedBranchPost(ctx *context.Context) {
f := web.GetForm(ctx).(*forms.ProtectBranchForm)
f := web.GetForm[*forms.ProtectBranchForm](ctx)
var protectBranch *git_model.ProtectedBranch
if f.RuleName == "" {
ctx.Flash.Error(ctx.Tr("repo.settings.protected_branch_required_rule_name"))
@@ -343,7 +343,7 @@ func UpdateBranchProtectionPriories(ctx *context.Context) {
// RenameBranchPost responses for rename a branch
func RenameBranchPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RenameBranchForm)
form := web.GetForm[*forms.RenameBranchForm](ctx)
if !ctx.Repo.CanCreateBranch() {
ctx.NotFound(nil)
+2 -2
View File
@@ -46,7 +46,7 @@ func NewProtectedTagPost(ctx *context.Context) {
}
repo := ctx.Repo.Repository
form := web.GetForm(ctx).(*forms.ProtectTagForm)
form := web.GetForm[*forms.ProtectTagForm](ctx)
pt := &git_model.ProtectedTag{
RepoID: repo.ID,
@@ -107,7 +107,7 @@ func EditProtectedTagPost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.ProtectTagForm)
form := web.GetForm[*forms.ProtectTagForm](ctx)
pt.NamePattern = strings.TrimSpace(form.NamePattern)
pt.AllowlistUserIDs, _ = base.StringsToInt64s(strings.Split(form.AllowlistUsers, ","))
+24 -23
View File
@@ -198,7 +198,7 @@ func SettingsPost(ctx *context.Context) {
}
func handleSettingsPostUpdate(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if ctx.HasError() {
ctx.HTML(http.StatusOK, tplSettingsOptions)
@@ -215,11 +215,13 @@ func handleSettingsPostUpdate(ctx *context.Context) {
}
if err := repo_service.ChangeRepositoryName(ctx, ctx.Doer, repo, newRepoName); err != nil {
ctx.Data["Err_RepoName"] = true
var errNameReserved db.ErrNameReserved
var errNamePatternNotAllowed db.ErrNamePatternNotAllowed
switch {
case repo_model.IsErrRepoAlreadyExist(err):
ctx.RenderWithErrDeprecated(ctx.Tr("form.repo_name_been_taken"), tplSettingsOptions, &form)
case db.IsErrNameReserved(err):
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplSettingsOptions, &form)
case errors.As(err, &errNameReserved):
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_reserved", errNameReserved.Name), tplSettingsOptions, &form)
case repo_model.IsErrRepoFilesAlreadyExist(err):
ctx.Data["Err_RepoName"] = true
switch {
@@ -232,8 +234,8 @@ func handleSettingsPostUpdate(ctx *context.Context) {
default:
ctx.RenderWithErrDeprecated(ctx.Tr("form.repository_files_already_exist"), tplSettingsOptions, form)
}
case db.IsErrNamePatternNotAllowed(err):
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form)
case errors.As(err, &errNamePatternNotAllowed):
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_pattern_not_allowed", errNamePatternNotAllowed.Pattern), tplSettingsOptions, &form)
default:
ctx.ServerError("ChangeRepositoryName", err)
}
@@ -260,7 +262,7 @@ func handleSettingsPostUpdate(ctx *context.Context) {
}
func handleSettingsPostMirror(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !setting.Mirror.Enabled || !repo.IsMirror || repo.IsArchived {
ctx.NotFound(nil)
@@ -375,7 +377,7 @@ func handleSettingsPostMirrorSync(ctx *context.Context) {
}
func handleSettingsPostPushMirrorSync(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !setting.Mirror.Enabled {
@@ -396,7 +398,7 @@ func handleSettingsPostPushMirrorSync(ctx *context.Context) {
}
func handleSettingsPostPushMirrorUpdate(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !setting.Mirror.Enabled || repo.IsArchived {
@@ -438,7 +440,7 @@ func handleSettingsPostPushMirrorUpdate(ctx *context.Context) {
}
func handleSettingsPostPushMirrorRemove(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !setting.Mirror.Enabled || repo.IsArchived {
@@ -471,7 +473,7 @@ func handleSettingsPostPushMirrorRemove(ctx *context.Context) {
}
func handleSettingsPostPushMirrorAdd(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if setting.Mirror.DisableNewPush || repo.IsArchived {
@@ -546,7 +548,7 @@ func newRepoUnit(repo *repo_model.Repository, unitType unit_model.Type, config c
}
func handleSettingsPostAdvanced(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
var repoChanged bool
var units []repo_model.RepoUnit
@@ -703,7 +705,7 @@ func handleSettingsPostAdvanced(ctx *context.Context) {
}
func handleSettingsPostSigning(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
trustModel := repo_model.ToTrustModel(form.TrustModel)
if trustModel != repo.TrustModel {
@@ -726,7 +728,7 @@ func handleSettingsPostAdmin(ctx *context.Context) {
}
repo := ctx.Repo.Repository
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
if repo.IsFsckEnabled != form.EnableHealthCheck {
repo.IsFsckEnabled = form.EnableHealthCheck
if err := repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_fsck_enabled"); err != nil {
@@ -741,7 +743,7 @@ func handleSettingsPostAdmin(ctx *context.Context) {
}
func handleSettingsPostAdminIndex(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !ctx.Doer.IsAdmin {
ctx.HTTPError(http.StatusForbidden)
@@ -772,7 +774,7 @@ func handleSettingsPostAdminIndex(ctx *context.Context) {
}
func handleSettingsPostConvert(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !ctx.Repo.Permission.IsOwner() {
ctx.JSONErrorNotFound()
@@ -802,7 +804,7 @@ func handleSettingsPostConvert(ctx *context.Context) {
}
func handleSettingsPostConvertFork(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !ctx.Repo.Permission.IsOwner() {
ctx.JSONErrorNotFound()
@@ -842,7 +844,7 @@ func handleSettingsPostConvertFork(ctx *context.Context) {
}
func handleSettingsPostTransfer(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !ctx.Repo.Permission.IsOwner() {
ctx.JSONErrorNotFound()
@@ -883,8 +885,8 @@ func handleSettingsPostTransfer(ctx *context.Context) {
ctx.JSONError(ctx.Tr("repo.settings.new_owner_has_same_repo"))
} else if repo_model.IsErrRepoTransferInProgress(err) {
ctx.JSONError(ctx.Tr("repo.settings.transfer_in_progress"))
} else if repo_service.IsRepositoryLimitReached(err) {
limit := err.(repo_service.LimitReachedError).Limit
} else if errLimitReached, ok := err.(repo_service.LimitReachedError); ok {
limit := errLimitReached.Limit
ctx.JSONError(ctx.TrN(limit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", limit))
} else if errors.Is(err, user_model.ErrBlockedUser) {
ctx.JSONError(ctx.Tr("repo.settings.transfer.blocked_user"))
@@ -934,7 +936,7 @@ func handleSettingsPostCancelTransfer(ctx *context.Context) {
}
func handleSettingsPostDelete(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !ctx.Repo.Permission.IsOwner() {
ctx.JSONErrorNotFound()
@@ -961,7 +963,7 @@ func handleSettingsPostDelete(ctx *context.Context) {
}
func handleSettingsPostDeleteWiki(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.RepoSettingForm)
form := web.GetForm[*forms.RepoSettingForm](ctx)
repo := ctx.Repo.Repository
if !ctx.Repo.Permission.IsOwner() {
ctx.JSONErrorNotFound()
@@ -1075,8 +1077,7 @@ func handleSettingsPostVisibility(ctx *context.Context) {
}
func handleSettingRemoteAddrError(ctx *context.Context, err error, form *forms.RepoSettingForm) {
if git.IsErrInvalidCloneAddr(err) {
addrErr := err.(*git.ErrInvalidCloneAddr)
if addrErr, ok := err.(*git.ErrInvalidCloneAddr); ok {
switch {
case addrErr.IsProtocolInvalid:
ctx.RenderWithErrDeprecated(ctx.Tr("repo.mirror_address_protocol_invalid"), tplSettingsOptions, form)
+11 -11
View File
@@ -326,7 +326,7 @@ func GiteaHooksEditPost(ctx *context.Context) {
}
func giteaHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewWebhookForm)
form := web.GetForm[*forms.NewWebhookForm](ctx)
contentType := webhook.ContentTypeJSON
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
@@ -353,7 +353,7 @@ func GogsHooksEditPost(ctx *context.Context) {
}
func gogsHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewGogshookForm)
form := web.GetForm[*forms.NewGogshookForm](ctx)
contentType := webhook.ContentTypeJSON
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
@@ -379,7 +379,7 @@ func DiscordHooksEditPost(ctx *context.Context) {
}
func discordHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewDiscordHookForm)
form := web.GetForm[*forms.NewDiscordHookForm](ctx)
return webhookParams{
Type: webhook_module.DISCORD,
@@ -404,7 +404,7 @@ func DingtalkHooksEditPost(ctx *context.Context) {
}
func dingtalkHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
form := web.GetForm[*forms.NewDingtalkHookForm](ctx)
return webhookParams{
Type: webhook_module.DINGTALK,
@@ -425,7 +425,7 @@ func TelegramHooksEditPost(ctx *context.Context) {
}
func telegramHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
form := web.GetForm[*forms.NewTelegramHookForm](ctx)
return webhookParams{
Type: webhook_module.TELEGRAM,
@@ -459,7 +459,7 @@ func matrixRoomIDEncode(roomID string) string {
}
func matrixHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
form := web.GetForm[*forms.NewMatrixHookForm](ctx)
// TODO: need to migrate to the latest (v3) API: https://spec.matrix.org/v1.18/client-server-api/
return webhookParams{
@@ -487,7 +487,7 @@ func MSTeamsHooksEditPost(ctx *context.Context) {
}
func mSTeamsHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm)
form := web.GetForm[*forms.NewMSTeamsHookForm](ctx)
return webhookParams{
Type: webhook_module.MSTEAMS,
@@ -508,7 +508,7 @@ func SlackHooksEditPost(ctx *context.Context) {
}
func slackHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewSlackHookForm)
form := web.GetForm[*forms.NewSlackHookForm](ctx)
return webhookParams{
Type: webhook_module.SLACK,
@@ -535,7 +535,7 @@ func FeishuHooksEditPost(ctx *context.Context) {
}
func feishuHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewFeishuHookForm)
form := web.GetForm[*forms.NewFeishuHookForm](ctx)
return webhookParams{
Type: webhook_module.FEISHU,
@@ -556,7 +556,7 @@ func WechatworkHooksEditPost(ctx *context.Context) {
}
func wechatworkHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm)
form := web.GetForm[*forms.NewWechatWorkHookForm](ctx)
return webhookParams{
Type: webhook_module.WECHATWORK,
@@ -577,7 +577,7 @@ func PackagistHooksEditPost(ctx *context.Context) {
}
func packagistHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
form := web.GetForm[*forms.NewPackagistHookForm](ctx)
return webhookParams{
Type: webhook_module.PACKAGIST,