Skip to content

Commit b616585

Browse files
committed
Improve issue indexer
1 parent d766d0c commit b616585

File tree

119 files changed

+15264
-1451
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+15264
-1451
lines changed

integrations/integration_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ func TestMain(m *testing.M) {
5757
fmt.Printf("Error initializing test database: %v\n", err)
5858
os.Exit(1)
5959
}
60-
os.Exit(m.Run())
60+
exitCode := m.Run()
61+
62+
if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil {
63+
fmt.Printf("os.RemoveAll: %v\n", err)
64+
os.Exit(1)
65+
}
66+
67+
os.Exit(exitCode)
6168
}
6269

6370
func initIntegrationTest() {

integrations/issue_test.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
"github.com/stretchr/testify/assert"
1919
)
2020

21-
func getIssuesSelection(htmlDoc *HTMLDoc) *goquery.Selection {
22-
return htmlDoc.doc.Find(".issue.list").Find("li").Find(".title")
21+
func getIssuesSelection(t testing.TB, htmlDoc *HTMLDoc) *goquery.Selection {
22+
issueList := htmlDoc.doc.Find(".issue.list")
23+
assert.EqualValues(t, 1, issueList.Length())
24+
return issueList.Find("li").Find(".title")
2325
}
2426

2527
func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *models.Issue {
@@ -31,26 +33,37 @@ func getIssue(t *testing.T, repoID int64, issueSelection *goquery.Selection) *mo
3133
return models.AssertExistsAndLoadBean(t, &models.Issue{RepoID: repoID, Index: int64(index)}).(*models.Issue)
3234
}
3335

36+
func assertMatch(t testing.TB, issue *models.Issue, keyword string) {
37+
matches := strings.Contains(strings.ToLower(issue.Title), keyword) ||
38+
strings.Contains(strings.ToLower(issue.Content), keyword)
39+
for _, comment := range issue.Comments {
40+
matches = matches || strings.Contains(
41+
strings.ToLower(comment.Content),
42+
keyword,
43+
)
44+
}
45+
assert.True(t, matches)
46+
}
47+
3448
func TestNoLoginViewIssues(t *testing.T) {
3549
prepareTestEnv(t)
3650

3751
req := NewRequest(t, "GET", "/user2/repo1/issues")
3852
MakeRequest(t, req, http.StatusOK)
3953
}
4054

41-
func TestNoLoginViewIssuesSortByType(t *testing.T) {
55+
func TestViewIssuesSortByType(t *testing.T) {
4256
prepareTestEnv(t)
4357

4458
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
4559
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
46-
repo.Owner = models.AssertExistsAndLoadBean(t, &models.User{ID: repo.OwnerID}).(*models.User)
4760

4861
session := loginUser(t, user.Name)
4962
req := NewRequest(t, "GET", repo.RelLink()+"/issues?type=created_by")
5063
resp := session.MakeRequest(t, req, http.StatusOK)
5164

5265
htmlDoc := NewHTMLParser(t, resp.Body)
53-
issuesSelection := getIssuesSelection(htmlDoc)
66+
issuesSelection := getIssuesSelection(t, htmlDoc)
5467
expectedNumIssues := models.GetCount(t,
5568
&models.Issue{RepoID: repo.ID, PosterID: user.ID},
5669
models.Cond("is_closed=?", false),
@@ -67,6 +80,26 @@ func TestNoLoginViewIssuesSortByType(t *testing.T) {
6780
})
6881
}
6982

83+
func TestViewIssuesKeyword(t *testing.T) {
84+
prepareTestEnv(t)
85+
86+
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
87+
88+
const keyword = "first"
89+
req := NewRequestf(t, "GET", "%s/issues?q=%s", repo.RelLink(), keyword)
90+
resp := MakeRequest(t, req, http.StatusOK)
91+
92+
htmlDoc := NewHTMLParser(t, resp.Body)
93+
issuesSelection := getIssuesSelection(t, htmlDoc)
94+
assert.EqualValues(t, 1, issuesSelection.Length())
95+
issuesSelection.Each(func(_ int, selection *goquery.Selection) {
96+
issue := getIssue(t, repo.ID, selection)
97+
assert.False(t, issue.IsClosed)
98+
assert.False(t, issue.IsPull)
99+
assertMatch(t, issue, keyword)
100+
})
101+
}
102+
70103
func TestNoLoginViewIssue(t *testing.T) {
71104
prepareTestEnv(t)
72105

integrations/mysql.ini.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PASSWD = {{TEST_MYSQL_PASSWORD}}
1010
SSL_MODE = disable
1111
PATH = data/gitea.db
1212

13+
[indexer]
14+
ISSUE_INDEXER_PATH = integrations/indexers/issues.bleve
15+
1316
[repository]
1417
ROOT = integrations/gitea-integration-mysql/gitea-repositories
1518

integrations/pgsql.ini.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PASSWD = {{TEST_PGSQL_PASSWORD}}
1010
SSL_MODE = disable
1111
PATH = data/gitea.db
1212

13+
[indexer]
14+
ISSUE_INDEXER_PATH = integrations/indexers/issues.bleve
15+
1316
[repository]
1417
ROOT = integrations/gitea-integration-pgsql/gitea-repositories
1518

integrations/sqlite.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ RUN_MODE = prod
55
DB_TYPE = sqlite3
66
PATH = :memory:
77

8+
[indexer]
9+
ISSUE_INDEXER_PATH = integrations/indexers/issues.bleve
10+
811
[repository]
912
ROOT = integrations/gitea-integration-sqlite/gitea-repositories
1013

models/fixtures/issue.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
poster_id: 1
66
assignee_id: 1
77
name: issue1
8-
content: content1
8+
content: content for the first issue
99
is_closed: false
1010
is_pull: false
1111
num_comments: 2
@@ -18,7 +18,7 @@
1818
index: 2
1919
poster_id: 1
2020
name: issue2
21-
content: content2
21+
content: content for the second issue
2222
milestone_id: 1
2323
is_closed: false
2424
is_pull: true
@@ -32,7 +32,7 @@
3232
index: 3
3333
poster_id: 1
3434
name: issue3
35-
content: content4
35+
content: content for the third issue
3636
is_closed: false
3737
is_pull: true
3838
created_unix: 946684820
@@ -44,7 +44,7 @@
4444
index: 1
4545
poster_id: 2
4646
name: issue4
47-
content: content4
47+
content: content for the fourth issue
4848
is_closed: true
4949
is_pull: false
5050

@@ -54,7 +54,7 @@
5454
index: 4
5555
poster_id: 2
5656
name: issue5
57-
content: content5
57+
content: content for the fifth issue
5858
is_closed: true
5959
is_pull: false
6060
-

models/issue.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ func (issue *Issue) loadPullRequest(e Engine) (err error) {
155155
return nil
156156
}
157157

158+
func (issue *Issue) loadComments(e Engine) (err error) {
159+
if issue.Comments != nil {
160+
return nil
161+
}
162+
issue.Comments, err = findComments(e, FindCommentsOptions{
163+
IssueID: issue.ID,
164+
Type: CommentTypeUnknown,
165+
})
166+
return err
167+
}
168+
158169
func (issue *Issue) loadAttributes(e Engine) (err error) {
159170
if err = issue.loadRepo(e); err != nil {
160171
return
@@ -191,14 +202,8 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
191202
}
192203
}
193204

194-
if issue.Comments == nil {
195-
issue.Comments, err = findComments(e, FindCommentsOptions{
196-
IssueID: issue.ID,
197-
Type: CommentTypeUnknown,
198-
})
199-
if err != nil {
200-
return fmt.Errorf("getCommentsByIssueID [%d]: %v", issue.ID, err)
201-
}
205+
if err = issue.loadComments(e); err != nil {
206+
return
202207
}
203208

204209
return nil
@@ -577,7 +582,7 @@ func updateIssueCols(e Engine, issue *Issue, cols ...string) error {
577582
if _, err := e.Id(issue.ID).Cols(cols...).Update(issue); err != nil {
578583
return err
579584
}
580-
UpdateIssueIndexer(issue)
585+
UpdateIssueIndexer(issue.ID)
581586
return nil
582587
}
583588

@@ -907,7 +912,7 @@ func newIssue(e *xorm.Session, doer *User, opts NewIssueOptions) (err error) {
907912
return err
908913
}
909914

910-
UpdateIssueIndexer(opts.Issue)
915+
UpdateIssueIndexer(opts.Issue.ID)
911916

912917
if len(opts.Attachments) > 0 {
913918
attachments, err := getAttachmentsByUUIDs(e, opts.Attachments)
@@ -1448,7 +1453,7 @@ func updateIssue(e Engine, issue *Issue) error {
14481453
if err != nil {
14491454
return err
14501455
}
1451-
UpdateIssueIndexer(issue)
1456+
UpdateIssueIndexer(issue.ID)
14521457
return nil
14531458
}
14541459

models/issue_comment.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,14 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
520520
return nil, err
521521
}
522522

523-
return comment, sess.Commit()
523+
if err = sess.Commit(); err != nil {
524+
return nil, err
525+
}
526+
527+
if opts.Type == CommentTypeComment {
528+
UpdateIssueIndexer(opts.Issue.ID)
529+
}
530+
return comment, nil
524531
}
525532

526533
// CreateIssueComment creates a plain issue comment.
@@ -645,8 +652,12 @@ func GetCommentsByRepoIDSince(repoID, since int64) ([]*Comment, error) {
645652

646653
// UpdateComment updates information of comment.
647654
func UpdateComment(c *Comment) error {
648-
_, err := x.Id(c.ID).AllCols().Update(c)
649-
return err
655+
if _, err := x.Id(c.ID).AllCols().Update(c); err != nil {
656+
return err
657+
} else if c.Type == CommentTypeComment {
658+
UpdateIssueIndexer(c.IssueID)
659+
}
660+
return nil
650661
}
651662

652663
// DeleteComment deletes the comment
@@ -672,5 +683,10 @@ func DeleteComment(comment *Comment) error {
672683
return err
673684
}
674685

675-
return sess.Commit()
686+
if err := sess.Commit(); err != nil {
687+
return err
688+
} else if comment.Type == CommentTypeComment {
689+
UpdateIssueIndexer(comment.IssueID)
690+
}
691+
return nil
676692
}

0 commit comments

Comments
 (0)