aboutsummaryrefslogtreecommitdiffstats
path: root/modules/references
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2020-09-04 18:37:37 +0300
committerGitHub <noreply@github.com>2020-09-04 11:37:37 -0400
commite710a3498129a53343e08279012ea9c45988349f (patch)
treead353035dbb8a1b3bbebe29f043c92bc150f29ba /modules/references
parent4c557eff5d4002299dd8a9dfca7917084c2307f6 (diff)
downloadgitea-e710a3498129a53343e08279012ea9c45988349f.tar.gz
gitea-e710a3498129a53343e08279012ea9c45988349f.zip
Add spent time to referenced issue in commit message (#12220)
Diffstat (limited to 'modules/references')
-rw-r--r--modules/references/references.go53
-rw-r--r--modules/references/references_test.go70
2 files changed, 85 insertions, 38 deletions
diff --git a/modules/references/references.go b/modules/references/references.go
index ce08dcc7ab..070c6e566a 100644
--- a/modules/references/references.go
+++ b/modules/references/references.go
@@ -37,6 +37,8 @@ var (
crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+/[0-9a-zA-Z-_\.]+[#!][0-9]+)(?:\s|$|\)|\]|[:;,.?!]\s|[:;,.?!]$)`)
// spaceTrimmedPattern let's us 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
@@ -62,10 +64,11 @@ const (
// IssueReference contains an unverified cross-reference to a local issue or pull request
type IssueReference struct {
- Index int64
- Owner string
- Name string
- Action XRefAction
+ Index int64
+ Owner string
+ Name string
+ Action XRefAction
+ TimeLog string
}
// RenderizableReference contains an unverified cross-reference to with rendering information
@@ -91,16 +94,18 @@ type rawReference struct {
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,
+ Index: r.index,
+ Owner: r.owner,
+ Name: r.name,
+ Action: r.action,
+ TimeLog: r.timeLog,
}
}
return refarr
@@ -386,6 +391,38 @@ func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference
}
}
+ 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
}
diff --git a/modules/references/references_test.go b/modules/references/references_test.go
index 48589c1637..0c4037f120 100644
--- a/modules/references/references_test.go
+++ b/modules/references/references_test.go
@@ -26,6 +26,7 @@ type testResult struct {
Action XRefAction
RefLocation *RefSpan
ActionLocation *RefSpan
+ TimeLog string
}
func TestFindAllIssueReferences(t *testing.T) {
@@ -34,19 +35,19 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"Simply closes: #29 yes",
[]testResult{
- {29, "", "", "29", false, 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}},
+ {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},
+ {124, "", "", "124", false, XRefActionNone, &RefSpan{Start: 0, End: 4}, nil, ""},
},
},
{
@@ -60,13 +61,13 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"This user3/repo4#200 yes.",
[]testResult{
- {200, "user3", "repo4", "200", false, 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},
+ {200, "user3", "repo4", "200", true, XRefActionNone, &RefSpan{Start: 5, End: 20}, nil, ""},
},
},
{
@@ -76,19 +77,19 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"This [two](/user2/repo1/issues/921) yes.",
[]testResult{
- {921, "user2", "repo1", "921", false, XRefActionNone, nil, nil},
+ {921, "user2", "repo1", "921", false, XRefActionNone, nil, nil, ""},
},
},
{
"This [three](/user2/repo1/pulls/922) yes.",
[]testResult{
- {922, "user2", "repo1", "922", true, 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", false, XRefActionNone, nil, nil},
+ {203, "user3", "repo4", "203", false, XRefActionNone, nil, nil, ""},
},
},
{
@@ -102,49 +103,49 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"This http://gitea.com:3000/user4/repo5/pulls/202 yes.",
[]testResult{
- {202, "user4", "repo5", "202", true, 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", true, XRefActionNone, nil, nil},
+ {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}},
+ {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}},
+ {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", false, 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", false, XRefActionNone, &RefSpan{Start: 8, End: 13}, nil},
+ {1235, "", "", "1235", false, XRefActionNone, &RefSpan{Start: 8, End: 13}, nil, ""},
},
},
{
"For [!123] yes",
[]testResult{
- {123, "", "", "123", true, XRefActionNone, &RefSpan{Start: 5, End: 9}, nil},
+ {123, "", "", "123", true, XRefActionNone, &RefSpan{Start: 5, End: 9}, nil, ""},
},
},
{
"For (#345) yes",
[]testResult{
- {345, "", "", "345", false, XRefActionNone, &RefSpan{Start: 5, End: 9}, nil},
+ {345, "", "", "345", false, XRefActionNone, &RefSpan{Start: 5, End: 9}, nil, ""},
},
},
{
@@ -154,31 +155,39 @@ func TestFindAllIssueReferences(t *testing.T) {
{
"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},
+ {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 user3/repo4#200, yes.",
[]testResult{
- {200, "user3", "repo4", "200", false, XRefActionNone, &RefSpan{Start: 5, End: 20}, nil},
+ {200, "user3", "repo4", "200", false, XRefActionNone, &RefSpan{Start: 5, End: 20}, nil, ""},
},
},
{
"Which abc. #9434 same as above",
[]testResult{
- {9434, "", "", "9434", false, 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", 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}},
+ {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"},
},
},
}
@@ -237,6 +246,7 @@ func testFixtures(t *testing.T, fixtures []testFixture, context string) {
issue: e.Issue,
refLocation: e.RefLocation,
actionLocation: e.ActionLocation,
+ timeLog: e.TimeLog,
}
}
expref := rawToIssueReferenceList(expraw)
@@ -382,25 +392,25 @@ func TestCustomizeCloseKeywords(t *testing.T) {
{
"Simplemente cierra: #29 yes",
[]testResult{
- {29, "", "", "29", false, 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", false, 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", false, 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", false, 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}, ""},
},
},
}