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
@@ -717,7 +717,7 @@ func fillViewRunResponseSummary(ctx *context_module.Context, resp *ViewResponse,
}
func fillViewRunResponseCurrentJob(ctx *context_module.Context, resp *ViewResponse, run *actions_model.ActionRun, jobs []*actions_model.ActionRunJob) {
req := web.GetForm(ctx).(*ViewRequest)
req := web.GetForm[*ViewRequest](ctx)
current, hasPathParam := findCurrentJobByPathParam(ctx, jobs)
if current == nil {
if hasPathParam {
+4 -7
View File
@@ -176,7 +176,7 @@ func jsonRedirectBranches(ctx *context.Context) {
// CreateBranch creates new branch in repository
func CreateBranch(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewBranchForm)
form := web.GetForm[*forms.NewBranchForm](ctx)
if !ctx.Repo.CanCreateBranch() {
ctx.NotFound(nil)
return
@@ -208,8 +208,7 @@ func CreateBranch(ctx *context.Context) {
return
}
if release_service.IsErrTagAlreadyExists(err) {
e := err.(release_service.ErrTagAlreadyExists)
if e, ok := err.(release_service.ErrTagAlreadyExists); ok {
ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
@@ -219,14 +218,12 @@ func CreateBranch(ctx *context.Context) {
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
if git_model.IsErrBranchNameConflict(err) {
e := err.(git_model.ErrBranchNameConflict)
if e, ok := err.(git_model.ErrBranchNameConflict); ok {
ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
if git.IsErrPushRejected(err) {
e := err.(*git.ErrPushRejected)
if e, ok := err.(*git.ErrPushRejected); ok {
if len(e.Message) == 0 {
ctx.Flash.Error(ctx.Tr("repo.editor.push_rejected_no_message"))
} else {
+1 -1
View File
@@ -112,7 +112,7 @@ func (f *preparedEditorCommitForm[T]) GetCommitMessage(defaultCommitMessage stri
}
func prepareEditorCommitSubmittedForm[T forms.CommitCommonFormInterface](ctx *context.Context) *preparedEditorCommitForm[T] {
form := web.GetForm(ctx).(T)
form := web.GetForm[T](ctx)
if ctx.HasError() {
ctx.JSONError(ctx.GetErrMsg())
return nil
+7 -5
View File
@@ -135,7 +135,7 @@ func Fork(ctx *context.Context) {
// ForkPost response for forking a repository
func ForkPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateRepoForm)
form := web.GetForm[*forms.CreateRepoForm](ctx)
ctx.Data["Title"] = ctx.Tr("new_fork")
ctxUser := checkContextUser(ctx, form.UID)
@@ -205,6 +205,8 @@ func ForkRepoTo(ctx *context.Context, owner *user_model.User, forkOpts repo_serv
repo, err := repo_service.ForkRepository(ctx, ctx.Doer, owner, forkOpts)
if err != nil {
ctx.Data["Err_RepoName"] = true
var errNameReserved db.ErrNameReserved
var errNamePatternNotAllowed db.ErrNamePatternNotAllowed
switch {
case repo_model.IsErrReachLimitOfRepo(err):
maxCreationLimit := owner.MaxCreationLimit()
@@ -223,10 +225,10 @@ func ForkRepoTo(ctx *context.Context, owner *user_model.User, forkOpts repo_serv
default:
ctx.JSONError(ctx.Tr("form.repository_files_already_exist"))
}
case db.IsErrNameReserved(err):
ctx.JSONError(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name))
case db.IsErrNamePatternNotAllowed(err):
ctx.JSONError(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern))
case errors.As(err, &errNameReserved):
ctx.JSONError(ctx.Tr("repo.form.name_reserved", errNameReserved.Name))
case errors.As(err, &errNamePatternNotAllowed):
ctx.JSONError(ctx.Tr("repo.form.name_pattern_not_allowed", errNamePatternNotAllowed.Pattern))
case errors.Is(err, user_model.ErrBlockedUser):
ctx.JSONError(ctx.Tr("repo.fork.blocked_user"))
default:
+1 -1
View File
@@ -480,7 +480,7 @@ func UpdateIssueAssignee(ctx *context.Context) {
// ChangeIssueReaction create a reaction for issue
func ChangeIssueReaction(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.ReactionForm)
form := web.GetForm[*forms.ReactionForm](ctx)
issue := GetActionIssue(ctx)
if ctx.Written() {
return
+2 -2
View File
@@ -41,7 +41,7 @@ func NewComment(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.CreateCommentForm)
form := web.GetForm[*forms.CreateCommentForm](ctx)
issueType := util.Iif(issue.IsPull, "pulls", "issues")
if !ctx.IsSigned || (ctx.Doer.ID != issue.PosterID && !ctx.Repo.Permission.CanReadIssuesOrPulls(issue.IsPull)) {
@@ -306,7 +306,7 @@ func DeleteComment(ctx *context.Context) {
// ChangeCommentReaction create a reaction for comment
func ChangeCommentReaction(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.ReactionForm)
form := web.GetForm[*forms.ReactionForm](ctx)
comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id"))
if err != nil {
ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err)
+3 -4
View File
@@ -36,16 +36,15 @@ func Labels(ctx *context.Context) {
// InitializeLabels init labels for a repository
func InitializeLabels(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.InitializeLabelsForm)
form := web.GetForm[*forms.InitializeLabelsForm](ctx)
if ctx.HasError() {
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
return
}
if err := repo_module.InitializeLabels(ctx, ctx.Repo.Repository.ID, form.TemplateName, false); err != nil {
if label.IsErrTemplateLoad(err) {
originalErr := err.(label.ErrTemplateLoad).OriginalError
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, originalErr))
if errTemplateLoad, ok := err.(label.ErrTemplateLoad); ok {
ctx.Flash.Error(ctx.Tr("repo.issues.label_templates.fail_to_load_file", form.TemplateName, errTemplateLoad.OriginalError))
ctx.Redirect(ctx.Repo.RepoLink + "/labels")
return
}
+1 -1
View File
@@ -13,7 +13,7 @@ import (
// LockIssue locks an issue. This would limit commenting abilities to
// users with write access to the repo.
func LockIssue(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.IssueLockForm)
form := web.GetForm[*forms.IssueLockForm](ctx)
issue := GetActionIssue(ctx)
if ctx.Written() {
return
+1 -1
View File
@@ -322,7 +322,7 @@ func ValidateRepoMetasForNewIssue(ctx *context.Context, form forms.CreateIssueFo
// NewIssuePost response for creating new issue
func NewIssuePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateIssueForm)
form := web.GetForm[*forms.CreateIssueForm](ctx)
repo := ctx.Repo.Repository
+1 -1
View File
@@ -19,7 +19,7 @@ import (
// AddTimeManually tracks time manually
func AddTimeManually(c *context.Context) {
form := web.GetForm(c).(*forms.AddTimeManuallyForm)
form := web.GetForm[*forms.AddTimeManuallyForm](c)
issue := GetActionIssue(c)
if c.Written() {
return
+5 -4
View File
@@ -499,8 +499,8 @@ func (prInfo *pullRequestViewInfo) prepareMergeBoxCommitSigning(ctx *context.Con
data.willSign = sign
data.signingKeyMergeDisplay = asymkey_model.GetDisplaySigningKey(key)
if err != nil {
if asymkey_service.IsErrWontSign(err) {
wontSignReason = string(err.(*asymkey_service.ErrWontSign).Reason)
if errWontSign, ok := err.(*asymkey_service.ErrWontSign); ok {
wontSignReason = string(errWontSign.Reason)
} else {
wontSignReason = "error"
if !errors.Is(err, util.ErrNotExist) {
@@ -560,8 +560,9 @@ func prepareIssueViewSidebarTimeTracker(ctx *context.Context, issue *issues_mode
if ctx.IsSigned {
// Deal with the stopwatch
ctx.Data["IsStopwatchRunning"] = issues_model.StopwatchExists(ctx, ctx.Doer.ID, issue.ID)
if !ctx.Data["IsStopwatchRunning"].(bool) {
isStopwatchRunning := issues_model.StopwatchExists(ctx, ctx.Doer.ID, issue.ID)
ctx.Data["IsStopwatchRunning"] = isStopwatchRunning
if !isStopwatchRunning {
exists, _, swIssue, err := issues_model.HasUserStopwatch(ctx, ctx.Doer.ID)
if err != nil {
ctx.ServerError("HasUserStopwatch", err)
+9 -7
View File
@@ -5,6 +5,7 @@
package repo
import (
"errors"
"net/http"
"net/url"
"strings"
@@ -77,6 +78,8 @@ func handleMigrateError(ctx *context.Context, owner *user_model.User, err error,
return
}
var errNameReserved db.ErrNameReserved
var errNamePatternNotAllowed db.ErrNamePatternNotAllowed
switch {
case migrations.IsRateLimitError(err):
ctx.RenderWithErrDeprecated(ctx.Tr("form.visit_rate_limit"), tpl, form)
@@ -101,12 +104,12 @@ func handleMigrateError(ctx *context.Context, owner *user_model.User, err error,
default:
ctx.RenderWithErrDeprecated(ctx.Tr("form.repository_files_already_exist"), tpl, form)
}
case db.IsErrNameReserved(err):
case errors.As(err, &errNameReserved):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form)
case db.IsErrNamePatternNotAllowed(err):
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_reserved", errNameReserved.Name), tpl, form)
case errors.As(err, &errNamePatternNotAllowed):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form)
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_pattern_not_allowed", errNamePatternNotAllowed.Pattern), tpl, form)
default:
err = util.SanitizeErrorCredentialURLs(err)
if strings.Contains(err.Error(), "Authentication failed") ||
@@ -124,8 +127,7 @@ func handleMigrateError(ctx *context.Context, owner *user_model.User, err error,
}
func handleMigrateRemoteAddrError(ctx *context.Context, err error, tpl templates.TplName, form *forms.MigrateRepoForm) {
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"), tpl, form)
@@ -151,7 +153,7 @@ func handleMigrateRemoteAddrError(ctx *context.Context, err error, tpl templates
// MigratePost response for migrating from external git repository
func MigratePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.MigrateRepoForm)
form := web.GetForm[*forms.MigrateRepoForm](ctx)
if setting.Repository.DisableMigrations {
ctx.HTTPError(http.StatusForbidden, "MigratePost: the site administrator has disabled migrations")
return
+2 -2
View File
@@ -105,7 +105,7 @@ func NewMilestone(ctx *context.Context) {
// NewMilestonePost response for creating milestone
func NewMilestonePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateMilestoneForm)
form := web.GetForm[*forms.CreateMilestoneForm](ctx)
ctx.Data["Title"] = ctx.Tr("repo.milestones.new")
ctx.Data["PageIsIssueList"] = true
ctx.Data["PageIsMilestones"] = true
@@ -161,7 +161,7 @@ func EditMilestone(ctx *context.Context) {
// EditMilestonePost response for edting milestone
func EditMilestonePost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateMilestoneForm)
form := web.GetForm[*forms.CreateMilestoneForm](ctx)
ctx.Data["Title"] = ctx.Tr("repo.milestones.edit")
ctx.Data["PageIsMilestones"] = true
ctx.Data["PageIsEditMilestone"] = true
+2 -2
View File
@@ -127,7 +127,7 @@ func RenderNewProject(ctx *context.Context) {
// NewProjectPost creates a new project
func NewProjectPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateProjectForm)
form := web.GetForm[*forms.CreateProjectForm](ctx)
ctx.Data["Title"] = ctx.Tr("repo.projects.new")
if ctx.HasError() {
@@ -231,7 +231,7 @@ func RenderEditProject(ctx *context.Context) {
// EditProjectPost response for editing a project
func EditProjectPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateProjectForm)
form := web.GetForm[*forms.CreateProjectForm](ctx)
projectID := ctx.PathParamInt64("id")
ctx.Data["Title"] = ctx.Tr("repo.projects.edit")
+13 -19
View File
@@ -1007,8 +1007,7 @@ func UpdatePullRequest(ctx *context.Context) {
// The update process should not be canceled by the user
// so we set the context to be a background context
if err = pull_service.Update(graceful.GetManager().ShutdownContext(), issue.PullRequest, ctx.Doer, message, rebase); err != nil {
if pull_service.IsErrMergeConflicts(err) {
conflictError := err.(pull_service.ErrMergeConflicts)
if conflictError, ok := err.(pull_service.ErrMergeConflicts); ok {
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
"Message": ctx.Tr("repo.pulls.merge_conflict"),
"Summary": ctx.Tr("repo.pulls.merge_conflict_summary"),
@@ -1020,8 +1019,7 @@ func UpdatePullRequest(ctx *context.Context) {
}
ctx.JSONError(flashError)
return
} else if pull_service.IsErrRebaseConflicts(err) {
conflictError := err.(pull_service.ErrRebaseConflicts)
} else if conflictError, ok := err.(pull_service.ErrRebaseConflicts); ok {
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.EscapeFlashErrorString(conflictError.CommitSHA)),
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
@@ -1047,7 +1045,7 @@ func UpdatePullRequest(ctx *context.Context) {
// MergePullRequest response for merging pull request
func MergePullRequest(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.MergePullRequestForm)
form := web.GetForm[*forms.MergePullRequestForm](ctx)
issue, ok := getPullInfo(ctx)
if !ok {
return
@@ -1156,8 +1154,7 @@ func MergePullRequest(ctx *context.Context) {
if err := pull_service.Merge(ctx, pr, ctx.Doer, repo_model.MergeStyle(form.Do), form.HeadCommitID, message, false); err != nil {
if pull_service.IsErrInvalidMergeStyle(err) {
ctx.JSONError(ctx.Tr("repo.pulls.invalid_merge_option"))
} else if pull_service.IsErrMergeConflicts(err) {
conflictError := err.(pull_service.ErrMergeConflicts)
} else if conflictError, ok := err.(pull_service.ErrMergeConflicts); ok {
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
"Message": ctx.Tr("repo.editor.merge_conflict"),
"Summary": ctx.Tr("repo.editor.merge_conflict_summary"),
@@ -1169,8 +1166,7 @@ func MergePullRequest(ctx *context.Context) {
}
ctx.Flash.Error(flashError)
ctx.JSONRedirect(issue.Link())
} else if pull_service.IsErrRebaseConflicts(err) {
conflictError := err.(pull_service.ErrRebaseConflicts)
} else if conflictError, ok := err.(pull_service.ErrRebaseConflicts); ok {
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
"Message": ctx.Tr("repo.pulls.rebase_conflict", utils.EscapeFlashErrorString(conflictError.CommitSHA)),
"Summary": ctx.Tr("repo.pulls.rebase_conflict_summary"),
@@ -1194,9 +1190,8 @@ func MergePullRequest(ctx *context.Context) {
log.Debug("MergeHeadOutOfDate error: %v", err)
ctx.Flash.Error(ctx.Tr("repo.pulls.head_out_of_date"))
ctx.JSONRedirect(issue.Link())
} else if git.IsErrPushRejected(err) {
} else if pushrejErr, ok := err.(*git.ErrPushRejected); ok {
log.Debug("MergePushRejected error: %v", err)
pushrejErr := err.(*git.ErrPushRejected)
message := pushrejErr.Message
if len(message) == 0 {
ctx.Flash.Error(ctx.Tr("repo.pulls.push_rejected_no_message"))
@@ -1322,7 +1317,7 @@ func PullsNewRedirect(ctx *context.Context) {
// CompareAndPullRequestPost response for creating pull request
func CompareAndPullRequestPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CreateIssueForm)
form := web.GetForm[*forms.CreateIssueForm](ctx)
repo := ctx.Repo.Repository
comparePageInfo := newComparePageInfo()
err := comparePageInfo.parseCompareInfo(ctx, ctx.PathParam("*"))
@@ -1416,11 +1411,11 @@ func CompareAndPullRequestPost(ctx *context.Context) {
ProjectIDs: projectIDs,
}
if err := pull_service.NewPullRequest(ctx, prOpts); err != nil {
var pushrejErr *git.ErrPushRejected
switch {
case repo_model.IsErrUserDoesNotHaveAccessToRepo(err):
ctx.HTTPError(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
case git.IsErrPushRejected(err):
pushrejErr := err.(*git.ErrPushRejected)
case errors.As(err, &pushrejErr):
message := pushrejErr.Message
if len(message) == 0 {
ctx.JSONError(ctx.Tr("repo.pulls.push_rejected_no_message"))
@@ -1537,6 +1532,7 @@ func UpdatePullRequestTarget(ctx *context.Context) {
}
if err := pull_service.ChangeTargetBranch(ctx, pr, ctx.Doer, targetBranch); err != nil {
var prExistsErr issues_model.ErrPullRequestAlreadyExists
switch {
case git_model.IsErrBranchNotExist(err):
errorMessage := ctx.Tr("form.target_branch_not_exist")
@@ -1546,11 +1542,9 @@ func UpdatePullRequestTarget(ctx *context.Context) {
"error": err.Error(),
"user_error": errorMessage,
})
case issues_model.IsErrPullRequestAlreadyExists(err):
err := err.(issues_model.ErrPullRequestAlreadyExists)
case errors.As(err, &prExistsErr):
RepoRelPath := ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name
errorMessage := ctx.Tr("repo.pulls.has_pull_request", html.EscapeString(ctx.Repo.RepoLink+"/pulls/"+strconv.FormatInt(err.IssueID, 10)), html.EscapeString(RepoRelPath), err.IssueID) // FIXME: Creates url inside locale string
errorMessage := ctx.Tr("repo.pulls.has_pull_request", html.EscapeString(ctx.Repo.RepoLink+"/pulls/"+strconv.FormatInt(prExistsErr.IssueID, 10)), html.EscapeString(RepoRelPath), prExistsErr.IssueID) // FIXME: Creates url inside locale string
ctx.Flash.Error(errorMessage)
ctx.JSON(http.StatusConflict, map[string]any{
@@ -1595,7 +1589,7 @@ func UpdatePullRequestTarget(ctx *context.Context) {
// SetAllowEdits allow edits from maintainers to PRs
func SetAllowEdits(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.UpdateAllowEditsForm)
form := web.GetForm[*forms.UpdateAllowEditsForm](ctx)
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
+3 -3
View File
@@ -62,7 +62,7 @@ func RenderNewCodeCommentForm(ctx *context.Context) {
// CreateCodeComment will create a code comment including an pending review if required
func CreateCodeComment(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.CodeCommentForm)
form := web.GetForm[*forms.CodeCommentForm](ctx)
issue := GetActionIssue(ctx)
if ctx.Written() {
return
@@ -221,7 +221,7 @@ func renderConversation(ctx *context.Context, comment *issues_model.Comment, ori
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
func SubmitReview(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.SubmitReviewForm)
form := web.GetForm[*forms.SubmitReviewForm](ctx)
issue := GetActionIssue(ctx)
if ctx.Written() {
return
@@ -279,7 +279,7 @@ func SubmitReview(ctx *context.Context) {
// DismissReview dismissing stale review by repo admin
func DismissReview(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.DismissReviewForm)
form := web.GetForm[*forms.DismissReviewForm](ctx)
comm, err := pull_service.DismissReview(ctx, form.ReviewID, ctx.Repo.Repository.ID, form.Message, ctx.Doer, true, true)
if err != nil {
if pull_service.IsErrDismissRequestOnClosedPR(err) {
+6 -6
View File
@@ -48,8 +48,8 @@ func calReleaseNumCommitsBehind(ctx stdCtx.Context, repoCtx *context.Repository,
if _, ok := countCache[target]; !ok {
commit, err := repoCtx.GitRepo.GetBranchCommit(ctx, target)
if err != nil {
var errNotExist git.ErrNotExist
if target == repoCtx.Repository.DefaultBranch || !errors.As(err, &errNotExist) {
_, isNotExist := errors.AsType[git.ErrNotExist](err)
if target == repoCtx.Repository.DefaultBranch || !isNotExist {
return fmt.Errorf("GetBranchCommit: %w", err)
}
// fallback to default branch
@@ -189,7 +189,7 @@ func Releases(ctx *context.Context) {
ctx.Data["Releases"] = releases
numReleases := ctx.Data["NumReleases"].(int64)
numReleases := ctx.Data["NumReleases"].(int64) //nolint:forcetypeassert // must exist
pager := context.NewPagination(numReleases, listOptions.PageSize, listOptions.Page, 5)
pager.AddParamFromRequest(ctx.Req)
ctx.Data["Page"] = pager
@@ -387,7 +387,7 @@ func NewRelease(ctx *context.Context) {
// GenerateReleaseNotes builds release notes content for the given tag and base.
func GenerateReleaseNotes(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.GenerateReleaseNotesForm)
form := web.GetForm[*forms.GenerateReleaseNotesForm](ctx)
if ctx.HasError() {
ctx.JSONError(ctx.GetErrMsg())
@@ -418,7 +418,7 @@ func NewReleasePost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.NewReleaseForm)
form := web.GetForm[*forms.NewReleaseForm](ctx)
// first, check whether the release exists, and prepare "ShowCreateTagOnlyButton"
// the logic should be done before the form error check to make the tmpl has correct variables
@@ -579,7 +579,7 @@ func EditReleasePost(ctx *context.Context) {
return
}
form := web.GetForm(ctx).(*forms.EditReleaseForm)
form := web.GetForm[*forms.EditReleaseForm](ctx)
tagName := ctx.PathParam("*")
rel, err := repo_model.GetRelease(ctx, ctx.Repo.Repository.ID, tagName)
+10 -7
View File
@@ -165,6 +165,8 @@ func Create(ctx *context.Context) {
}
func handleCreateError(ctx *context.Context, owner *user_model.User, err error, name string, tpl templates.TplName, form any) {
var errNameReserved db.ErrNameReserved
var errNamePatternNotAllowed db.ErrNamePatternNotAllowed
switch {
case repo_model.IsErrReachLimitOfRepo(err):
maxCreationLimit := owner.MaxCreationLimit()
@@ -185,12 +187,12 @@ func handleCreateError(ctx *context.Context, owner *user_model.User, err error,
default:
ctx.RenderWithErrDeprecated(ctx.Tr("form.repository_files_already_exist"), tpl, form)
}
case db.IsErrNameReserved(err):
case errors.As(err, &errNameReserved):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tpl, form)
case db.IsErrNamePatternNotAllowed(err):
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_reserved", errNameReserved.Name), tpl, form)
case errors.As(err, &errNamePatternNotAllowed):
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tpl, form)
ctx.RenderWithErrDeprecated(ctx.Tr("repo.form.name_pattern_not_allowed", errNamePatternNotAllowed.Pattern), tpl, form)
default:
ctx.ServerError(name, err)
}
@@ -199,7 +201,7 @@ func handleCreateError(ctx *context.Context, owner *user_model.User, err error,
// CreatePost response for creating repository
func CreatePost(ctx *context.Context) {
createCommon(ctx)
form := web.GetForm(ctx).(*forms.CreateRepoForm)
form := web.GetForm[*forms.CreateRepoForm](ctx)
ctxUser := checkContextUser(ctx, form.UID)
if ctx.Written() {
@@ -283,11 +285,12 @@ func CreatePost(ctx *context.Context) {
}
func handleActionError(ctx *context.Context, err error) {
var errLimitReached repo_service.LimitReachedError
switch {
case errors.Is(err, user_model.ErrBlockedUser):
ctx.JSONError(ctx.Tr("repo.action.blocked_user"))
case repo_service.IsRepositoryLimitReached(err):
limit := err.(repo_service.LimitReachedError).Limit
case errors.As(err, &errLimitReached):
limit := errLimitReached.Limit
ctx.JSONError(ctx.TrN(limit, "repo.form.reach_limit_of_creation_1", "repo.form.reach_limit_of_creation_n", limit))
case errors.Is(err, util.ErrPermissionDenied):
ctx.JSONError(ctx.Tr("error.permission_denied"))
+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,
+2 -2
View File
@@ -655,7 +655,7 @@ func NewWiki(ctx *context.Context) {
// NewWikiPost response for wiki create request
func NewWikiPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewWikiForm)
form := web.GetForm[*forms.NewWikiForm](ctx)
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
if ctx.HasError() {
@@ -711,7 +711,7 @@ func EditWiki(ctx *context.Context) {
// EditWikiPost response for wiki modify request
func EditWikiPost(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewWikiForm)
form := web.GetForm[*forms.NewWikiForm](ctx)
ctx.Data["Title"] = ctx.Tr("repo.wiki.new_page")
if ctx.HasError() {