Skip to content

Commit dd5b24d

Browse files
committed
Merge remote-tracking branch 'giteaofficial/main'
* giteaofficial/main: Disable `vet` as part of `go test` (go-gitea#33662) Refactor error system (go-gitea#33771) Add migrations and doctor fixes (go-gitea#33556) Refactor mail code (go-gitea#33768) Refactor global init code and add more comments (go-gitea#33755)
2 parents acf52c7 + 43c8d85 commit dd5b24d

File tree

37 files changed

+816
-698
lines changed

37 files changed

+816
-698
lines changed

.github/workflows/pull-compliance.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ jobs:
9595
go-version-file: go.mod
9696
check-latest: true
9797
- run: make deps-backend deps-tools
98-
- run: make lint-go-windows lint-go-vet
98+
- run: make lint-go-windows lint-go-gitea-vet
9999
env:
100100
TAGS: bindata sqlite sqlite_unlock_notify
101101
GOOS: windows

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ EXTRA_GOFLAGS ?=
7373
MAKE_VERSION := $(shell "$(MAKE)" -v | cat | head -n 1)
7474
MAKE_EVIDENCE_DIR := .make_evidence
7575

76+
GOTESTFLAGS ?= -vet=off
7677
ifeq ($(RACE_ENABLED),true)
7778
GOFLAGS += -race
7879
GOTESTFLAGS += -race
@@ -311,10 +312,10 @@ lint-frontend: lint-js lint-css ## lint frontend files
311312
lint-frontend-fix: lint-js-fix lint-css-fix ## lint frontend files and fix issues
312313

313314
.PHONY: lint-backend
314-
lint-backend: lint-go lint-go-vet lint-go-gopls lint-editorconfig ## lint backend files
315+
lint-backend: lint-go lint-go-gitea-vet lint-go-gopls lint-editorconfig ## lint backend files
315316

316317
.PHONY: lint-backend-fix
317-
lint-backend-fix: lint-go-fix lint-go-vet lint-editorconfig ## lint backend files and fix issues
318+
lint-backend-fix: lint-go-fix lint-go-gitea-vet lint-editorconfig ## lint backend files and fix issues
318319

319320
.PHONY: lint-js
320321
lint-js: node_modules ## lint js files
@@ -365,9 +366,9 @@ lint-go-windows:
365366
@GOOS= GOARCH= $(GO) install $(GOLANGCI_LINT_PACKAGE)
366367
golangci-lint run
367368

368-
.PHONY: lint-go-vet
369-
lint-go-vet: ## lint go files with vet
370-
@echo "Running go vet..."
369+
.PHONY: lint-go-gitea-vet
370+
lint-go-gitea-vet: ## lint go files with gitea-vet
371+
@echo "Running gitea-vet..."
371372
@GOOS= GOARCH= $(GO) build code.gitea.io/gitea-vet
372373
@$(GO) vet -vettool=gitea-vet ./...
373374

models/actions/runner.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,17 @@ func FixRunnersWithoutBelongingRepo(ctx context.Context) (int64, error) {
337337
}
338338
return res.RowsAffected()
339339
}
340+
341+
func CountWrongRepoLevelRunners(ctx context.Context) (int64, error) {
342+
var result int64
343+
_, err := db.GetEngine(ctx).SQL("SELECT count(`id`) FROM `action_runner` WHERE `repo_id` > 0 AND `owner_id` > 0").Get(&result)
344+
return result, err
345+
}
346+
347+
func UpdateWrongRepoLevelRunners(ctx context.Context) (int64, error) {
348+
result, err := db.GetEngine(ctx).Exec("UPDATE `action_runner` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0")
349+
if err != nil {
350+
return 0, err
351+
}
352+
return result.RowsAffected()
353+
}

models/actions/variable.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,17 @@ func GetVariablesOfRun(ctx context.Context, run *ActionRun) (map[string]string,
147147

148148
return variables, nil
149149
}
150+
151+
func CountWrongRepoLevelVariables(ctx context.Context) (int64, error) {
152+
var result int64
153+
_, err := db.GetEngine(ctx).SQL("SELECT count(`id`) FROM `action_variable` WHERE `repo_id` > 0 AND `owner_id` > 0").Get(&result)
154+
return result, err
155+
}
156+
157+
func UpdateWrongRepoLevelVariables(ctx context.Context) (int64, error) {
158+
result, err := db.GetEngine(ctx).Exec("UPDATE `action_variable` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0")
159+
if err != nil {
160+
return 0, err
161+
}
162+
return result.RowsAffected()
163+
}

