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

refactor some functions to support ctx as first parameter #21878

Merged
merged 12 commits into from
Dec 3, 2022
2 changes: 1 addition & 1 deletion cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ func runDeleteUser(c *cli.Context) error {
} else if c.IsSet("username") {
user, err = user_model.GetUserByName(ctx, c.String("username"))
} else {
user, err = user_model.GetUserByID(c.Int64("id"))
user, err = user_model.GetUserByID(ctx, c.Int64("id"))
}
if err != nil {
return err
Expand Down
22 changes: 11 additions & 11 deletions models/activities/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ func (a *Action) GetOpType() ActionType {
}

// LoadActUser loads a.ActUser
func (a *Action) LoadActUser() {
func (a *Action) LoadActUser(ctx context.Context) {
if a.ActUser != nil {
return
}
var err error
a.ActUser, err = user_model.GetUserByID(a.ActUserID)
a.ActUser, err = user_model.GetUserByID(ctx, a.ActUserID)
if err == nil {
return
} else if user_model.IsErrUserNotExist(err) {
Expand All @@ -129,26 +129,26 @@ func (a *Action) LoadActUser() {
}
}

func (a *Action) loadRepo() {
func (a *Action) loadRepo(ctx context.Context) {
if a.Repo != nil {
return
}
var err error
a.Repo, err = repo_model.GetRepositoryByID(a.RepoID)
a.Repo, err = repo_model.GetRepositoryByID(ctx, a.RepoID)
if err != nil {
log.Error("repo_model.GetRepositoryByID(%d): %v", a.RepoID, err)
}
}

// GetActFullName gets the action's user full name.
func (a *Action) GetActFullName() string {
a.LoadActUser()
a.LoadActUser(db.DefaultContext)
return a.ActUser.FullName
}

// GetActUserName gets the action's user name.
func (a *Action) GetActUserName() string {
a.LoadActUser()
a.LoadActUser(db.DefaultContext)
return a.ActUser.Name
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func (a *Action) GetDisplayNameTitle() string {

// GetRepoUserName returns the name of the action repository owner.
func (a *Action) GetRepoUserName() string {
a.loadRepo()
a.loadRepo(db.DefaultContext)
return a.Repo.OwnerName
}

Expand All @@ -191,7 +191,7 @@ func (a *Action) ShortRepoUserName() string {

// GetRepoName returns the name of the action repository.
func (a *Action) GetRepoName() string {
a.loadRepo()
a.loadRepo(db.DefaultContext)
return a.Repo.Name
}

Expand Down Expand Up @@ -379,7 +379,7 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
cond := builder.NewCond()

if opts.RequestedTeam != nil && opts.RequestedUser == nil {
org, err := user_model.GetUserByID(opts.RequestedTeam.OrgID)
org, err := user_model.GetUserByID(db.DefaultContext, opts.RequestedTeam.OrgID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -489,7 +489,7 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
}

if repoChanged {
act.loadRepo()
act.loadRepo(ctx)
repo = act.Repo

// check repo owner exist.
Expand All @@ -514,7 +514,7 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
permIssue = make([]bool, len(watchers))
permPR = make([]bool, len(watchers))
for i, watcher := range watchers {
user, err := user_model.GetUserByIDCtx(ctx, watcher.UserID)
user, err := user_model.GetUserByID(ctx, watcher.UserID)
if err != nil {
permCode[i] = false
permIssue[i] = false
Expand Down
2 changes: 1 addition & 1 deletion models/activities/action_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*
}
repoOwner, ok := userMap[action.Repo.OwnerID]
if !ok {
repoOwner, err = user_model.GetUserByIDCtx(ctx, action.Repo.OwnerID)
repoOwner, err = user_model.GetUserByID(ctx, action.Repo.OwnerID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
continue
Expand Down
6 changes: 3 additions & 3 deletions models/activities/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
// notify
for userID := range toNotify {
issue.Repo.Units = nil
user, err := user_model.GetUserByIDCtx(ctx, userID)
user, err := user_model.GetUserByID(ctx, userID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
continue
Expand Down Expand Up @@ -388,7 +388,7 @@ func (n *Notification) LoadAttributes(ctx context.Context) (err error) {

func (n *Notification) loadRepo(ctx context.Context) (err error) {
if n.Repository == nil {
n.Repository, err = repo_model.GetRepositoryByIDCtx(ctx, n.RepoID)
n.Repository, err = repo_model.GetRepositoryByID(ctx, n.RepoID)
if err != nil {
return fmt.Errorf("getRepositoryByID [%d]: %w", n.RepoID, err)
}
Expand Down Expand Up @@ -425,7 +425,7 @@ func (n *Notification) loadComment(ctx context.Context) (err error) {

func (n *Notification) loadUser(ctx context.Context) (err error) {
if n.User == nil {
n.User, err = user_model.GetUserByIDCtx(ctx, n.UserID)
n.User, err = user_model.GetUserByID(ctx, n.UserID)
if err != nil {
return fmt.Errorf("getUserByID [%d]: %w", n.UserID, err)
}
Expand Down
2 changes: 1 addition & 1 deletion models/asymkey/gpg_key_commit_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *use
Email: email,
}
if key.OwnerID != 0 {
owner, err := user_model.GetUserByID(key.OwnerID)
owner, err := user_model.GetUserByID(db.DefaultContext, key.OwnerID)
if err == nil {
signer = owner
} else if !user_model.IsErrUserNotExist(err) {
Expand Down
19 changes: 7 additions & 12 deletions models/git/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
}

if !protectBranch.EnableWhitelist {
if user, err := user_model.GetUserByID(userID); err != nil {
if user, err := user_model.GetUserByID(db.DefaultContext, userID); err != nil {
log.Error("GetUserByID: %v", err)
return false
} else if repo, err := repo_model.GetRepositoryByID(protectBranch.RepoID); err != nil {
} else if repo, err := repo_model.GetRepositoryByID(db.DefaultContext, protectBranch.RepoID); err != nil {
log.Error("repo_model.GetRepositoryByID: %v", err)
return false
} else if writeAccess, err := access_model.HasAccessUnit(db.DefaultContext, user, repo, unit.TypeCode, perm.AccessModeWrite); err != nil {
Expand Down Expand Up @@ -127,13 +127,8 @@ func IsUserMergeWhitelisted(ctx context.Context, protectBranch *ProtectedBranch,
}

// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
func IsUserOfficialReviewer(protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
return IsUserOfficialReviewerCtx(db.DefaultContext, protectBranch, user)
}

// IsUserOfficialReviewerCtx check if user is official reviewer for the branch (counts towards required approvals)
func IsUserOfficialReviewerCtx(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
repo, err := repo_model.GetRepositoryByIDCtx(ctx, protectBranch.RepoID)
func IsUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
repo, err := repo_model.GetRepositoryByID(ctx, protectBranch.RepoID)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -375,7 +370,7 @@ func updateUserWhitelist(ctx context.Context, repo *repo_model.Repository, curre

whitelist = make([]int64, 0, len(newWhitelist))
for _, userID := range newWhitelist {
user, err := user_model.GetUserByIDCtx(ctx, userID)
user, err := user_model.GetUserByID(ctx, userID)
if err != nil {
return nil, fmt.Errorf("GetUserByID [user_id: %d, repo_id: %d]: %w", userID, repo.ID, err)
}
Expand Down Expand Up @@ -494,8 +489,8 @@ func RemoveDeletedBranchByID(repoID, id int64) (err error) {

// LoadUser loads the user that deleted the branch
// When there's no user found it returns a user_model.NewGhostUser
func (deletedBranch *DeletedBranch) LoadUser() {
user, err := user_model.GetUserByID(deletedBranch.DeletedByID)
func (deletedBranch *DeletedBranch) LoadUser(ctx context.Context) {
user, err := user_model.GetUserByID(ctx, deletedBranch.DeletedByID)
if err != nil {
user = user_model.NewGhostUser()
}
Expand Down
4 changes: 2 additions & 2 deletions models/git/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func TestDeletedBranchLoadUser(t *testing.T) {

branch := getDeletedBranch(t, firstBranch)
assert.Nil(t, branch.DeletedBy)
branch.LoadUser()
branch.LoadUser(db.DefaultContext)
assert.NotNil(t, branch.DeletedBy)
assert.Equal(t, "user1", branch.DeletedBy.Name)

branch = getDeletedBranch(t, secondBranch)
assert.Nil(t, branch.DeletedBy)
branch.LoadUser()
branch.LoadUser(db.DefaultContext)
assert.NotNil(t, branch.DeletedBy)
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions models/git/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (in

func (status *CommitStatus) loadAttributes(ctx context.Context) (err error) {
if status.Repo == nil {
status.Repo, err = repo_model.GetRepositoryByIDCtx(ctx, status.RepoID)
status.Repo, err = repo_model.GetRepositoryByID(ctx, status.RepoID)
if err != nil {
return fmt.Errorf("getRepositoryByID [%d]: %w", status.RepoID, err)
}
}
if status.Creator == nil && status.CreatorID > 0 {
status.Creator, err = user_model.GetUserByIDCtx(ctx, status.CreatorID)
status.Creator, err = user_model.GetUserByID(ctx, status.CreatorID)
if err != nil {
return fmt.Errorf("getUserByID [%d]: %w", status.CreatorID, err)
}
Expand Down
2 changes: 1 addition & 1 deletion models/git/lfs_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func CheckLFSAccessForRepo(ctx context.Context, ownerID int64, repo *repo_model.
if ownerID == 0 {
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
}
u, err := user_model.GetUserByIDCtx(ctx, ownerID)
u, err := user_model.GetUserByID(ctx, ownerID)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion models/issues/assignees.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.Use
// toggles user assignee state in database
func toggleUserAssignee(ctx context.Context, issue *Issue, assigneeID int64) (removed bool, err error) {
// Check if the user exists
assignee, err := user_model.GetUserByIDCtx(ctx, assigneeID)
assignee, err := user_model.GetUserByID(ctx, assigneeID)
if err != nil {
return false, err
}
Expand Down
6 changes: 3 additions & 3 deletions models/issues/assignees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func TestUpdateAssignee(t *testing.T) {
assert.NoError(t, err)

// Assign multiple users
user2, err := user_model.GetUserByID(2)
user2, err := user_model.GetUserByID(db.DefaultContext, 2)
assert.NoError(t, err)
_, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user2.ID)
assert.NoError(t, err)

user3, err := user_model.GetUserByID(3)
user3, err := user_model.GetUserByID(db.DefaultContext, 3)
assert.NoError(t, err)
_, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user3.ID)
assert.NoError(t, err)

user1, err := user_model.GetUserByID(1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him
user1, err := user_model.GetUserByID(db.DefaultContext, 1) // This user is already assigned (see the definition in fixtures), so running UpdateAssignee should unassign him
assert.NoError(t, err)
_, _, err = issues_model.ToggleIssueAssignee(issue, &user_model.User{ID: 1}, user1.ID)
assert.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions models/issues/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (c *Comment) LoadPoster(ctx context.Context) (err error) {
return nil
}

c.Poster, err = user_model.GetUserByIDCtx(ctx, c.PosterID)
c.Poster, err = user_model.GetUserByID(ctx, c.PosterID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
c.PosterID = -1
Expand Down Expand Up @@ -580,7 +580,7 @@ func (c *Comment) LoadAssigneeUserAndTeam() error {
var err error

if c.AssigneeID > 0 && c.Assignee == nil {
c.Assignee, err = user_model.GetUserByIDCtx(db.DefaultContext, c.AssigneeID)
c.Assignee, err = user_model.GetUserByID(db.DefaultContext, c.AssigneeID)
if err != nil {
if !user_model.IsErrUserNotExist(err) {
return err
Expand Down Expand Up @@ -615,7 +615,7 @@ func (c *Comment) LoadResolveDoer() (err error) {
if c.ResolveDoerID == 0 || c.Type != CommentTypeCode {
return nil
}
c.ResolveDoer, err = user_model.GetUserByIDCtx(db.DefaultContext, c.ResolveDoerID)
c.ResolveDoer, err = user_model.GetUserByID(db.DefaultContext, c.ResolveDoerID)
if err != nil {
if user_model.IsErrUserNotExist(err) {
c.ResolveDoer = user_model.NewGhostUser()
Expand Down
2 changes: 1 addition & 1 deletion models/issues/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestCreateIssueDependency(t *testing.T) {
// Prepare
assert.NoError(t, unittest.PrepareTestDatabase())

user1, err := user_model.GetUserByID(1)
user1, err := user_model.GetUserByID(db.DefaultContext, 1)
assert.NoError(t, err)

issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
Expand Down
4 changes: 2 additions & 2 deletions models/issues/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (issue *Issue) IsOverdue() bool {
// LoadRepo loads issue's repository
func (issue *Issue) LoadRepo(ctx context.Context) (err error) {
if issue.Repo == nil {
issue.Repo, err = repo_model.GetRepositoryByIDCtx(ctx, issue.RepoID)
issue.Repo, err = repo_model.GetRepositoryByID(ctx, issue.RepoID)
if err != nil {
return fmt.Errorf("getRepositoryByID [%d]: %w", issue.RepoID, err)
}
Expand Down Expand Up @@ -242,7 +242,7 @@ func (issue *Issue) LoadLabels(ctx context.Context) (err error) {
// LoadPoster loads poster
func (issue *Issue) LoadPoster(ctx context.Context) (err error) {
if issue.Poster == nil {
issue.Poster, err = user_model.GetUserByIDCtx(ctx, issue.PosterID)
issue.Poster, err = user_model.GetUserByID(ctx, issue.PosterID)
if err != nil {
issue.PosterID = -1
issue.Poster = user_model.NewGhostUser()
Expand Down
2 changes: 1 addition & 1 deletion models/issues/issue_xref.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (issue *Issue) getCrossReferences(stdCtx context.Context, ctx *crossReferen
refRepo = ctx.OrigIssue.Repo
} else {
// Issues in other repositories
refRepo, err = repo_model.GetRepositoryByOwnerAndNameCtx(stdCtx, ref.Owner, ref.Name)
refRepo, err = repo_model.GetRepositoryByOwnerAndName(stdCtx, ref.Owner, ref.Name)
if err != nil {
if repo_model.IsErrRepoNotExist(err) {
continue
Expand Down
2 changes: 1 addition & 1 deletion models/issues/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func DeleteMilestoneByRepoID(repoID, id int64) error {
return err
}

repo, err := repo_model.GetRepositoryByID(m.RepoID)
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, m.RepoID)
if err != nil {
return err
}
Expand Down
17 changes: 6 additions & 11 deletions models/issues/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (pr *PullRequest) MustHeadUserName(ctx context.Context) string {
// Note: don't try to get Issue because will end up recursive querying.
func (pr *PullRequest) LoadAttributes(ctx context.Context) (err error) {
if pr.HasMerged && pr.Merger == nil {
pr.Merger, err = user_model.GetUserByIDCtx(ctx, pr.MergerID)
pr.Merger, err = user_model.GetUserByID(ctx, pr.MergerID)
if user_model.IsErrUserNotExist(err) {
pr.MergerID = -1
pr.Merger = user_model.NewGhostUser()
Expand All @@ -248,9 +248,9 @@ func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) {
}
}

pr.HeadRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.HeadRepoID)
pr.HeadRepo, err = repo_model.GetRepositoryByID(ctx, pr.HeadRepoID)
if err != nil && !repo_model.IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work
return fmt.Errorf("getRepositoryByID(head): %w", err)
return fmt.Errorf("GetRepositoryByID(head): %w", err)
}
pr.isHeadRepoLoaded = true
}
Expand All @@ -273,7 +273,7 @@ func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) {
return nil
}

pr.BaseRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.BaseRepoID)
pr.BaseRepo, err = repo_model.GetRepositoryByID(ctx, pr.BaseRepoID)
if err != nil {
return fmt.Errorf("repo_model.GetRepositoryByID(base): %w", err)
}
Expand All @@ -294,18 +294,13 @@ func (pr *PullRequest) LoadIssue(ctx context.Context) (err error) {
}

// LoadProtectedBranch loads the protected branch of the base branch
func (pr *PullRequest) LoadProtectedBranch() (err error) {
return pr.LoadProtectedBranchCtx(db.DefaultContext)
}

// LoadProtectedBranchCtx loads the protected branch of the base branch
func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) {
func (pr *PullRequest) LoadProtectedBranch(ctx context.Context) (err error) {
if pr.ProtectedBranch == nil {
if pr.BaseRepo == nil {
if pr.BaseRepoID == 0 {
return nil
}
pr.BaseRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.BaseRepoID)
pr.BaseRepo, err = repo_model.GetRepositoryByID(ctx, pr.BaseRepoID)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion models/issues/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (r *Reaction) LoadUser() (*user_model.User, error) {
if r.User != nil {
return r.User, nil
}
user, err := user_model.GetUserByIDCtx(db.DefaultContext, r.UserID)
user, err := user_model.GetUserByID(db.DefaultContext, r.UserID)
if err != nil {
return nil, err
}
Expand Down
Loading