Skip to content

Commit

Permalink
feat: use RepoIndexerTypeHook
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre committed Feb 29, 2024
1 parent ae44e3f commit 4a8bbde
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
6 changes: 6 additions & 0 deletions models/repo/repo_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const (
RepoIndexerTypeCode RepoIndexerType = iota // 0
// RepoIndexerTypeStats repository stats indexer
RepoIndexerTypeStats // 1
// RepoIndexerTypeHook doesn't index anything, it's a health check for git hooks.
RepoIndexerTypeHook // 2
)

// RepoIndexerStatus status of a repo's entry in the repo indexer
Expand Down Expand Up @@ -73,6 +75,8 @@ func GetIndexerStatus(ctx context.Context, repo *Repository, indexerType RepoInd
if repo.StatsIndexerStatus != nil {
return repo.StatsIndexerStatus, nil
}
default:
// Do nothing since other types do not need to be patched to `repo`.
}
status := &RepoIndexerStatus{RepoID: repo.ID}
if has, err := db.GetEngine(ctx).Where("`indexer_type` = ?", indexerType).Get(status); err != nil {
Expand All @@ -86,6 +90,8 @@ func GetIndexerStatus(ctx context.Context, repo *Repository, indexerType RepoInd
repo.CodeIndexerStatus = status
case RepoIndexerTypeStats:
repo.StatsIndexerStatus = status
default:
// Do nothing since other types do not need to be patched to `repo`.
}
return status, nil
}
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2591,6 +2591,7 @@ find_file.no_matching = No matching file found
error.csv.too_large = Can't render this file because it is too large.
error.csv.unexpected = Can't render this file because it contains an unexpected character in line %d and column %d.
error.csv.invalid_field_count = Can't render this file because it has a wrong number of fields in line %d.
error.broken_git_hook = Git hooks of this repository seem to be broken. Please follow the <a target="_blank" rel="noreferrer" href="https://docs.gitea.com/help/faq#push-hook--webhook-arent-running">documentation</a> to fix them, then push/merge to the default branch to refresh the status.
[graphs]
component_loading = Loading %s...
Expand Down
15 changes: 15 additions & 0 deletions routers/private/default_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,20 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
})
return
}

commitID, err := ctx.Repo.GitRepo.GetBranchCommitID(ctx.Repo.Repository.DefaultBranch)
if err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to get commit ID for new default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
})
return
}
if err := repo_model.UpdateIndexerStatus(ctx, ctx.Repo.Repository, repo_model.RepoIndexerTypeHook, commitID); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to update hook status for new default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
})
return
}

ctx.PlainText(http.StatusOK, "success")
}
27 changes: 27 additions & 0 deletions routers/private/hook_post_receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
}
}

// Update the hook status
hookStatus :=
for i, update := range updates {
if !update.RefFullName.IsBranch() {
continue
}
if repo == nil {
repo = loadRepository(ctx, ownerName, repoName)
if ctx.Written() {
return
}
}
if repo.IsEmpty {

}
if ref.BranchName() != repo.DefaultBranch {
continue
}
if err := repo_model.UpdateIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeHook, opts.NewCommitIDs[i]); err != nil {
log.Error("Failed to update hook status: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
Err: fmt.Sprintf("Failed to update hook status: %s/%s Error: %v", ownerName, repoName, err),
})
return
}
}

// Handle Push Options
if len(opts.GitPushOptions) > 0 {
// load the repository
Expand Down
26 changes: 26 additions & 0 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,30 @@ func prepareOpenWithEditorApps(ctx *context.Context) {
ctx.Data["OpenWithEditorApps"] = tmplApps
}

func checkGitHookStatus(ctx *context.Context) {
if ctx.Repo.Repository.IsEmpty {
return
}
status, err := repo_model.GetIndexerStatus(ctx, ctx.Repo.Repository, repo_model.RepoIndexerTypeHook)
if err != nil {
log.Error("GetIndexerStatus: %v", err)
// Don't return an error page, as it can be rechecked the next time the user opens the page.
return
}

// get the head commit of the branch since ctx.Repo.CommitID is not always the head commit of the default branch
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
if err != nil {
log.Error("GetBranchCommitID: %v", err)
// Don't return an error page, as it can be rechecked the next time the user opens the page.
return
}

if status.CommitSha != commit.ID.String() {
ctx.Flash.Warning(ctx.Tr("repo.error.broken_git_hook"), true)
}
}

func renderHomeCode(ctx *context.Context) {
ctx.Data["PageIsViewCode"] = true
ctx.Data["RepositoryUploadEnabled"] = setting.Repository.Upload.Enabled
Expand Down Expand Up @@ -1016,6 +1040,8 @@ func renderHomeCode(ctx *context.Context) {
return
}

checkGitHookStatus(ctx)

if entry.IsDir() {
renderDirectory(ctx)
} else {
Expand Down
10 changes: 10 additions & 0 deletions services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,22 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
}
}

commitID, err := gitRepo.GetBranchCommitID(newBranchName)
if err != nil {
return err
}

oldDefaultBranchName := repo.DefaultBranch
repo.DefaultBranch = newBranchName
if err := db.WithTx(ctx, func(ctx context.Context) error {
if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil {
return err
}

if err := repo_model.UpdateIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeHook, commitID); err != nil {
return err
}

if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
log.Error("DeleteCronTaskByRepo: %v", err)
}
Expand All @@ -499,6 +508,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
return err
}
}

return nil
}); err != nil {
return err
Expand Down

0 comments on commit 4a8bbde

Please sign in to comment.