diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 09510ac2c5756..686a3ddffaac2 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -33,6 +33,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/routers" + markup_service "code.gitea.io/gitea/services/markup" "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/config" @@ -112,7 +113,7 @@ func runPR() { log.Printf("[PR] Setting up router\n") // routers.GlobalInit() external.RegisterRenderers() - markup.Init() + markup.Init(markup_service.ProcessorHelper()) c := routers.NormalRoutes(graceful.GetManager().HammerContext()) log.Printf("[PR] Ready for testing !\n") diff --git a/models/fixtures/user.yml b/models/fixtures/user.yml index b332b9ff36f47..0e3348e146a82 100644 --- a/models/fixtures/user.yml +++ b/models/fixtures/user.yml @@ -1220,40 +1220,3 @@ repo_admin_change_team_access: false theme: "" keep_activity_private: false - -- - id: 34 - lower_name: r-lyeh - name: r-lyeh - full_name: user34 - email: user34@example.com - keep_email_private: true - email_notifications_preference: enabled - passwd: a3d5fcd92bae586c2e3dbe72daea7a0d27833a8d0227aa1704f4bbd775c1f3b03535b76dd93b0d4d8d22a519dca47df1547b - passwd_hash_algo: argon2 - must_change_password: false - login_source: 0 - login_name: user34 - type: 0 - salt: ZogKvWdyEx - max_repo_creation: -1 - is_active: true - is_admin: false - is_restricted: false - allow_git_hook: false - allow_import_local: false - allow_create_organization: true - prohibit_login: false - avatar: avatar34 - avatar_email: user34@example.com - use_custom_avatar: false - num_followers: 0 - num_following: 0 - num_stars: 0 - num_repos: 0 - num_teams: 0 - num_members: 0 - visibility: 0 - repo_admin_change_team_access: false - theme: "" - keep_activity_private: false \ No newline at end of file diff --git a/models/user/user_test.go b/models/user/user_test.go index 014bb784f4c61..5f2ac0a60c17d 100644 --- a/models/user/user_test.go +++ b/models/user/user_test.go @@ -96,13 +96,13 @@ func TestSearchUsers(t *testing.T) { } testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}}, - []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) + []int64{1, 2, 4, 5, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&user_model.SearchUserOptions{ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolFalse}, []int64{9}) testUserSuccess(&user_model.SearchUserOptions{OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, - []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32, 34}) + []int64{1, 2, 4, 5, 8, 10, 11, 12, 13, 14, 15, 16, 18, 20, 21, 24, 27, 28, 29, 30, 32}) testUserSuccess(&user_model.SearchUserOptions{Keyword: "user1", OrderBy: "id ASC", ListOptions: db.ListOptions{Page: 1}, IsActive: util.OptionalBoolTrue}, []int64{1, 10, 11, 12, 13, 14, 15, 16, 18}) diff --git a/modules/markup/html.go b/modules/markup/html.go index 6defcc90cbc0f..ae00c3905fe8b 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -14,7 +14,6 @@ import ( "strings" "sync" - "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/git" @@ -606,13 +605,7 @@ func mentionProcessor(ctx *RenderContext, node *html.Node) { } mentionedUsername := mention[1:] - // Only link if the user actually exists - userExists, err := user.IsUserExist(ctx.Ctx, 0, mentionedUsername) - if err != nil { - log.Error("Failed to validate user in mention %v exists, assuming it does", mention) - userExists = true - } - if userExists { + if processorHelper.IsUsernameMentionable != nil && processorHelper.IsUsernameMentionable(ctx.Ctx, mentionedUsername) { replaceContent(node, loc.Start, loc.End, createLink(util.URLJoin(setting.AppURL, mentionedUsername), mention, "mention")) node = node.NextSibling.NextSibling } else { diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index 8077b679e9196..12c6288c24d12 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -6,11 +6,10 @@ package markdown_test import ( "context" - "path/filepath" + "os" "strings" "testing" - "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" @@ -39,10 +38,7 @@ func TestMain(m *testing.M) { if err := git.InitSimple(context.Background()); err != nil { log.Fatal("git init failed, err: %v", err) } - unittest.MainTest(m, &unittest.TestOptions{ - GiteaRootPath: filepath.Join("..", "..", ".."), - FixtureFiles: []string{"user.yml"}, - }) + os.Exit(m.Run()) } func TestRender_StandardLinks(t *testing.T) { @@ -304,8 +300,6 @@ func TestTotal_RenderWiki(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL - assert.NoError(t, unittest.PrepareTestDatabase()) - answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) for i := 0; i < len(sameCases); i++ { diff --git a/modules/markup/renderer.go b/modules/markup/renderer.go index 5f69dc72354f0..b3289cb3c3b04 100644 --- a/modules/markup/renderer.go +++ b/modules/markup/renderer.go @@ -19,8 +19,18 @@ import ( "code.gitea.io/gitea/modules/setting" ) +type ProcessorHelper struct { + IsUsernameMentionable func(ctx context.Context, username string) bool +} + +var processorHelper ProcessorHelper + // Init initialize regexps for markdown parsing -func Init() { +func Init(ph *ProcessorHelper) { + if ph != nil { + processorHelper = *ph + } + NewSanitizer() if len(setting.Markdown.CustomURLSchemes) > 0 { CustomLinkURLSchemes(setting.Markdown.CustomURLSchemes) diff --git a/routers/api/v1/misc/main_test.go b/routers/api/v1/misc/main_test.go deleted file mode 100644 index e08538b70f089..0000000000000 --- a/routers/api/v1/misc/main_test.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2022 The Gitea Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package misc - -import ( - "path/filepath" - "testing" - - "code.gitea.io/gitea/models/unittest" -) - -func TestMain(m *testing.M) { - unittest.MainTest(m, &unittest.TestOptions{ - GiteaRootPath: filepath.Join("..", "..", "..", ".."), - FixtureFiles: []string{"user.yml"}, - }) -} diff --git a/routers/init.go b/routers/init.go index 0f2e993413af8..9045437f872b5 100644 --- a/routers/init.go +++ b/routers/init.go @@ -41,6 +41,7 @@ import ( "code.gitea.io/gitea/services/automerge" "code.gitea.io/gitea/services/cron" "code.gitea.io/gitea/services/mailer" + markup_service "code.gitea.io/gitea/services/markup" repo_migrations "code.gitea.io/gitea/services/migrations" mirror_service "code.gitea.io/gitea/services/mirror" pull_service "code.gitea.io/gitea/services/pull" @@ -123,7 +124,7 @@ func GlobalInitInstalled(ctx context.Context) { highlight.NewContext() external.RegisterRenderers() - markup.Init() + markup.Init(markup_service.ProcessorHelper()) if setting.EnableSQLite3 { log.Info("SQLite3 support is enabled") diff --git a/services/markup/processorhelper.go b/services/markup/processorhelper.go new file mode 100644 index 0000000000000..60e4a933db36e --- /dev/null +++ b/services/markup/processorhelper.go @@ -0,0 +1,24 @@ +package markup + +import ( + "context" + + "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" +) + +func ProcessorHelper() *markup.ProcessorHelper { + return &markup.ProcessorHelper{ + IsUsernameMentionable: func(ctx context.Context, username string) bool { + // here, you could also try to cast the ctx to a modules/context.Context + // Only link if the user actually exists + userExists, err := user.IsUserExist(ctx, 0, username) + if err != nil { + log.Error("Failed to validate user in mention %q exists, assuming it does", username) + userExists = true + } + return userExists + }, + } +}