Skip to content

Commit 4a8bbde

Browse files
committed
feat: use RepoIndexerTypeHook
1 parent ae44e3f commit 4a8bbde

File tree

6 files changed

+85
-0
lines changed

6 files changed

+85
-0
lines changed

models/repo/repo_indexer.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const (
2020
RepoIndexerTypeCode RepoIndexerType = iota // 0
2121
// RepoIndexerTypeStats repository stats indexer
2222
RepoIndexerTypeStats // 1
23+
// RepoIndexerTypeHook doesn't index anything, it's a health check for git hooks.
24+
RepoIndexerTypeHook // 2
2325
)
2426

2527
// RepoIndexerStatus status of a repo's entry in the repo indexer
@@ -73,6 +75,8 @@ func GetIndexerStatus(ctx context.Context, repo *Repository, indexerType RepoInd
7375
if repo.StatsIndexerStatus != nil {
7476
return repo.StatsIndexerStatus, nil
7577
}
78+
default:
79+
// Do nothing since other types do not need to be patched to `repo`.
7680
}
7781
status := &RepoIndexerStatus{RepoID: repo.ID}
7882
if has, err := db.GetEngine(ctx).Where("`indexer_type` = ?", indexerType).Get(status); err != nil {
@@ -86,6 +90,8 @@ func GetIndexerStatus(ctx context.Context, repo *Repository, indexerType RepoInd
8690
repo.CodeIndexerStatus = status
8791
case RepoIndexerTypeStats:
8892
repo.StatsIndexerStatus = status
93+
default:
94+
// Do nothing since other types do not need to be patched to `repo`.
8995
}
9096
return status, nil
9197
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2591,6 +2591,7 @@ find_file.no_matching = No matching file found
25912591
error.csv.too_large = Can't render this file because it is too large.
25922592
error.csv.unexpected = Can't render this file because it contains an unexpected character in line %d and column %d.
25932593
error.csv.invalid_field_count = Can't render this file because it has a wrong number of fields in line %d.
2594+
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.
25942595
25952596
[graphs]
25962597
component_loading = Loading %s...

routers/private/default_branch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,20 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
3535
})
3636
return
3737
}
38+
39+
commitID, err := ctx.Repo.GitRepo.GetBranchCommitID(ctx.Repo.Repository.DefaultBranch)
40+
if err != nil {
41+
ctx.JSON(http.StatusInternalServerError, private.Response{
42+
Err: fmt.Sprintf("Unable to get commit ID for new default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
43+
})
44+
return
45+
}
46+
if err := repo_model.UpdateIndexerStatus(ctx, ctx.Repo.Repository, repo_model.RepoIndexerTypeHook, commitID); err != nil {
47+
ctx.JSON(http.StatusInternalServerError, private.Response{
48+
Err: fmt.Sprintf("Unable to update hook status for new default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
49+
})
50+
return
51+
}
52+
3853
ctx.PlainText(http.StatusOK, "success")
3954
}

routers/private/hook_post_receive.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,33 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
8989
}
9090
}
9191

92+
// Update the hook status
93+
hookStatus :=
94+
for i, update := range updates {
95+
if !update.RefFullName.IsBranch() {
96+
continue
97+
}
98+
if repo == nil {
99+
repo = loadRepository(ctx, ownerName, repoName)
100+
if ctx.Written() {
101+
return
102+
}
103+
}
104+
if repo.IsEmpty {
105+
106+
}
107+
if ref.BranchName() != repo.DefaultBranch {
108+
continue
109+
}
110+
if err := repo_model.UpdateIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeHook, opts.NewCommitIDs[i]); err != nil {
111+
log.Error("Failed to update hook status: %s/%s Error: %v", ownerName, repoName, err)
112+
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
113+
Err: fmt.Sprintf("Failed to update hook status: %s/%s Error: %v", ownerName, repoName, err),
114+
})
115+
return
116+
}
117+
}
118+
92119
// Handle Push Options
93120
if len(opts.GitPushOptions) > 0 {
94121
// load the repository

routers/web/repo/view.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,30 @@ func prepareOpenWithEditorApps(ctx *context.Context) {
940940
ctx.Data["OpenWithEditorApps"] = tmplApps
941941
}
942942

943+
func checkGitHookStatus(ctx *context.Context) {
944+
if ctx.Repo.Repository.IsEmpty {
945+
return
946+
}
947+
status, err := repo_model.GetIndexerStatus(ctx, ctx.Repo.Repository, repo_model.RepoIndexerTypeHook)
948+
if err != nil {
949+
log.Error("GetIndexerStatus: %v", err)
950+
// Don't return an error page, as it can be rechecked the next time the user opens the page.
951+
return
952+
}
953+
954+
// get the head commit of the branch since ctx.Repo.CommitID is not always the head commit of the default branch
955+
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
956+
if err != nil {
957+
log.Error("GetBranchCommitID: %v", err)
958+
// Don't return an error page, as it can be rechecked the next time the user opens the page.
959+
return
960+
}
961+
962+
if status.CommitSha != commit.ID.String() {
963+
ctx.Flash.Warning(ctx.Tr("repo.error.broken_git_hook"), true)
964+
}
965+
}
966+
943967
func renderHomeCode(ctx *context.Context) {
944968
ctx.Data["PageIsViewCode"] = true
945969
ctx.Data["RepositoryUploadEnabled"] = setting.Repository.Upload.Enabled
@@ -1016,6 +1040,8 @@ func renderHomeCode(ctx *context.Context) {
10161040
return
10171041
}
10181042

1043+
checkGitHookStatus(ctx)
1044+
10191045
if entry.IsDir() {
10201046
renderDirectory(ctx)
10211047
} else {

services/repository/branch.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,22 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
473473
}
474474
}
475475

476+
commitID, err := gitRepo.GetBranchCommitID(newBranchName)
477+
if err != nil {
478+
return err
479+
}
480+
476481
oldDefaultBranchName := repo.DefaultBranch
477482
repo.DefaultBranch = newBranchName
478483
if err := db.WithTx(ctx, func(ctx context.Context) error {
479484
if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil {
480485
return err
481486
}
482487

488+
if err := repo_model.UpdateIndexerStatus(ctx, repo, repo_model.RepoIndexerTypeHook, commitID); err != nil {
489+
return err
490+
}
491+
483492
if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
484493
log.Error("DeleteCronTaskByRepo: %v", err)
485494
}
@@ -499,6 +508,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
499508
return err
500509
}
501510
}
511+
502512
return nil
503513
}); err != nil {
504514
return err

0 commit comments

Comments
 (0)