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
+4 -4
View File
@@ -135,7 +135,7 @@ func (Action) CreateOrUpdateSecret(ctx *context.APIContext) {
repo := ctx.Repo.Repository
opt := web.GetForm(ctx).(*api.CreateOrUpdateSecretOption)
opt := web.GetForm[*api.CreateOrUpdateSecretOption](ctx)
_, created, err := secret_service.CreateOrUpdateSecret(ctx, 0, repo.ID, ctx.PathParam("secretname"), opt.Data, opt.Description)
if err != nil {
@@ -346,7 +346,7 @@ func (Action) CreateVariable(ctx *context.APIContext) {
// "500":
// "$ref": "#/responses/error"
opt := web.GetForm(ctx).(*api.CreateVariableOption)
opt := web.GetForm[*api.CreateVariableOption](ctx)
repoID := ctx.Repo.Repository.ID
variableName := ctx.PathParam("variablename")
@@ -413,7 +413,7 @@ func (Action) UpdateVariable(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
opt := web.GetForm(ctx).(*api.UpdateVariableOption)
opt := web.GetForm[*api.UpdateVariableOption](ctx)
v, err := actions_service.GetVariable(ctx, actions_model.FindVariablesOpts{
RepoID: ctx.Repo.Repository.ID,
@@ -1170,7 +1170,7 @@ func ActionsDispatchWorkflow(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
workflowID := ctx.PathParam("workflow_id")
opt := web.GetForm(ctx).(*api.CreateActionWorkflowDispatch)
opt := web.GetForm[*api.CreateActionWorkflowDispatch](ctx)
if opt.Ref == "" {
ctx.APIError(http.StatusUnprocessableEntity, "ref is required parameter")
return
+1 -1
View File
@@ -40,7 +40,7 @@ func UpdateAvatar(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.UpdateRepoAvatarOption)
form := web.GetForm[*api.UpdateRepoAvatarOption](ctx)
content, err := base64.StdEncoding.DecodeString(form.Image)
if err != nil {
+10 -10
View File
@@ -212,7 +212,7 @@ func CreateBranch(ctx *context.APIContext) {
return
}
opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
opt := web.GetForm[*api.CreateBranchRepoOption](ctx)
var oldCommit *git.Commit
var err error
@@ -426,7 +426,7 @@ func UpdateBranch(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opt := web.GetForm(ctx).(*api.UpdateBranchRepoOption)
opt := web.GetForm[*api.UpdateBranchRepoOption](ctx)
branchName := ctx.PathParam("*")
repo := ctx.Repo.Repository
@@ -443,14 +443,14 @@ func UpdateBranch(ctx *context.APIContext) {
// permission check has been done in api.go
if err := repo_service.UpdateBranch(ctx, repo, ctx.Repo.GitRepo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil {
var errPushRejected *git.ErrPushRejected
switch {
case git_model.IsErrBranchNotExist(err):
ctx.APIErrorNotFound()
case errors.Is(err, util.ErrInvalidArgument):
ctx.APIError(http.StatusUnprocessableEntity, err.Error())
case git.IsErrPushRejected(err):
rej := err.(*git.ErrPushRejected)
ctx.APIError(http.StatusForbidden, rej.Message)
case errors.As(err, &errPushRejected):
ctx.APIError(http.StatusForbidden, errPushRejected.Message)
default:
ctx.APIErrorInternal(err)
}
@@ -499,7 +499,7 @@ func RenameBranch(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opt := web.GetForm(ctx).(*api.RenameBranchRepoOption)
opt := web.GetForm[*api.RenameBranchRepoOption](ctx)
oldName := ctx.PathParam("*")
repo := ctx.Repo.Repository
@@ -654,7 +654,7 @@ func CreateBranchProtection(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateBranchProtectionOption)
form := web.GetForm[*api.CreateBranchProtectionOption](ctx)
repo := ctx.Repo.Repository
ruleName := form.RuleName
@@ -875,7 +875,7 @@ func EditBranchProtection(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.EditBranchProtectionOption)
form := web.GetForm[*api.EditBranchProtectionOption](ctx)
repo := ctx.Repo.Repository
bpName := ctx.PathParam("*")
protectBranch, err := git_model.GetProtectedBranchRuleByName(ctx, repo.ID, bpName)
@@ -1292,7 +1292,7 @@ func UpdateBranchProtectionPriories(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.UpdateBranchProtectionPriories)
form := web.GetForm[*api.UpdateBranchProtectionPriories](ctx)
repo := ctx.Repo.Repository
if err := git_model.UpdateProtectBranchPriorities(ctx, repo, form.IDs); err != nil {
@@ -1331,7 +1331,7 @@ func MergeUpstream(ctx *context.APIContext) {
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.MergeUpstreamRequest)
form := web.GetForm[*api.MergeUpstreamRequest](ctx)
mergeStyle, err := repo_service.MergeUpstream(ctx, ctx.Doer, ctx.Repo.Repository, form.Branch, form.FfOnly)
if err != nil {
if errors.Is(err, util.ErrInvalidArgument) {
+1 -1
View File
@@ -162,7 +162,7 @@ func AddOrUpdateCollaborator(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.AddCollaboratorOption)
form := web.GetForm[*api.AddCollaboratorOption](ctx)
collaborator, err := user_model.GetUserByName(ctx, ctx.PathParam("collaborator"))
if err != nil {
+23 -26
View File
@@ -323,14 +323,19 @@ func base64Reader(s string) (io.ReadSeeker, error) {
}
func ReqChangeRepoFileOptionsAndCheck(ctx *context.APIContext) {
commonOpts := web.GetForm(ctx).(api.FileOptionsInterface).GetFileOptions()
commonOpts := web.GetForm[api.FileOptionsInterface](ctx).GetFileOptions()
commonOpts.BranchName = util.IfZero(commonOpts.BranchName, ctx.Repo.Repository.DefaultBranch)
commonOpts.NewBranchName = util.IfZero(commonOpts.NewBranchName, commonOpts.BranchName)
if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, commonOpts.NewBranchName) && !ctx.IsUserSiteAdmin() {
ctx.APIError(http.StatusForbidden, "user should have a permission to write to the target branch")
return
}
changeFileOpts := &files_service.ChangeRepoFilesOptions{
}
// getAPIChangeRepoFileOptions requires ReqChangeRepoFileOptionsAndCheck to have run, it fills in the branch defaults
func getAPIChangeRepoFileOptions[T api.FileOptionsInterface](ctx *context.APIContext) (apiOpts T, opts *files_service.ChangeRepoFilesOptions) {
apiOpts = web.GetForm[T](ctx)
commonOpts := apiOpts.GetFileOptions()
opts = &files_service.ChangeRepoFilesOptions{
Message: commonOpts.Message,
OldBranch: commonOpts.BranchName,
NewBranch: commonOpts.NewBranchName,
@@ -349,17 +354,13 @@ func ReqChangeRepoFileOptionsAndCheck(ctx *context.APIContext) {
},
Signoff: commonOpts.Signoff,
}
if changeFileOpts.Dates.Author.IsZero() {
changeFileOpts.Dates.Author = time.Now()
if opts.Dates.Author.IsZero() {
opts.Dates.Author = time.Now()
}
if changeFileOpts.Dates.Committer.IsZero() {
changeFileOpts.Dates.Committer = time.Now()
if opts.Dates.Committer.IsZero() {
opts.Dates.Committer = time.Now()
}
ctx.Data["__APIChangeRepoFilesOptions"] = changeFileOpts
}
func getAPIChangeRepoFileOptions[T api.FileOptionsInterface](ctx *context.APIContext) (apiOpts T, opts *files_service.ChangeRepoFilesOptions) {
return web.GetForm(ctx).(T), ctx.Data["__APIChangeRepoFilesOptions"].(*files_service.ChangeRepoFilesOptions)
return apiOpts, opts
}
// ChangeFiles handles API call for modifying multiple files
@@ -574,9 +575,8 @@ func UpdateFile(ctx *context.APIContext) {
}
func handleChangeRepoFilesError(ctx *context.APIContext, err error) {
if git.IsErrPushRejected(err) {
err := err.(*git.ErrPushRejected)
ctx.APIError(http.StatusForbidden, err.Message)
if errPushRejected, ok := err.(*git.ErrPushRejected); ok {
ctx.APIError(http.StatusForbidden, errPushRejected.Message)
return
}
if files_service.IsErrUserCannotCommit(err) || pull_service.IsErrFilePathProtected(err) {
@@ -896,7 +896,12 @@ func GetFileContentsGet(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// The POST method requires "write" permission, so we also support this "GET" method
handleGetFileContents(ctx)
opts := &api.GetFilesOptions{}
if err := json.Unmarshal(util.UnsafeStringToBytes(ctx.FormString("body")), opts); err != nil {
ctx.APIError(http.StatusBadRequest, "invalid body parameter")
return
}
handleGetFileContents(ctx, opts)
}
func GetFileContentsPost(ctx *context.APIContext) {
@@ -940,18 +945,10 @@ func GetFileContentsPost(ctx *context.APIContext) {
// This is actually a "read" request, but we need to accept a "files" list, then POST method seems easy to use.
// But the permission system requires that the caller must have "write" permission to use POST method.
// At the moment, there is no other way to get around the permission check, so there is a "GET" workaround method above.
handleGetFileContents(ctx)
handleGetFileContents(ctx, web.GetForm[*api.GetFilesOptions](ctx))
}
func handleGetFileContents(ctx *context.APIContext) {
opts, ok := web.GetForm(ctx).(*api.GetFilesOptions)
if !ok {
err := json.Unmarshal(util.UnsafeStringToBytes(ctx.FormString("body")), &opts)
if err != nil {
ctx.APIError(http.StatusBadRequest, "invalid body parameter")
return
}
}
func handleGetFileContents(ctx *context.APIContext, opts *api.GetFilesOptions) {
refCommit := resolveRefCommit(ctx, ctx.FormTrim("ref"))
if ctx.Written() {
return
+1 -1
View File
@@ -148,7 +148,7 @@ func CreateFork(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.CreateForkOption)
form := web.GetForm[*api.CreateForkOption](ctx)
forkOwner := ctx.Doer // user/org that will own the fork
if form.Organization != nil {
org := prepareDoerCreateRepoInOrg(ctx, *form.Organization)
+1 -1
View File
@@ -126,7 +126,7 @@ func EditGitHook(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditGitHookOption)
form := web.GetForm[*api.EditGitHookOption](ctx)
hookID := ctx.PathParam("id")
hook, err := git.GetHook(ctx.Repo.GitRepo, hookID)
if err != nil {
+2 -2
View File
@@ -226,7 +226,7 @@ func CreateHook(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
utils.AddRepoHook(ctx, web.GetForm(ctx).(*api.CreateHookOption))
utils.AddRepoHook(ctx, web.GetForm[*api.CreateHookOption](ctx))
}
// EditHook modify a hook of a repository
@@ -262,7 +262,7 @@ func EditHook(ctx *context.APIContext) {
// "$ref": "#/responses/Hook"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditHookOption)
form := web.GetForm[*api.EditHookOption](ctx)
hookID := ctx.PathParamInt64("id")
utils.EditRepoHook(ctx, form, hookID)
}
+3 -3
View File
@@ -631,7 +631,7 @@ func CreateIssue(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateIssueOption)
form := web.GetForm[*api.CreateIssueOption](ctx)
var deadlineUnix timeutil.TimeStamp
if form.Deadline != nil && ctx.Repo.Permission.CanWrite(unit.TypeIssues) {
deadlineUnix = timeutil.TimeStamp(form.Deadline.Unix())
@@ -759,7 +759,7 @@ func EditIssue(ctx *context.APIContext) {
// "412":
// "$ref": "#/responses/error"
form := web.GetForm(ctx).(*api.EditIssueOption)
form := web.GetForm[*api.EditIssueOption](ctx)
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
if issues_model.IsErrIssueNotExist(err) {
@@ -1012,7 +1012,7 @@ func UpdateIssueDeadline(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditDeadlineOption)
form := web.GetForm[*api.EditDeadlineOption](ctx)
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
if issues_model.IsErrIssueNotExist(err) {
+2 -2
View File
@@ -60,7 +60,7 @@ func AddIssueAssignees(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.IssueAssigneesOption)
opts := web.GetForm[*api.IssueAssigneesOption](ctx)
updateIssueAssignees(ctx, *opts, true)
}
@@ -105,7 +105,7 @@ func DeleteIssueAssignees(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.IssueAssigneesOption)
opts := web.GetForm[*api.IssueAssigneesOption](ctx)
updateIssueAssignees(ctx, *opts, false)
}
+1 -1
View File
@@ -265,7 +265,7 @@ func EditIssueAttachment(ctx *context.APIContext) {
}
// do changes to attachment. only meaningful change is name.
form := web.GetForm(ctx).(*api.EditAttachmentOptions)
form := web.GetForm[*api.EditAttachmentOptions](ctx)
if form.Name != "" {
attachment.Name = form.Name
}
+3 -3
View File
@@ -379,7 +379,7 @@ func CreateIssueComment(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateIssueCommentOption)
form := web.GetForm[*api.CreateIssueCommentOption](ctx)
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
ctx.APIErrorInternal(err)
@@ -511,7 +511,7 @@ func EditIssueComment(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.EditIssueCommentOption)
form := web.GetForm[*api.EditIssueCommentOption](ctx)
editIssueComment(ctx, *form)
}
@@ -561,7 +561,7 @@ func EditIssueCommentDeprecated(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditIssueCommentOption)
form := web.GetForm[*api.EditIssueCommentOption](ctx)
editIssueComment(ctx, *form)
}
@@ -278,7 +278,7 @@ func EditIssueCommentAttachment(ctx *context.APIContext) {
return
}
form := web.GetForm(ctx).(*api.EditAttachmentOptions)
form := web.GetForm[*api.EditAttachmentOptions](ctx)
if form.Name != "" {
attach.Name = form.Name
}
+4 -4
View File
@@ -183,7 +183,7 @@ func CreateIssueDependency(ctx *context.APIContext) {
}
// and <Form> represents the dependency
form := web.GetForm(ctx).(*api.IssueMeta)
form := web.GetForm[*api.IssueMeta](ctx)
dependency := getFormIssue(ctx, form)
if ctx.Written() {
return
@@ -244,7 +244,7 @@ func RemoveIssueDependency(ctx *context.APIContext) {
}
// and <Form> represents the dependency
form := web.GetForm(ctx).(*api.IssueMeta)
form := web.GetForm[*api.IssueMeta](ctx)
dependency := getFormIssue(ctx, form)
if ctx.Written() {
return
@@ -404,7 +404,7 @@ func CreateIssueBlocking(ctx *context.APIContext) {
return
}
form := web.GetForm(ctx).(*api.IssueMeta)
form := web.GetForm[*api.IssueMeta](ctx)
target := getFormIssue(ctx, form)
if ctx.Written() {
return
@@ -461,7 +461,7 @@ func RemoveIssueBlocking(ctx *context.APIContext) {
return
}
form := web.GetForm(ctx).(*api.IssueMeta)
form := web.GetForm[*api.IssueMeta](ctx)
target := getFormIssue(ctx, form)
if ctx.Written() {
return
+2 -2
View File
@@ -103,7 +103,7 @@ func AddIssueLabels(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.IssueLabelsOption)
form := web.GetForm[*api.IssueLabelsOption](ctx)
issue, labels, err := prepareForReplaceOrAdd(ctx, *form)
if err != nil {
return
@@ -232,7 +232,7 @@ func ReplaceIssueLabels(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.IssueLabelsOption)
form := web.GetForm[*api.IssueLabelsOption](ctx)
issue, labels, err := prepareForReplaceOrAdd(ctx, *form)
if err != nil {
return
+1 -1
View File
@@ -50,7 +50,7 @@ func LockIssue(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
reason := web.GetForm(ctx).(*api.LockIssueOption).Reason
reason := web.GetForm[*api.LockIssueOption](ctx).Reason
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
ctx.APIErrorAuto(err)
+4 -4
View File
@@ -135,7 +135,7 @@ func PostIssueCommentReaction(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditReactionOption)
form := web.GetForm[*api.EditReactionOption](ctx)
changeIssueCommentReaction(ctx, *form, true)
}
@@ -178,7 +178,7 @@ func DeleteIssueCommentReaction(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditReactionOption)
form := web.GetForm[*api.EditReactionOption](ctx)
changeIssueCommentReaction(ctx, *form, false)
}
@@ -364,7 +364,7 @@ func PostIssueReaction(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditReactionOption)
form := web.GetForm[*api.EditReactionOption](ctx)
changeIssueReaction(ctx, *form, true)
}
@@ -405,7 +405,7 @@ func DeleteIssueReaction(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditReactionOption)
form := web.GetForm[*api.EditReactionOption](ctx)
changeIssueReaction(ctx, *form, false)
}
+1 -1
View File
@@ -199,7 +199,7 @@ func AddTime(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.AddTimeOption)
form := web.GetForm[*api.AddTimeOption](ctx)
issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
ctx.APIErrorAuto(err)
+1 -1
View File
@@ -231,7 +231,7 @@ func CreateDeployKey(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.CreateKeyOption)
form := web.GetForm[*api.CreateKeyOption](ctx)
content, err := asymkey_model.CheckPublicKeyString(form.Key)
if err != nil {
HandleCheckKeyStringError(ctx, err)
+2 -2
View File
@@ -145,7 +145,7 @@ func CreateLabel(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.CreateLabelOption)
form := web.GetForm[*api.CreateLabelOption](ctx)
color, err := label.NormalizeColor(form.Color)
if err != nil {
@@ -207,7 +207,7 @@ func EditLabel(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.EditLabelOption)
form := web.GetForm[*api.EditLabelOption](ctx)
l, err := issues_model.GetLabelInRepoByID(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("id"))
if err != nil {
ctx.APIErrorAuto(err)
+13 -9
View File
@@ -56,7 +56,7 @@ func Migrate(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.MigrateRepoOptions)
form := web.GetForm[*api.MigrateRepoOptions](ctx)
// get repoOwner
var (
@@ -217,6 +217,11 @@ func Migrate(ctx *context.APIContext) {
}
func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, err error) {
var (
errNameReserved db.ErrNameReserved
errNameCharsNotAllowed db.ErrNameCharsNotAllowed
errNamePatternNotAllowed db.ErrNamePatternNotAllowed
)
switch {
case repo_model.IsErrRepoAlreadyExist(err):
ctx.APIError(http.StatusConflict, "The repository with the same name already exists.")
@@ -228,12 +233,12 @@ func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, err
ctx.APIError(http.StatusUnprocessableEntity, "Remote visit required two factors authentication.")
case repo_model.IsErrReachLimitOfRepo(err):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("You have already reached your limit of %d repositories.", repoOwner.MaxCreationLimit()))
case db.IsErrNameReserved(err):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The username '%s' is reserved.", err.(db.ErrNameReserved).Name))
case db.IsErrNameCharsNotAllowed(err):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The username '%s' contains invalid characters.", err.(db.ErrNameCharsNotAllowed).Name))
case db.IsErrNamePatternNotAllowed(err):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The pattern '%s' is not allowed in a username.", err.(db.ErrNamePatternNotAllowed).Pattern))
case errors.As(err, &errNameReserved):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The username '%s' is reserved.", errNameReserved.Name))
case errors.As(err, &errNameCharsNotAllowed):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The username '%s' contains invalid characters.", errNameCharsNotAllowed.Name))
case errors.As(err, &errNamePatternNotAllowed):
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("The pattern '%s' is not allowed in a username.", errNamePatternNotAllowed.Pattern))
case git.IsErrInvalidCloneAddr(err):
ctx.APIError(http.StatusUnprocessableEntity, err.Error())
case base.IsErrNotSupported(err):
@@ -253,8 +258,7 @@ func handleMigrateError(ctx *context.APIContext, repoOwner *user_model.User, err
}
func handleRemoteAddrError(ctx *context.APIContext, err error) {
if git.IsErrInvalidCloneAddr(err) {
addrErr := err.(*git.ErrInvalidCloneAddr)
if addrErr, ok := err.(*git.ErrInvalidCloneAddr); ok {
switch {
case addrErr.IsURLError:
ctx.APIError(http.StatusUnprocessableEntity, "The provided URL is invalid.")
+2 -2
View File
@@ -147,7 +147,7 @@ func CreateMilestone(ctx *context.APIContext) {
// "$ref": "#/responses/Milestone"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.CreateMilestoneOption)
form := web.GetForm[*api.CreateMilestoneOption](ctx)
var deadlineUnix int64
if form.Deadline != nil {
@@ -207,7 +207,7 @@ func EditMilestone(ctx *context.APIContext) {
// "$ref": "#/responses/Milestone"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditMilestoneOption)
form := web.GetForm[*api.EditMilestoneOption](ctx)
milestone := getMilestoneByIDOrName(ctx)
if ctx.Written() {
return
+2 -3
View File
@@ -291,7 +291,7 @@ func AddPushMirror(ctx *context.APIContext) {
return
}
pushMirror := web.GetForm(ctx).(*api.CreatePushMirrorOption)
pushMirror := web.GetForm[*api.CreatePushMirrorOption](ctx)
CreatePushMirror(ctx, pushMirror)
}
@@ -403,8 +403,7 @@ func CreatePushMirror(ctx *context.APIContext, mirrorOption *api.CreatePushMirro
}
func HandleRemoteAddressError(ctx *context.APIContext, err error) {
if git.IsErrInvalidCloneAddr(err) {
addrErr := err.(*git.ErrInvalidCloneAddr)
if addrErr, ok := err.(*git.ErrInvalidCloneAddr); ok {
switch {
case addrErr.IsProtocolInvalid:
ctx.APIError(http.StatusBadRequest, "Invalid mirror protocol")
+7 -11
View File
@@ -404,7 +404,7 @@ func CreatePullRequest(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := *web.GetForm(ctx).(*api.CreatePullRequestOption)
form := *web.GetForm[*api.CreatePullRequestOption](ctx)
if form.Head == form.Base {
ctx.APIError(http.StatusUnprocessableEntity, "Invalid PullRequest: There are no changes between the head and the base")
return
@@ -628,7 +628,7 @@ func EditPullRequest(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.EditPullRequestOption)
form := web.GetForm[*api.EditPullRequestOption](ctx)
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
if issues_model.IsErrPullRequestNotExist(err) {
@@ -922,7 +922,7 @@ func MergePullRequest(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*forms.MergePullRequestForm)
form := web.GetForm[*forms.MergePullRequestForm](ctx)
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
@@ -1044,21 +1044,17 @@ func MergePullRequest(ctx *context.APIContext) {
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.APIError(http.StatusMethodNotAllowed, fmt.Sprintf("%s is not allowed an allowed merge style for this repository", repo_model.MergeStyle(form.Do)))
} else if pull_service.IsErrMergeConflicts(err) {
conflictError := err.(pull_service.ErrMergeConflicts)
} else if conflictError, ok := err.(pull_service.ErrMergeConflicts); ok {
ctx.JSON(http.StatusConflict, conflictError)
} else if pull_service.IsErrRebaseConflicts(err) {
conflictError := err.(pull_service.ErrRebaseConflicts)
} else if conflictError, ok := err.(pull_service.ErrRebaseConflicts); ok {
ctx.JSON(http.StatusConflict, conflictError)
} else if pull_service.IsErrMergeUnrelatedHistories(err) {
conflictError := err.(pull_service.ErrMergeUnrelatedHistories)
} else if conflictError, ok := err.(pull_service.ErrMergeUnrelatedHistories); ok {
ctx.JSON(http.StatusConflict, conflictError)
} else if git.IsErrPushOutOfDate(err) {
ctx.APIError(http.StatusConflict, "merge push out of date")
} else if pull_service.IsErrSHADoesNotMatch(err) {
ctx.APIError(http.StatusConflict, "head out of date")
} else if git.IsErrPushRejected(err) {
errPushRej := err.(*git.ErrPushRejected)
} else if errPushRej, ok := err.(*git.ErrPushRejected); ok {
if len(errPushRej.Message) == 0 {
ctx.APIError(http.StatusConflict, "PushRejected without remote error message")
} else {
+6 -6
View File
@@ -252,7 +252,7 @@ func CreatePullReviewCommentReply(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.CreatePullReviewCommentReplyOptions)
opts := web.GetForm[*api.CreatePullReviewCommentReplyOptions](ctx)
parent := getPullReviewCommentToResolve(ctx)
if parent == nil {
@@ -499,7 +499,7 @@ func CreatePullReview(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.CreatePullReviewOptions)
opts := web.GetForm[*api.CreatePullReviewOptions](ctx)
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
if err != nil {
ctx.APIErrorAuto(err)
@@ -622,7 +622,7 @@ func SubmitPullReview(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.SubmitPullReviewOptions)
opts := web.GetForm[*api.SubmitPullReviewOptions](ctx)
review, pr, isWrong := prepareSingleReview(ctx)
if isWrong {
return
@@ -792,7 +792,7 @@ func CreateReviewRequests(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
opts := web.GetForm(ctx).(*api.PullReviewRequestOptions)
opts := web.GetForm[*api.PullReviewRequestOptions](ctx)
apiReviewRequest(ctx, *opts, true)
}
@@ -834,7 +834,7 @@ func DeleteReviewRequests(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "404":
// "$ref": "#/responses/notFound"
opts := web.GetForm(ctx).(*api.PullReviewRequestOptions)
opts := web.GetForm[*api.PullReviewRequestOptions](ctx)
apiReviewRequest(ctx, *opts, false)
}
@@ -1014,7 +1014,7 @@ func DismissPullReview(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.DismissPullReviewOptions)
opts := web.GetForm[*api.DismissPullReviewOptions](ctx)
dismissReview(ctx, opts.Message, true, opts.Priors)
}
+3 -3
View File
@@ -28,7 +28,7 @@ func canAccessReleaseDraft(ctx *context.APIContext) bool {
return true
}
// the request is from an access token with scope
scope := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope)
scope := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope) //nolint:forcetypeassert // must exist
requiredScopes := auth_model.GetRequiredScopes(auth_model.Write, auth_model.AccessTokenScopeCategoryRepository)
allow, _ := scope.HasScope(requiredScopes...) // err (invalid token) can be safely ignored
return allow
@@ -244,7 +244,7 @@ func CreateRelease(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.CreateReleaseOption)
form := web.GetForm[*api.CreateReleaseOption](ctx)
if ctx.Repo.Repository.IsEmpty {
ctx.APIError(http.StatusUnprocessableEntity, "repo is empty")
return
@@ -346,7 +346,7 @@ func EditRelease(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditReleaseOption)
form := web.GetForm[*api.EditReleaseOption](ctx)
id := ctx.PathParamInt64("id")
rel, err := repo_model.GetReleaseForRepoByID(ctx, ctx.Repo.Repository.ID, id)
if err != nil && !repo_model.IsErrReleaseNotExist(err) {
+1 -1
View File
@@ -310,7 +310,7 @@ func EditReleaseAttachment(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.EditAttachmentOptions)
form := web.GetForm[*api.EditAttachmentOptions](ctx)
// Check if release exists an load release
releaseID := ctx.PathParamInt64("id")
+4 -4
View File
@@ -298,7 +298,7 @@ func Create(ctx *context.APIContext) {
// description: The repository with the same name already exists.
// "422":
// "$ref": "#/responses/validationError"
opt := web.GetForm(ctx).(*api.CreateRepoOption)
opt := web.GetForm[*api.CreateRepoOption](ctx)
if ctx.Doer.IsOrganization() {
// Shouldn't reach this condition, but just in case.
ctx.APIError(http.StatusUnprocessableEntity, "not allowed creating repository for organization")
@@ -342,7 +342,7 @@ func Generate(ctx *context.APIContext) {
// description: The repository with the same name already exists.
// "422":
// "$ref": "#/responses/validationError"
form := web.GetForm(ctx).(*api.GenerateRepoOption)
form := web.GetForm[*api.GenerateRepoOption](ctx)
if !ctx.Repo.Repository.IsTemplate {
ctx.APIError(http.StatusUnprocessableEntity, "this is not a template repo")
@@ -484,7 +484,7 @@ func CreateOrgRepo(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "403":
// "$ref": "#/responses/forbidden"
opt := web.GetForm(ctx).(*api.CreateRepoOption)
opt := web.GetForm[*api.CreateRepoOption](ctx)
orgName := ctx.PathParam("org")
org := prepareDoerCreateRepoInOrg(ctx, orgName)
if ctx.Written() {
@@ -603,7 +603,7 @@ func Edit(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := *web.GetForm(ctx).(*api.EditRepoOption)
opts := *web.GetForm[*api.EditRepoOption](ctx)
if err := updateBasicProperties(ctx, opts); err != nil {
return
+1 -1
View File
@@ -52,7 +52,7 @@ func NewCommitStatus(ctx *context.APIContext) {
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.CreateStatusOption)
form := web.GetForm[*api.CreateStatusOption](ctx)
sha := ctx.PathParam("sha")
if len(sha) == 0 {
ctx.APIError(http.StatusBadRequest, "sha not provided")
+3 -3
View File
@@ -194,7 +194,7 @@ func CreateTag(ctx *context.APIContext) {
// "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateTagOption)
form := web.GetForm[*api.CreateTagOption](ctx)
// If target is not provided use default branch
if len(form.Target) == 0 {
@@ -411,7 +411,7 @@ func CreateTagProtection(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateTagProtectionOption)
form := web.GetForm[*api.CreateTagProtectionOption](ctx)
repo := ctx.Repo.Repository
namePattern := strings.TrimSpace(form.NamePattern)
@@ -522,7 +522,7 @@ func EditTagProtection(ctx *context.APIContext) {
// "$ref": "#/responses/repoArchivedError"
repo := ctx.Repo.Repository
form := web.GetForm(ctx).(*api.EditTagProtectionOption)
form := web.GetForm[*api.EditTagProtectionOption](ctx)
id := ctx.PathParamInt64("id")
pt, err := git_model.GetProtectedTagByID(ctx, id)
+1 -1
View File
@@ -101,7 +101,7 @@ func UpdateTopics(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/invalidTopicsError"
form := web.GetForm(ctx).(*api.RepoTopicOptions)
form := web.GetForm[*api.RepoTopicOptions](ctx)
topicNames := form.Topics
validTopics, invalidTopics := repo_model.SanitizeAndValidateTopics(topicNames)
+1 -1
View File
@@ -56,7 +56,7 @@ func Transfer(ctx *context.APIContext) {
// "422":
// "$ref": "#/responses/validationError"
opts := web.GetForm(ctx).(*api.TransferRepoOption)
opts := web.GetForm[*api.TransferRepoOption](ctx)
newOwner, err := user_model.GetUserByName(ctx, opts.NewOwner)
if err != nil {
+2 -2
View File
@@ -55,7 +55,7 @@ func NewWikiPage(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateWikiPageOptions)
form := web.GetForm[*api.CreateWikiPageOptions](ctx)
if util.IsEmptyString(form.Title) {
ctx.APIError(http.StatusBadRequest, "title is required")
@@ -133,7 +133,7 @@ func EditWikiPage(ctx *context.APIContext) {
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateWikiPageOptions)
form := web.GetForm[*api.CreateWikiPageOptions](ctx)
oldWikiName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("pageName"))
newWikiName := wiki_service.UserTitleToWebPath("", form.Title)