-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4e27cb
commit adf30d8
Showing
63 changed files
with
1,048 additions
and
1,180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package renderhelper | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/gitrepo" | ||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
type commitChecker struct { | ||
ctx context.Context | ||
commitCache map[string]bool | ||
gitRepoFacade gitrepo.Repository | ||
|
||
gitRepo *git.Repository | ||
gitRepoCloser io.Closer | ||
} | ||
|
||
func newCommitChecker(ctx context.Context, gitRepo gitrepo.Repository) *commitChecker { | ||
return &commitChecker{ctx: ctx, commitCache: make(map[string]bool), gitRepoFacade: gitRepo} | ||
} | ||
|
||
func (c *commitChecker) Close() error { | ||
if c != nil && c.gitRepoCloser != nil { | ||
return c.gitRepoCloser.Close() | ||
} | ||
return nil | ||
} | ||
|
||
func (c *commitChecker) IsCommitIDExisting(commitID string) bool { | ||
exist, inCache := c.commitCache[commitID] | ||
if inCache { | ||
return exist | ||
} | ||
|
||
if c.gitRepo == nil { | ||
r, closer, err := gitrepo.RepositoryFromContextOrOpen(c.ctx, c.gitRepoFacade) | ||
if err != nil { | ||
log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(c.gitRepoFacade), err) | ||
return false | ||
} | ||
c.gitRepo, c.gitRepoCloser = r, closer | ||
} | ||
|
||
exist = c.gitRepo.IsReferenceExist(commitID) // Don't use IsObjectExist since it doesn't support short hashs with gogit edition. | ||
c.commitCache[commitID] = exist | ||
return exist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package renderhelper | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/unittest" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
unittest.MainTest(m, &unittest.TestOptions{ | ||
FixtureFiles: []string{"repository.yml", "user.yml"}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package renderhelper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/modules/markup" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
type RepoComment struct { | ||
ctx *markup.RenderContext | ||
opts RepoCommentOptions | ||
|
||
commitChecker *commitChecker | ||
repoLink string | ||
} | ||
|
||
func (r *RepoComment) CleanUp() { | ||
_ = r.commitChecker.Close() | ||
} | ||
|
||
func (r *RepoComment) IsCommitIDExisting(commitID string) bool { | ||
return r.commitChecker.IsCommitIDExisting(commitID) | ||
} | ||
|
||
func (r *RepoComment) ResolveLink(link string, likeType markup.LinkType) (finalLink string) { | ||
switch likeType { | ||
case markup.LinkTypeApp: | ||
finalLink = r.ctx.ResolveLinkApp(link) | ||
default: | ||
finalLink = r.ctx.ResolveLinkRelative(r.repoLink, r.opts.CurrentRefPath, link) | ||
} | ||
return finalLink | ||
} | ||
|
||
var _ markup.RenderHelper = (*RepoComment)(nil) | ||
|
||
type RepoCommentOptions struct { | ||
DeprecatedRepoName string // it is only a patch for the non-standard "markup" api | ||
DeprecatedOwnerName string // it is only a patch for the non-standard "markup" api | ||
CurrentRefPath string // eg: "branch/main" or "commit/11223344" | ||
} | ||
|
||
func NewRenderContextRepoComment(ctx context.Context, repo *repo_model.Repository, opts ...RepoCommentOptions) *markup.RenderContext { | ||
helper := &RepoComment{ | ||
repoLink: repo.Link(), | ||
opts: util.OptionalArg(opts), | ||
} | ||
rctx := markup.NewRenderContext(ctx) | ||
helper.ctx = rctx | ||
if repo != nil { | ||
helper.repoLink = repo.Link() | ||
helper.commitChecker = newCommitChecker(ctx, repo) | ||
rctx = rctx.WithMetas(repo.ComposeMetas(ctx)) | ||
} else { | ||
// this is almost dead code, only to pass the incorrect tests | ||
helper.repoLink = fmt.Sprintf("%s/%s", helper.opts.DeprecatedOwnerName, helper.opts.DeprecatedRepoName) | ||
rctx = rctx.WithMetas(map[string]string{ | ||
"user": helper.opts.DeprecatedOwnerName, | ||
"repo": helper.opts.DeprecatedRepoName, | ||
|
||
"markdownLineBreakStyle": "comment", | ||
"markupAllowShortIssuePattern": "true", | ||
}) | ||
} | ||
rctx = rctx.WithHelper(helper) | ||
return rctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package renderhelper | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/unittest" | ||
"code.gitea.io/gitea/modules/markup" | ||
"code.gitea.io/gitea/modules/markup/markdown" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRepoComment(t *testing.T) { | ||
unittest.PrepareTestEnv(t) | ||
markup.RenderBehaviorForTesting.DisableAdditionalAttributes = true | ||
markup.Init(&markup.RenderHelperFuncs{ | ||
IsUsernameMentionable: func(ctx context.Context, username string) bool { | ||
return username == "user2" | ||
}, | ||
}) | ||
|
||
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) | ||
|
||
t.Run("AutoLink", func(t *testing.T) { | ||
rctx := NewRenderContextRepoComment(context.Background(), repo1).WithMarkupType(markdown.MarkupName) | ||
rendered, err := markup.RenderString(rctx, ` | ||
65f1bf27bc3bf70f64657658635e66094edbcb4d | ||
#1 | ||
@user2 | ||
`) | ||
assert.NoError(t, err) | ||
assert.Equal(t, | ||
`<p><a href="/user2/repo1/commit/65f1bf27bc3bf70f64657658635e66094edbcb4d" rel="nofollow"><code>65f1bf27bc</code></a><br/> | ||
<a href="/user2/repo1/issues/1" class="ref-issue" rel="nofollow">#1</a><br/> | ||
<a href="/user2" rel="nofollow">@user2</a></p> | ||
`, rendered) | ||
}) | ||
|
||
t.Run("AbsoluteAndRelative", func(t *testing.T) { | ||
rctx := NewRenderContextRepoComment(context.Background(), repo1).WithMarkupType(markdown.MarkupName) | ||
rendered, err := markup.RenderString(rctx, ` | ||
[/test](/test) | ||
[./test](./test) | ||
![/image](/image) | ||
![./image](./image) | ||
`) | ||
assert.NoError(t, err) | ||
assert.Equal(t, | ||
`<p><a href="/user2/repo1/test" rel="nofollow">/test</a><br/> | ||
<a href="/user2/repo1/test" rel="nofollow">./test</a><br/> | ||
<a href="/user2/repo1/image" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/image" alt="/image"/></a><br/> | ||
<a href="/user2/repo1/image" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/image" alt="./image"/></a></p> | ||
`, rendered) | ||
}) | ||
|
||
t.Run("WithCurrentRefPath", func(t *testing.T) { | ||
rctx := NewRenderContextRepoComment(context.Background(), repo1, RepoCommentOptions{CurrentRefPath: "/commit/1234"}). | ||
WithMarkupType(markdown.MarkupName) | ||
rendered, err := markup.RenderString(rctx, ` | ||
[/test](/test) | ||
[./test](./test) | ||
![/image](/image) | ||
![./image](./image) | ||
`) | ||
assert.NoError(t, err) | ||
assert.Equal(t, `<p><a href="/user2/repo1/test" rel="nofollow">/test</a><br/> | ||
<a href="/user2/repo1/commit/1234/test" rel="nofollow">./test</a><br/> | ||
<a href="/user2/repo1/image" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/image" alt="/image"/></a><br/> | ||
<a href="/user2/repo1/commit/1234/image" target="_blank" rel="nofollow noopener"><img src="/user2/repo1/commit/1234/image" alt="./image"/></a></p> | ||
`, rendered) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package renderhelper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"path" | ||
|
||
repo_model "code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/modules/markup" | ||
"code.gitea.io/gitea/modules/util" | ||
) | ||
|
||
type RepoFile struct { | ||
ctx *markup.RenderContext | ||
opts RepoFileOptions | ||
|
||
commitChecker *commitChecker | ||
repoLink string | ||
} | ||
|
||
func (r *RepoFile) CleanUp() { | ||
_ = r.commitChecker.Close() | ||
} | ||
|
||
func (r *RepoFile) IsCommitIDExisting(commitID string) bool { | ||
return r.commitChecker.IsCommitIDExisting(commitID) | ||
} | ||
|
||
func (r *RepoFile) ResolveLink(link string, likeType markup.LinkType) string { | ||
finalLink := link | ||
switch likeType { | ||
case markup.LinkTypeApp: | ||
finalLink = r.ctx.ResolveLinkApp(link) | ||
case markup.LinkTypeDefault: | ||
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "src", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) | ||
case markup.LinkTypeRaw: | ||
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "raw", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) | ||
case markup.LinkTypeMedia: | ||
finalLink = r.ctx.ResolveLinkRelative(path.Join(r.repoLink, "media", r.opts.CurrentRefPath), r.opts.CurrentTreePath, link) | ||
} | ||
return finalLink | ||
} | ||
|
||
var _ markup.RenderHelper = (*RepoFile)(nil) | ||
|
||
type RepoFileOptions struct { | ||
DeprecatedRepoName string // it is only a patch for the non-standard "markup" api | ||
DeprecatedOwnerName string // it is only a patch for the non-standard "markup" api | ||
|
||
CurrentRefPath string // eg: "branch/main" | ||
CurrentTreePath string // eg: "path/to/file" in the repo | ||
} | ||
|
||
func NewRenderContextRepoFile(ctx context.Context, repo *repo_model.Repository, opts ...RepoFileOptions) *markup.RenderContext { | ||
helper := &RepoFile{opts: util.OptionalArg(opts)} | ||
rctx := markup.NewRenderContext(ctx) | ||
helper.ctx = rctx | ||
if repo != nil { | ||
helper.repoLink = repo.Link() | ||
helper.commitChecker = newCommitChecker(ctx, repo) | ||
rctx = rctx.WithMetas(repo.ComposeDocumentMetas(ctx)) | ||
} else { | ||
// this is almost dead code, only to pass the incorrect tests | ||
helper.repoLink = fmt.Sprintf("%s/%s", helper.opts.DeprecatedOwnerName, helper.opts.DeprecatedRepoName) | ||
rctx = rctx.WithMetas(map[string]string{ | ||
"user": helper.opts.DeprecatedOwnerName, | ||
"repo": helper.opts.DeprecatedRepoName, | ||
|
||
"markdownLineBreakStyle": "document", | ||
}) | ||
} | ||
rctx = rctx.WithHelper(helper) | ||
return rctx | ||
} |
Oops, something went wrong.