Skip to content

Commit 102bcff

Browse files
committed
Update for use of context metas
Now that we include the user and repo name inside context metas, update various code and tests for this new logic
1 parent aaab9df commit 102bcff

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

models/repo_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ func TestRepo(t *testing.T) {
2020
repo.Owner = &User{Name: "testOwner"}
2121

2222
repo.Units = nil
23-
assert.Nil(t, repo.ComposeMetas())
23+
24+
metas := repo.ComposeMetas()
25+
assert.Equal(t, "testRepo", metas["repo"])
26+
assert.Equal(t, "testOwner", metas["user"])
2427

2528
externalTracker := RepoUnit{
2629
Type: UnitTypeExternalTracker,

modules/markup/html.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ func shortLinkProcessorFull(ctx *postProcessCtx, node *html.Node, noLink bool) {
534534
}
535535

536536
func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) {
537+
if ctx.metas == nil {
538+
return
539+
}
537540
m := getIssueFullPattern().FindStringSubmatchIndex(node.Data)
538541
if m == nil {
539542
return
@@ -547,15 +550,7 @@ func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) {
547550
matchOrg := linkParts[len(linkParts)-4]
548551
matchRepo := linkParts[len(linkParts)-3]
549552

550-
// extract the current org and repo from ctx URL like
551-
// http://localhost:3000/gituser/myrepo/
552-
url := strings.Replace(ctx.urlPrefix, setting.AppURL, "", -1)
553-
urlParts := strings.Split(path.Clean(url), "/")
554-
555-
currentOrg := urlParts[0]
556-
currentRepo := urlParts[1]
557-
558-
if matchOrg == currentOrg && matchRepo == currentRepo {
553+
if matchOrg == ctx.metas["user"] && matchRepo == ctx.metas["repo"] {
559554
// TODO if m[4]:m[5] is not nil, then link is to a comment,
560555
// and we should indicate that in the text somehow
561556
replaceContent(node, m[0], m[1], createLink(link, id))
@@ -567,8 +562,9 @@ func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) {
567562
}
568563

569564
func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
570-
prefix := cutoutVerbosePrefix(ctx.urlPrefix)
571-
565+
if ctx.metas == nil {
566+
return
567+
}
572568
// default to numeric pattern, unless alphanumeric is requested.
573569
pattern := issueNumericPattern
574570
if ctx.metas["style"] == IssueNameStyleAlphanumeric {
@@ -579,18 +575,19 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
579575
if match == nil {
580576
return
581577
}
578+
582579
id := node.Data[match[2]:match[3]]
583580
var link *html.Node
584-
if ctx.metas == nil {
585-
link = createLink(util.URLJoin(prefix, "issues", id[1:]), id)
586-
} else {
581+
if _, ok := ctx.metas["format"]; ok {
587582
// Support for external issue tracker
588583
if ctx.metas["style"] == IssueNameStyleAlphanumeric {
589584
ctx.metas["index"] = id
590585
} else {
591586
ctx.metas["index"] = id[1:]
592587
}
593588
link = createLink(com.Expand(ctx.metas["format"], ctx.metas), id)
589+
} else {
590+
link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", id[1:]), id)
594591
}
595592
replaceContent(node, match[2], match[3], link)
596593
}

modules/markup/html_internal_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ var alphanumericMetas = map[string]string{
5353
"style": IssueNameStyleAlphanumeric,
5454
}
5555

56+
// these values should match the Repo const above
57+
var localMetas = map[string]string{
58+
"user": "gogits",
59+
"repo": "gogs",
60+
}
61+
5662
func TestRender_IssueIndexPattern(t *testing.T) {
5763
// numeric: render inputs without valid mentions
5864
test := func(s string) {
@@ -90,7 +96,7 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
9096
links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), index)
9197
}
9298
expectedNil := fmt.Sprintf(expectedFmt, links...)
93-
testRenderIssueIndexPattern(t, s, expectedNil, nil)
99+
testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas})
94100

95101
for i, index := range indices {
96102
links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index)
@@ -168,6 +174,7 @@ func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *post
168174
if ctx.urlPrefix == "" {
169175
ctx.urlPrefix = AppSubURL
170176
}
177+
171178
res, err := ctx.postProcess([]byte(input))
172179
assert.NoError(t, err)
173180
assert.Equal(t, expected, string(res))
@@ -178,10 +185,10 @@ func TestRender_AutoLink(t *testing.T) {
178185
setting.AppSubURL = AppSubURL
179186

180187
test := func(input, expected string) {
181-
buffer, err := PostProcess([]byte(input), setting.AppSubURL, nil, false)
188+
buffer, err := PostProcess([]byte(input), setting.AppSubURL, localMetas, false)
182189
assert.Equal(t, err, nil)
183190
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
184-
buffer, err = PostProcess([]byte(input), setting.AppSubURL, nil, true)
191+
buffer, err = PostProcess([]byte(input), setting.AppSubURL, localMetas, true)
185192
assert.Equal(t, err, nil)
186193
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer)))
187194
}
@@ -211,6 +218,7 @@ func TestRender_FullIssueURLs(t *testing.T) {
211218
if ctx.urlPrefix == "" {
212219
ctx.urlPrefix = AppSubURL
213220
}
221+
ctx.metas = localMetas
214222
result, err := ctx.postProcess([]byte(input))
215223
assert.NoError(t, err)
216224
assert.Equal(t, expected, string(result))

modules/markup/markdown/markdown_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ const AppURL = "http://localhost:3000/"
1919
const Repo = "gogits/gogs"
2020
const AppSubURL = AppURL + Repo + "/"
2121

22+
// these values should match the Repo const above
23+
var localMetas = map[string]string{
24+
"user": "gogits",
25+
"repo": "gogs",
26+
}
27+
2228
func TestRender_StandardLinks(t *testing.T) {
2329
setting.AppURL = AppURL
2430
setting.AppSubURL = AppSubURL
@@ -245,7 +251,7 @@ func TestTotal_RenderWiki(t *testing.T) {
245251
answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/"))
246252

247253
for i := 0; i < len(sameCases); i++ {
248-
line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil)
254+
line := RenderWiki([]byte(sameCases[i]), AppSubURL, localMetas)
249255
assert.Equal(t, answers[i], line)
250256
}
251257

@@ -272,7 +278,7 @@ func TestTotal_RenderString(t *testing.T) {
272278
answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/"))
273279

274280
for i := 0; i < len(sameCases); i++ {
275-
line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), nil)
281+
line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), localMetas)
276282
assert.Equal(t, answers[i], line)
277283
}
278284

routers/api/v1/misc/markdown.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"code.gitea.io/gitea/modules/markup/markdown"
1414
"code.gitea.io/gitea/modules/setting"
1515
"code.gitea.io/gitea/modules/util"
16+
17+
"mvdan.cc/xurls/v2"
1618
)
1719

1820
// Markdown render markdown document to HTML
@@ -50,9 +52,12 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
5052
urlPrefix := form.Context
5153
var meta map[string]string
5254
if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) {
53-
// This is still incorrect...
54-
// Need to check if urlPrefix is an url - if so no join
55-
urlPrefix = util.URLJoin(setting.AppURL, form.Context)
55+
// check if urlPrefix is already set to a URL
56+
linkRegex, _ := xurls.StrictMatchingScheme("https?://")
57+
m := linkRegex.FindStringIndex(urlPrefix)
58+
if m == nil {
59+
urlPrefix = util.URLJoin(setting.AppURL, form.Context)
60+
}
5661
}
5762
if ctx.Repo != nil && ctx.Repo.Repository != nil {
5863
meta = ctx.Repo.Repository.ComposeMetas()

0 commit comments

Comments
 (0)