From 015efcd8bfd451ef593192eb43cfcfb7001f7861 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 30 May 2024 15:04:01 +0800 Subject: [PATCH] Use repo as of renderctx's member rather than a repoPath on metas (#29222) Use a `gitrepo.Repository` in the markup's RenderContext but not store the repository's path. --- models/issues/comment_code.go | 3 +- models/repo/repo.go | 7 ++-- modules/gitrepo/url.go | 8 ++++ modules/markup/html.go | 11 +++--- modules/markup/html_test.go | 41 +++++++++++++------- modules/markup/main_test.go | 14 +++++++ modules/markup/markdown/main_test.go | 21 ++++++++++ modules/markup/markdown/markdown_test.go | 49 ++++++++++++++---------- modules/markup/renderer.go | 2 + routers/common/markup.go | 6 ++- routers/web/feed/convert.go | 3 +- routers/web/repo/commit.go | 1 + routers/web/repo/issue.go | 5 +++ routers/web/repo/milestone.go | 2 + routers/web/repo/projects.go | 2 + routers/web/repo/release.go | 1 + routers/web/user/home.go | 1 + services/mailer/mail.go | 3 +- services/mailer/mail_release.go | 3 +- 19 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 modules/gitrepo/url.go create mode 100644 modules/markup/main_test.go create mode 100644 modules/markup/markdown/main_test.go diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index f860dacfac5..6f23d3326a2 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -113,7 +113,8 @@ func findCodeComments(ctx context.Context, opts FindCommentsOptions, issue *Issu var err error if comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{ - Ctx: ctx, + Ctx: ctx, + Repo: issue.Repo, Links: markup.Links{ Base: issue.Repo.Link(), }, diff --git a/models/repo/repo.go b/models/repo/repo.go index 5d5707d1acb..f02c55fc895 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -472,10 +472,9 @@ func (repo *Repository) MustOwner(ctx context.Context) *user_model.User { func (repo *Repository) ComposeMetas(ctx context.Context) map[string]string { if len(repo.RenderingMetas) == 0 { metas := map[string]string{ - "user": repo.OwnerName, - "repo": repo.Name, - "repoPath": repo.RepoPath(), - "mode": "comment", + "user": repo.OwnerName, + "repo": repo.Name, + "mode": "comment", } unit, err := repo.GetUnit(ctx, unit.TypeExternalTracker) diff --git a/modules/gitrepo/url.go b/modules/gitrepo/url.go new file mode 100644 index 00000000000..b355d0fa93a --- /dev/null +++ b/modules/gitrepo/url.go @@ -0,0 +1,8 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo + +func RepoGitURL(repo Repository) string { + return repoPath(repo) +} diff --git a/modules/markup/html.go b/modules/markup/html.go index 2958dc96467..0af74d26806 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -16,7 +16,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/emoji" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup/common" "code.gitea.io/gitea/modules/references" @@ -1140,7 +1140,7 @@ func emojiProcessor(ctx *RenderContext, node *html.Node) { // hashCurrentPatternProcessor renders SHA1 strings to corresponding links that // are assumed to be in the same repository. func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) { - if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || ctx.Metas["repoPath"] == "" { + if ctx.Metas == nil || ctx.Metas["user"] == "" || ctx.Metas["repo"] == "" || (ctx.Repo == nil && ctx.GitRepo == nil) { return } @@ -1172,13 +1172,14 @@ func hashCurrentPatternProcessor(ctx *RenderContext, node *html.Node) { if !inCache { if ctx.GitRepo == nil { var err error - ctx.GitRepo, err = git.OpenRepository(ctx.Ctx, ctx.Metas["repoPath"]) + var closer io.Closer + ctx.GitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx.Ctx, ctx.Repo) if err != nil { - log.Error("unable to open repository: %s Error: %v", ctx.Metas["repoPath"], err) + log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(ctx.Repo), err) return } ctx.AddCancel(func() { - ctx.GitRepo.Close() + closer.Close() ctx.GitRepo = nil }) } diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index a2ae18d7771..0091397768b 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -4,16 +4,13 @@ package markup_test import ( - "context" "io" - "os" "strings" "testing" - "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/emoji" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" @@ -22,18 +19,33 @@ import ( "github.com/stretchr/testify/assert" ) -var localMetas = map[string]string{ - "user": "gogits", - "repo": "gogs", - "repoPath": "../../tests/gitea-repositories-meta/user13/repo11.git/", +var ( + testRepoOwnerName = "user13" + testRepoName = "repo11" + localMetas = map[string]string{ + "user": testRepoOwnerName, + "repo": testRepoName, + } +) + +type mockRepo struct { + OwnerName string + RepoName string } -func TestMain(m *testing.M) { - unittest.InitSettings() - if err := git.InitSimple(context.Background()); err != nil { - log.Fatal("git init failed, err: %v", err) +func (m *mockRepo) GetOwnerName() string { + return m.OwnerName +} + +func (m *mockRepo) GetName() string { + return m.RepoName +} + +func newMockRepo(ownerName, repoName string) gitrepo.Repository { + return &mockRepo{ + OwnerName: ownerName, + RepoName: repoName, } - os.Exit(m.Run()) } func TestRender_Commits(t *testing.T) { @@ -46,6 +58,7 @@ func TestRender_Commits(t *testing.T) { AbsolutePrefix: true, Base: markup.TestRepoURL, }, + Repo: newMockRepo(testRepoOwnerName, testRepoName), Metas: localMetas, }, input) assert.NoError(t, err) @@ -53,7 +66,7 @@ func TestRender_Commits(t *testing.T) { } sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d" - repo := markup.TestRepoURL + repo := markup.TestAppURL + testRepoOwnerName + "/" + testRepoName + "/" commit := util.URLJoin(repo, "commit", sha) tree := util.URLJoin(repo, "tree", sha, "src") diff --git a/modules/markup/main_test.go b/modules/markup/main_test.go new file mode 100644 index 00000000000..a8f6f1c5646 --- /dev/null +++ b/modules/markup/main_test.go @@ -0,0 +1,14 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package markup_test + +import ( + "testing" + + "code.gitea.io/gitea/models/unittest" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m) +} diff --git a/modules/markup/markdown/main_test.go b/modules/markup/markdown/main_test.go new file mode 100644 index 00000000000..f33eeb13b2d --- /dev/null +++ b/modules/markup/markdown/main_test.go @@ -0,0 +1,21 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package markdown + +import ( + "context" + "testing" + + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/markup" +) + +func TestMain(m *testing.M) { + markup.Init(&markup.ProcessorHelper{ + IsUsernameMentionable: func(ctx context.Context, username string) bool { + return username == "r-lyeh" + }, + }) + unittest.MainTest(m) +} diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index bc6ad7fb3c7..b4a7efa8dd8 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -6,12 +6,11 @@ package markdown_test import ( "context" "html/template" - "os" "strings" "testing" - "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -25,28 +24,36 @@ import ( ) const ( - AppURL = "http://localhost:3000/" - FullURL = AppURL + "gogits/gogs/" + AppURL = "http://localhost:3000/" + testRepoOwnerName = "user13" + testRepoName = "repo11" + FullURL = AppURL + testRepoOwnerName + "/" + testRepoName + "/" ) // these values should match the const above var localMetas = map[string]string{ - "user": "gogits", - "repo": "gogs", - "repoPath": "../../../tests/gitea-repositories-meta/user13/repo11.git/", + "user": testRepoOwnerName, + "repo": testRepoName, } -func TestMain(m *testing.M) { - unittest.InitSettings() - if err := git.InitSimple(context.Background()); err != nil { - log.Fatal("git init failed, err: %v", err) +type mockRepo struct { + OwnerName string + RepoName string +} + +func (m *mockRepo) GetOwnerName() string { + return m.OwnerName +} + +func (m *mockRepo) GetName() string { + return m.RepoName +} + +func newMockRepo(ownerName, repoName string) gitrepo.Repository { + return &mockRepo{ + OwnerName: ownerName, + RepoName: repoName, } - markup.Init(&markup.ProcessorHelper{ - IsUsernameMentionable: func(ctx context.Context, username string) bool { - return username == "r-lyeh" - }, - }) - os.Exit(m.Run()) } func TestRender_StandardLinks(t *testing.T) { @@ -133,11 +140,11 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
  • Links, Language bindings, Engine bindings
  • Tips
  • -

    See commit 65f1bf27bc

    +

    See commit 65f1bf27bc

    Ideas and codes