Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebuild-indexes cmd #7753

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
88 changes: 88 additions & 0 deletions cmd/rebuild_indexes.go
@@ -0,0 +1,88 @@
// Copyright 2018 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 cmd

import (
"errors"
"fmt"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/setting"

"github.com/urfave/cli"
)

// CmdRebuildIndexes represents the available rebuild-indexes sub-command
var CmdRebuildIndexes = cli.Command{
Name: "rebuild-indexes",
Usage: "Rebuild text indexes",
Description: "This command rebuilds text indexes for issues and repositories",
Action: runRebuildIndexes,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "repositories",
Usage: "Rebuild text indexes for repository content",
},
cli.BoolFlag{
Name: "issues",
Usage: "Rebuild text indexes for issues",
},
cli.BoolFlag{
Name: "all",
Usage: "Rebuild all text indexes",
},
},
}

func runRebuildIndexes(ctx *cli.Context) error {
setting.NewContext()
setting.NewServices()

var rebuildIssues bool
var rebuildRepositories bool

if ctx.IsSet("repositories") || ctx.IsSet("all") {
rebuildRepositories = true
log.Info("Rebuild text indexes for repository content")
}

if ctx.IsSet("issues") || ctx.IsSet("all") {
rebuildIssues = true
log.Info("Rebuild text indexes for issues")
}

if !rebuildIssues && !rebuildRepositories {
return errors.New("At least one of --repositories, --issues or --all must be used")
}

if !rebuildIssues && !setting.Indexer.RepoIndexerEnabled {
return errors.New("Repository level text indexes are not enabled")
}

if err := initDB(); err != nil {
return err
}

repos, err := models.GetAllRepositories()
if err != nil {
return err
}

for _, r := range repos {
fmt.Printf("Rebuilding text indexes for %s\n", r.FullName())
if rebuildRepositories {
if err = private.RebuildRepoIndex(r.ID); err != nil {
fmt.Printf("Internal error: %v\n", err)
return err
}
}
}

log.Info("Rebuild text indexes done")
fmt.Println("Done.")
return nil
}
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -68,6 +68,7 @@ arguments - which can alternatively be run by running the subcommand web.`
cmd.CmdMigrate,
cmd.CmdKeys,
cmd.CmdConvert,
cmd.CmdRebuildIndexes,
}
// Now adjust these commands to add our global configuration options

Expand Down
11 changes: 11 additions & 0 deletions models/repo.go
Expand Up @@ -2703,3 +2703,14 @@ func (repo *Repository) GetOriginalURLHostname() string {

return u.Host
}

func getAllRepositories(e Engine) ([]*Repository, error) {
repos := make([]*Repository, 0, 10)
return repos, e.
Find(&repos)
}

// GetAllRepositories returns all repositories, for administrative operations.
func GetAllRepositories() ([]*Repository, error) {
return getAllRepositories(x)
}
15 changes: 15 additions & 0 deletions models/repo_indexer.go
Expand Up @@ -360,3 +360,18 @@ func addOperationToQueue(op repoIndexerOperation) {
}()
}
}

// RebuildRepoIndex deletes and rebuilds text indexes for a repo
func RebuildRepoIndex(repoID int64) error {
repo := &Repository{ID: repoID}
has, err := x.Get(repo)
if err != nil {
return err
}
if !has {
return nil
}
DeleteRepoFromIndexer(repo)
UpdateRepoIndexer(repo)
return nil
}
29 changes: 29 additions & 0 deletions modules/private/rebuild_indexes.go
@@ -0,0 +1,29 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package private

import (
"fmt"

"code.gitea.io/gitea/modules/setting"
)

// RebuildRepoIndex rebuild a repository index
func RebuildRepoIndex(repoID int64) error {
// Ask for running deliver hook and test pull request tasks.
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/maint/rebuild-index/repo/%d", repoID)
resp, err := newInternalRequest(reqURL, "POST").Response()
if err != nil {
return err
}

defer resp.Body.Close()

// All 2XX status codes are accepted and others will return an error
if resp.StatusCode/100 != 2 {
return fmt.Errorf("Failed to rebuild indexes for repository: %s", decodeJSONError(resp).Err)
}
return nil
}
1 change: 1 addition & 0 deletions routers/private/internal.go
Expand Up @@ -81,5 +81,6 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/hook/post-receive/:owner/:repo", HookPostReceive)
m.Get("/serv/none/:keyid", ServNoCommand)
m.Get("/serv/command/:keyid/:owner/:repo", ServCommand)
m.Post("/maint/rebuild-index/repo/:repoid", RebuildRepoIndex)
}, CheckInternalToken)
}
25 changes: 25 additions & 0 deletions routers/private/rebuild_indexes.go
@@ -0,0 +1,25 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

// Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
package private

import (
"code.gitea.io/gitea/models"

macaron "gopkg.in/macaron.v1"
)

// RebuildRepoIndex rebuild a repository index
func RebuildRepoIndex(ctx *macaron.Context) {
repoID := ctx.ParamsInt64(":repoid")
if err := models.RebuildRepoIndex(repoID); err != nil {
ctx.JSON(500, map[string]interface{}{
"err": err.Error(),
})
return
}

ctx.PlainText(200, []byte("success"))
}