Alternate syntax for cross references (#9116)

* Add support for local vs. remote xrefs

* Add doc for references

* Docs: fix cases not currently supported

* One more doc fix

* Doc: mentions for teams and orgs

* Change !num ref concept, no change in functionality

* Fix test

* Improve table of issue reference types

* Fix paragraph mark
This commit is contained in:
guillep2k 2019-12-01 10:57:05 -03:00 committed by Lauris BH
parent 2011a5b818
commit 6a90c7e3dd
5 changed files with 309 additions and 65 deletions

View file

@ -640,10 +640,19 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
ref *references.RenderizableReference
)
if ctx.metas["style"] == IssueNameStyleAlphanumeric {
found, ref = references.FindRenderizableReferenceAlphanumeric(node.Data)
} else {
found, ref = references.FindRenderizableReferenceNumeric(node.Data)
_, exttrack := ctx.metas["format"]
alphanum := ctx.metas["style"] == IssueNameStyleAlphanumeric
// Repos with external issue trackers might still need to reference local PRs
// We need to concern with the first one that shows up in the text, whichever it is
found, ref = references.FindRenderizableReferenceNumeric(node.Data, exttrack && alphanum)
if exttrack && alphanum {
if found2, ref2 := references.FindRenderizableReferenceAlphanumeric(node.Data); found2 {
if !found || ref2.RefLocation.Start < ref.RefLocation.Start {
found = true
ref = ref2
}
}
}
if !found {
return
@ -651,13 +660,22 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
var link *html.Node
reftext := node.Data[ref.RefLocation.Start:ref.RefLocation.End]
if _, ok := ctx.metas["format"]; ok {
if exttrack && !ref.IsPull {
ctx.metas["index"] = ref.Issue
link = createLink(com.Expand(ctx.metas["format"], ctx.metas), reftext, "issue")
} else if ref.Owner == "" {
link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], "issues", ref.Issue), reftext, "issue")
} else {
link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, "issues", ref.Issue), reftext, "issue")
// Path determines the type of link that will be rendered. It's unknown at this point whether
// the linked item is actually a PR or an issue. Luckily it's of no real consequence because
// Gitea will redirect on click as appropriate.
path := "issues"
if ref.IsPull {
path = "pulls"
}
if ref.Owner == "" {
link = createLink(util.URLJoin(setting.AppURL, ctx.metas["user"], ctx.metas["repo"], path, ref.Issue), reftext, "issue")
} else {
link = createLink(util.URLJoin(setting.AppURL, ref.Owner, ref.Name, path, ref.Issue), reftext, "issue")
}
}
if ref.Action == references.XRefActionNone {
@ -667,7 +685,7 @@ func issueIndexPatternProcessor(ctx *postProcessCtx, node *html.Node) {
// Decorate action keywords if actionable
var keyword *html.Node
if references.IsXrefActionable(ref.Action) {
if references.IsXrefActionable(ref, exttrack, alphanum) {
keyword = createKeyword(node.Data[ref.ActionLocation.Start:ref.ActionLocation.End])
} else {
keyword = &html.Node{

View file

@ -25,8 +25,8 @@ func alphanumIssueLink(baseURL, class, name string) string {
}
// numericLink an HTML to a numeric-style issue
func numericIssueLink(baseURL, class string, index int) string {
return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("#%d", index))
func numericIssueLink(baseURL, class string, index int, marker string) string {
return link(util.URLJoin(baseURL, strconv.Itoa(index)), class, fmt.Sprintf("%s%d", marker, index))
}
// link an HTML link
@ -75,8 +75,12 @@ func TestRender_IssueIndexPattern(t *testing.T) {
test("#abcd")
test("test#1234")
test("#1234test")
test(" test #1234test")
test("#abcd")
test("test!1234")
test("!1234test")
test(" test !1234test")
test("/home/gitea/#1234")
test("/home/gitea/!1234")
// should not render issue mention without leading space
test("test#54321 issue")
@ -90,42 +94,54 @@ func TestRender_IssueIndexPattern2(t *testing.T) {
setting.AppSubURL = AppSubURL
// numeric: render inputs with valid mentions
test := func(s, expectedFmt string, indices ...int) {
test := func(s, expectedFmt, marker string, indices ...int) {
var path, prefix string
if marker == "!" {
path = "pulls"
prefix = "http://localhost:3000/someUser/someRepo/pulls/"
} else {
path = "issues"
prefix = "https://someurl.com/someUser/someRepo/"
}
links := make([]interface{}, len(indices))
for i, index := range indices {
links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", index)
links[i] = numericIssueLink(util.URLJoin(setting.AppSubURL, path), "issue", index, marker)
}
expectedNil := fmt.Sprintf(expectedFmt, links...)
testRenderIssueIndexPattern(t, s, expectedNil, &postProcessCtx{metas: localMetas})
for i, index := range indices {
links[i] = numericIssueLink("https://someurl.com/someUser/someRepo/", "issue", index)
links[i] = numericIssueLink(prefix, "issue", index, marker)
}
expectedNum := fmt.Sprintf(expectedFmt, links...)
testRenderIssueIndexPattern(t, s, expectedNum, &postProcessCtx{metas: numericMetas})
}
// should render freestanding mentions
test("#1234 test", "%s test", 1234)
test("test #8 issue", "test %s issue", 8)
test("test issue #1234", "test issue %s", 1234)
test("fixes issue #1234.", "fixes issue %s.", 1234)
test("#1234 test", "%s test", "#", 1234)
test("test #8 issue", "test %s issue", "#", 8)
test("!1234 test", "%s test", "!", 1234)
test("test !8 issue", "test %s issue", "!", 8)
test("test issue #1234", "test issue %s", "#", 1234)
test("fixes issue #1234.", "fixes issue %s.", "#", 1234)
// should render mentions in parentheses / brackets
test("(#54321 issue)", "(%s issue)", 54321)
test("[#54321 issue]", "[%s issue]", 54321)
test("test (#9801 extra) issue", "test (%s extra) issue", 9801)
test("test (#1)", "test (%s)", 1)
test("(#54321 issue)", "(%s issue)", "#", 54321)
test("[#54321 issue]", "[%s issue]", "#", 54321)
test("test (#9801 extra) issue", "test (%s extra) issue", "#", 9801)
test("test (!9801 extra) issue", "test (%s extra) issue", "!", 9801)
test("test (#1)", "test (%s)", "#", 1)
// should render multiple issue mentions in the same line
test("#54321 #1243", "%s %s", 54321, 1243)
test("wow (#54321 #1243)", "wow (%s %s)", 54321, 1243)
test("(#4)(#5)", "(%s)(%s)", 4, 5)
test("#1 (#4321) test", "%s (%s) test", 1, 4321)
test("#54321 #1243", "%s %s", "#", 54321, 1243)
test("wow (#54321 #1243)", "wow (%s %s)", "#", 54321, 1243)
test("(#4)(#5)", "(%s)(%s)", "#", 4, 5)
test("#1 (#4321) test", "%s (%s) test", "#", 1, 4321)
// should render with :
test("#1234: test", "%s: test", 1234)
test("wow (#54321: test)", "wow (%s: test)", 54321)
test("#1234: test", "%s: test", "#", 1234)
test("wow (#54321: test)", "wow (%s: test)", "#", 54321)
}
func TestRender_IssueIndexPattern3(t *testing.T) {
@ -201,7 +217,7 @@ func TestRender_AutoLink(t *testing.T) {
// render valid issue URLs
test(util.URLJoin(setting.AppSubURL, "issues", "3333"),
numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", 3333))
numericIssueLink(util.URLJoin(setting.AppSubURL, "issues"), "issue", 3333, "#"))
// render valid commit URLs
tmp := util.URLJoin(AppSubURL, "commit", "d8a994ef243349f321568f9e36d5c3f444b99cae")

View file

@ -29,12 +29,12 @@ var (
// mentionPattern matches all mentions in the form of "@user"
mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_][0-9a-zA-Z-_.]+[0-9a-zA-Z-_])(?:\s|[:,;.?!]\s|[:,;.?!]?$|\)|\])`)
// issueNumericPattern matches string that references to a numeric issue, e.g. #1287
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(#[0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([#!][0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
// issueAlphanumericPattern matches string that references to an alphanumeric issue, e.g. ABC-1234
issueAlphanumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([A-Z]{1,10}-[1-9][0-9]*)(?:\s|$|\)|\]|:|\.(\s|$))`)
// crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
// e.g. gogits/gogs#12345
crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+#[0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)
crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+[#!][0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)
issueCloseKeywordsPat, issueReopenKeywordsPat *regexp.Regexp
issueKeywordsOnce sync.Once
@ -66,10 +66,14 @@ type IssueReference struct {
}
// RenderizableReference contains an unverified cross-reference to with rendering information
// The IsPull member means that a `!num` reference was used instead of `#num`.
// This kind of reference is used to make pulls available when an external issue tracker
// is used. Otherwise, `#` and `!` are completely interchangeable.
type RenderizableReference struct {
Issue string
Owner string
Name string
IsPull bool
RefLocation *RefSpan
Action XRefAction
ActionLocation *RefSpan
@ -79,6 +83,7 @@ type rawReference struct {
index int64
owner string
name string
isPull bool
action XRefAction
issue string
refLocation *RefSpan
@ -202,14 +207,14 @@ func FindAllIssueReferences(content string) []IssueReference {
}
// FindRenderizableReferenceNumeric returns the first unvalidated reference found in a string.
func FindRenderizableReferenceNumeric(content string) (bool, *RenderizableReference) {
func FindRenderizableReferenceNumeric(content string, prOnly bool) (bool, *RenderizableReference) {
match := issueNumericPattern.FindStringSubmatchIndex(content)
if match == nil {
if match = crossReferenceIssueNumericPattern.FindStringSubmatchIndex(content); match == nil {
return false, nil
}
}
r := getCrossReference([]byte(content), match[2], match[3], false)
r := getCrossReference([]byte(content), match[2], match[3], false, prOnly)
if r == nil {
return false, nil
}
@ -218,6 +223,7 @@ func FindRenderizableReferenceNumeric(content string) (bool, *RenderizableRefere
Issue: r.issue,
Owner: r.owner,
Name: r.name,
IsPull: r.isPull,
RefLocation: r.refLocation,
Action: r.action,
ActionLocation: r.actionLocation,
@ -238,6 +244,7 @@ func FindRenderizableReferenceAlphanumeric(content string) (bool, *RenderizableR
RefLocation: &RefSpan{Start: match[2], End: match[3]},
Action: action,
ActionLocation: location,
IsPull: false,
}
}
@ -248,14 +255,14 @@ func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference
matches := issueNumericPattern.FindAllSubmatchIndex(content, -1)
for _, match := range matches {
if ref := getCrossReference(content, match[2], match[3], false); ref != nil {
if ref := getCrossReference(content, match[2], match[3], false, false); ref != nil {
ret = append(ret, ref)
}
}
matches = crossReferenceIssueNumericPattern.FindAllSubmatchIndex(content, -1)
for _, match := range matches {
if ref := getCrossReference(content, match[2], match[3], false); ref != nil {
if ref := getCrossReference(content, match[2], match[3], false, false); ref != nil {
ret = append(ret, ref)
}
}
@ -273,12 +280,17 @@ func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference
if len(parts) != 5 || parts[0] != "" {
continue
}
if parts[3] != "issues" && parts[3] != "pulls" {
var sep string
if parts[3] == "issues" {
sep = "#"
} else if parts[3] == "pulls" {
sep = "!"
} else {
continue
}
// Note: closing/reopening keywords not supported with URLs
bytes := []byte(parts[1] + "/" + parts[2] + "#" + parts[4])
if ref := getCrossReference(bytes, 0, len(bytes), true); ref != nil {
bytes := []byte(parts[1] + "/" + parts[2] + sep + parts[4])
if ref := getCrossReference(bytes, 0, len(bytes), true, false); ref != nil {
ref.refLocation = nil
ret = append(ret, ref)
}
@ -288,13 +300,18 @@ func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference
return ret
}
func getCrossReference(content []byte, start, end int, fromLink bool) *rawReference {
func getCrossReference(content []byte, start, end int, fromLink bool, prOnly bool) *rawReference {
refid := string(content[start:end])
parts := strings.Split(refid, "#")
if len(parts) != 2 {
sep := strings.IndexAny(refid, "#!")
if sep < 0 {
return nil
}
repo, issue := parts[0], parts[1]
isPull := refid[sep] == '!'
if prOnly && !isPull {
return nil
}
repo := refid[:sep]
issue := refid[sep+1:]
index, err := strconv.ParseInt(issue, 10, 64)
if err != nil {
return nil
@ -309,11 +326,12 @@ func getCrossReference(content []byte, start, end int, fromLink bool) *rawRefere
index: index,
action: action,
issue: issue,
isPull: isPull,
refLocation: &RefSpan{Start: start, End: end},
actionLocation: location,
}
}
parts = strings.Split(strings.ToLower(repo), "/")
parts := strings.Split(strings.ToLower(repo), "/")
if len(parts) != 2 {
return nil
}
@ -328,6 +346,7 @@ func getCrossReference(content []byte, start, end int, fromLink bool) *rawRefere
name: name,
action: action,
issue: issue,
isPull: isPull,
refLocation: &RefSpan{Start: start, End: end},
actionLocation: location,
}
@ -352,6 +371,10 @@ func findActionKeywords(content []byte, start int) (XRefAction, *RefSpan) {
}
// IsXrefActionable returns true if the xref action is actionable (i.e. produces a result when resolved)
func IsXrefActionable(a XRefAction) bool {
return a == XRefActionCloses || a == XRefActionReopens
func IsXrefActionable(ref *RenderizableReference, extTracker bool, alphaNum bool) bool {
if extTracker {
// External issues cannot be automatically closed
return false
}
return ref.Action == XRefActionCloses || ref.Action == XRefActionReopens
}

View file

@ -22,6 +22,7 @@ type testResult struct {
Owner string
Name string
Issue string
IsPull bool
Action XRefAction
RefLocation *RefSpan
ActionLocation *RefSpan
@ -33,7 +34,13 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"Simply closes: #29 yes",
[]testResult{
{29, "", "", "29", XRefActionCloses, &RefSpan{Start: 15, End: 18}, &RefSpan{Start: 7, End: 13}},
{29, "", "", "29", false, XRefActionCloses, &RefSpan{Start: 15, End: 18}, &RefSpan{Start: 7, End: 13}},
},
},
{
"Simply closes: !29 yes",
[]testResult{
{29, "", "", "29", true, XRefActionCloses, &RefSpan{Start: 15, End: 18}, &RefSpan{Start: 7, End: 13}},
},
},
{
@ -43,7 +50,7 @@ func TestFindAllIssueReferences(t *testing.T) {
{
" #124 yes, this is a reference.",
[]testResult{
{124, "", "", "124", XRefActionNone, &RefSpan{Start: 0, End: 4}, nil},
{124, "", "", "124", false, XRefActionNone, &RefSpan{Start: 0, End: 4}, nil},
},
},
{
@ -57,7 +64,13 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"This user3/repo4#200 yes.",
[]testResult{
{200, "user3", "repo4", "200", XRefActionNone, &RefSpan{Start: 5, End: 20}, nil},
{200, "user3", "repo4", "200", false, XRefActionNone, &RefSpan{Start: 5, End: 20}, nil},
},
},
{
"This user3/repo4!200 yes.",
[]testResult{
{200, "user3", "repo4", "200", true, XRefActionNone, &RefSpan{Start: 5, End: 20}, nil},
},
},
{
@ -67,19 +80,19 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"This [two](/user2/repo1/issues/921) yes.",
[]testResult{
{921, "user2", "repo1", "921", XRefActionNone, nil, nil},
{921, "user2", "repo1", "921", false, XRefActionNone, nil, nil},
},
},
{
"This [three](/user2/repo1/pulls/922) yes.",
[]testResult{
{922, "user2", "repo1", "922", XRefActionNone, nil, nil},
{922, "user2", "repo1", "922", true, XRefActionNone, nil, nil},
},
},
{
"This [four](http://gitea.com:3000/user3/repo4/issues/203) yes.",
[]testResult{
{203, "user3", "repo4", "203", XRefActionNone, nil, nil},
{203, "user3", "repo4", "203", false, XRefActionNone, nil, nil},
},
},
{
@ -93,50 +106,50 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"This http://gitea.com:3000/user4/repo5/pulls/202 yes.",
[]testResult{
{202, "user4", "repo5", "202", XRefActionNone, nil, nil},
{202, "user4", "repo5", "202", true, XRefActionNone, nil, nil},
},
},
{
"This http://GiTeA.COM:3000/user4/repo6/pulls/205 yes.",
[]testResult{
{205, "user4", "repo6", "205", XRefActionNone, nil, nil},
{205, "user4", "repo6", "205", true, XRefActionNone, nil, nil},
},
},
{
"Reopens #15 yes",
[]testResult{
{15, "", "", "15", XRefActionReopens, &RefSpan{Start: 8, End: 11}, &RefSpan{Start: 0, End: 7}},
{15, "", "", "15", false, XRefActionReopens, &RefSpan{Start: 8, End: 11}, &RefSpan{Start: 0, End: 7}},
},
},
{
"This closes #20 for you yes",
[]testResult{
{20, "", "", "20", XRefActionCloses, &RefSpan{Start: 12, End: 15}, &RefSpan{Start: 5, End: 11}},
{20, "", "", "20", false, XRefActionCloses, &RefSpan{Start: 12, End: 15}, &RefSpan{Start: 5, End: 11}},
},
},
{
"Do you fix user6/repo6#300 ? yes",
[]testResult{
{300, "user6", "repo6", "300", XRefActionCloses, &RefSpan{Start: 11, End: 26}, &RefSpan{Start: 7, End: 10}},
{300, "user6", "repo6", "300", false, XRefActionCloses, &RefSpan{Start: 11, End: 26}, &RefSpan{Start: 7, End: 10}},
},
},
{
"For 999 #1235 no keyword, but yes",
[]testResult{
{1235, "", "", "1235", XRefActionNone, &RefSpan{Start: 8, End: 13}, nil},
{1235, "", "", "1235", false, XRefActionNone, &RefSpan{Start: 8, End: 13}, nil},
},
},
{
"Which abc. #9434 same as above",
[]testResult{
{9434, "", "", "9434", XRefActionNone, &RefSpan{Start: 11, End: 16}, nil},
{9434, "", "", "9434", false, XRefActionNone, &RefSpan{Start: 11, End: 16}, nil},
},
},
{
"This closes #600 and reopens #599",
[]testResult{
{600, "", "", "600", XRefActionCloses, &RefSpan{Start: 12, End: 16}, &RefSpan{Start: 5, End: 11}},
{599, "", "", "599", XRefActionReopens, &RefSpan{Start: 29, End: 33}, &RefSpan{Start: 21, End: 28}},
{600, "", "", "600", false, XRefActionCloses, &RefSpan{Start: 12, End: 16}, &RefSpan{Start: 5, End: 11}},
{599, "", "", "599", false, XRefActionReopens, &RefSpan{Start: 29, End: 33}, &RefSpan{Start: 21, End: 28}},
},
},
}
@ -190,6 +203,7 @@ func testFixtures(t *testing.T, fixtures []testFixture, context string) {
index: e.Index,
owner: e.Owner,
name: e.Name,
isPull: e.IsPull,
action: e.Action,
issue: e.Issue,
refLocation: e.RefLocation,
@ -329,25 +343,25 @@ func TestCustomizeCloseKeywords(t *testing.T) {
{
"Simplemente cierra: #29 yes",
[]testResult{
{29, "", "", "29", XRefActionCloses, &RefSpan{Start: 20, End: 23}, &RefSpan{Start: 12, End: 18}},
{29, "", "", "29", false, XRefActionCloses, &RefSpan{Start: 20, End: 23}, &RefSpan{Start: 12, End: 18}},
},
},
{
"Closes: #123 no, this English.",
[]testResult{
{123, "", "", "123", XRefActionNone, &RefSpan{Start: 8, End: 12}, nil},
{123, "", "", "123", false, XRefActionNone, &RefSpan{Start: 8, End: 12}, nil},
},
},
{
"Cerró user6/repo6#300 yes",
[]testResult{
{300, "user6", "repo6", "300", XRefActionCloses, &RefSpan{Start: 7, End: 22}, &RefSpan{Start: 0, End: 6}},
{300, "user6", "repo6", "300", false, XRefActionCloses, &RefSpan{Start: 7, End: 22}, &RefSpan{Start: 0, End: 6}},
},
},
{
"Reabre user3/repo4#200 yes",
[]testResult{
{200, "user3", "repo4", "200", XRefActionReopens, &RefSpan{Start: 7, End: 22}, &RefSpan{Start: 0, End: 6}},
{200, "user3", "repo4", "200", false, XRefActionReopens, &RefSpan{Start: 7, End: 22}, &RefSpan{Start: 0, End: 6}},
},
},
}