Performance improvement for add team user when org has more than 1000 repositories (#19227) (#19289)

This commit is contained in:
Lunny Xiao 2022-04-01 16:36:12 +08:00 committed by GitHub
parent 123c254b84
commit 471a1e8111
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 12 deletions

View File

@ -940,11 +940,6 @@ func AddTeamMember(team *Team, userID int64) error {
return err return err
} }
// Get team and its repositories.
if err := team.GetRepositories(&SearchOrgTeamOptions{}); err != nil {
return err
}
ctx, committer, err := db.TxContext() ctx, committer, err := db.TxContext()
if err != nil { if err != nil {
return err return err
@ -966,17 +961,51 @@ func AddTeamMember(team *Team, userID int64) error {
team.NumMembers++ team.NumMembers++
// Give access to team repositories. // Give access to team repositories.
for _, repo := range team.Repos { // update exist access if mode become bigger
if err := recalculateUserAccess(ctx, repo, userID); err != nil { subQuery := builder.Select("repo_id").From("team_repo").
return err Where(builder.Eq{"team_id": team.ID})
}
if setting.Service.AutoWatchNewRepos { if _, err := sess.Where("user_id=?", userID).
if err = repo_model.WatchRepoCtx(ctx, userID, repo.ID, true); err != nil { In("repo_id", subQuery).
return err And("mode < ?", team.AccessMode).
SetExpr("mode", team.AccessMode).
Update(new(Access)); err != nil {
return fmt.Errorf("update user accesses: %v", err)
}
// for not exist access
var repoIDs []int64
accessSubQuery := builder.Select("repo_id").From("access").Where(builder.Eq{"user_id": userID})
if err := sess.SQL(subQuery.And(builder.NotIn("repo_id", accessSubQuery))).Find(&repoIDs); err != nil {
return fmt.Errorf("select id accesses: %v", err)
}
accesses := make([]*Access, 0, 100)
for i, repoID := range repoIDs {
accesses = append(accesses, &Access{RepoID: repoID, UserID: userID, Mode: team.AccessMode})
if (i%100 == 0 || i == len(repoIDs)-1) && len(accesses) > 0 {
if err = db.Insert(ctx, accesses); err != nil {
return fmt.Errorf("insert new user accesses: %v", err)
} }
accesses = accesses[:0]
} }
} }
// watch could be failed, so run it in a goroutine
if setting.Service.AutoWatchNewRepos {
// Get team and its repositories.
if err := team.GetRepositories(&SearchOrgTeamOptions{}); err != nil {
log.Error("getRepositories failed: %v", err)
}
go func(repos []*repo_model.Repository) {
for _, repo := range repos {
if err = repo_model.WatchRepoCtx(db.DefaultContext, userID, repo.ID, true); err != nil {
log.Error("watch repo failed: %v", err)
}
}
}(team.Repos)
}
return committer.Commit() return committer.Commit()
} }