2017-01-25 02:43:02 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-02-19 14:39:39 +00:00
|
|
|
"code.gitea.io/gitea/modules/indexer/issues"
|
2017-01-25 02:43:02 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
)
|
|
|
|
|
2019-02-19 14:39:39 +00:00
|
|
|
var (
|
|
|
|
// issueIndexerUpdateQueue queue of issue ids to be updated
|
|
|
|
issueIndexerUpdateQueue issues.Queue
|
|
|
|
issueIndexer issues.Indexer
|
|
|
|
)
|
2017-01-25 02:43:02 +00:00
|
|
|
|
|
|
|
// InitIssueIndexer initialize issue indexer
|
2019-02-19 14:39:39 +00:00
|
|
|
func InitIssueIndexer() error {
|
|
|
|
var populate bool
|
|
|
|
switch setting.Indexer.IssueType {
|
|
|
|
case "bleve":
|
|
|
|
issueIndexer = issues.NewBleveIndexer(setting.Indexer.IssuePath)
|
|
|
|
exist, err := issueIndexer.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
populate = !exist
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknow issue indexer type: %s", setting.Indexer.IssueType)
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
switch setting.Indexer.IssueIndexerQueueType {
|
|
|
|
case setting.LevelQueueType:
|
|
|
|
issueIndexerUpdateQueue, err = issues.NewLevelQueue(
|
|
|
|
issueIndexer,
|
|
|
|
setting.Indexer.IssueIndexerQueueDir,
|
|
|
|
setting.Indexer.IssueIndexerQueueBatchNumber)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case setting.ChannelQueueType:
|
|
|
|
issueIndexerUpdateQueue = issues.NewChannelQueue(issueIndexer, setting.Indexer.IssueIndexerQueueBatchNumber)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unsupported indexer queue type: %v", setting.Indexer.IssueIndexerQueueType)
|
|
|
|
}
|
|
|
|
|
|
|
|
go issueIndexerUpdateQueue.Run()
|
|
|
|
|
|
|
|
if populate {
|
|
|
|
go populateIssueIndexer()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// populateIssueIndexer populate the issue indexer with issue data
|
2019-02-19 14:39:39 +00:00
|
|
|
func populateIssueIndexer() {
|
2017-01-25 02:43:02 +00:00
|
|
|
for page := 1; ; page++ {
|
2017-10-10 20:37:18 +00:00
|
|
|
repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
|
2017-10-26 21:16:13 +00:00
|
|
|
Page: page,
|
2017-12-31 14:45:46 +00:00
|
|
|
PageSize: RepositoryListDefaultPageSize,
|
2017-10-26 21:16:13 +00:00
|
|
|
OrderBy: SearchOrderByID,
|
|
|
|
Private: true,
|
|
|
|
Collaborate: util.OptionalBoolFalse,
|
2017-01-25 02:43:02 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2019-02-19 14:39:39 +00:00
|
|
|
log.Error(4, "SearchRepositoryByName: %v", err)
|
|
|
|
continue
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|
|
|
|
if len(repos) == 0 {
|
2019-02-19 14:39:39 +00:00
|
|
|
return
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|
2019-02-19 14:39:39 +00:00
|
|
|
|
2017-01-25 02:43:02 +00:00
|
|
|
for _, repo := range repos {
|
2019-02-19 14:39:39 +00:00
|
|
|
is, err := Issues(&IssuesOptions{
|
2017-12-25 23:25:16 +00:00
|
|
|
RepoIDs: []int64{repo.ID},
|
2017-01-25 02:43:02 +00:00
|
|
|
IsClosed: util.OptionalBoolNone,
|
|
|
|
IsPull: util.OptionalBoolNone,
|
|
|
|
})
|
2017-09-25 00:08:48 +00:00
|
|
|
if err != nil {
|
2019-02-19 14:39:39 +00:00
|
|
|
log.Error(4, "Issues: %v", err)
|
|
|
|
continue
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|
2019-02-19 14:39:39 +00:00
|
|
|
if err = IssueList(is).LoadDiscussComments(); err != nil {
|
|
|
|
log.Error(4, "LoadComments: %v", err)
|
|
|
|
continue
|
2017-11-21 05:28:22 +00:00
|
|
|
}
|
2019-02-19 14:39:39 +00:00
|
|
|
for _, issue := range is {
|
|
|
|
UpdateIssueIndexer(issue)
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:39:39 +00:00
|
|
|
// UpdateIssueIndexer add/update an issue to the issue indexer
|
|
|
|
func UpdateIssueIndexer(issue *Issue) {
|
|
|
|
var comments []string
|
2017-09-16 20:16:21 +00:00
|
|
|
for _, comment := range issue.Comments {
|
|
|
|
if comment.Type == CommentTypeComment {
|
|
|
|
comments = append(comments, comment.Content)
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 14:39:39 +00:00
|
|
|
issueIndexerUpdateQueue.Push(&issues.IndexerData{
|
|
|
|
ID: issue.ID,
|
|
|
|
RepoID: issue.RepoID,
|
|
|
|
Title: issue.Title,
|
|
|
|
Content: issue.Content,
|
|
|
|
Comments: comments,
|
|
|
|
})
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:39:39 +00:00
|
|
|
// DeleteRepoIssueIndexer deletes repo's all issues indexes
|
|
|
|
func DeleteRepoIssueIndexer(repo *Repository) {
|
|
|
|
var ids []int64
|
|
|
|
ids, err := getIssueIDsByRepoID(x, repo.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(4, "getIssueIDsByRepoID failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ids) <= 0 {
|
|
|
|
return
|
2017-12-17 11:53:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:39:39 +00:00
|
|
|
issueIndexerUpdateQueue.Push(&issues.IndexerData{
|
|
|
|
IDs: ids,
|
|
|
|
IsDelete: true,
|
|
|
|
})
|
2017-12-17 11:53:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 14:39:39 +00:00
|
|
|
// SearchIssuesByKeyword search issue ids by keywords and repo id
|
|
|
|
func SearchIssuesByKeyword(repoID int64, keyword string) ([]int64, error) {
|
|
|
|
var issueIDs []int64
|
|
|
|
res, err := issueIndexer.Search(keyword, repoID, 1000, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, r := range res.Hits {
|
|
|
|
issueIDs = append(issueIDs, r.ID)
|
2017-09-16 20:16:21 +00:00
|
|
|
}
|
2019-02-19 14:39:39 +00:00
|
|
|
return issueIDs, nil
|
2017-01-25 02:43:02 +00:00
|
|
|
}
|