models/asymkey/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (err ErrKeyUnableVerify) Error() string {
2525
}
2626

2727
// ErrKeyIsPrivate is returned when the provided key is a private key not a public key
28-
var ErrKeyIsPrivate = util.NewSilentWrapErrorf(util.ErrInvalidArgument, "the provided key is a private key")
28+
var ErrKeyIsPrivate = util.ErrorWrap(util.ErrInvalidArgument, "the provided key is a private key")
2929

3030
// ErrKeyNotExist represents a "KeyNotExist" kind of error.
3131
type ErrKeyNotExist struct {

models/db/name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (err ErrNameCharsNotAllowed) Unwrap() error {
7777
func IsUsableName(reservedNames, reservedPatterns []string, name string) error {
7878
name = strings.TrimSpace(strings.ToLower(name))
7979
if utf8.RuneCountInString(name) == 0 {
80-
return util.SilentWrap{Message: "name is empty", Err: util.ErrInvalidArgument}
80+
return util.NewInvalidArgumentErrorf("name is empty")
8181
}
8282

8383
for i := range reservedNames {

models/migrations/migrations.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ func prepareMigrationTasks() []*migration {
374374
// Gitea 1.23.0-rc0 ends at migration ID number 311 (database version 312)
375375
newMigration(312, "Add DeleteBranchAfterMerge to AutoMerge", v1_24.AddDeleteBranchAfterMergeForAutoMerge),
376376
newMigration(313, "Move PinOrder from issue table to a new table issue_pin", v1_24.MovePinOrderToTableIssuePin),
377+
newMigration(314, "Update OwnerID as zero for repository level action tables", v1_24.UpdateOwnerIDOfRepoLevelActionsTables),
377378
}
378379
return preparedMigrations
379380
}

models/migrations/v1_24/v314.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_24 //nolint
5+
6+
import (
7+
"xorm.io/xorm"
8+
)
9+
10+
func UpdateOwnerIDOfRepoLevelActionsTables(x *xorm.Engine) error {
11+
if _, err := x.Exec("UPDATE `action_runner` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0"); err != nil {
12+
return err
13+
}
14+
if _, err := x.Exec("UPDATE `action_variable` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0"); err != nil {
15+
return err
16+
}
17+
_, err := x.Exec("UPDATE `secret` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0")
18+
return err
19+
}

models/repo/archiver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func (archiver *RepoArchiver) RelativePath() string {
5050
func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
5151
parts := strings.SplitN(relativePath, "/", 3)
5252
if len(parts) != 3 {
53-
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
53+
return nil, util.NewInvalidArgumentErrorf("invalid storage path: must have 3 parts")
5454
}
5555
repoID, err := strconv.ParseInt(parts[0], 10, 64)
5656
if err != nil {
57-
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
57+
return nil, util.NewInvalidArgumentErrorf("invalid storage path: invalid repo id")
5858
}
5959
commitID, archiveType := git.SplitArchiveNameType(parts[2])
6060
if archiveType == git.ArchiveUnknown {
61-
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
61+
return nil, util.NewInvalidArgumentErrorf("invalid storage path: invalid archive type")
6262
}
6363
return &RepoArchiver{RepoID: repoID, CommitID: commitID, Type: archiveType}, nil
6464
}

models/secret/secret.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,17 @@ func GetSecretsOfTask(ctx context.Context, task *actions_model.ActionTask) (map[
165165

166166
return secrets, nil
167167
}
168+
169+
func CountWrongRepoLevelSecrets(ctx context.Context) (int64, error) {
170+
var result int64
171+
_, err := db.GetEngine(ctx).SQL("SELECT count(`id`) FROM `secret` WHERE `repo_id` > 0 AND `owner_id` > 0").Get(&result)
172+
return result, err
173+
}
174+
175+
func UpdateWrongRepoLevelSecrets(ctx context.Context) (int64, error) {
176+
result, err := db.GetEngine(ctx).Exec("UPDATE `secret` SET `owner_id` = 0 WHERE `repo_id` > 0 AND `owner_id` > 0")
177+
if err != nil {
178+
return 0, err
179+
}
180+
return result.RowsAffected()
181+
}

models/user/must_change_password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func SetMustChangePassword(ctx context.Context, all, mustChangePassword bool, in
3434
if !all {
3535
include = sliceTrimSpaceDropEmpty(include)
3636
if len(include) == 0 {
37-
return 0, util.NewSilentWrapErrorf(util.ErrInvalidArgument, "no users to include provided")
37+
return 0, util.ErrorWrap(util.ErrInvalidArgument, "no users to include provided")
3838
}
3939

4040
cond = cond.And(builder.In("lower_name", include))

modules/markup/sanitizer_default_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ func TestSanitizer(t *testing.T) {
6262
`<a href="javascript:alert('xss')">bad</a>`, `bad`,
6363
`<a href="vbscript:no">bad</a>`, `bad`,
6464
`<a href="data:1234">bad</a>`, `bad`,
65+
66+
// Some classes and attributes are used by the frontend framework and will execute JS code, so make sure they are removed
67+
`<div class="link-action" data-attr-class="foo" data-url="xxx">txt</div>`, `<div data-attr-class="foo">txt</div>`,
68+
`<div class="form-fetch-action" data-markdown-generated-content="bar" data-global-init="a" data-global-click="b">txt</div>`, `<div data-markdown-generated-content="bar">txt</div>`,
6569
}
6670

6771
for i := 0; i < len(testCases); i += 2 {

modules/packages/conda/metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
)
1818

1919
var (
20-
ErrInvalidStructure = util.SilentWrap{Message: "package structure is invalid", Err: util.ErrInvalidArgument}
21-
ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument}
22-
ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument}
20+
ErrInvalidStructure = util.NewInvalidArgumentErrorf("package structure is invalid")
21+
ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid")
22+
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
2323
)
2424

2525
const (

modules/translation/i18n/errors.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

modules/translation/i18n/format.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package i18n
55

66
import (
7+
"errors"
78
"fmt"
89
"reflect"
910
)
@@ -30,7 +31,7 @@ func Format(format string, args ...any) (msg string, err error) {
3031
fmtArgs = append(fmtArgs, val.Index(i).Interface())
3132
}
3233
} else {
33-
err = ErrUncertainArguments
34+
err = errors.New("arguments to i18n should not contain uncertain slices")
3435
break
3536
}
3637
} else {

modules/translation/i18n/localestore.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package i18n
55

66
import (
7+
"errors"
78
"fmt"
89
"html/template"
910
"slices"
@@ -41,7 +42,7 @@ func NewLocaleStore() LocaleStore {
4142
// AddLocaleByIni adds locale by ini into the store
4243
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
4344
if _, ok := store.localeMap[langName]; ok {
44-
return ErrLocaleAlreadyExist
45+
return errors.New("lang has already been added")
4546
}
4647

4748
store.langNames = append(store.langNames, langName)

modules/util/error.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,74 +22,74 @@ var (
2222
ErrUnprocessableContent = errors.New("unprocessable content")
2323
)
2424

25-
// SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
25+
// errorWrapper provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
2626
// Especially useful for "untyped" errors created with "errors.New(…)" that can be classified as 'invalid argument', 'permission denied', 'exists already', or 'does not exist'
27-
type SilentWrap struct {
27+
type errorWrapper struct {
2828
Message string
2929
Err error
3030
}
3131

3232
// Error returns the message
33-
func (w SilentWrap) Error() string {
33+
func (w errorWrapper) Error() string {
3434
return w.Message
3535
}
3636

3737
// Unwrap returns the underlying error
38-
func (w SilentWrap) Unwrap() error {
38+
func (w errorWrapper) Unwrap() error {
3939
return w.Err
4040
}
4141

42-
type LocaleWrap struct {
42+
type LocaleWrapper struct {
4343
err error
4444
TrKey string
4545
TrArgs []any
4646
}
4747

4848
// Error returns the message
49-
func (w LocaleWrap) Error() string {
49+
func (w LocaleWrapper) Error() string {
5050
return w.err.Error()
5151
}
5252

5353
// Unwrap returns the underlying error
54-
func (w LocaleWrap) Unwrap() error {
54+
func (w LocaleWrapper) Unwrap() error {
5555
return w.err
5656
}
5757

58-
// NewSilentWrapErrorf returns an error that formats as the given text but unwraps as the provided error
59-
func NewSilentWrapErrorf(unwrap error, message string, args ...any) error {
58+
// ErrorWrap returns an error that formats as the given text but unwraps as the provided error
59+
func ErrorWrap(unwrap error, message string, args ...any) error {
6060
if len(args) == 0 {
61-
return SilentWrap{Message: message, Err: unwrap}
61+
return errorWrapper{Message: message, Err: unwrap}
6262
}
63-
return SilentWrap{Message: fmt.Sprintf(message, args...), Err: unwrap}
63+
return errorWrapper{Message: fmt.Sprintf(message, args...), Err: unwrap}
6464
}
6565

6666
// NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument
6767
func NewInvalidArgumentErrorf(message string, args ...any) error {
68-
return NewSilentWrapErrorf(ErrInvalidArgument, message, args...)
68+
return ErrorWrap(ErrInvalidArgument, message, args...)
6969
}
7070

7171
// NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied
7272
func NewPermissionDeniedErrorf(message string, args ...any) error {
73-
return NewSilentWrapErrorf(ErrPermissionDenied, message, args...)
73+
return ErrorWrap(ErrPermissionDenied, message, args...)
7474
}
7575

7676
// NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist
7777
func NewAlreadyExistErrorf(message string, args ...any) error {
78-
return NewSilentWrapErrorf(ErrAlreadyExist, message, args...)
78+
return ErrorWrap(ErrAlreadyExist, message, args...)
7979
}
8080

8181
// NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist
8282
func NewNotExistErrorf(message string, args ...any) error {
83-
return NewSilentWrapErrorf(ErrNotExist, message, args...)
83+
return ErrorWrap(ErrNotExist, message, args...)
8484
}
8585

86-
// ErrWrapLocale wraps an err with a translation key and arguments
87-
func ErrWrapLocale(err error, trKey string, trArgs ...any) error {
88-
return LocaleWrap{err: err, TrKey: trKey, TrArgs: trArgs}
86+
// ErrorWrapLocale wraps an err with a translation key and arguments
87+
func ErrorWrapLocale(err error, trKey string, trArgs ...any) error {
88+
return LocaleWrapper{err: err, TrKey: trKey, TrArgs: trArgs}
8989
}
9090

91-
func ErrAsLocale(err error) *LocaleWrap {
92-
var e LocaleWrap
91+
func ErrorAsLocale(err error) *LocaleWrapper {
92+
var e LocaleWrapper
9393
if errors.As(err, &e) {
9494
return &e
9595
}

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func Run(ctx *context_module.Context) {
789789
return nil
790790
})
791791
if err != nil {
792-
if errLocale := util.ErrAsLocale(err); errLocale != nil {
792+
if errLocale := util.ErrorAsLocale(err); errLocale != nil {
793793
ctx.Flash.Error(ctx.Tr(errLocale.TrKey, errLocale.TrArgs...))
794794
ctx.Redirect(redirectURL)
795795
} else {

services/actions/workflow.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionW
141141

142142
func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, workflowID, ref string, processInputs func(model *model.WorkflowDispatch, inputs map[string]any) error) error {
143143
if workflowID == "" {
144-
return util.ErrWrapLocale(
144+
return util.ErrorWrapLocale(
145145
util.NewNotExistErrorf("workflowID is empty"),
146146
"actions.workflow.not_found", workflowID,
147147
)
148148
}
149149

150150
if ref == "" {
151-
return util.ErrWrapLocale(
151+
return util.ErrorWrapLocale(
152152
util.NewNotExistErrorf("ref is empty"),
153153
"form.target_ref_not_exist", ref,
154154
)
@@ -158,7 +158,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
158158
cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions)
159159
cfg := cfgUnit.ActionsConfig()
160160
if cfg.IsWorkflowDisabled(workflowID) {
161-
return util.ErrWrapLocale(
161+
return util.ErrorWrapLocale(
162162
util.NewPermissionDeniedErrorf("workflow is disabled"),
163163
"actions.workflow.disabled",
164164
)
@@ -177,7 +177,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
177177
runTargetCommit, err = gitRepo.GetBranchCommit(ref)
178178
}
179179
if err != nil {
180-
return util.ErrWrapLocale(
180+
return util.ErrorWrapLocale(
181181
util.NewNotExistErrorf("ref %q doesn't exist", ref),
182182
"form.target_ref_not_exist", ref,
183183
)
@@ -208,7 +208,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
208208
}
209209

210210
if len(workflows) == 0 {
211-
return util.ErrWrapLocale(
211+
return util.ErrorWrapLocale(
212212
util.NewNotExistErrorf("workflow %q doesn't exist", workflowID),
213213
"actions.workflow.not_found", workflowID,
214214
)

0 commit comments

Comments
 (0)