Skip to content

Commit

Permalink
Add repository setting to enable/disable health checks
Browse files Browse the repository at this point in the history
New Feature:
  * Repository struct field for IsFsckEnabled (default true of course)
  * Admin Settings section on repo options page, accessible only by
    admin users

Possible Enhancements:
  * There's no way to force running health checks on all repos
    regardless of their IsFsckEnabled setting. This would be useful if
    there were an admin API or dashboard button to run fsck immediately.

Issue: #1712
Signed-off-by: Allen Wild <allenwild93@gmail.com>
  • Loading branch information
aswild committed Mar 2, 2018
1 parent 2cd3622 commit 4f2f470
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ var migrations = []Migration{
NewMigration("remove is_owner, num_teams columns from org_user", removeIsOwnerColumnFromOrgUser),
// v57 -> v58
NewMigration("add closed_unix column for issues", addIssueClosedTime),
// v58 -> v59
NewMigration("add is_fsck_enabled column for repos", addFsckEnabledToRepo),
}

// Migrate database to current version
Expand Down
22 changes: 22 additions & 0 deletions models/migrations/v58.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 migrations

import (
"fmt"

"github.com/go-xorm/xorm"
)

func addFsckEnabledToRepo(x *xorm.Engine) error {
type Repository struct {
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
}

if err := x.Sync2(new(Repository)); err != nil {
return fmt.Errorf("Sync2: %v", err)
}
return nil
}
5 changes: 4 additions & 1 deletion models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ type Repository struct {
BaseRepo *Repository `xorm:"-"`
Size int64 `xorm:"NOT NULL DEFAULT 0"`
IndexerStatus *RepoIndexerStatus `xorm:"-"`
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`

CreatedUnix util.TimeStamp `xorm:"INDEX created"`
UpdatedUnix util.TimeStamp `xorm:"INDEX updated"`
Expand Down Expand Up @@ -2167,11 +2168,12 @@ func GitFsck() {
log.Trace("Doing: GitFsck")

if err := x.
Where("id>0").BufferSize(setting.IterateBufferSize).
Where("id>0 AND is_fsck_enabled=?", true).BufferSize(setting.IterateBufferSize).
Iterate(new(Repository),
func(idx int, bean interface{}) error {
repo := bean.(*Repository)
repoPath := repo.RepoPath()
log.Trace("Running health check on repository %s", repoPath)
if err := git.Fsck(repoPath, setting.Cron.RepoHealthCheck.Timeout, setting.Cron.RepoHealthCheck.Args...); err != nil {
desc := fmt.Sprintf("Failed to health check repository (%s): %v", repoPath, err)
log.Warn(desc)
Expand All @@ -2183,6 +2185,7 @@ func GitFsck() {
}); err != nil {
log.Error(4, "GitFsck: %v", err)
}
log.Trace("Finished: GitFsck")
}

// GitGcRepos calls 'git gc' to remove unnecessary files and optimize the local repository
Expand Down
3 changes: 3 additions & 0 deletions modules/auth/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ type RepoSettingForm struct {
PullsAllowSquash bool
EnableTimetracker bool
AllowOnlyContributorsToTrackTime bool

// Admin settings
EnableHealthCheck bool
}

// Validate validates the fields
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ settings.pulls.ignore_whitespace = Ignore changes in whitespace when checking co
settings.pulls.allow_merge_commits = Allow merge commits
settings.pulls.allow_rebase_merge = Allow rebase to merge commits
settings.pulls.allow_squash_commits = Allow to squash commits before merging
settings.admin_settings = Admin Settings
settings.admin_enable_health_check = Enable health checks (git fsck) for this repo
settings.danger_zone = Danger Zone
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.
settings.convert = Convert To Regular Repository
Expand Down
18 changes: 18 additions & 0 deletions routers/repo/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings")

case "admin":
if !ctx.User.IsAdmin {
ctx.Error(403)
return
}

if repo.IsFsckEnabled != form.EnableHealthCheck {
repo.IsFsckEnabled = form.EnableHealthCheck
if err := models.UpdateRepository(repo, false); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}
log.Trace("Repository admin settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
}

ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings")

case "convert":
if !ctx.Repo.IsOwner() {
ctx.Error(404)
Expand Down
23 changes: 23 additions & 0 deletions templates/repo/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,29 @@
</form>
</div>

{{if .IsAdmin}}
<h4 class="ui top attached header">
{{.i18n.Tr "repo.settings.admin_settings"}}
</h4>
<div class="ui attached segment">
<form class="ui form" method="post">
{{.CsrfTokenHtml}}
<input type="hidden" name="action" value="admin">
<div class="field">
<div class="ui checkbox">
<input name="enable_health_check" type="checkbox" {{if .Repository.IsFsckEnabled}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.admin_enable_health_check"}}</label>
</div>
</div>

<div class="ui divider"></div>
<div class="field">
<button class="ui green button">{{$.i18n.Tr "repo.settings.update_settings"}}</button>
</div>
</form>
</div>
{{end}}

{{if .IsRepositoryOwner}}
<h4 class="ui top attached warning header">
{{.i18n.Tr "repo.settings.danger_zone"}}
Expand Down

0 comments on commit 4f2f470

Please sign in to comment.