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

Sync branches to DB immediately when handle git hook calling #29493

Merged
merged 12 commits into from
Mar 6, 2024
Prev Previous commit
Next Next commit
fix: check if never synced
  • Loading branch information
wolfogre committed Mar 4, 2024
commit d1bb3e0084be3d26cca632f389d7831eba5f1600
61 changes: 29 additions & 32 deletions services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,10 @@ func checkBranchName(ctx context.Context, repo *repo_model.Repository, name stri
return err
}

// SyncBranchesToDB sync the branch information in the database. It will try to update the branch first,
// if updated success with affect records > 0, then all are done. Because that means the branch has been in the database.
// If no record is affected, that means the branch does not exist in database. So there are two possibilities.
// One is this is a new branch, then we just need to insert the record. Another is the branches haven't been synced,
// then we need to sync all the branches into database.
// SyncBranchesToDB sync the branch information in the database.
// It will check whether the branches of the repository have never been synced before.
// If so, it will sync all branches of the repository.
// Otherwise, it will sync the branches that need to be updated.
func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames, commitIDs []string, getCommit func(commitID string) (*git.Commit, error)) error {
// Some designs that make the code look strange but are made for performance optimization purposes:
// 1. Sync branches in a batch to reduce the number of DB queries.
Expand All @@ -248,18 +247,36 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
if err != nil {
return fmt.Errorf("git_model.GetBranches: %v", err)
}

if len(branches) == 0 {
// if user haven't visit UI but directly push to a branch after upgrading from 1.20 -> 1.21,
// we cannot simply insert the branch but need to check we have branches or not
hasBranch, err := db.Exist[git_model.Branch](ctx, git_model.FindBranchOptions{
RepoID: repoID,
IsDeletedBranch: optional.Some(false),
}.ToConds())
if err != nil {
return err
}
if !hasBranch {
if _, err = repo_module.SyncRepoBranches(ctx, repoID, pusherID); err != nil {
return fmt.Errorf("repo_module.SyncRepoBranches %d failed: %v", repoID, err)
}
return nil
}
}

branchMap := make(map[string]*git_model.Branch, len(branches))
for _, branch := range branches {
branchMap[branch.Name] = branch
}

hasBranch := len(branches) > 0

newBranches := make([]*git_model.Branch, 0, len(branchNames))

for i, branchName := range branchNames {
commitID := commitIDs[i]
if branch, ok := branchMap[branchName]; ok && branch.CommitID == commitID {
branch, exist := branchMap[branchName]
if exist && branch.CommitID == commitID {
continue
}

Expand All @@ -268,31 +285,11 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
return fmt.Errorf("get commit of %s failed: %v", branchName, err)
}

cnt, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit)
if err != nil {
return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
}
if cnt > 0 { // This means branch does exist, so it's a normal update. It also means the branch has been synced.
hasBranch = true
continue
}

if !hasBranch {
// if user haven't visit UI but directly push to a branch after upgrading from 1.20 -> 1.21,
// we cannot simply insert the branch but need to check we have branches or not
hasBranch, err = db.Exist[git_model.Branch](ctx, git_model.FindBranchOptions{
RepoID: repoID,
IsDeletedBranch: optional.Some(false),
}.ToConds())
if err != nil {
return err
}
if !hasBranch {
if _, err = repo_module.SyncRepoBranches(ctx, repoID, pusherID); err != nil {
return fmt.Errorf("repo_module.SyncRepoBranches %d:%s failed: %v", repoID, branchName, err)
}
return nil
if exist {
if _, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit); err != nil {
return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
}
return nil
}

// if database have branches but not this branch, it means this is a new branch
Expand Down
Loading