Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use repo as of renderctx's member rather than a repoPath on metas #29222

Merged
merged 18 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion models/issues/comment_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
7 changes: 3 additions & 4 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions modules/gitrepo/url.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 6 additions & 5 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
})
}
Expand Down
41 changes: 27 additions & 14 deletions modules/markup/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (m *mockRepo) GetOwnerName() string {
return m.OwnerName
}

func (m *mockRepo) GetName() string {
return m.RepoName
}

func TestMain(m *testing.M) {
unittest.InitSettings()
if err := git.InitSimple(context.Background()); err != nil {
log.Fatal("git init failed, err: %v", err)
func newMockRepo(ownerName, repoName string) gitrepo.Repository {
return &mockRepo{
OwnerName: ownerName,
RepoName: repoName,
}
os.Exit(m.Run())
}

func TestRender_Commits(t *testing.T) {
Expand All @@ -46,14 +58,15 @@ func TestRender_Commits(t *testing.T) {
AbsolutePrefix: true,
Base: markup.TestRepoURL,
},
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
}, input)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(buffer))
}

sha := "65f1bf27bc3bf70f64657658635e66094edbcb4d"
repo := markup.TestRepoURL
repo := markup.TestAppURL + testRepoOwnerName + "/" + testRepoName + "/"
commit := util.URLJoin(repo, "commit", sha)
tree := util.URLJoin(repo, "tree", sha, "src")

Expand Down
14 changes: 14 additions & 0 deletions modules/markup/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
21 changes: 21 additions & 0 deletions modules/markup/markdown/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
49 changes: 29 additions & 20 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -133,11 +140,11 @@ func testAnswers(baseURLContent, baseURLImages string) []string {
<li><a href="` + baseURLContent + `/Links" rel="nofollow">Links, Language bindings, Engine bindings</a></li>
<li><a href="` + baseURLContent + `/Tips" rel="nofollow">Tips</a></li>
</ul>
<p>See commit <a href="/gogits/gogs/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
<p>See commit <a href="/user13/repo11/commit/65f1bf27bc" rel="nofollow"><code>65f1bf27bc</code></a></p>
lunny marked this conversation as resolved.
Show resolved Hide resolved
<p>Ideas and codes</p>
<ul>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/ocornut/imgui/issues/786" class="ref-issue" rel="nofollow">ocornut/imgui#786</a></li>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/gogits/gogs/issues/786" class="ref-issue" rel="nofollow">#786</a></li>
<li>Bezier widget (by <a href="/r-lyeh" rel="nofollow">@r-lyeh</a>) <a href="http://localhost:3000/user13/repo11/issues/786" class="ref-issue" rel="nofollow">#786</a></li>
lunny marked this conversation as resolved.
Show resolved Hide resolved
<li>Node graph editors <a href="https://github.com/ocornut/imgui/issues/306" rel="nofollow">https://github.com/ocornut/imgui/issues/306</a></li>
<li><a href="` + baseURLContent + `/memory_editor_example" rel="nofollow">Memory Editor</a></li>
<li><a href="` + baseURLContent + `/plot_var_example" rel="nofollow">Plot var helper</a></li>
Expand Down Expand Up @@ -222,7 +229,7 @@ See commit 65f1bf27bc
Ideas and codes

- Bezier widget (by @r-lyeh) ` + AppURL + `ocornut/imgui/issues/786
- Bezier widget (by @r-lyeh) ` + AppURL + `gogits/gogs/issues/786
- Bezier widget (by @r-lyeh) ` + AppURL + `user13/repo11/issues/786
lunny marked this conversation as resolved.
Show resolved Hide resolved
- Node graph editors https://github.com/ocornut/imgui/issues/306
- [[Memory Editor|memory_editor_example]]
- [[Plot var helper|plot_var_example]]`,
Expand Down Expand Up @@ -299,6 +306,7 @@ func TestTotal_RenderWiki(t *testing.T) {
Links: markup.Links{
Base: FullURL,
},
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
IsWiki: true,
}, sameCases[i])
Expand Down Expand Up @@ -344,6 +352,7 @@ func TestTotal_RenderString(t *testing.T) {
Base: FullURL,
BranchPath: "master",
},
Repo: newMockRepo(testRepoOwnerName, testRepoName),
Metas: localMetas,
}, sameCases[i])
assert.NoError(t, err)
Expand Down
2 changes: 2 additions & 0 deletions modules/markup/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"sync"

"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"

Expand Down Expand Up @@ -77,6 +78,7 @@ type RenderContext struct {
Metas map[string]string
DefaultLink string
GitRepo *git.Repository
Repo gitrepo.Repository
ShaExistCache map[string]bool
cancelFn func()
SidebarTocNode ast.Node
Expand Down
6 changes: 5 additions & 1 deletion routers/common/markup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strings"

repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -66,7 +67,9 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
}

meta := map[string]string{}
var repoCtx *repo_model.Repository
if repo != nil && repo.Repository != nil {
repoCtx = repo.Repository
if mode == "comment" {
meta = repo.Repository.ComposeMetas(ctx)
} else {
Expand All @@ -78,7 +81,8 @@ func RenderMarkup(ctx *context.Base, repo *context.Repository, mode, text, urlPr
}

if err := markup.Render(&markup.RenderContext{
Ctx: ctx,
Ctx: ctx,
Repo: repoCtx,
Links: markup.Links{
AbsolutePrefix: true,
Base: urlPrefix,
Expand Down
3 changes: 2 additions & 1 deletion routers/web/feed/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ func releasesToFeedItems(ctx *context.Context, releases []*repo_model.Release) (

link := &feeds.Link{Href: rel.HTMLURL()}
content, err = markdown.RenderString(&markup.RenderContext{
Ctx: ctx,
Ctx: ctx,
Repo: rel.Repo,
Links: markup.Links{
Base: rel.Repo.Link(),
},
Expand Down
1 change: 1 addition & 0 deletions routers/web/repo/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ func Diff(ctx *context.Context) {
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, template.HTMLEscapeString(string(charset.ToUTF8WithFallback(note.Message, charset.ConvertOpts{}))))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions routers/web/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,7 @@ func ViewIssue(ctx *context.Context) {
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, issue.Content)
if err != nil {
Expand Down Expand Up @@ -1622,6 +1623,7 @@ func ViewIssue(ctx *context.Context) {
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, comment.Content)
if err != nil {
Expand Down Expand Up @@ -1699,6 +1701,7 @@ func ViewIssue(ctx *context.Context) {
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, comment.Content)
if err != nil {
Expand Down Expand Up @@ -2270,6 +2273,7 @@ func UpdateIssueContent(ctx *context.Context) {
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, issue.Content)
if err != nil {
Expand Down Expand Up @@ -3186,6 +3190,7 @@ func UpdateCommentContent(ctx *context.Context) {
},
Metas: ctx.Repo.Repository.ComposeMetas(ctx),
GitRepo: ctx.Repo.GitRepo,
Repo: ctx.Repo.Repository,
Ctx: ctx,
}, comment.Content)
if err != nil {
Expand Down
Loading