Skip to content

Commit

Permalink
Update for use of context metas
Browse files Browse the repository at this point in the history
Now that we include the user and repo name inside context metas, update
various code and tests for this new logic
  • Loading branch information
mrsdizzie committed Mar 28, 2019
1 parent aaab9df commit 102bcff
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
5 changes: 4 additions & 1 deletion models/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 11 additions & 14 deletions modules/markup/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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 {
Expand All @@ -579,18 +575,19 @@ 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
} else {
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)
}
Expand Down
14 changes: 11 additions & 3 deletions modules/markup/html_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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)))
}
Expand Down Expand Up @@ -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))
Expand Down
10 changes: 8 additions & 2 deletions modules/markup/markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down
11 changes: 8 additions & 3 deletions routers/api/v1/misc/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 102bcff

Please sign in to comment.