diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-12-03 10:48:26 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-03 10:48:26 +0800 |
commit | 0a7d3ff786508284224ada17956a65bb759d9265 (patch) | |
tree | 4860fca95c1432ab59c6723ee2b053b1c7d6779d /services | |
parent | 8698458f48eafeab21014db544aa7160368856e1 (diff) | |
download | gitea-0a7d3ff786508284224ada17956a65bb759d9265.tar.gz gitea-0a7d3ff786508284224ada17956a65bb759d9265.zip |
refactor some functions to support ctx as first parameter (#21878)
Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'services')
30 files changed, 60 insertions, 57 deletions
diff --git a/services/agit/agit.go b/services/agit/agit.go index e21a1ea29d..b4f4438791 100644 --- a/services/agit/agit.go +++ b/services/agit/agit.go @@ -115,7 +115,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. description = opts.GitPushOptions["description"] } - pusher, err := user_model.GetUserByID(opts.UserID) + pusher, err := user_model.GetUserByID(ctx, opts.UserID) if err != nil { return nil, fmt.Errorf("Failed to get user. Error: %w", err) } @@ -198,7 +198,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. } pull_service.AddToTaskQueue(pr) - pusher, err := user_model.GetUserByID(opts.UserID) + pusher, err := user_model.GetUserByID(ctx, opts.UserID) if err != nil { return nil, fmt.Errorf("Failed to get user. Error: %w", err) } diff --git a/services/auth/basic.go b/services/auth/basic.go index 0a82fc5b72..839aaa7a4e 100644 --- a/services/auth/basic.go +++ b/services/auth/basic.go @@ -74,7 +74,7 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore if uid != 0 { log.Trace("Basic Authorization: Valid OAuthAccessToken for user[%d]", uid) - u, err := user_model.GetUserByID(uid) + u, err := user_model.GetUserByID(req.Context(), uid) if err != nil { log.Error("GetUserByID: %v", err) return nil @@ -87,7 +87,7 @@ func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore token, err := auth_model.GetAccessTokenBySHA(authToken) if err == nil { log.Trace("Basic Authorization: Valid AccessToken for user[%d]", uid) - u, err := user_model.GetUserByID(token.UID) + u, err := user_model.GetUserByID(req.Context(), token.UID) if err != nil { log.Error("GetUserByID: %v", err) return nil diff --git a/services/auth/httpsign.go b/services/auth/httpsign.go index dcdc2de83a..e73db4f248 100644 --- a/services/auth/httpsign.go +++ b/services/auth/httpsign.go @@ -72,7 +72,7 @@ func (h *HTTPSign) Verify(req *http.Request, w http.ResponseWriter, store DataSt } } - u, err := user_model.GetUserByID(publicKey.OwnerID) + u, err := user_model.GetUserByID(req.Context(), publicKey.OwnerID) if err != nil { log.Error("GetUserByID: %v", err) return nil diff --git a/services/auth/oauth2.go b/services/auth/oauth2.go index da8c526382..7aea3eadf3 100644 --- a/services/auth/oauth2.go +++ b/services/auth/oauth2.go @@ -123,7 +123,7 @@ func (o *OAuth2) Verify(req *http.Request, w http.ResponseWriter, store DataStor } log.Trace("OAuth2 Authorization: Found token for user[%d]", id) - user, err := user_model.GetUserByID(id) + user, err := user_model.GetUserByID(req.Context(), id) if err != nil { if !user_model.IsErrUserNotExist(err) { log.Error("GetUserByName: %v", err) diff --git a/services/auth/session.go b/services/auth/session.go index f35fad1a0c..ef2c35d58a 100644 --- a/services/auth/session.go +++ b/services/auth/session.go @@ -6,6 +6,7 @@ package auth import ( "net/http" + "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" ) @@ -55,7 +56,7 @@ func SessionUser(sess SessionStore) *user_model.User { } // Get user object - user, err := user_model.GetUserByID(id) + user, err := user_model.GetUserByID(db.DefaultContext, id) if err != nil { if !user_model.IsErrUserNotExist(err) { log.Error("GetUserById: %v", err) diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go index f5bf1f4491..15d94e7920 100644 --- a/services/automerge/automerge.go +++ b/services/automerge/automerge.go @@ -218,7 +218,7 @@ func handlePull(pullID int64, sha string) { } // Merge if all checks succeeded - doer, err := user_model.GetUserByIDCtx(ctx, scheduledPRM.DoerID) + doer, err := user_model.GetUserByID(ctx, scheduledPRM.DoerID) if err != nil { log.Error("GetUserByIDCtx: %v", err) return diff --git a/services/issue/assignee.go b/services/issue/assignee.go index 2e62f7cf1c..e5e1456c3f 100644 --- a/services/issue/assignee.go +++ b/services/issue/assignee.go @@ -50,7 +50,7 @@ func ToggleAssignee(issue *issues_model.Issue, doer *user_model.User, assigneeID return } - assignee, err1 := user_model.GetUserByIDCtx(db.DefaultContext, assigneeID) + assignee, err1 := user_model.GetUserByID(db.DefaultContext, assigneeID) if err1 != nil { err = err1 return diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go index 946085cb91..114ace078e 100644 --- a/services/issue/assignee_test.go +++ b/services/issue/assignee_test.go @@ -22,7 +22,7 @@ func TestDeleteNotPassedAssignee(t *testing.T) { assert.NoError(t, err) assert.EqualValues(t, 1, len(issue.Assignees)) - 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) // Check if he got removed diff --git a/services/issue/commit.go b/services/issue/commit.go index f9d59b6bd0..efbcd639a5 100644 --- a/services/issue/commit.go +++ b/services/issue/commit.go @@ -119,7 +119,7 @@ func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, comm // issue is from another repo if len(ref.Owner) > 0 && len(ref.Name) > 0 { - refRepo, err = repo_model.GetRepositoryByOwnerAndName(ref.Owner, ref.Name) + refRepo, err = repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, ref.Owner, ref.Name) if err != nil { if repo_model.IsErrRepoNotExist(err) { log.Warn("Repository referenced in commit but does not exist: %v", err) diff --git a/services/issue/issue.go b/services/issue/issue.go index 23b48c2861..5073dff819 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -160,7 +160,7 @@ func DeleteIssue(doer *user_model.User, gitRepo *git.Repository, issue *issues_m // AddAssigneeIfNotAssigned adds an assignee only if he isn't already assigned to the issue. // Also checks for access of assigned user func AddAssigneeIfNotAssigned(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (err error) { - assignee, err := user_model.GetUserByIDCtx(db.DefaultContext, assigneeID) + assignee, err := user_model.GetUserByID(db.DefaultContext, assigneeID) if err != nil { return err } diff --git a/services/issue/issue_test.go b/services/issue/issue_test.go index 44168388e6..b67d2e2e79 100644 --- a/services/issue/issue_test.go +++ b/services/issue/issue_test.go @@ -66,7 +66,7 @@ func TestIssue_DeleteIssue(t *testing.T) { } // check issue dependencies - user, err := user_model.GetUserByID(1) + user, err := user_model.GetUserByID(db.DefaultContext, 1) assert.NoError(t, err) issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1) assert.NoError(t, err) diff --git a/services/lfs/locks.go b/services/lfs/locks.go index aab4f1a44e..0dc51f063c 100644 --- a/services/lfs/locks.go +++ b/services/lfs/locks.go @@ -39,7 +39,7 @@ func handleLockListOut(ctx *context.Context, repo *repo_model.Repository, lock * return } ctx.JSON(http.StatusOK, api.LFSLockList{ - Locks: []*api.LFSLock{convert.ToLFSLock(lock)}, + Locks: []*api.LFSLock{convert.ToLFSLock(ctx, lock)}, }) } @@ -47,7 +47,7 @@ func handleLockListOut(ctx *context.Context, repo *repo_model.Repository, lock * func GetListLockHandler(ctx *context.Context) { rv := getRequestContext(ctx) - repository, err := repo_model.GetRepositoryByOwnerAndName(rv.User, rv.Repo) + repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, rv.User, rv.Repo) if err != nil { log.Debug("Could not find repository: %s/%s - %s", rv.User, rv.Repo, err) ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") @@ -117,7 +117,7 @@ func GetListLockHandler(ctx *context.Context) { lockListAPI := make([]*api.LFSLock, len(lockList)) next := "" for i, l := range lockList { - lockListAPI[i] = convert.ToLFSLock(l) + lockListAPI[i] = convert.ToLFSLock(ctx, l) } if limit > 0 && len(lockList) == limit { next = strconv.Itoa(cursor + 1) @@ -134,7 +134,7 @@ func PostLockHandler(ctx *context.Context) { repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git") authorization := ctx.Req.Header.Get("Authorization") - repository, err := repo_model.GetRepositoryByOwnerAndName(userName, repoName) + repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err) ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") @@ -174,7 +174,7 @@ func PostLockHandler(ctx *context.Context) { if err != nil { if git_model.IsErrLFSLockAlreadyExist(err) { ctx.JSON(http.StatusConflict, api.LFSLockError{ - Lock: convert.ToLFSLock(lock), + Lock: convert.ToLFSLock(ctx, lock), Message: "already created lock", }) return @@ -192,7 +192,7 @@ func PostLockHandler(ctx *context.Context) { }) return } - ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)}) + ctx.JSON(http.StatusCreated, api.LFSLockResponse{Lock: convert.ToLFSLock(ctx, lock)}) } // VerifyLockHandler list locks for verification @@ -201,7 +201,7 @@ func VerifyLockHandler(ctx *context.Context) { repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git") authorization := ctx.Req.Header.Get("Authorization") - repository, err := repo_model.GetRepositoryByOwnerAndName(userName, repoName) + repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err) ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") @@ -249,9 +249,9 @@ func VerifyLockHandler(ctx *context.Context) { lockTheirsListAPI := make([]*api.LFSLock, 0, len(lockList)) for _, l := range lockList { if l.OwnerID == ctx.Doer.ID { - lockOursListAPI = append(lockOursListAPI, convert.ToLFSLock(l)) + lockOursListAPI = append(lockOursListAPI, convert.ToLFSLock(ctx, l)) } else { - lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(l)) + lockTheirsListAPI = append(lockTheirsListAPI, convert.ToLFSLock(ctx, l)) } } ctx.JSON(http.StatusOK, api.LFSLockListVerify{ @@ -267,7 +267,7 @@ func UnLockHandler(ctx *context.Context) { repoName := strings.TrimSuffix(ctx.Params("reponame"), ".git") authorization := ctx.Req.Header.Get("Authorization") - repository, err := repo_model.GetRepositoryByOwnerAndName(userName, repoName) + repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, userName, repoName) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", userName, repoName, err) ctx.Resp.Header().Set("WWW-Authenticate", "Basic realm=gitea-lfs") @@ -315,5 +315,5 @@ func UnLockHandler(ctx *context.Context) { }) return } - ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(lock)}) + ctx.JSON(http.StatusOK, api.LFSLockResponse{Lock: convert.ToLFSLock(ctx, lock)}) } diff --git a/services/lfs/server.go b/services/lfs/server.go index 32bd843ebf..8fd2759132 100644 --- a/services/lfs/server.go +++ b/services/lfs/server.go @@ -4,6 +4,7 @@ package lfs import ( + stdCtx "context" "crypto/sha256" "encoding/base64" "encoding/hex" @@ -408,7 +409,7 @@ func getAuthenticatedMeta(ctx *context.Context, rc *requestContext, p lfs_module } func getAuthenticatedRepository(ctx *context.Context, rc *requestContext, requireWrite bool) *repo_model.Repository { - repository, err := repo_model.GetRepositoryByOwnerAndName(rc.User, rc.Repo) + repository, err := repo_model.GetRepositoryByOwnerAndName(ctx, rc.User, rc.Repo) if err != nil { log.Error("Unable to get repository: %s/%s Error: %v", rc.User, rc.Repo, err) writeStatus(ctx, http.StatusNotFound) @@ -506,7 +507,7 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho return true } - user, err := parseToken(authorization, repository, accessMode) + user, err := parseToken(ctx, authorization, repository, accessMode) if err != nil { // Most of these are Warn level - the true internal server errors are logged in parseToken already log.Warn("Authentication failure for provided token with Error: %v", err) @@ -516,7 +517,7 @@ func authenticate(ctx *context.Context, repository *repo_model.Repository, autho return true } -func handleLFSToken(tokenSHA string, target *repo_model.Repository, mode perm.AccessMode) (*user_model.User, error) { +func handleLFSToken(ctx stdCtx.Context, tokenSHA string, target *repo_model.Repository, mode perm.AccessMode) (*user_model.User, error) { if !strings.Contains(tokenSHA, ".") { return nil, nil } @@ -543,7 +544,7 @@ func handleLFSToken(tokenSHA string, target *repo_model.Repository, mode perm.Ac return nil, fmt.Errorf("invalid token claim") } - u, err := user_model.GetUserByID(claims.UserID) + u, err := user_model.GetUserByID(ctx, claims.UserID) if err != nil { log.Error("Unable to GetUserById[%d]: Error: %v", claims.UserID, err) return nil, err @@ -551,7 +552,7 @@ func handleLFSToken(tokenSHA string, target *repo_model.Repository, mode perm.Ac return u, nil } -func parseToken(authorization string, target *repo_model.Repository, mode perm.AccessMode) (*user_model.User, error) { +func parseToken(ctx stdCtx.Context, authorization string, target *repo_model.Repository, mode perm.AccessMode) (*user_model.User, error) { if authorization == "" { return nil, fmt.Errorf("no token") } @@ -565,7 +566,7 @@ func parseToken(authorization string, target *repo_model.Repository, mode perm.A case "bearer": fallthrough case "token": - return handleLFSToken(tokenSHA, target, mode) + return handleLFSToken(ctx, tokenSHA, target, mode) } return nil, fmt.Errorf("token not found") } diff --git a/services/mailer/mail_team_invite.go b/services/mailer/mail_team_invite.go index f01d6f47dd..54e82b0234 100644 --- a/services/mailer/mail_team_invite.go +++ b/services/mailer/mail_team_invite.go @@ -26,7 +26,7 @@ func MailTeamInvite(ctx context.Context, inviter *user_model.User, team *org_mod return nil } - org, err := user_model.GetUserByIDCtx(ctx, team.OrgID) + org, err := user_model.GetUserByID(ctx, team.OrgID) if err != nil { return err } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index ea9b1a7142..f3848e616c 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -109,7 +109,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate Status: repo_model.RepositoryBeingMigrated, }) } else { - r, err = repo_model.GetRepositoryByID(opts.MigrateToRepoID) + r, err = repo_model.GetRepositoryByID(g.ctx, opts.MigrateToRepoID) } if err != nil { return err diff --git a/services/pull/check.go b/services/pull/check.go index af8f2df0b6..4f720f0a0c 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -126,7 +126,7 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce // isSignedIfRequired check if merge will be signed if required func isSignedIfRequired(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) (bool, error) { - if err := pr.LoadProtectedBranchCtx(ctx); err != nil { + if err := pr.LoadProtectedBranch(ctx); err != nil { return false, err } @@ -165,7 +165,7 @@ func checkAndUpdateStatus(ctx context.Context, pr *issues_model.PullRequest) { func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Commit, error) { if pr.BaseRepo == nil { var err error - pr.BaseRepo, err = repo_model.GetRepositoryByID(pr.BaseRepoID) + pr.BaseRepo, err = repo_model.GetRepositoryByID(ctx, pr.BaseRepoID) if err != nil { return nil, fmt.Errorf("GetRepositoryByID: %w", err) } diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index 7e4db311d9..e075248a36 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -83,7 +83,7 @@ func IsCommitStatusContextSuccess(commitStatuses []*git_model.CommitStatus, requ // IsPullCommitStatusPass returns if all required status checks PASS func IsPullCommitStatusPass(ctx context.Context, pr *issues_model.PullRequest) (bool, error) { - if err := pr.LoadProtectedBranchCtx(ctx); err != nil { + if err := pr.LoadProtectedBranch(ctx); err != nil { return false, errors.Wrap(err, "GetLatestCommitStatus") } if pr.ProtectedBranch == nil || !pr.ProtectedBranch.EnableStatusCheck { @@ -137,7 +137,7 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullR return "", errors.Wrap(err, "GetLatestCommitStatus") } - if err := pr.LoadProtectedBranchCtx(ctx); err != nil { + if err := pr.LoadProtectedBranch(ctx); err != nil { return "", errors.Wrap(err, "LoadProtectedBranch") } var requiredContexts []string diff --git a/services/pull/merge.go b/services/pull/merge.go index afed98989b..9270fb8cbc 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -752,7 +752,7 @@ func IsUserAllowedToMerge(ctx context.Context, pr *issues_model.PullRequest, p a return false, nil } - err := pr.LoadProtectedBranchCtx(ctx) + err := pr.LoadProtectedBranch(ctx) if err != nil { return false, err } @@ -770,7 +770,7 @@ func CheckPullBranchProtections(ctx context.Context, pr *issues_model.PullReques return fmt.Errorf("LoadBaseRepo: %w", err) } - if err = pr.LoadProtectedBranchCtx(ctx); err != nil { + if err = pr.LoadProtectedBranch(ctx); err != nil { return fmt.Errorf("LoadProtectedBranch: %w", err) } if pr.ProtectedBranch == nil { diff --git a/services/pull/patch.go b/services/pull/patch.go index 925f310a2c..2d495a3d72 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -14,6 +14,7 @@ import ( "strings" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/container" @@ -533,7 +534,7 @@ func checkPullFilesProtection(pr *issues_model.PullRequest, gitRepo *git.Reposit return nil } - if err := pr.LoadProtectedBranch(); err != nil { + if err := pr.LoadProtectedBranch(db.DefaultContext); err != nil { return err } diff --git a/services/pull/pull.go b/services/pull/pull.go index ddb407b053..fd907491d4 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -230,7 +230,7 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer } func checkForInvalidation(ctx context.Context, requests issues_model.PullRequestList, repoID int64, doer *user_model.User, branch string) error { - repo, err := repo_model.GetRepositoryByIDCtx(ctx, repoID) + repo, err := repo_model.GetRepositoryByID(ctx, repoID) if err != nil { return fmt.Errorf("GetRepositoryByIDCtx: %w", err) } @@ -594,7 +594,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ if pr.HeadRepo == nil { var err error - pr.HeadRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.HeadRepoID) + pr.HeadRepo, err = repo_model.GetRepositoryByID(ctx, pr.HeadRepoID) if err != nil { log.Error("GetRepositoryByIdCtx[%d]: %v", pr.HeadRepoID, err) return "" diff --git a/services/pull/update.go b/services/pull/update.go index 6ff554e612..bd6a37b277 100644 --- a/services/pull/update.go +++ b/services/pull/update.go @@ -99,7 +99,7 @@ func IsUserAllowedToUpdate(ctx context.Context, pull *issues_model.PullRequest, BaseBranch: pull.HeadBranch, } - err = pr.LoadProtectedBranch() + err = pr.LoadProtectedBranch(ctx) if err != nil { return false, false, err } diff --git a/services/release/release.go b/services/release/release.go index 07e329cf43..1d599fcda1 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -289,7 +289,7 @@ func DeleteReleaseByID(ctx context.Context, id int64, doer *user_model.User, del return fmt.Errorf("GetReleaseByID: %w", err) } - repo, err := repo_model.GetRepositoryByIDCtx(ctx, rel.RepoID) + repo, err := repo_model.GetRepositoryByID(ctx, rel.RepoID) if err != nil { return fmt.Errorf("GetRepositoryByID: %w", err) } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index fb7adcfd29..828068e8ec 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -116,7 +116,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r // Re-fetch the repository from database before updating it (else it would // override changes that were done earlier with sql) - if repo, err = repo_model.GetRepositoryByIDCtx(ctx, repo.ID); err != nil { + if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil { return fmt.Errorf("getRepositoryByID: %w", err) } diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go index 94fff6ffa2..1da4425cfc 100644 --- a/services/repository/archiver/archiver.go +++ b/services/repository/archiver/archiver.go @@ -226,7 +226,7 @@ func doArchive(r *ArchiveRequest) (*repo_model.RepoArchiver, error) { rd.Close() }() done := make(chan error, 1) // Ensure that there is some capacity which will ensure that the goroutine below can always finish - repo, err := repo_model.GetRepositoryByID(archiver.RepoID) + repo, err := repo_model.GetRepositoryByID(ctx, archiver.RepoID) if err != nil { return nil, fmt.Errorf("archiver.LoadRepo failed: %w", err) } diff --git a/services/repository/files/upload.go b/services/repository/files/upload.go index 0ada3885b1..240564d401 100644 --- a/services/repository/files/upload.go +++ b/services/repository/files/upload.go @@ -69,7 +69,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use return err } if lfsLock != nil && lfsLock.OwnerID != doer.ID { - u, err := user_model.GetUserByID(lfsLock.OwnerID) + u, err := user_model.GetUserByID(ctx, lfsLock.OwnerID) if err != nil { return err } diff --git a/services/repository/fork.go b/services/repository/fork.go index cf9b3992cd..3ed0f4ffa5 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -184,7 +184,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork // ConvertForkToNormalRepository convert the provided repo from a forked repo to normal repo func ConvertForkToNormalRepository(repo *repo_model.Repository) error { err := db.WithTx(db.DefaultContext, func(ctx context.Context) error { - repo, err := repo_model.GetRepositoryByIDCtx(ctx, repo.ID) + repo, err := repo_model.GetRepositoryByID(ctx, repo.ID) if err != nil { return err } diff --git a/services/repository/push.go b/services/repository/push.go index bce01cb76a..f1eedb8e08 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -81,7 +81,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("PushUpdates: %s/%s", optsList[0].RepoUserName, optsList[0].RepoName)) defer finished() - repo, err := repo_model.GetRepositoryByOwnerAndName(optsList[0].RepoUserName, optsList[0].RepoName) + repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, optsList[0].RepoUserName, optsList[0].RepoName) if err != nil { return fmt.Errorf("GetRepositoryByOwnerAndName failed: %w", err) } @@ -109,7 +109,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { if opts.IsTag() { // If is tag reference if pusher == nil || pusher.ID != opts.PusherID { var err error - if pusher, err = user_model.GetUserByID(opts.PusherID); err != nil { + if pusher, err = user_model.GetUserByID(ctx, opts.PusherID); err != nil { return err } } @@ -149,7 +149,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } else if opts.IsBranch() { // If is branch reference if pusher == nil || pusher.ID != opts.PusherID { var err error - if pusher, err = user_model.GetUserByID(opts.PusherID); err != nil { + if pusher, err = user_model.GetUserByID(ctx, opts.PusherID); err != nil { return err } } diff --git a/services/repository/repository.go b/services/repository/repository.go index 859c32cf8f..41fae4b4cf 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -103,24 +103,24 @@ func UpdateRepository(repo *repo_model.Repository, visibilityChanged bool) (err } // LinkedRepository returns the linked repo if any -func LinkedRepository(a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) { +func LinkedRepository(ctx context.Context, a *repo_model.Attachment) (*repo_model.Repository, unit.Type, error) { if a.IssueID != 0 { - iss, err := issues_model.GetIssueByID(db.DefaultContext, a.IssueID) + iss, err := issues_model.GetIssueByID(ctx, a.IssueID) if err != nil { return nil, unit.TypeIssues, err } - repo, err := repo_model.GetRepositoryByID(iss.RepoID) + repo, err := repo_model.GetRepositoryByID(ctx, iss.RepoID) unitType := unit.TypeIssues if iss.IsPull { unitType = unit.TypePullRequests } return repo, unitType, err } else if a.ReleaseID != 0 { - rel, err := repo_model.GetReleaseByID(db.DefaultContext, a.ReleaseID) + rel, err := repo_model.GetReleaseByID(ctx, a.ReleaseID) if err != nil { return nil, unit.TypeReleases, err } - repo, err := repo_model.GetRepositoryByID(rel.RepoID) + repo, err := repo_model.GetRepositoryByID(ctx, rel.RepoID) return repo, unit.TypeReleases, err } return nil, -1, nil diff --git a/services/repository/repository_test.go b/services/repository/repository_test.go index 5f5283bddb..892a11a23e 100644 --- a/services/repository/repository_test.go +++ b/services/repository/repository_test.go @@ -31,7 +31,7 @@ func TestLinkedRepository(t *testing.T) { t.Run(tc.name, func(t *testing.T) { attach, err := repo_model.GetAttachmentByID(db.DefaultContext, tc.attachID) assert.NoError(t, err) - repo, unitType, err := LinkedRepository(attach) + repo, unitType, err := LinkedRepository(db.DefaultContext, attach) assert.NoError(t, err) if tc.expectedRepo != nil { assert.Equal(t, tc.expectedRepo.ID, repo.ID) diff --git a/services/repository/transfer.go b/services/repository/transfer.go index af28f5a4a5..9fba9c44eb 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -43,7 +43,7 @@ func TransferOwnership(doer, newOwner *user_model.User, repo *repo_model.Reposit } repoWorkingPool.CheckOut(fmt.Sprint(repo.ID)) - newRepo, err := repo_model.GetRepositoryByID(repo.ID) + newRepo, err := repo_model.GetRepositoryByID(db.DefaultContext, repo.ID) if err != nil { return err } |