From dd136858f1ea40ad3c94191d647487fa4f31926c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 18 Oct 2024 20:33:49 +0200 Subject: Adding upstream version 9.0.0. Signed-off-by: Daniel Baumann --- modules/references/references.go | 594 ++++++++++++++++++++++++++++++++++ modules/references/references_test.go | 563 ++++++++++++++++++++++++++++++++ 2 files changed, 1157 insertions(+) create mode 100644 modules/references/references.go create mode 100644 modules/references/references_test.go (limited to 'modules/references') diff --git a/modules/references/references.go b/modules/references/references.go new file mode 100644 index 0000000..c61d06d --- /dev/null +++ b/modules/references/references.go @@ -0,0 +1,594 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package references + +import ( + "bytes" + "net/url" + "regexp" + "strconv" + "strings" + "sync" + + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup/mdstripper" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" +) + +var ( + // validNamePattern performs only the most basic validation for user or repository names + // Repository name should contain only alphanumeric, dash ('-'), underscore ('_') and dot ('.') characters. + validNamePattern = regexp.MustCompile(`^[a-z0-9_.-]+$`) + + // NOTE: All below regex matching do not perform any extra validation. + // Thus a link is produced even if the linked entity does not exist. + // While fast, this is also incorrect and lead to false positives. + // TODO: fix invalid linking issue + + // mentionPattern matches all mentions in the form of "@user" or "@org/team" + mentionPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@[0-9a-zA-Z-_]+|@[0-9a-zA-Z-_]+\/?[0-9a-zA-Z-_]+|@[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|[:;,.?!]$)`) + // 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. org/repo#12345 + crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+[#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`) + // crossReferenceCommitPattern matches a string that references a commit in a different repository + // e.g. go-gitea/gitea@d8a994ef, go-gitea/gitea@d8a994ef243349f321568f9e36d5c3f444b99cae (7-40 characters) + crossReferenceCommitPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+)@([0-9a-f]{7,64})(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`) + // spaceTrimmedPattern let's find the trailing space + spaceTrimmedPattern = regexp.MustCompile(`(?:.*[0-9a-zA-Z-_])\s`) + // timeLogPattern matches string for time tracking + timeLogPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(@([0-9]+([\.,][0-9]+)?(w|d|m|h))+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`) + + issueCloseKeywordsPat, issueReopenKeywordsPat *regexp.Regexp + issueKeywordsOnce sync.Once + + giteaHostInit sync.Once + giteaHost string + giteaIssuePullPattern *regexp.Regexp + + actionStrings = []string{ + "none", + "closes", + "reopens", + "neutered", + } +) + +// XRefAction represents the kind of effect a cross reference has once is resolved +type XRefAction int64 + +const ( + // XRefActionNone means the cross-reference is simply a comment + XRefActionNone XRefAction = iota // 0 + // XRefActionCloses means the cross-reference should close an issue if it is resolved + XRefActionCloses // 1 + // XRefActionReopens means the cross-reference should reopen an issue if it is resolved + XRefActionReopens // 2 + // XRefActionNeutered means the cross-reference will no longer affect the source + XRefActionNeutered // 3 +) + +func (a XRefAction) String() string { + return actionStrings[a] +} + +// IssueReference contains an unverified cross-reference to a local issue or pull request +type IssueReference struct { + Index int64 + Owner string + Name string + Action XRefAction + TimeLog string +} + +// 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 + CommitSha string + IsPull bool + RefLocation *RefSpan + Action XRefAction + ActionLocation *RefSpan +} + +type rawReference struct { + index int64 + owner string + name string + isPull bool + action XRefAction + issue string + refLocation *RefSpan + actionLocation *RefSpan + timeLog string +} + +func rawToIssueReferenceList(reflist []*rawReference) []IssueReference { + refarr := make([]IssueReference, len(reflist)) + for i, r := range reflist { + refarr[i] = IssueReference{ + Index: r.index, + Owner: r.owner, + Name: r.name, + Action: r.action, + TimeLog: r.timeLog, + } + } + return refarr +} + +// RefSpan is the position where the reference was found within the parsed text +type RefSpan struct { + Start int + End int +} + +func makeKeywordsPat(words []string) *regexp.Regexp { + acceptedWords := parseKeywords(words) + if len(acceptedWords) == 0 { + // Never match + return nil + } + return regexp.MustCompile(`(?i)(?:\s|^|\(|\[)(` + strings.Join(acceptedWords, `|`) + `):? $`) +} + +func parseKeywords(words []string) []string { + acceptedWords := make([]string, 0, 5) + wordPat := regexp.MustCompile(`^[\pL]+$`) + for _, word := range words { + word = strings.ToLower(strings.TrimSpace(word)) + // Accept Unicode letter class runes (a-z, á, à, ä, ) + if wordPat.MatchString(word) { + acceptedWords = append(acceptedWords, word) + } else { + log.Info("Invalid keyword: %s", word) + } + } + return acceptedWords +} + +func newKeywords() { + issueKeywordsOnce.Do(func() { + // Delay initialization until after the settings module is initialized + doNewKeywords(setting.Repository.PullRequest.CloseKeywords, setting.Repository.PullRequest.ReopenKeywords) + }) +} + +func doNewKeywords(close, reopen []string) { + issueCloseKeywordsPat = makeKeywordsPat(close) + issueReopenKeywordsPat = makeKeywordsPat(reopen) +} + +// getGiteaHostName returns a normalized string with the local host name, with no scheme or port information +func getGiteaHostName() string { + giteaHostInit.Do(func() { + if uapp, err := url.Parse(setting.AppURL); err == nil { + giteaHost = strings.ToLower(uapp.Host) + giteaIssuePullPattern = regexp.MustCompile( + `(\s|^|\(|\[)` + + regexp.QuoteMeta(strings.TrimSpace(setting.AppURL)) + + `([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+)/` + + `((?:issues)|(?:pulls))/([0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`) + } else { + giteaHost = "" + giteaIssuePullPattern = nil + } + }) + return giteaHost +} + +// getGiteaIssuePullPattern +func getGiteaIssuePullPattern() *regexp.Regexp { + getGiteaHostName() + return giteaIssuePullPattern +} + +// FindAllMentionsMarkdown matches mention patterns in given content and +// returns a list of found unvalidated user names **not including** the @ prefix. +func FindAllMentionsMarkdown(content string) []string { + bcontent, _ := mdstripper.StripMarkdownBytes([]byte(content)) + locations := FindAllMentionsBytes(bcontent) + mentions := make([]string, len(locations)) + for i, val := range locations { + mentions[i] = string(bcontent[val.Start+1 : val.End]) + } + return mentions +} + +// FindAllMentionsBytes matches mention patterns in given content +// and returns a list of locations for the unvalidated user names, including the @ prefix. +func FindAllMentionsBytes(content []byte) []RefSpan { + // Sadly we can't use FindAllSubmatchIndex because our pattern checks for starting and + // trailing spaces (\s@mention,\s), so if we get two consecutive references, the space + // from the second reference will be "eaten" by the first one: + // ...\s@mention1\s@mention2\s... --> ...`\s@mention1\s`, (not) `@mention2,\s...` + ret := make([]RefSpan, 0, 5) + pos := 0 + for { + match := mentionPattern.FindSubmatchIndex(content[pos:]) + if match == nil { + break + } + ret = append(ret, RefSpan{Start: match[2] + pos, End: match[3] + pos}) + notrail := spaceTrimmedPattern.FindSubmatchIndex(content[match[2]+pos : match[3]+pos]) + if notrail == nil { + pos = match[3] + pos + } else { + pos = match[3] + pos + notrail[1] - notrail[3] + } + } + return ret +} + +// FindFirstMentionBytes matches the first mention in then given content +// and returns the location of the unvalidated user name, including the @ prefix. +func FindFirstMentionBytes(content []byte) (bool, RefSpan) { + mention := mentionPattern.FindSubmatchIndex(content) + if mention == nil { + return false, RefSpan{} + } + return true, RefSpan{Start: mention[2], End: mention[3]} +} + +// FindAllIssueReferencesMarkdown strips content from markdown markup +// and returns a list of unvalidated references found in it. +func FindAllIssueReferencesMarkdown(content string) []IssueReference { + return rawToIssueReferenceList(findAllIssueReferencesMarkdown(content)) +} + +func findAllIssueReferencesMarkdown(content string) []*rawReference { + bcontent, links := mdstripper.StripMarkdownBytes([]byte(content)) + return findAllIssueReferencesBytes(bcontent, links) +} + +func convertFullHTMLReferencesToShortRefs(re *regexp.Regexp, contentBytes *[]byte) { + // We will iterate through the content, rewrite and simplify full references. + // + // We want to transform something like: + // + // this is a https://ourgitea.com/git/owner/repo/issues/123456789, foo + // https://ourgitea.com/git/owner/repo/pulls/123456789 + // + // Into something like: + // + // this is a #123456789, foo + // !123456789 + + pos := 0 + for { + // re looks for something like: (\s|^|\(|\[)https://ourgitea.com/git/(owner/repo)/(issues)/(123456789)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$) + match := re.FindSubmatchIndex((*contentBytes)[pos:]) + if match == nil { + break + } + // match is a bunch of indices into the content from pos onwards so + // to simplify things let's just add pos to all of the indices in match + for i := range match { + match[i] += pos + } + + // match[0]-match[1] is whole string + // match[2]-match[3] is preamble + + // move the position to the end of the preamble + pos = match[3] + + // match[4]-match[5] is owner/repo + // now copy the owner/repo to end of the preamble + endPos := pos + match[5] - match[4] + copy((*contentBytes)[pos:endPos], (*contentBytes)[match[4]:match[5]]) + + // move the current position to the end of the newly copied owner/repo + pos = endPos + + // Now set the issue/pull marker: + // + // match[6]-match[7] == 'issues' + (*contentBytes)[pos] = '#' + if string((*contentBytes)[match[6]:match[7]]) == "pulls" { + (*contentBytes)[pos] = '!' + } + pos++ + + // Then add the issue/pull number + // + // match[8]-match[9] is the number + endPos = pos + match[9] - match[8] + copy((*contentBytes)[pos:endPos], (*contentBytes)[match[8]:match[9]]) + + // Now copy what's left at the end of the string to the new end position + copy((*contentBytes)[endPos:], (*contentBytes)[match[9]:]) + // now we reset the length + + // our new section has length endPos - match[3] + // our old section has length match[9] - match[3] + *contentBytes = (*contentBytes)[:len(*contentBytes)-match[9]+endPos] + pos = endPos + } +} + +// FindAllIssueReferences returns a list of unvalidated references found in a string. +func FindAllIssueReferences(content string) []IssueReference { + // Need to convert fully qualified html references to local system to #/! short codes + contentBytes := []byte(content) + if re := getGiteaIssuePullPattern(); re != nil { + convertFullHTMLReferencesToShortRefs(re, &contentBytes) + } else { + log.Debug("No GiteaIssuePullPattern pattern") + } + return rawToIssueReferenceList(findAllIssueReferencesBytes(contentBytes, []string{})) +} + +// FindRenderizableReferenceNumeric returns the first unvalidated reference found in a string. +func FindRenderizableReferenceNumeric(content string, prOnly, crossLinkOnly bool) (bool, *RenderizableReference) { + var match []int + if !crossLinkOnly { + match = issueNumericPattern.FindStringSubmatchIndex(content) + } + if match == nil { + if match = crossReferenceIssueNumericPattern.FindStringSubmatchIndex(content); match == nil { + return false, nil + } + } + r := getCrossReference(util.UnsafeStringToBytes(content), match[2], match[3], false, prOnly) + if r == nil { + return false, nil + } + + return true, &RenderizableReference{ + Issue: r.issue, + Owner: r.owner, + Name: r.name, + IsPull: r.isPull, + RefLocation: r.refLocation, + Action: r.action, + ActionLocation: r.actionLocation, + } +} + +// FindRenderizableCommitCrossReference returns the first unvalidated commit cross reference found in a string. +func FindRenderizableCommitCrossReference(content string) (bool, *RenderizableReference) { + m := crossReferenceCommitPattern.FindStringSubmatchIndex(content) + if len(m) < 8 { + return false, nil + } + + return true, &RenderizableReference{ + Owner: content[m[2]:m[3]], + Name: content[m[4]:m[5]], + CommitSha: content[m[6]:m[7]], + RefLocation: &RefSpan{Start: m[2], End: m[7]}, + } +} + +// FindRenderizableReferenceRegexp returns the first regexp unvalidated references found in a string. +func FindRenderizableReferenceRegexp(content string, pattern *regexp.Regexp) (bool, *RenderizableReference) { + match := pattern.FindStringSubmatchIndex(content) + if len(match) < 4 { + return false, nil + } + + action, location := findActionKeywords([]byte(content), match[2]) + + return true, &RenderizableReference{ + Issue: content[match[2]:match[3]], + RefLocation: &RefSpan{Start: match[0], End: match[1]}, + Action: action, + ActionLocation: location, + IsPull: false, + } +} + +// FindRenderizableReferenceAlphanumeric returns the first alphanumeric unvalidated references found in a string. +func FindRenderizableReferenceAlphanumeric(content string) (bool, *RenderizableReference) { + match := issueAlphanumericPattern.FindStringSubmatchIndex(content) + if match == nil { + return false, nil + } + + action, location := findActionKeywords([]byte(content), match[2]) + + return true, &RenderizableReference{ + Issue: content[match[2]:match[3]], + RefLocation: &RefSpan{Start: match[2], End: match[3]}, + Action: action, + ActionLocation: location, + IsPull: false, + } +} + +// FindAllIssueReferencesBytes returns a list of unvalidated references found in a byte slice. +func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference { + ret := make([]*rawReference, 0, 10) + pos := 0 + + // Sadly we can't use FindAllSubmatchIndex because our pattern checks for starting and + // trailing spaces (\s#ref,\s), so if we get two consecutive references, the space + // from the second reference will be "eaten" by the first one: + // ...\s#ref1\s#ref2\s... --> ...`\s#ref1\s`, (not) `#ref2,\s...` + for { + match := issueNumericPattern.FindSubmatchIndex(content[pos:]) + if match == nil { + break + } + if ref := getCrossReference(content, match[2]+pos, match[3]+pos, false, false); ref != nil { + ret = append(ret, ref) + } + notrail := spaceTrimmedPattern.FindSubmatchIndex(content[match[2]+pos : match[3]+pos]) + if notrail == nil { + pos = match[3] + pos + } else { + pos = match[3] + pos + notrail[1] - notrail[3] + } + } + + pos = 0 + + for { + match := crossReferenceIssueNumericPattern.FindSubmatchIndex(content[pos:]) + if match == nil { + break + } + if ref := getCrossReference(content, match[2]+pos, match[3]+pos, false, false); ref != nil { + ret = append(ret, ref) + } + notrail := spaceTrimmedPattern.FindSubmatchIndex(content[match[2]+pos : match[3]+pos]) + if notrail == nil { + pos = match[3] + pos + } else { + pos = match[3] + pos + notrail[1] - notrail[3] + } + } + + localhost := getGiteaHostName() + for _, link := range links { + if u, err := url.Parse(link); err == nil { + // Note: we're not attempting to match the URL scheme (http/https) + host := strings.ToLower(u.Host) + if host != "" && host != localhost { + continue + } + parts := strings.Split(u.EscapedPath(), "/") + // /user/repo/issues/3 + if len(parts) != 5 || parts[0] != "" { + continue + } + 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] + sep + parts[4]) + if ref := getCrossReference(bytes, 0, len(bytes), true, false); ref != nil { + ref.refLocation = nil + ret = append(ret, ref) + } + } + } + + if len(ret) == 0 { + return ret + } + + pos = 0 + + for { + match := timeLogPattern.FindSubmatchIndex(content[pos:]) + if match == nil { + break + } + + timeLogEntry := string(content[match[2]+pos+1 : match[3]+pos]) + + var f *rawReference + for _, ref := range ret { + if ref.refLocation != nil && ref.refLocation.End < match[2]+pos && (f == nil || f.refLocation.End < ref.refLocation.End) { + f = ref + } + } + + pos = match[1] + pos + + if f == nil { + f = ret[0] + } + + if len(f.timeLog) == 0 { + f.timeLog = timeLogEntry + } + } + + return ret +} + +func getCrossReference(content []byte, start, end int, fromLink, prOnly bool) *rawReference { + sep := bytes.IndexAny(content[start:end], "#!") + if sep < 0 { + return nil + } + isPull := content[start+sep] == '!' + if prOnly && !isPull { + return nil + } + repo := string(content[start : start+sep]) + issue := string(content[start+sep+1 : end]) + index, err := strconv.ParseInt(issue, 10, 64) + if err != nil { + return nil + } + if repo == "" { + if fromLink { + // Markdown links must specify owner/repo + return nil + } + action, location := findActionKeywords(content, start) + return &rawReference{ + index: index, + action: action, + issue: issue, + isPull: isPull, + refLocation: &RefSpan{Start: start, End: end}, + actionLocation: location, + } + } + parts := strings.Split(strings.ToLower(repo), "/") + if len(parts) != 2 { + return nil + } + owner, name := parts[0], parts[1] + if !validNamePattern.MatchString(owner) || !validNamePattern.MatchString(name) { + return nil + } + action, location := findActionKeywords(content, start) + return &rawReference{ + index: index, + owner: owner, + name: name, + action: action, + issue: issue, + isPull: isPull, + refLocation: &RefSpan{Start: start, End: end}, + actionLocation: location, + } +} + +func findActionKeywords(content []byte, start int) (XRefAction, *RefSpan) { + newKeywords() + var m []int + if issueCloseKeywordsPat != nil { + m = issueCloseKeywordsPat.FindSubmatchIndex(content[:start]) + if m != nil { + return XRefActionCloses, &RefSpan{Start: m[2], End: m[3]} + } + } + if issueReopenKeywordsPat != nil { + m = issueReopenKeywordsPat.FindSubmatchIndex(content[:start]) + if m != nil { + return XRefActionReopens, &RefSpan{Start: m[2], End: m[3]} + } + } + return XRefActionNone, nil +} + +// IsXrefActionable returns true if the xref action is actionable (i.e. produces a result when resolved) +func IsXrefActionable(ref *RenderizableReference, extTracker bool) bool { + if extTracker { + // External issues cannot be automatically closed + return false + } + return ref.Action == XRefActionCloses || ref.Action == XRefActionReopens +} diff --git a/modules/references/references_test.go b/modules/references/references_test.go new file mode 100644 index 0000000..ffa7f99 --- /dev/null +++ b/modules/references/references_test.go @@ -0,0 +1,563 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package references + +import ( + "regexp" + "testing" + + "code.gitea.io/gitea/modules/setting" + + "github.com/stretchr/testify/assert" +) + +type testFixture struct { + input string + expected []testResult +} + +type testResult struct { + Index int64 + Owner string + Name string + Issue string + IsPull bool + Action XRefAction + RefLocation *RefSpan + ActionLocation *RefSpan + TimeLog string +} + +func TestConvertFullHTMLReferencesToShortRefs(t *testing.T) { + re := regexp.MustCompile(`(\s|^|\(|\[)` + + regexp.QuoteMeta("https://ourgitea.com/git/") + + `([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+)/` + + `((?:issues)|(?:pulls))/([0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`) + test := `this is a https://ourgitea.com/git/owner/repo/issues/123456789, foo +https://ourgitea.com/git/owner/repo/pulls/123456789 + And https://ourgitea.com/git/owner/repo/pulls/123 +` + expect := `this is a owner/repo#123456789, foo +owner/repo!123456789 + And owner/repo!123 +` + + contentBytes := []byte(test) + convertFullHTMLReferencesToShortRefs(re, &contentBytes) + result := string(contentBytes) + assert.EqualValues(t, expect, result) +} + +func TestFindAllIssueReferences(t *testing.T) { + fixtures := []testFixture{ + { + "Simply closes: #29 yes", + []testResult{ + {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}, ""}, + }, + }, + { + " #124 yes, this is a reference.", + []testResult{ + {124, "", "", "124", false, XRefActionNone, &RefSpan{Start: 0, End: 4}, nil, ""}, + }, + }, + { + "```\nThis is a code block.\n#723 no, it's a code block.```", + []testResult{}, + }, + { + "This `#724` no, it's inline code.", + []testResult{}, + }, + { + "This org3/repo4#200 yes.", + []testResult{ + {200, "org3", "repo4", "200", false, XRefActionNone, &RefSpan{Start: 5, End: 19}, nil, ""}, + }, + }, + { + "This org3/repo4!200 yes.", + []testResult{ + {200, "org3", "repo4", "200", true, XRefActionNone, &RefSpan{Start: 5, End: 19}, nil, ""}, + }, + }, + { + "This [one](#919) no, this is a URL fragment.", + []testResult{}, + }, + { + "This [two](/user2/repo1/issues/921) yes.", + []testResult{ + {921, "user2", "repo1", "921", false, XRefActionNone, nil, nil, ""}, + }, + }, + { + "This [three](/user2/repo1/pulls/922) yes.", + []testResult{ + {922, "user2", "repo1", "922", true, XRefActionNone, nil, nil, ""}, + }, + }, + { + "This [four](http://gitea.com:3000/org3/repo4/issues/203) yes.", + []testResult{ + {203, "org3", "repo4", "203", false, XRefActionNone, nil, nil, ""}, + }, + }, + { + "This [five](http://github.com/org3/repo4/issues/204) no.", + []testResult{}, + }, + { + "This http://gitea.com:3000/user4/repo5/201 no, bad URL.", + []testResult{}, + }, + { + "This http://gitea.com:3000/user4/repo5/pulls/202 yes.", + []testResult{ + {202, "user4", "repo5", "202", true, XRefActionNone, nil, nil, ""}, + }, + }, + { + "This http://gitea.com:3000/user4/repo5/pulls/202 yes. http://gitea.com:3000/user4/repo5/pulls/203 no", + []testResult{ + {202, "user4", "repo5", "202", true, XRefActionNone, nil, nil, ""}, + {203, "user4", "repo5", "203", true, XRefActionNone, nil, nil, ""}, + }, + }, + { + "This http://GiTeA.COM:3000/user4/repo6/pulls/205 yes.", + []testResult{ + {205, "user4", "repo6", "205", true, XRefActionNone, nil, nil, ""}, + }, + }, + { + "Reopens #15 yes", + []testResult{ + {15, "", "", "15", false, XRefActionReopens, &RefSpan{Start: 8, End: 11}, &RefSpan{Start: 0, End: 7}, ""}, + }, + }, + { + "This closes #20 for you yes", + []testResult{ + {20, "", "", "20", false, XRefActionCloses, &RefSpan{Start: 12, End: 15}, &RefSpan{Start: 5, End: 11}, ""}, + }, + }, + { + "Do you fix org6/repo6#300 ? yes", + []testResult{ + {300, "org6", "repo6", "300", false, XRefActionCloses, &RefSpan{Start: 11, End: 25}, &RefSpan{Start: 7, End: 10}, ""}, + }, + }, + { + "For 999 #1235 no keyword, but yes", + []testResult{ + {1235, "", "", "1235", false, XRefActionNone, &RefSpan{Start: 8, End: 13}, nil, ""}, + }, + }, + { + "For [!123] yes", + []testResult{ + {123, "", "", "123", true, XRefActionNone, &RefSpan{Start: 5, End: 9}, nil, ""}, + }, + }, + { + "For (#345) yes", + []testResult{ + {345, "", "", "345", false, XRefActionNone, &RefSpan{Start: 5, End: 9}, nil, ""}, + }, + }, + { + "For #22,#23 no, neither #28:#29 or !30!31#32;33 should", + []testResult{}, + }, + { + "For #24, and #25. yes; also #26; #27? #28! and #29: should", + []testResult{ + {24, "", "", "24", false, XRefActionNone, &RefSpan{Start: 4, End: 7}, nil, ""}, + {25, "", "", "25", false, XRefActionNone, &RefSpan{Start: 13, End: 16}, nil, ""}, + {26, "", "", "26", false, XRefActionNone, &RefSpan{Start: 28, End: 31}, nil, ""}, + {27, "", "", "27", false, XRefActionNone, &RefSpan{Start: 33, End: 36}, nil, ""}, + {28, "", "", "28", false, XRefActionNone, &RefSpan{Start: 38, End: 41}, nil, ""}, + {29, "", "", "29", false, XRefActionNone, &RefSpan{Start: 47, End: 50}, nil, ""}, + }, + }, + { + "This org3/repo4#200, yes.", + []testResult{ + {200, "org3", "repo4", "200", false, XRefActionNone, &RefSpan{Start: 5, End: 19}, nil, ""}, + }, + }, + { + "Merge pull request '#12345 My fix for a bug' (!1337) from feature-branch into main", + []testResult{ + {12345, "", "", "12345", false, XRefActionNone, &RefSpan{Start: 20, End: 26}, nil, ""}, + {1337, "", "", "1337", true, XRefActionNone, &RefSpan{Start: 46, End: 51}, nil, ""}, + }, + }, + { + "Which abc. #9434 same as above", + []testResult{ + {9434, "", "", "9434", false, XRefActionNone, &RefSpan{Start: 11, End: 16}, nil, ""}, + }, + }, + { + "This closes #600 and reopens #599", + []testResult{ + {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}, ""}, + }, + }, + { + "This fixes #100 spent @40m and reopens #101, also fixes #102 spent @4h15m", + []testResult{ + {100, "", "", "100", false, XRefActionCloses, &RefSpan{Start: 11, End: 15}, &RefSpan{Start: 5, End: 10}, "40m"}, + {101, "", "", "101", false, XRefActionReopens, &RefSpan{Start: 39, End: 43}, &RefSpan{Start: 31, End: 38}, ""}, + {102, "", "", "102", false, XRefActionCloses, &RefSpan{Start: 56, End: 60}, &RefSpan{Start: 50, End: 55}, "4h15m"}, + }, + }, + } + + testFixtures(t, fixtures, "default") + + type alnumFixture struct { + input string + issue string + refLocation *RefSpan + action XRefAction + actionLocation *RefSpan + } + + alnumFixtures := []alnumFixture{ + { + "This ref ABC-123 is alphanumeric", + "ABC-123", &RefSpan{Start: 9, End: 16}, + XRefActionNone, nil, + }, + { + "This closes ABCD-1234 alphanumeric", + "ABCD-1234", &RefSpan{Start: 12, End: 21}, + XRefActionCloses, &RefSpan{Start: 5, End: 11}, + }, + } + + for _, fixture := range alnumFixtures { + found, ref := FindRenderizableReferenceAlphanumeric(fixture.input) + if fixture.issue == "" { + assert.False(t, found, "Failed to parse: {%s}", fixture.input) + } else { + assert.True(t, found, "Failed to parse: {%s}", fixture.input) + assert.Equal(t, fixture.issue, ref.Issue, "Failed to parse: {%s}", fixture.input) + assert.Equal(t, fixture.refLocation, ref.RefLocation, "Failed to parse: {%s}", fixture.input) + assert.Equal(t, fixture.action, ref.Action, "Failed to parse: {%s}", fixture.input) + assert.Equal(t, fixture.actionLocation, ref.ActionLocation, "Failed to parse: {%s}", fixture.input) + } + } +} + +func testFixtures(t *testing.T, fixtures []testFixture, context string) { + // Save original value for other tests that may rely on it + prevURL := setting.AppURL + setting.AppURL = "https://gitea.com:3000/" + + for _, fixture := range fixtures { + expraw := make([]*rawReference, len(fixture.expected)) + for i, e := range fixture.expected { + expraw[i] = &rawReference{ + index: e.Index, + owner: e.Owner, + name: e.Name, + isPull: e.IsPull, + action: e.Action, + issue: e.Issue, + refLocation: e.RefLocation, + actionLocation: e.ActionLocation, + timeLog: e.TimeLog, + } + } + expref := rawToIssueReferenceList(expraw) + refs := FindAllIssueReferencesMarkdown(fixture.input) + assert.EqualValues(t, expref, refs, "[%s] Failed to parse: {%s}", context, fixture.input) + rawrefs := findAllIssueReferencesMarkdown(fixture.input) + assert.EqualValues(t, expraw, rawrefs, "[%s] Failed to parse: {%s}", context, fixture.input) + } + + // Restore for other tests that may rely on the original value + setting.AppURL = prevURL +} + +func TestFindAllMentions(t *testing.T) { + res := FindAllMentionsBytes([]byte("@tasha, @mike; @lucy: @john")) + assert.EqualValues(t, []RefSpan{ + {Start: 0, End: 6}, + {Start: 8, End: 13}, + {Start: 15, End: 20}, + {Start: 22, End: 27}, + }, res) +} + +func TestFindRenderizableCommitCrossReference(t *testing.T) { + cases := []struct { + Input string + Expected *RenderizableReference + }{ + { + Input: "", + Expected: nil, + }, + { + Input: "test", + Expected: nil, + }, + { + Input: "go-gitea/gitea@test", + Expected: nil, + }, + { + Input: "go-gitea/gitea@ab1234", + Expected: nil, + }, + { + Input: "go-gitea/gitea@abcd1234", + Expected: &RenderizableReference{ + Owner: "go-gitea", + Name: "gitea", + CommitSha: "abcd1234", + RefLocation: &RefSpan{Start: 0, End: 23}, + }, + }, + { + Input: "go-gitea/gitea@abcd1234abcd1234abcd1234abcd1234abcd1234", + Expected: &RenderizableReference{ + Owner: "go-gitea", + Name: "gitea", + CommitSha: "abcd1234abcd1234abcd1234abcd1234abcd1234", + RefLocation: &RefSpan{Start: 0, End: 55}, + }, + }, + { + Input: "go-gitea/gitea@abcd1234abcd1234abcd1234abcd1234abcd12341234512345123451234512345", // longer than 64 characters + Expected: nil, + }, + { + Input: "test go-gitea/gitea@abcd1234 test", + Expected: &RenderizableReference{ + Owner: "go-gitea", + Name: "gitea", + CommitSha: "abcd1234", + RefLocation: &RefSpan{Start: 5, End: 28}, + }, + }, + } + + for _, c := range cases { + found, ref := FindRenderizableCommitCrossReference(c.Input) + assert.Equal(t, ref != nil, found) + assert.Equal(t, c.Expected, ref) + } +} + +func TestRegExp_mentionPattern(t *testing.T) { + trueTestCases := []struct { + pat string + exp string + }{ + {"@User", "@User"}, + {"@ANT_123", "@ANT_123"}, + {"@xxx-DiN0-z-A..uru..s-xxx", "@xxx-DiN0-z-A..uru..s-xxx"}, + {" @lol ", "@lol"}, + {" @Te-st", "@Te-st"}, + {"(@gitea)", "@gitea"}, + {"[@gitea]", "@gitea"}, + {"@gitea! this", "@gitea"}, + {"@gitea? this", "@gitea"}, + {"@gitea. this", "@gitea"}, + {"@gitea, this", "@gitea"}, + {"@gitea; this", "@gitea"}, + {"@gitea!\nthis", "@gitea"}, + {"\n@gitea?\nthis", "@gitea"}, + {"\t@gitea.\nthis", "@gitea"}, + {"@gitea,\nthis", "@gitea"}, + {"@gitea;\nthis", "@gitea"}, + {"@gitea!", "@gitea"}, + {"@gitea?", "@gitea"}, + {"@gitea.", "@gitea"}, + {"@gitea,", "@gitea"}, + {"@gitea;", "@gitea"}, + {"@gitea/team1;", "@gitea/team1"}, + {"@jess'", "@jess"}, + {"@forgejo's", "@forgejo"}, + {"Оно сломалось из-за коммитов от @jopik'а", "@jopik"}, + } + falseTestCases := []string{ + "@ 0", + "@ ", + "@", + "", + "ABC", + "@.ABC", + "/home/gitea/@gitea", + "\"@gitea\"", + "@@gitea", + "@gitea!this", + "@gitea?this", + "@gitea,this", + "@gitea;this", + "@gitea/team1/more", + } + + for _, testCase := range trueTestCases { + found := mentionPattern.FindStringSubmatch(testCase.pat) + assert.Len(t, found, 2) + assert.Equal(t, testCase.exp, found[1]) + } + for _, testCase := range falseTestCases { + res := mentionPattern.MatchString(testCase) + assert.False(t, res, "[%s] should be false", testCase) + } +} + +func TestRegExp_issueNumericPattern(t *testing.T) { + trueTestCases := []string{ + "#1234", + "#0", + "#1234567890987654321", + " #12", + "#12:", + "ref: #12: msg", + "\"#1234\"", + "'#1234'", + } + falseTestCases := []string{ + "# 1234", + "# 0", + "# ", + "#", + "#ABC", + "#1A2B", + "", + "ABC", + } + + for _, testCase := range trueTestCases { + assert.True(t, issueNumericPattern.MatchString(testCase)) + } + for _, testCase := range falseTestCases { + assert.False(t, issueNumericPattern.MatchString(testCase)) + } +} + +func TestRegExp_issueAlphanumericPattern(t *testing.T) { + trueTestCases := []string{ + "ABC-1234", + "A-1", + "RC-80", + "ABCDEFGHIJ-1234567890987654321234567890", + "ABC-123.", + "(ABC-123)", + "[ABC-123]", + "ABC-123:", + "\"ABC-123\"", + "'ABC-123'", + } + falseTestCases := []string{ + "RC-08", + "PR-0", + "ABCDEFGHIJK-1", + "PR_1", + "", + "#ABC", + "", + "ABC", + "GG-", + "rm-1", + "/home/gitea/ABC-1234", + "MY-STRING-ABC-123", + } + + for _, testCase := range trueTestCases { + assert.True(t, issueAlphanumericPattern.MatchString(testCase)) + } + for _, testCase := range falseTestCases { + assert.False(t, issueAlphanumericPattern.MatchString(testCase)) + } +} + +func TestCustomizeCloseKeywords(t *testing.T) { + fixtures := []testFixture{ + { + "Simplemente cierra: #29 yes", + []testResult{ + {29, "", "", "29", false, XRefActionCloses, &RefSpan{Start: 20, End: 23}, &RefSpan{Start: 12, End: 18}, ""}, + }, + }, + { + "Closes: #123 no, this English.", + []testResult{ + {123, "", "", "123", false, XRefActionNone, &RefSpan{Start: 8, End: 12}, nil, ""}, + }, + }, + { + "Cerró org6/repo6#300 yes", + []testResult{ + {300, "org6", "repo6", "300", false, XRefActionCloses, &RefSpan{Start: 7, End: 21}, &RefSpan{Start: 0, End: 6}, ""}, + }, + }, + { + "Reabre org3/repo4#200 yes", + []testResult{ + {200, "org3", "repo4", "200", false, XRefActionReopens, &RefSpan{Start: 7, End: 21}, &RefSpan{Start: 0, End: 6}, ""}, + }, + }, + } + + issueKeywordsOnce.Do(func() {}) + + doNewKeywords([]string{"cierra", "cerró"}, []string{"reabre"}) + testFixtures(t, fixtures, "spanish") + + // Restore default settings + doNewKeywords(setting.Repository.PullRequest.CloseKeywords, setting.Repository.PullRequest.ReopenKeywords) +} + +func TestParseCloseKeywords(t *testing.T) { + // Test parsing of CloseKeywords and ReopenKeywords + assert.Empty(t, parseKeywords([]string{""})) + assert.Len(t, parseKeywords([]string{" aa ", " bb ", "99", "#", "", "this is", "cc"}), 3) + + for _, test := range []struct { + pattern string + match string + expected string + }{ + {"close", "This PR will close ", "close"}, + {"cerró", "cerró ", "cerró"}, + {"cerró", "AQUÍ SE CERRÓ: ", "CERRÓ"}, + {"закрывается", "закрывается ", "закрывается"}, + {"κλείνει", "κλείνει: ", "κλείνει"}, + {"关闭", "关闭 ", "关闭"}, + {"閉じます", "閉じます ", "閉じます"}, + {",$!", "", ""}, + {"1234", "", ""}, + } { + // The pattern only needs to match the part that precedes the reference. + // getCrossReference() takes care of finding the reference itself. + pat := makeKeywordsPat([]string{test.pattern}) + if test.expected == "" { + assert.Nil(t, pat) + } else { + assert.NotNil(t, pat) + res := pat.FindAllStringSubmatch(test.match, -1) + assert.Len(t, res, 1) + assert.Len(t, res[0], 2) + assert.EqualValues(t, test.expected, res[0][1]) + } + } +} -- cgit v1.2.3