Skip to content

Commit 6bef773

Browse files
committed
feat: sync to db in hook
1 parent db545b2 commit 6bef773

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

routers/private/hook_post_receive.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
issues_model "code.gitea.io/gitea/models/issues"
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/gitrepo"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/private"
1617
repo_module "code.gitea.io/gitea/modules/repository"
@@ -27,14 +28,19 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
2728

2829
// We don't rely on RepoAssignment here because:
2930
// a) we don't need the git repo in this function
31+
// OUT OF DATE: we do need the git repo to sync the branch to the db now.
3032
// b) our update function will likely change the repository in the db so we will need to refresh it
3133
// c) we don't always need the repo
3234

3335
ownerName := ctx.Params(":owner")
3436
repoName := ctx.Params(":repo")
3537

3638
// defer getting the repository at this point - as we should only retrieve it if we're going to call update
37-
var repo *repo_model.Repository
39+
var (
40+
repo *repo_model.Repository
41+
gitRepo *git.Repository
42+
)
43+
defer gitRepo.Close() // it's safe to call Close on a nil pointer
3844

3945
updates := make([]*repo_module.PushUpdateOptions, 0, len(opts.OldCommitIDs))
4046
wasEmpty := false
@@ -87,6 +93,43 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
8793
})
8894
return
8995
}
96+
97+
for _, update := range updates {
98+
if !update.RefFullName.IsBranch() {
99+
continue
100+
}
101+
if repo == nil {
102+
repo = loadRepository(ctx, ownerName, repoName)
103+
if ctx.Written() {
104+
return
105+
}
106+
wasEmpty = repo.IsEmpty
107+
}
108+
if gitRepo == nil {
109+
var err error
110+
gitRepo, err = gitrepo.OpenRepository(ctx, repo)
111+
if err != nil {
112+
log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
113+
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
114+
Err: fmt.Sprintf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err),
115+
})
116+
return
117+
}
118+
}
119+
commit, err := gitRepo.GetCommit(update.NewCommitID)
120+
if err != nil {
121+
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
122+
Err: fmt.Sprintf("Failed to get commit %s in repository: %s/%s Error: %v", update.NewCommitID, ownerName, repoName, err),
123+
})
124+
return
125+
}
126+
if err := repo_service.SyncBranchToDB(ctx, repo.ID, update.PusherID, update.RefFullName.BranchName(), commit); err != nil {
127+
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
128+
Err: fmt.Sprintf("Failed to sync branch to DB in repository: %s/%s Error: %v", ownerName, repoName, err),
129+
})
130+
return
131+
}
132+
}
90133
}
91134

92135
// Handle Push Options

services/repository/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ func checkBranchName(ctx context.Context, repo *repo_model.Repository, name stri
222222
return err
223223
}
224224

225-
// syncBranchToDB sync the branch information in the database. It will try to update the branch first,
225+
// SyncBranchToDB sync the branch information in the database. It will try to update the branch first,
226226
// if updated success with affect records > 0, then all are done. Because that means the branch has been in the database.
227227
// If no record is affected, that means the branch does not exist in database. So there are two possibilities.
228228
// One is this is a new branch, then we just need to insert the record. Another is the branches haven't been synced,
229229
// then we need to sync all the branches into database.
230-
func syncBranchToDB(ctx context.Context, repoID, pusherID int64, branchName string, commit *git.Commit) error {
230+
func SyncBranchToDB(ctx context.Context, repoID, pusherID int64, branchName string, commit *git.Commit) error {
231231
cnt, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit)
232232
if err != nil {
233233
return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)

services/repository/push.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,6 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
259259
commits.Commits = commits.Commits[:setting.UI.FeedMaxCommitNum]
260260
}
261261

262-
if err = syncBranchToDB(ctx, repo.ID, opts.PusherID, branch, newCommit); err != nil {
263-
return fmt.Errorf("git_model.UpdateBranch %s:%s failed: %v", repo.FullName(), branch, err)
264-
}
265-
266262
notify_service.PushCommits(ctx, pusher, repo, opts, commits)
267263

268264
// Cache for big repository

0 commit comments

Comments
 (0)