From 102bcffcb6aa60c5bee1054d5c31daa483f2c0a0 Mon Sep 17 00:00:00 2001 From: "j. mccann" Date: Thu, 28 Mar 2019 14:17:46 -0400 Subject: [PATCH] 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 --- models/repo_test.go | 5 ++++- modules/markup/html.go | 25 +++++++++++------------- modules/markup/html_internal_test.go | 14 ++++++++++--- modules/markup/markdown/markdown_test.go | 10 ++++++++-- routers/api/v1/misc/markdown.go | 11 ++++++++--- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/models/repo_test.go b/models/repo_test.go index 752ffc2dd9085..a5b8cce9b993a 100644 --- a/models/repo_test.go +++ b/models/repo_test.go @@ -20,7 +20,10 @@ func TestRepo(t *testing.T) { repo.Owner = &User{Name: "testOwner"} repo.Units = nil - assert.Nil(t, repo.ComposeMetas()) + + metas := repo.ComposeMetas() + assert.Equal(t, "testRepo", metas["repo"]) + assert.Equal(t, "testOwner", metas["user"]) externalTracker := RepoUnit{ Type: UnitTypeExternalTracker, diff --git a/modules/markup/html.go b/modules/markup/html.go index 963568dd26ebd..427f3361c25b7 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -534,6 +534,9 @@ func shortLinkProcessorFull(ctx *postProcessCtx, node *html.Node, noLink bool) { } func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) { + if ctx.metas == nil { + return + } m := getIssueFullPattern().FindStringSubmatchIndex(node.Data) if m == nil { return @@ -547,15 +550,7 @@ func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) { matchOrg := linkParts[len(linkParts)-4] matchRepo := linkParts[len(linkParts)-3] - // extract the current org and repo from ctx URL like - // http://localhost:3000/gituser/myrepo/ - url := strings.Replace(ctx.urlPrefix, setting.AppURL, "", -1) - urlParts := strings.Split(path.Clean(url), "/") - - currentOrg := urlParts[0] - currentRepo := urlParts[1] - - if matchOrg == currentOrg && matchRepo == currentRepo { + if matchOrg == ctx.metas["user"] && matchRepo == ctx.metas["repo"] { // TODO if m[4]:m[5] is not nil, then link is to a comment, // and we should indicate that in the text somehow replaceContent(node, m[0], m[1], createLink(link, id)) @@ -567,8 +562,9 @@ func fullIssuePatternProcessor(ctx *postProcessCtx, node *html.Node) { } func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { - prefix := cutoutVerbosePrefix(ctx.urlPrefix) - + if ctx.metas == nil { + return + } // default to numeric pattern, unless alphanumeric is requested. pattern := issueNumericPattern if ctx.metas["style"] == IssueNameStyleAlphanumeric { @@ -579,11 +575,10 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { if match == nil { return } + id := node.Data[match[2]:match[3]] var link *html.Node - if ctx.metas == nil { - link = createLink(util.URLJoin(prefix, "issues", id[1:]), id) - } else { + if _, ok := ctx.metas["format"]; ok { // Support for external issue tracker if ctx.metas["style"] == IssueNameStyleAlphanumeric { ctx.metas["index"] = id @@ -591,6 +586,8 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) { ctx.metas["index"] = id[1:] } link = createLink(com.Expand(ctx.metas["format"], ctx.metas), id) + } else { + link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", id[1:]), id) } replaceContent(node, match[2], match[3], link) } diff --git a/modules/markup/html_internal_test.go b/modules/markup/html_internal_test.go index 7e60b8ab34137..034a4018b268b 100644 --- a/modules/markup/html_internal_test.go +++ b/modules/markup/html_internal_test.go @@ -53,6 +53,12 @@ var alphanumericMetas = map[string]string{ "style": IssueNameStyleAlphanumeric, } +// these values should match the Repo const above +var localMetas = map[string]string{ + "user": "gogits", + "repo": "gogs", +} + func TestRender_IssueIndexPattern(t *testing.T) { // numeric: render inputs without valid mentions test := func(s string) { @@ -90,7 +96,7 @@ func TestRender_IssueIndexPattern2(t *testing.T) { links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), index) } expectedNil := fmt.Sprintf(expectedFmt, links...) - testRenderIssueIndexPattern(t, s, expectedNil, nil) + testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas}) for i, index := range indices { links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", index) @@ -168,6 +174,7 @@ func testRenderIssueIndexPattern(t *testing.T, input, expected string, ctx *post if ctx.urlPrefix == "" { ctx.urlPrefix = AppSubURL } + res, err := ctx.postProcess([]byte(input)) assert.NoError(t, err) assert.Equal(t, expected, string(res)) @@ -178,10 +185,10 @@ func TestRender_AutoLink(t *testing.T) { setting.AppSubURL = AppSubURL test := func(input, expected string) { - buffer, err := PostProcess([]byte(input), setting.AppSubURL, nil, false) + buffer, err := PostProcess([]byte(input), setting.AppSubURL, localMetas, false) assert.Equal(t, err, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) - buffer, err = PostProcess([]byte(input), setting.AppSubURL, nil, true) + buffer, err = PostProcess([]byte(input), setting.AppSubURL, localMetas, true) assert.Equal(t, err, nil) assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(buffer))) } @@ -211,6 +218,7 @@ func TestRender_FullIssueURLs(t *testing.T) { if ctx.urlPrefix == "" { ctx.urlPrefix = AppSubURL } + ctx.metas = localMetas result, err := ctx.postProcess([]byte(input)) assert.NoError(t, err) assert.Equal(t, expected, string(result)) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index c473c92bc8d5e..8ba51e6a1bb50 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -19,6 +19,12 @@ const AppURL = "http://localhost:3000/" const Repo = "gogits/gogs" const AppSubURL = AppURL + Repo + "/" +// these values should match the Repo const above +var localMetas = map[string]string{ + "user": "gogits", + "repo": "gogs", +} + func TestRender_StandardLinks(t *testing.T) { setting.AppURL = AppURL setting.AppSubURL = AppSubURL @@ -245,7 +251,7 @@ func TestTotal_RenderWiki(t *testing.T) { answers := testAnswers(util.URLJoin(AppSubURL, "wiki/"), util.URLJoin(AppSubURL, "wiki", "raw/")) for i := 0; i < len(sameCases); i++ { - line := RenderWiki([]byte(sameCases[i]), AppSubURL, nil) + line := RenderWiki([]byte(sameCases[i]), AppSubURL, localMetas) assert.Equal(t, answers[i], line) } @@ -272,7 +278,7 @@ func TestTotal_RenderString(t *testing.T) { answers := testAnswers(util.URLJoin(AppSubURL, "src", "master/"), util.URLJoin(AppSubURL, "raw", "master/")) for i := 0; i < len(sameCases); i++ { - line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), nil) + line := RenderString(sameCases[i], util.URLJoin(AppSubURL, "src", "master/"), localMetas) assert.Equal(t, answers[i], line) } diff --git a/routers/api/v1/misc/markdown.go b/routers/api/v1/misc/markdown.go index bd6d00308c78b..9ae7a6c58cee1 100644 --- a/routers/api/v1/misc/markdown.go +++ b/routers/api/v1/misc/markdown.go @@ -13,6 +13,8 @@ import ( "code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" + + "mvdan.cc/xurls/v2" ) // Markdown render markdown document to HTML @@ -50,9 +52,12 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) { urlPrefix := form.Context var meta map[string]string if !strings.HasPrefix(setting.AppSubURL+"/", urlPrefix) { - // This is still incorrect... - // Need to check if urlPrefix is an url - if so no join - urlPrefix = util.URLJoin(setting.AppURL, form.Context) + // check if urlPrefix is already set to a URL + linkRegex, _ := xurls.StrictMatchingScheme("https?://") + m := linkRegex.FindStringIndex(urlPrefix) + if m == nil { + urlPrefix = util.URLJoin(setting.AppURL, form.Context) + } } if ctx.Repo != nil && ctx.Repo.Repository != nil { meta = ctx.Repo.Repository.ComposeMetas()