diff options
Diffstat (limited to 'services')
34 files changed, 81 insertions, 67 deletions
diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index 6b17c017fc..2431146f97 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -310,7 +310,7 @@ Loop: return false, "", nil, &ErrWontSign{twofa} } case approved: - protectedBranch, err := models.GetProtectedBranchBy(repo.ID, pr.BaseBranch) + protectedBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, pr.BaseBranch) if err != nil { return false, "", nil, err } diff --git a/services/asymkey/ssh_key.go b/services/asymkey/ssh_key.go index 1f6b93eb24..143c807a10 100644 --- a/services/asymkey/ssh_key.go +++ b/services/asymkey/ssh_key.go @@ -42,7 +42,7 @@ func DeletePublicKey(doer *user_model.User, id int64) (err error) { committer.Close() if key.Type == asymkey_model.KeyTypePrincipal { - return asymkey_model.RewriteAllPrincipalKeys() + return asymkey_model.RewriteAllPrincipalKeys(db.DefaultContext) } return asymkey_model.RewriteAllPublicKeys() diff --git a/services/attachment/attachment_test.go b/services/attachment/attachment_test.go index ffce5943e5..889151d8f3 100644 --- a/services/attachment/attachment_test.go +++ b/services/attachment/attachment_test.go @@ -9,6 +9,7 @@ import ( "path/filepath" "testing" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -39,7 +40,7 @@ func TestUploadAttachment(t *testing.T) { }, f) assert.NoError(t, err) - attachment, err := repo_model.GetAttachmentByUUID(attach.UUID) + attachment, err := repo_model.GetAttachmentByUUID(db.DefaultContext, attach.UUID) assert.NoError(t, err) assert.EqualValues(t, user.ID, attachment.UploaderID) assert.Equal(t, int64(0), attachment.DownloadCount) diff --git a/services/auth/oauth2.go b/services/auth/oauth2.go index 42c91fac37..68638a0806 100644 --- a/services/auth/oauth2.go +++ b/services/auth/oauth2.go @@ -38,7 +38,7 @@ func CheckOAuthAccessToken(accessToken string) int64 { return 0 } var grant *auth.OAuth2Grant - if grant, err = auth.GetOAuth2GrantByID(token.GrantID); err != nil || grant == nil { + if grant, err = auth.GetOAuth2GrantByID(db.DefaultContext, token.GrantID); err != nil || grant == nil { return 0 } if token.Type != oauth2.TypeAccessToken { diff --git a/services/auth/reverseproxy.go b/services/auth/reverseproxy.go index 299d7abd34..05d6af78f1 100644 --- a/services/auth/reverseproxy.go +++ b/services/auth/reverseproxy.go @@ -63,7 +63,7 @@ func (r *ReverseProxy) Verify(req *http.Request, w http.ResponseWriter, store Da } log.Trace("ReverseProxy Authorization: Found username: %s", username) - user, err := user_model.GetUserByName(username) + user, err := user_model.GetUserByName(req.Context(), username) if err != nil { if !user_model.IsErrUserNotExist(err) || !r.isAutoRegisterAllowed() { log.Error("GetUserByName: %v", err) diff --git a/services/auth/source/ldap/source_authenticate.go b/services/auth/source/ldap/source_authenticate.go index d8d11f18e1..785cb8ed31 100644 --- a/services/auth/source/ldap/source_authenticate.go +++ b/services/auth/source/ldap/source_authenticate.go @@ -34,11 +34,11 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str isAttributeSSHPublicKeySet := len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0 // Update User admin flag if exist - if isExist, err := user_model.IsUserExist(0, sr.Username); err != nil { + if isExist, err := user_model.IsUserExist(db.DefaultContext, 0, sr.Username); err != nil { return nil, err } else if isExist { if user == nil { - user, err = user_model.GetUserByName(sr.Username) + user, err = user_model.GetUserByName(db.DefaultContext, sr.Username) if err != nil { return nil, err } diff --git a/services/auth/source/ldap/source_sync.go b/services/auth/source/ldap/source_sync.go index a245f4c6ff..eb5ee8463f 100644 --- a/services/auth/source/ldap/source_sync.go +++ b/services/auth/source/ldap/source_sync.go @@ -118,7 +118,6 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { } err = user_model.CreateUser(usr, overwriteDefault) - if err != nil { log.Error("SyncExternalUsers[%s]: Error creating user %s: %v", source.authSource.Name, su.Username, err) } @@ -161,7 +160,7 @@ func (source *Source) Sync(ctx context.Context, updateExisting bool) error { } usr.IsActive = true - err = user_model.UpdateUser(usr, emailChanged, "full_name", "email", "is_admin", "is_restricted", "is_active") + err = user_model.UpdateUser(ctx, usr, emailChanged, "full_name", "email", "is_admin", "is_restricted", "is_active") if err != nil { log.Error("SyncExternalUsers[%s]: Error updating user %s: %v", source.authSource.Name, usr.Name, err) } diff --git a/services/auth/sspi_windows.go b/services/auth/sspi_windows.go index 9bc4041a74..7c9529a76b 100644 --- a/services/auth/sspi_windows.go +++ b/services/auth/sspi_windows.go @@ -127,7 +127,7 @@ func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore, } log.Info("Authenticated as %s\n", username) - user, err := user_model.GetUserByName(username) + user, err := user_model.GetUserByName(req.Context(), username) if err != nil { if !user_model.IsErrUserNotExist(err) { log.Error("GetUserByName: %v", err) diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go index 3ce4883aa7..3c7346ab58 100644 --- a/services/automerge/automerge.go +++ b/services/automerge/automerge.go @@ -145,7 +145,7 @@ func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model. continue } - p, err := models.GetPullRequestByIndexCtx(ctx, repo.ID, prIndex) + p, err := models.GetPullRequestByIndex(ctx, repo.ID, prIndex) if err != nil { // If there is no pull request for this branch, we don't try to merge it. if models.IsErrPullRequestNotExist(err) { diff --git a/services/comments/comments.go b/services/comments/comments.go index c1b3ab73c9..b80fddf93f 100644 --- a/services/comments/comments.go +++ b/services/comments/comments.go @@ -48,7 +48,7 @@ func UpdateComment(c *models.Comment, doer *user_model.User, oldContent string) return err } if !hasContentHistory { - if err = issues.SaveIssueContentHistory(db.GetEngine(db.DefaultContext), c.PosterID, c.IssueID, c.ID, + if err = issues.SaveIssueContentHistory(db.DefaultContext, c.PosterID, c.IssueID, c.ID, c.CreatedUnix, oldContent, true); err != nil { return err } @@ -60,7 +60,7 @@ func UpdateComment(c *models.Comment, doer *user_model.User, oldContent string) } if needsContentHistory { - err := issues.SaveIssueContentHistory(db.GetEngine(db.DefaultContext), doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false) + err := issues.SaveIssueContentHistory(db.DefaultContext, doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false) if err != nil { return err } diff --git a/services/context/user.go b/services/context/user.go index c5efd43782..1c92d24d4a 100644 --- a/services/context/user.go +++ b/services/context/user.go @@ -44,7 +44,7 @@ func userAssignment(ctx *context.Context, errCb func(int, string, interface{})) ctx.ContextUser = ctx.Doer } else { var err error - ctx.ContextUser, err = user_model.GetUserByName(username) + ctx.ContextUser, err = user_model.GetUserByName(ctx, username) if err != nil { if user_model.IsErrUserNotExist(err) { if redirectUserID, err := user_model.LookupUserRedirect(username); err == nil { diff --git a/services/cron/tasks_extended.go b/services/cron/tasks_extended.go index ec7ced99e9..41bd5c4420 100644 --- a/services/cron/tasks_extended.go +++ b/services/cron/tasks_extended.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/admin" asymkey_model "code.gitea.io/gitea/models/asymkey" + "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/updatechecker" @@ -79,7 +80,7 @@ func registerRewriteAllPrincipalKeys() { RunAtStart: false, Schedule: "@every 72h", }, func(_ context.Context, _ *user_model.User, _ Config) error { - return asymkey_model.RewriteAllPrincipalKeys() + return asymkey_model.RewriteAllPrincipalKeys(db.DefaultContext) }) } diff --git a/services/issue/assignee.go b/services/issue/assignee.go index 0b6d0045fd..8cad03351c 100644 --- a/services/issue/assignee.go +++ b/services/issue/assignee.go @@ -21,9 +21,10 @@ import ( // DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array func DeleteNotPassedAssignee(issue *models.Issue, doer *user_model.User, assignees []*user_model.User) (err error) { var found bool + oriAssignes := make([]*user_model.User, len(issue.Assignees)) + _ = copy(oriAssignes, issue.Assignees) - for _, assignee := range issue.Assignees { - + for _, assignee := range oriAssignes { found = false for _, alreadyAssignee := range assignees { if assignee.ID == alreadyAssignee.ID { @@ -110,7 +111,7 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, } } - lastreview, err := models.GetReviewByIssueIDAndUserID(issue.ID, reviewer.ID) + lastreview, err := models.GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID) if err != nil && !models.IsErrReviewNotExist(err) { return err } @@ -132,7 +133,7 @@ func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, pemResult = permDoer.CanAccessAny(perm.AccessModeWrite, unit.TypePullRequests) if !pemResult { - pemResult, err = models.IsOfficialReviewer(issue, doer) + pemResult, err = models.IsOfficialReviewer(ctx, issue, doer) if err != nil { return err } @@ -201,7 +202,7 @@ func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, doerCanWrite := permission.CanAccessAny(perm.AccessModeWrite, unit.TypePullRequests) if !doerCanWrite { - official, err := models.IsOfficialReviewer(issue, doer) + official, err := models.IsOfficialReviewer(ctx, issue, doer) if err != nil { log.Error("Unable to Check if IsOfficialReviewer for %-v in %-v#%d", doer, issue.Repo, issue.Index) return err diff --git a/services/issue/assignee_test.go b/services/issue/assignee_test.go index d3d7ad74f8..ff4d7029eb 100644 --- a/services/issue/assignee_test.go +++ b/services/issue/assignee_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -20,21 +21,23 @@ func TestDeleteNotPassedAssignee(t *testing.T) { // Fake issue with assignees issue, err := models.GetIssueWithAttrsByID(1) 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 assert.NoError(t, err) // Check if he got removed - isAssigned, err := models.IsUserAssignedToIssue(issue, user1) + isAssigned, err := models.IsUserAssignedToIssue(db.DefaultContext, issue, user1) assert.NoError(t, err) assert.True(t, isAssigned) // Clean everyone err = DeleteNotPassedAssignee(issue, user1, []*user_model.User{}) assert.NoError(t, err) + assert.EqualValues(t, 0, len(issue.Assignees)) // Check they're gone - assignees, err := models.GetAssigneesByIssue(issue) - assert.NoError(t, err) - assert.Empty(t, assignees) + assert.NoError(t, issue.LoadAssignees(db.DefaultContext)) + assert.EqualValues(t, 0, len(issue.Assignees)) + assert.Empty(t, issue.Assignee) } diff --git a/services/issue/issue.go b/services/issue/issue.go index db304a46b7..78a486727a 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -100,7 +100,7 @@ func UpdateAssignees(issue *models.Issue, oneAssignee string, multipleAssignees // Loop through all assignees to add them for _, assigneeName := range multipleAssignees { - assignee, err := user_model.GetUserByName(assigneeName) + assignee, err := user_model.GetUserByName(db.DefaultContext, assigneeName) if err != nil { return err } @@ -164,7 +164,7 @@ func AddAssigneeIfNotAssigned(issue *models.Issue, doer *user_model.User, assign } // Check if the user is already assigned - isAssigned, err := models.IsUserAssignedToIssue(issue, assignee) + isAssigned, err := models.IsUserAssignedToIssue(db.DefaultContext, issue, assignee) if err != nil { return err } diff --git a/services/issue/label.go b/services/issue/label.go index 94e52482fb..289466f604 100644 --- a/services/issue/label.go +++ b/services/issue/label.go @@ -80,7 +80,7 @@ func RemoveLabel(issue *models.Issue, doer *user_model.User, label *models.Label // ReplaceLabels removes all current labels and add new labels to the issue. func ReplaceLabels(issue *models.Issue, doer *user_model.User, labels []*models.Label) error { - old, err := models.GetLabelsByIssueID(issue.ID) + old, err := models.GetLabelsByIssueID(db.DefaultContext, issue.ID) if err != nil { return err } diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index c24edf50c9..4abf7eefda 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -71,7 +71,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo unfiltered = append(unfiltered, ids...) // =========== Issue watchers =========== - ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, true) + ids, err = models.GetIssueWatchersIDs(ctx, ctx.Issue.ID, true) if err != nil { return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err) } @@ -98,7 +98,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo } // Avoid mailing explicit unwatched - ids, err = models.GetIssueWatchersIDs(ctx.Issue.ID, false) + ids, err = models.GetIssueWatchersIDs(ctx, ctx.Issue.ID, false) if err != nil { return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err) } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 34dd59d7fc..fec1fc8c7d 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -93,7 +93,7 @@ func (g *GiteaLocalUploader) MaxBatchInsertSize(tp string) int { // CreateRepo creates a repository func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.MigrateOptions) error { - owner, err := user_model.GetUserByName(g.repoOwner) + owner, err := user_model.GetUserByName(g.ctx, g.repoOwner) if err != nil { return err } @@ -826,7 +826,7 @@ func (g *GiteaLocalUploader) Finish() error { } g.repo.Status = repo_model.RepositoryReady - return repo_model.UpdateRepositoryCols(g.repo, "status") + return repo_model.UpdateRepositoryCols(g.ctx, g.repo, "status") } func (g *GiteaLocalUploader) remapUser(source user_model.ExternalUserMigrated, target user_model.ExternalUserRemappable) error { diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index f57c8e2333..bd7c6e0657 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -40,7 +40,8 @@ func TestGiteaUploadRepo(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}).(*user_model.User) var ( - downloader = NewGithubDownloaderV3(context.Background(), "https://github.com", "", "", "", "go-xorm", "builder") + ctx = context.Background() + downloader = NewGithubDownloaderV3(ctx, "https://github.com", "", "", "", "go-xorm", "builder") repoName = "builder-" + time.Now().Format("2006-01-02-15-04-05") uploader = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName) ) @@ -80,7 +81,7 @@ func TestGiteaUploadRepo(t *testing.T) { assert.NoError(t, err) assert.Empty(t, milestones) - labels, err := models.GetLabelsByRepoID(repo.ID, "", db.ListOptions{}) + labels, err := models.GetLabelsByRepoID(ctx, repo.ID, "", db.ListOptions{}) assert.NoError(t, err) assert.Len(t, labels, 12) diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index ecd031b387..c51483821b 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models" admin_model "code.gitea.io/gitea/models/admin" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" @@ -71,7 +72,7 @@ func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error } m.Repo.OriginalURL = addr - return repo_model.UpdateRepositoryCols(m.Repo, "original_url") + return repo_model.UpdateRepositoryCols(ctx, m.Repo, "original_url") } // mirrorSyncResult contains information of a updated reference. @@ -395,11 +396,12 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { log.Error("PANIC whilst SyncMirrors[repo_id: %d] Panic: %v\nStacktrace: %s", repoID, err, log.Stack(2)) }() - m, err := repo_model.GetMirrorByRepoID(repoID) + m, err := repo_model.GetMirrorByRepoID(ctx, repoID) if err != nil { log.Error("SyncMirrors [repo_id: %v]: unable to GetMirrorByRepoID: %v", repoID, err) return false } + _ = m.GetRepository() // force load repository of mirror ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Syncing Mirror %s/%s", m.Repo.OwnerName, m.Repo.Name)) defer finished() @@ -415,7 +417,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { log.Trace("SyncMirrors [repo: %-v]: Scheduling next update", m.Repo) m.ScheduleNextUpdate() - if err = repo_model.UpdateMirror(m); err != nil { + if err = repo_model.UpdateMirror(ctx, m); err != nil { log.Error("SyncMirrors [repo: %-v]: failed to UpdateMirror with next update date: %v", m.Repo, err) return false } @@ -574,7 +576,7 @@ func checkAndUpdateEmptyRepository(m *repo_model.Mirror, gitRepo *git.Repository } m.Repo.IsEmpty = false // Update the is empty and default_branch columns - if err := repo_model.UpdateRepositoryCols(m.Repo, "default_branch", "is_empty"); err != nil { + if err := repo_model.UpdateRepositoryCols(db.DefaultContext, m.Repo, "default_branch", "is_empty"); err != nil { log.Error("Failed to update default branch of repository %-v. Error: %v", m.Repo, err) desc := fmt.Sprintf("Failed to uupdate default branch of repository '%s': %v", m.Repo.RepoPath(), err) if err = admin_model.CreateRepositoryNotice(desc); err != nil { diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index 5c0c14c627..138ebb737b 100644 --- a/services/mirror/mirror_push.go +++ b/services/mirror/mirror_push.go @@ -66,6 +66,7 @@ func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr str // RemovePushMirrorRemote removes the push mirror remote. func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error { cmd := git.NewCommand(ctx, "remote", "rm", m.RemoteName) + _ = m.GetRepository() if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: m.Repo.RepoPath()}); err != nil { return err @@ -99,6 +100,8 @@ func SyncPushMirror(ctx context.Context, mirrorID int64) bool { return false } + _ = m.GetRepository() + m.LastError = "" ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Syncing PushMirror %s/%s to %s", m.Repo.OwnerName, m.Repo.Name, m.RemoteName)) diff --git a/services/org/org.go b/services/org/org.go index d7b3019e74..b24b7e34c4 100644 --- a/services/org/org.go +++ b/services/org/org.go @@ -26,7 +26,7 @@ func DeleteOrganization(org *organization.Organization) error { defer commiter.Close() // Check ownership of repository. - count, err := repo_model.GetRepositoryCount(ctx, org.ID) + count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: org.ID}) if err != nil { return fmt.Errorf("GetRepositoryCount: %v", err) } else if count > 0 { diff --git a/services/pull/check.go b/services/pull/check.go index d88dd3a550..94e7ca7161 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -99,7 +99,7 @@ func CheckPullMergable(stdCtx context.Context, doer *user_model.User, perm *acce if err := CheckPullBranchProtections(ctx, pr, false); err != nil { if models.IsErrDisallowedToMerge(err) { if force { - if isRepoAdmin, err2 := access_model.IsUserRepoAdminCtx(ctx, pr.BaseRepo, doer); err2 != nil { + if isRepoAdmin, err2 := access_model.IsUserRepoAdmin(ctx, pr.BaseRepo, doer); err2 != nil { return err2 } else if !isRepoAdmin { return err diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index ec4cc2aa07..539b3c8520 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -132,7 +132,7 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *models.PullRequest return "", errors.Wrap(err, "LoadBaseRepo") } - commitStatuses, _, err := models.GetLatestCommitStatusCtx(ctx, pr.BaseRepo.ID, sha, db.ListOptions{}) + commitStatuses, _, err := models.GetLatestCommitStatus(ctx, pr.BaseRepo.ID, sha, db.ListOptions{}) if err != nil { return "", errors.Wrap(err, "GetLatestCommitStatus") } diff --git a/services/pull/pull.go b/services/pull/pull.go index b94b6769a4..efac3f019e 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -290,7 +290,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, if err != nil { log.Error("GetDiverging: %v", err) } else { - err = pr.UpdateCommitDivergence(divergence.Ahead, divergence.Behind) + err = pr.UpdateCommitDivergence(ctx, divergence.Ahead, divergence.Behind) if err != nil { log.Error("UpdateCommitDivergence: %v", err) } @@ -336,7 +336,7 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string, log.Error("GetDiverging: %v", err) } } else { - err = pr.UpdateCommitDivergence(divergence.Ahead, divergence.Behind) + err = pr.UpdateCommitDivergence(ctx, divergence.Ahead, divergence.Behind) if err != nil { log.Error("UpdateCommitDivergence: %v", err) } @@ -793,7 +793,7 @@ func getAllCommitStatus(gitRepo *git.Repository, pr *models.PullRequest) (status return nil, nil, shaErr } - statuses, _, err = models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, db.ListOptions{}) + statuses, _, err = models.GetLatestCommitStatus(db.DefaultContext, pr.BaseRepo.ID, sha, db.ListOptions{}) lastStatus = models.CalcCommitStatus(statuses) return statuses, lastStatus, err } diff --git a/services/pull/review.go b/services/pull/review.go index 940fe4470d..eac7279f9b 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -71,13 +71,13 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git. return comment, nil } - review, err := models.GetCurrentReview(doer, issue) + review, err := models.GetCurrentReview(ctx, doer, issue) if err != nil { if !models.IsErrReviewNotExist(err) { return nil, err } - if review, err = models.CreateReview(models.CreateReviewOptions{ + if review, err = models.CreateReview(ctx, models.CreateReviewOptions{ Type: models.ReviewTypePending, Reviewer: doer, Issue: issue, @@ -135,7 +135,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo head := pr.GetGitRefName() if line > 0 { if reviewID != 0 { - first, err := models.FindComments(&models.FindCommentsOptions{ + first, err := models.FindComments(ctx, &models.FindCommentsOptions{ ReviewID: reviewID, Line: line, TreePath: treePath, @@ -152,7 +152,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo } else if err != nil && !models.IsErrCommentNotExist(err) { return nil, fmt.Errorf("Find first comment for %d line %d path %s. Error: %v", reviewID, line, treePath, err) } else { - review, err := models.GetReviewByID(reviewID) + review, err := models.GetReviewByID(ctx, reviewID) if err == nil && len(review.CommitID) > 0 { head = review.CommitID } else if err != nil && !models.IsErrReviewNotExist(err) { @@ -272,7 +272,7 @@ func SubmitReview(ctx context.Context, doer *user_model.User, gitRepo *git.Repos // DismissReview dismissing stale review by repo admin func DismissReview(ctx context.Context, reviewID int64, message string, doer *user_model.User, isDismiss bool) (comment *models.Comment, err error) { - review, err := models.GetReviewByID(reviewID) + review, err := models.GetReviewByID(ctx, reviewID) if err != nil { return } diff --git a/services/release/release_test.go b/services/release/release_test.go index 19d985491f..823560a092 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -11,6 +11,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -248,7 +249,7 @@ func TestRelease_Update(t *testing.T) { assert.NoError(t, err) assert.NoError(t, UpdateRelease(user, gitRepo, release, []string{attach.UUID}, nil, nil)) - assert.NoError(t, models.GetReleaseAttachments(release)) + assert.NoError(t, models.GetReleaseAttachments(db.DefaultContext, release)) assert.Len(t, release.Attachments, 1) assert.EqualValues(t, attach.UUID, release.Attachments[0].UUID) assert.EqualValues(t, release.ID, release.Attachments[0].ReleaseID) @@ -259,7 +260,7 @@ func TestRelease_Update(t *testing.T) { attach.UUID: "test2.txt", })) release.Attachments = nil - assert.NoError(t, models.GetReleaseAttachments(release)) + assert.NoError(t, models.GetReleaseAttachments(db.DefaultContext, release)) assert.Len(t, release.Attachments, 1) assert.EqualValues(t, attach.UUID, release.Attachments[0].UUID) assert.EqualValues(t, release.ID, release.Attachments[0].ReleaseID) @@ -268,7 +269,7 @@ func TestRelease_Update(t *testing.T) { // delete the attachment assert.NoError(t, UpdateRelease(user, gitRepo, release, nil, []string{attach.UUID}, nil)) release.Attachments = nil - assert.NoError(t, models.GetReleaseAttachments(release)) + assert.NoError(t, models.GetReleaseAttachments(db.DefaultContext, release)) assert.Empty(t, release.Attachments) } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index b287d94f9d..1e8c22a479 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -208,7 +208,7 @@ func DeleteUnadoptedRepository(doer, u *user_model.User, repoName string) error } } - if exist, err := repo_model.IsRepositoryExist(u, repoName); err != nil { + if exist, err := repo_model.IsRepositoryExist(db.DefaultContext, u, repoName); err != nil { return err } else if exist { return repo_model.ErrRepoAlreadyExist{ @@ -238,7 +238,7 @@ func checkUnadoptedRepositories(userName string, repoNamesToCheck []string, unad if len(repoNamesToCheck) == 0 { return nil } - ctxUser, err := user_model.GetUserByName(userName) + ctxUser, err := user_model.GetUserByName(db.DefaultContext, userName) if err != nil { if user_model.IsErrUserNotExist(err) { log.Debug("Missing user: %s", userName) diff --git a/services/repository/avatar.go b/services/repository/avatar.go index f51a312e17..dcf04c7e54 100644 --- a/services/repository/avatar.go +++ b/services/repository/avatar.go @@ -44,7 +44,7 @@ func UploadAvatar(repo *repo_model.Repository, data []byte) error { // Users can upload the same image to other repo - prefix it with ID // Then repo will be removed - only it avatar file will be removed repo.Avatar = newAvatar - if err := repo_model.UpdateRepositoryColsCtx(ctx, repo, "avatar"); err != nil { + if err := repo_model.UpdateRepositoryCols(ctx, repo, "avatar"); err != nil { return fmt.Errorf("UploadAvatar: Update repository avatar: %v", err) } @@ -83,7 +83,7 @@ func DeleteAvatar(repo *repo_model.Repository) error { defer committer.Close() repo.Avatar = "" - if err := repo_model.UpdateRepositoryColsCtx(ctx, repo, "avatar"); err != nil { + if err := repo_model.UpdateRepositoryCols(ctx, repo, "avatar"); err != nil { return fmt.Errorf("DeleteAvatar: Update repository avatar: %v", err) } @@ -117,5 +117,5 @@ func generateAvatar(ctx context.Context, templateRepo, generateRepo *repo_model. return err } - return repo_model.UpdateRepositoryColsCtx(ctx, generateRepo, "avatar") + return repo_model.UpdateRepositoryCols(ctx, generateRepo, "avatar") } diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go index 240cb4fe2c..73464f31f3 100644 --- a/services/repository/files/patch.go +++ b/services/repository/files/patch.go @@ -66,7 +66,7 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode return err } } else { - protectedBranch, err := models.GetProtectedBranchBy(repo.ID, opts.OldBranch) + protectedBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, opts.OldBranch) if err != nil { return err } diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 2cb40aac47..a093ee5da7 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -462,7 +462,7 @@ func CreateOrUpdateRepoFile(ctx context.Context, repo *repo_model.Repository, do // VerifyBranchProtection verify the branch protection for modifying the given treePath on the given branch func VerifyBranchProtection(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, treePath string) error { - protectedBranch, err := models.GetProtectedBranchBy(repo.ID, branchName) + protectedBranch, err := models.GetProtectedBranchBy(ctx, repo.ID, branchName) if err != nil { return err } diff --git a/services/repository/push.go b/services/repository/push.go index 4eb52c18c2..5ca8c73983 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -181,7 +181,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } } // Update the is empty and default_branch columns - if err := repo_model.UpdateRepositoryCols(repo, "default_branch", "is_empty"); err != nil { + if err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "default_branch", "is_empty"); err != nil { return fmt.Errorf("UpdateRepositoryCols: %v", err) } } @@ -269,7 +269,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } // Even if user delete a branch on a repository which he didn't watch, he will be watch that. - if err = repo_model.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil { + if err = repo_model.WatchIfAuto(db.DefaultContext, opts.PusherID, repo.ID, true); err != nil { log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err) } } else { diff --git a/services/user/user.go b/services/user/user.go index d41fc42493..4db4d7ca17 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -44,7 +44,7 @@ func DeleteUser(u *user_model.User) error { // cannot perform delete operation. // Check ownership of repository. - count, err := repo_model.GetRepositoryCount(ctx, u.ID) + count, err := repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{OwnerID: u.ID}) if err != nil { return fmt.Errorf("GetRepositoryCount: %v", err) } else if count > 0 { @@ -78,7 +78,7 @@ func DeleteUser(u *user_model.User) error { if err = asymkey_model.RewriteAllPublicKeys(); err != nil { return err } - if err = asymkey_model.RewriteAllPrincipalKeys(); err != nil { + if err = asymkey_model.RewriteAllPrincipalKeys(db.DefaultContext); err != nil { return err } diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index b15b8173f5..68cfe147aa 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -5,10 +5,12 @@ package webhook import ( + "context" "fmt" "strconv" "strings" + "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" @@ -218,15 +220,15 @@ func prepareWebhook(w *webhook_model.Webhook, repo *repo_model.Repository, event // PrepareWebhooks adds new webhooks to task queue for given payload. func PrepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventType, p api.Payloader) error { - if err := prepareWebhooks(repo, event, p); err != nil { + if err := prepareWebhooks(db.DefaultContext, repo, event, p); err != nil { return err } return addToTask(repo.ID) } -func prepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventType, p api.Payloader) error { - ws, err := webhook_model.ListWebhooksByOpts(&webhook_model.ListWebhookOptions{ +func prepareWebhooks(ctx context.Context, repo *repo_model.Repository, event webhook_model.HookEventType, p api.Payloader) error { + ws, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{ RepoID: repo.ID, IsActive: util.OptionalBoolTrue, }) @@ -237,7 +239,7 @@ func prepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventT // check if repo belongs to org and append additional webhooks if repo.MustOwner().IsOrganization() { // get hooks for org - orgHooks, err := webhook_model.ListWebhooksByOpts(&webhook_model.ListWebhookOptions{ + orgHooks, err := webhook_model.ListWebhooksByOpts(ctx, &webhook_model.ListWebhookOptions{ OrgID: repo.OwnerID, IsActive: util.OptionalBoolTrue, }) @@ -248,7 +250,7 @@ func prepareWebhooks(repo *repo_model.Repository, event webhook_model.HookEventT } // Add any admin-defined system webhooks - systemHooks, err := webhook_model.GetSystemWebhooks(util.OptionalBoolTrue) + systemHooks, err := webhook_model.GetSystemWebhooks(ctx, util.OptionalBoolTrue) if err != nil { return fmt.Errorf("GetSystemWebhooks: %v", err) } |