Skip to content

Commit 0a7d3ff

Browse files
lunnyKN4CK3Rlafriks
authored
refactor some functions to support ctx as first parameter (go-gitea#21878)
Co-authored-by: KN4CK3R <admin@oldschoolhack.me> Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent 8698458 commit 0a7d3ff

File tree

145 files changed

+360
-369
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+360
-369
lines changed

cmd/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ func runDeleteUser(c *cli.Context) error {
665665
} else if c.IsSet("username") {
666666
user, err = user_model.GetUserByName(ctx, c.String("username"))
667667
} else {
668-
user, err = user_model.GetUserByID(c.Int64("id"))
668+
user, err = user_model.GetUserByID(ctx, c.Int64("id"))
669669
}
670670
if err != nil {
671671
return err

models/activities/action.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ func (a *Action) GetOpType() ActionType {
114114
}
115115

116116
// LoadActUser loads a.ActUser
117-
func (a *Action) LoadActUser() {
117+
func (a *Action) LoadActUser(ctx context.Context) {
118118
if a.ActUser != nil {
119119
return
120120
}
121121
var err error
122-
a.ActUser, err = user_model.GetUserByID(a.ActUserID)
122+
a.ActUser, err = user_model.GetUserByID(ctx, a.ActUserID)
123123
if err == nil {
124124
return
125125
} else if user_model.IsErrUserNotExist(err) {
@@ -129,26 +129,26 @@ func (a *Action) LoadActUser() {
129129
}
130130
}
131131

132-
func (a *Action) loadRepo() {
132+
func (a *Action) loadRepo(ctx context.Context) {
133133
if a.Repo != nil {
134134
return
135135
}
136136
var err error
137-
a.Repo, err = repo_model.GetRepositoryByID(a.RepoID)
137+
a.Repo, err = repo_model.GetRepositoryByID(ctx, a.RepoID)
138138
if err != nil {
139139
log.Error("repo_model.GetRepositoryByID(%d): %v", a.RepoID, err)
140140
}
141141
}
142142

143143
// GetActFullName gets the action's user full name.
144144
func (a *Action) GetActFullName() string {
145-
a.LoadActUser()
145+
a.LoadActUser(db.DefaultContext)
146146
return a.ActUser.FullName
147147
}
148148

149149
// GetActUserName gets the action's user name.
150150
func (a *Action) GetActUserName() string {
151-
a.LoadActUser()
151+
a.LoadActUser(db.DefaultContext)
152152
return a.ActUser.Name
153153
}
154154

@@ -179,7 +179,7 @@ func (a *Action) GetDisplayNameTitle() string {
179179

180180
// GetRepoUserName returns the name of the action repository owner.
181181
func (a *Action) GetRepoUserName() string {
182-
a.loadRepo()
182+
a.loadRepo(db.DefaultContext)
183183
return a.Repo.OwnerName
184184
}
185185

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

192192
// GetRepoName returns the name of the action repository.
193193
func (a *Action) GetRepoName() string {
194-
a.loadRepo()
194+
a.loadRepo(db.DefaultContext)
195195
return a.Repo.Name
196196
}
197197

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

381381
if opts.RequestedTeam != nil && opts.RequestedUser == nil {
382-
org, err := user_model.GetUserByID(opts.RequestedTeam.OrgID)
382+
org, err := user_model.GetUserByID(db.DefaultContext, opts.RequestedTeam.OrgID)
383383
if err != nil {
384384
return nil, err
385385
}
@@ -489,7 +489,7 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
489489
}
490490

491491
if repoChanged {
492-
act.loadRepo()
492+
act.loadRepo(ctx)
493493
repo = act.Repo
494494

495495
// check repo owner exist.
@@ -514,7 +514,7 @@ func NotifyWatchers(ctx context.Context, actions ...*Action) error {
514514
permIssue = make([]bool, len(watchers))
515515
permPR = make([]bool, len(watchers))
516516
for i, watcher := range watchers {
517-
user, err := user_model.GetUserByIDCtx(ctx, watcher.UserID)
517+
user, err := user_model.GetUserByID(ctx, watcher.UserID)
518518
if err != nil {
519519
permCode[i] = false
520520
permIssue[i] = false

models/activities/action_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (actions ActionList) loadRepoOwner(ctx context.Context, userMap map[int64]*
8181
}
8282
repoOwner, ok := userMap[action.Repo.OwnerID]
8383
if !ok {
84-
repoOwner, err = user_model.GetUserByIDCtx(ctx, action.Repo.OwnerID)
84+
repoOwner, err = user_model.GetUserByID(ctx, action.Repo.OwnerID)
8585
if err != nil {
8686
if user_model.IsErrUserNotExist(err) {
8787
continue

models/activities/notification.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func createOrUpdateIssueNotifications(ctx context.Context, issueID, commentID, n
245245
// notify
246246
for userID := range toNotify {
247247
issue.Repo.Units = nil
248-
user, err := user_model.GetUserByIDCtx(ctx, userID)
248+
user, err := user_model.GetUserByID(ctx, userID)
249249
if err != nil {
250250
if user_model.IsErrUserNotExist(err) {
251251
continue
@@ -388,7 +388,7 @@ func (n *Notification) LoadAttributes(ctx context.Context) (err error) {
388388

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

426426
func (n *Notification) loadUser(ctx context.Context) (err error) {
427427
if n.User == nil {
428-
n.User, err = user_model.GetUserByIDCtx(ctx, n.UserID)
428+
n.User, err = user_model.GetUserByID(ctx, n.UserID)
429429
if err != nil {
430430
return fmt.Errorf("getUserByID [%d]: %w", n.UserID, err)
431431
}

models/asymkey/gpg_key_commit_verification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func hashAndVerifyForKeyID(sig *packet.Signature, payload string, committer *use
426426
Email: email,
427427
}
428428
if key.OwnerID != 0 {
429-
owner, err := user_model.GetUserByID(key.OwnerID)
429+
owner, err := user_model.GetUserByID(db.DefaultContext, key.OwnerID)
430430
if err == nil {
431431
signer = owner
432432
} else if !user_model.IsErrUserNotExist(err) {

models/git/branches.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
7373
}
7474

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

129129
// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
130-
func IsUserOfficialReviewer(protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
131-
return IsUserOfficialReviewerCtx(db.DefaultContext, protectBranch, user)
132-
}
133-
134-
// IsUserOfficialReviewerCtx check if user is official reviewer for the branch (counts towards required approvals)
135-
func IsUserOfficialReviewerCtx(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
136-
repo, err := repo_model.GetRepositoryByIDCtx(ctx, protectBranch.RepoID)
130+
func IsUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
131+
repo, err := repo_model.GetRepositoryByID(ctx, protectBranch.RepoID)
137132
if err != nil {
138133
return false, err
139134
}
@@ -375,7 +370,7 @@ func updateUserWhitelist(ctx context.Context, repo *repo_model.Repository, curre
375370

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

495490
// LoadUser loads the user that deleted the branch
496491
// When there's no user found it returns a user_model.NewGhostUser
497-
func (deletedBranch *DeletedBranch) LoadUser() {
498-
user, err := user_model.GetUserByID(deletedBranch.DeletedByID)
492+
func (deletedBranch *DeletedBranch) LoadUser(ctx context.Context) {
493+
user, err := user_model.GetUserByID(ctx, deletedBranch.DeletedByID)
499494
if err != nil {
500495
user = user_model.NewGhostUser()
501496
}

models/git/branches_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func TestDeletedBranchLoadUser(t *testing.T) {
4848

4949
branch := getDeletedBranch(t, firstBranch)
5050
assert.Nil(t, branch.DeletedBy)
51-
branch.LoadUser()
51+
branch.LoadUser(db.DefaultContext)
5252
assert.NotNil(t, branch.DeletedBy)
5353
assert.Equal(t, "user1", branch.DeletedBy.Name)
5454

5555
branch = getDeletedBranch(t, secondBranch)
5656
assert.Nil(t, branch.DeletedBy)
57-
branch.LoadUser()
57+
branch.LoadUser(db.DefaultContext)
5858
assert.NotNil(t, branch.DeletedBy)
5959
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
6060
}

models/git/commit_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (in
114114

115115
func (status *CommitStatus) loadAttributes(ctx context.Context) (err error) {
116116
if status.Repo == nil {
117-
status.Repo, err = repo_model.GetRepositoryByIDCtx(ctx, status.RepoID)
117+
status.Repo, err = repo_model.GetRepositoryByID(ctx, status.RepoID)
118118
if err != nil {
119119
return fmt.Errorf("getRepositoryByID [%d]: %w", status.RepoID, err)
120120
}
121121
}
122122
if status.Creator == nil && status.CreatorID > 0 {
123-
status.Creator, err = user_model.GetUserByIDCtx(ctx, status.CreatorID)
123+
status.Creator, err = user_model.GetUserByID(ctx, status.CreatorID)
124124
if err != nil {
125125
return fmt.Errorf("getUserByID [%d]: %w", status.CreatorID, err)
126126
}

models/git/lfs_lock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func CheckLFSAccessForRepo(ctx context.Context, ownerID int64, repo *repo_model.
167167
if ownerID == 0 {
168168
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
169169
}
170-
u, err := user_model.GetUserByIDCtx(ctx, ownerID)
170+
u, err := user_model.GetUserByID(ctx, ownerID)
171171
if err != nil {
172172
return err
173173
}

models/issues/assignees.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.Use
118118
// toggles user assignee state in database
119119
func toggleUserAssignee(ctx context.Context, issue *Issue, assigneeID int64) (removed bool, err error) {
120120
// Check if the user exists
121-
assignee, err := user_model.GetUserByIDCtx(ctx, assigneeID)
121+
assignee, err := user_model.GetUserByID(ctx, assigneeID)
122122
if err != nil {
123123
return false, err
124124
}

0 commit comments

Comments
 (0)