Skip to content

Commit 8438de6

Browse files
committed
rewrite
1 parent 2a57ea3 commit 8438de6

File tree

13 files changed

+191
-254
lines changed

13 files changed

+191
-254
lines changed

models/git/protected_branch.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package git
55

66
import (
77
"context"
8-
"errors"
98
"fmt"
109
"slices"
1110
"strings"
@@ -25,7 +24,7 @@ import (
2524
"xorm.io/builder"
2625
)
2726

28-
var ErrBranchIsProtected = errors.New("branch is protected")
27+
var ErrBranchIsProtected = util.ErrorWrap(util.ErrPermissionDenied, "branch is protected")
2928

3029
// ProtectedBranch struct
3130
type ProtectedBranch struct {

modules/util/error.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package util
66
import (
77
"errors"
88
"fmt"
9+
"html/template"
910
)
1011

1112
// Common Errors forming the base of our error system
@@ -39,22 +40,6 @@ func (w errorWrapper) Unwrap() error {
3940
return w.Err
4041
}
4142

42-
type LocaleWrapper struct {
43-
err error
44-
TrKey string
45-
TrArgs []any
46-
}
47-
48-
// Error returns the message
49-
func (w LocaleWrapper) Error() string {
50-
return w.err.Error()
51-
}
52-
53-
// Unwrap returns the underlying error
54-
func (w LocaleWrapper) Unwrap() error {
55-
return w.err
56-
}
57-
5843
// ErrorWrap returns an error that formats as the given text but unwraps as the provided error
5944
func ErrorWrap(unwrap error, message string, args ...any) error {
6045
if len(args) == 0 {
@@ -83,15 +68,39 @@ func NewNotExistErrorf(message string, args ...any) error {
8368
return ErrorWrap(ErrNotExist, message, args...)
8469
}
8570

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}
71+
// ErrorTranslatable wraps an error with translation information
72+
type ErrorTranslatable interface {
73+
error
74+
Unwrap() error
75+
Translate(ErrorLocaleTranslator) template.HTML
76+
}
77+
78+
type errorTranslatableWrapper struct {
79+
err error
80+
trKey string
81+
trArgs []any
82+
}
83+
84+
type ErrorLocaleTranslator interface {
85+
Tr(key string, args ...any) template.HTML
86+
}
87+
88+
func (w *errorTranslatableWrapper) Error() string { return w.err.Error() }
89+
90+
func (w *errorTranslatableWrapper) Unwrap() error { return w.err }
91+
92+
func (w *errorTranslatableWrapper) Translate(t ErrorLocaleTranslator) template.HTML {
93+
return t.Tr(w.trKey, w.trArgs...)
94+
}
95+
96+
func ErrorWrapTranslatable(err error, trKey string, trArgs ...any) ErrorTranslatable {
97+
return &errorTranslatableWrapper{err: err, trKey: trKey, trArgs: trArgs}
8998
}
9099

91-
func ErrorAsLocale(err error) *LocaleWrapper {
92-
var e LocaleWrapper
100+
func ErrorAsTranslatable(err error) ErrorTranslatable {
101+
var e *errorTranslatableWrapper
93102
if errors.As(err, &e) {
94-
return &e
103+
return e
95104
}
96105
return nil
97106
}

routers/api/v1/repo/pull.go

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414

1515
activities_model "code.gitea.io/gitea/models/activities"
16-
git_model "code.gitea.io/gitea/models/git"
1716
issues_model "code.gitea.io/gitea/models/issues"
1817
access_model "code.gitea.io/gitea/models/perm/access"
1918
pull_model "code.gitea.io/gitea/models/pull"
@@ -989,25 +988,12 @@ func MergePullRequest(ctx *context.APIContext) {
989988
message += "\n\n" + form.MergeMessageField
990989
}
991990

992-
prUnit, err := ctx.Repo.Repository.GetUnit(ctx, unit.TypePullRequests)
991+
deleteBranchAfterMerge, err := pull_service.ShouldDeleteBranchAfterMerge(ctx, form.DeleteBranchAfterMerge, ctx.Repo.Repository, pr)
993992
if err != nil {
994993
ctx.APIErrorInternal(err)
995994
return
996995
}
997996

998-
// for agit flow, we should not delete the agit reference after merge
999-
// FIXME: old code has that comment above. Is that comment valid? What would go wrong if a agit branch is deleted after merge?
1000-
// * If a agit branch can be deleted after merge, then fix the comment and maybe other related code
1001-
// * If a agit branch should not be deleted, then we need to fix the logic and add more tests
1002-
deleteBranchAfterMerge := pr.Flow == issues_model.PullRequestFlowGithub
1003-
if form.DeleteBranchAfterMerge != nil {
1004-
// if the form field is defined, it takes precedence over the repo setting equivalent
1005-
deleteBranchAfterMerge = deleteBranchAfterMerge && *form.DeleteBranchAfterMerge
1006-
} else {
1007-
// otherwise, we look at the repo setting to make the determination
1008-
deleteBranchAfterMerge = deleteBranchAfterMerge && prUnit.PullRequestsConfig().DefaultDeleteBranchAfterMerge
1009-
}
1010-
1011997
if form.MergeWhenChecksSucceed {
1012998
scheduled, err := automerge.ScheduleAutoMerge(ctx, ctx.Doer, pr, repo_model.MergeStyle(form.Do), message, deleteBranchAfterMerge)
1013999
if err != nil {
@@ -1055,45 +1041,9 @@ func MergePullRequest(ctx *context.APIContext) {
10551041
log.Trace("Pull request merged: %d", pr.ID)
10561042

10571043
if deleteBranchAfterMerge {
1058-
// check permission even it has been checked in repo_service.DeleteBranch so that we don't need to
1059-
// do RetargetChildrenOnMerge
1060-
if err := repo_service.CanDeleteBranch(ctx, pr.HeadRepo, pr.HeadBranch, ctx.Doer); err == nil {
1061-
// Don't cleanup when there are other PR's that use this branch as head branch.
1062-
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pr.HeadRepoID, pr.HeadBranch)
1063-
if err != nil {
1064-
ctx.APIErrorInternal(err)
1065-
return
1066-
}
1067-
if exist {
1068-
ctx.Status(http.StatusOK)
1069-
return
1070-
}
1071-
1072-
var headRepo *git.Repository
1073-
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
1074-
headRepo = ctx.Repo.GitRepo
1075-
} else {
1076-
headRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
1077-
if err != nil {
1078-
ctx.APIErrorInternal(err)
1079-
return
1080-
}
1081-
defer headRepo.Close()
1082-
}
1083-
1084-
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch, pr); err != nil {
1085-
switch {
1086-
case git.IsErrBranchNotExist(err):
1087-
ctx.APIErrorNotFound(err)
1088-
case errors.Is(err, repo_service.ErrBranchIsDefault):
1089-
ctx.APIError(http.StatusForbidden, errors.New("can not delete default branch"))
1090-
case errors.Is(err, git_model.ErrBranchIsProtected):
1091-
ctx.APIError(http.StatusForbidden, errors.New("branch protected"))
1092-
default:
1093-
ctx.APIErrorInternal(err)
1094-
}
1095-
return
1096-
}
1044+
// no way to tell users that what error happens, and the PR has been merged, so ignore the error
1045+
if err = repo_service.DeleteBranchAfterMerge(ctx, ctx.Doer, pr.ID, nil); err != nil {
1046+
log.Debug("DeleteBranchAfterMerge: pr %d, err: %v", pr.ID, err)
10971047
}
10981048
}
10991049

routers/web/repo/actions/view.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,8 +887,8 @@ func Run(ctx *context_module.Context) {
887887
return nil
888888
})
889889
if err != nil {
890-
if errLocale := util.ErrorAsLocale(err); errLocale != nil {
891-
ctx.Flash.Error(ctx.Tr(errLocale.TrKey, errLocale.TrArgs...))
890+
if errTr := util.ErrorAsTranslatable(err); errTr != nil {
891+
ctx.Flash.Error(errTr.Translate(ctx.Locale))
892892
ctx.Redirect(redirectURL)
893893
} else {
894894
ctx.ServerError("DispatchActionWorkflow", err)

routers/web/repo/editor_apply_patch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewDiffPatchPost(ctx *context.Context) {
4141
Committer: parsed.GitCommitter,
4242
})
4343
if err != nil {
44-
err = util.ErrorWrapLocale(err, "repo.editor.fail_to_apply_patch")
44+
err = util.ErrorWrapTranslatable(err, "repo.editor.fail_to_apply_patch")
4545
}
4646
if err != nil {
4747
editorHandleFileOperationError(ctx, parsed.NewBranchName, err)

routers/web/repo/editor_cherry_pick.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func CherryPickPost(ctx *context.Context) {
7474
opts.Content = buf.String()
7575
_, err = files.ApplyDiffPatch(ctx, ctx.Repo.Repository, ctx.Doer, opts)
7676
if err != nil {
77-
err = util.ErrorWrapLocale(err, "repo.editor.fail_to_apply_patch")
77+
err = util.ErrorWrapTranslatable(err, "repo.editor.fail_to_apply_patch")
7878
}
7979
}
8080
if err != nil {

routers/web/repo/editor_error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func editorHandleFileOperationErrorRender(ctx *context_service.Context, message,
3838
}
3939

4040
func editorHandleFileOperationError(ctx *context_service.Context, targetBranchName string, err error) {
41-
if errAs := util.ErrorAsLocale(err); errAs != nil {
42-
ctx.JSONError(ctx.Tr(errAs.TrKey, errAs.TrArgs...))
41+
if errAs := util.ErrorAsTranslatable(err); errAs != nil {
42+
ctx.JSONError(errAs.Translate(ctx.Locale))
4343
} else if errAs, ok := errorAs[git.ErrNotExist](err); ok {
4444
ctx.JSONError(ctx.Tr("repo.editor.file_modifying_no_longer_exists", errAs.RelPath))
4545
} else if errAs, ok := errorAs[git_model.ErrLFSFileLocked](err); ok {

0 commit comments

Comments
 (0)