aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/branches.go20
-rw-r--r--models/db/context.go11
-rw-r--r--models/issue.go39
-rw-r--r--models/issue_comment.go9
-rw-r--r--models/issue_dependency.go10
-rw-r--r--models/issue_dependency_test.go7
-rw-r--r--models/issue_test.go4
-rw-r--r--models/issue_xref.go6
-rw-r--r--models/issue_xref_test.go4
-rw-r--r--models/pull.go29
-rw-r--r--models/pull_list.go4
-rw-r--r--models/pull_test.go21
-rw-r--r--models/repo.go6
-rw-r--r--models/repo/repo.go15
-rw-r--r--models/repo_permission.go5
15 files changed, 93 insertions, 97 deletions
diff --git a/models/branches.go b/models/branches.go
index f4ded8588f..47fd3dc0a4 100644
--- a/models/branches.go
+++ b/models/branches.go
@@ -104,7 +104,7 @@ func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
}
// IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
-func IsUserMergeWhitelisted(protectBranch *ProtectedBranch, userID int64, permissionInRepo Permission) bool {
+func IsUserMergeWhitelisted(ctx context.Context, protectBranch *ProtectedBranch, userID int64, permissionInRepo Permission) bool {
if !protectBranch.EnableMergeWhitelist {
// Then we need to fall back on whether the user has write permission
return permissionInRepo.CanWrite(unit.TypeCode)
@@ -118,7 +118,7 @@ func IsUserMergeWhitelisted(protectBranch *ProtectedBranch, userID int64, permis
return false
}
- in, err := organization.IsUserInTeams(db.DefaultContext, userID, protectBranch.MergeWhitelistTeamIDs)
+ in, err := organization.IsUserInTeams(ctx, userID, protectBranch.MergeWhitelistTeamIDs)
if err != nil {
log.Error("IsUserInTeams: %v", err)
return false
@@ -159,16 +159,16 @@ func isUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch,
}
// HasEnoughApprovals returns true if pr has enough granted approvals.
-func (protectBranch *ProtectedBranch) HasEnoughApprovals(pr *PullRequest) bool {
+func (protectBranch *ProtectedBranch) HasEnoughApprovals(ctx context.Context, pr *PullRequest) bool {
if protectBranch.RequiredApprovals == 0 {
return true
}
- return protectBranch.GetGrantedApprovalsCount(pr) >= protectBranch.RequiredApprovals
+ return protectBranch.GetGrantedApprovalsCount(ctx, pr) >= protectBranch.RequiredApprovals
}
// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
-func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) int64 {
- sess := db.GetEngine(db.DefaultContext).Where("issue_id = ?", pr.IssueID).
+func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(ctx context.Context, pr *PullRequest) int64 {
+ sess := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeApprove).
And("official = ?", true).
And("dismissed = ?", false)
@@ -185,11 +185,11 @@ func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest)
}
// MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
-func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullRequest) bool {
+func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(ctx context.Context, pr *PullRequest) bool {
if !protectBranch.BlockOnRejectedReviews {
return false
}
- rejectExist, err := db.GetEngine(db.DefaultContext).Where("issue_id = ?", pr.IssueID).
+ rejectExist, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeReject).
And("official = ?", true).
And("dismissed = ?", false).
@@ -204,11 +204,11 @@ func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullReque
// MergeBlockedByOfficialReviewRequests block merge because of some review request to official reviewer
// of from official review
-func (protectBranch *ProtectedBranch) MergeBlockedByOfficialReviewRequests(pr *PullRequest) bool {
+func (protectBranch *ProtectedBranch) MergeBlockedByOfficialReviewRequests(ctx context.Context, pr *PullRequest) bool {
if !protectBranch.BlockOnOfficialReviewRequests {
return false
}
- has, err := db.GetEngine(db.DefaultContext).Where("issue_id = ?", pr.IssueID).
+ has, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeRequest).
And("official = ?", true).
Exist(new(Review))
diff --git a/models/db/context.go b/models/db/context.go
index 1cd23d453c..c823952cf6 100644
--- a/models/db/context.go
+++ b/models/db/context.go
@@ -103,7 +103,14 @@ func WithContext(f func(ctx *Context) error) error {
}
// WithTx represents executing database operations on a transaction
-func WithTx(f func(ctx context.Context) error) error {
+// you can optionally change the context to a parrent one
+func WithTx(f func(ctx context.Context) error, stdCtx ...context.Context) error {
+ parentCtx := DefaultContext
+ if len(stdCtx) != 0 && stdCtx[0] != nil {
+ // TODO: make sure parent context has no open session
+ parentCtx = stdCtx[0]
+ }
+
sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
@@ -111,7 +118,7 @@ func WithTx(f func(ctx context.Context) error) error {
}
if err := f(&Context{
- Context: DefaultContext,
+ Context: parentCtx,
e: sess,
}); err != nil {
return err
diff --git a/models/issue.go b/models/issue.go
index 2a41cbc28e..98e64adafd 100644
--- a/models/issue.go
+++ b/models/issue.go
@@ -591,7 +591,7 @@ func ReplaceIssueLabels(issue *Issue, labels []*Label, doer *user_model.User) (e
}
// ReadBy sets issue to be read by given user.
-func (issue *Issue) ReadBy(userID int64) error {
+func (issue *Issue) ReadBy(ctx context.Context, userID int64) error {
if err := UpdateIssueUserByRead(userID, issue.ID); err != nil {
return err
}
@@ -635,7 +635,7 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
// Check for open dependencies
if issue.IsClosed && issue.Repo.IsDependenciesEnabledCtx(ctx) {
// only check if dependencies are enabled and we're about to close an issue, otherwise reopening an issue would fail when there are unsatisfied dependencies
- noDeps, err := issueNoDependenciesLeft(e, issue)
+ noDeps, err := IssueNoDependenciesLeft(ctx, issue)
if err != nil {
return nil, err
}
@@ -693,13 +693,7 @@ func doChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.Use
}
// ChangeIssueStatus changes issue status to open or closed.
-func ChangeIssueStatus(issue *Issue, doer *user_model.User, isClosed bool) (*Comment, error) {
- ctx, committer, err := db.TxContext()
- if err != nil {
- return nil, err
- }
- defer committer.Close()
-
+func ChangeIssueStatus(ctx context.Context, issue *Issue, doer *user_model.User, isClosed bool) (*Comment, error) {
if err := issue.LoadRepo(ctx); err != nil {
return nil, err
}
@@ -707,16 +701,7 @@ func ChangeIssueStatus(issue *Issue, doer *user_model.User, isClosed bool) (*Com
return nil, err
}
- comment, err := changeIssueStatus(ctx, issue, doer, isClosed, false)
- if err != nil {
- return nil, err
- }
-
- if err = committer.Commit(); err != nil {
- return nil, fmt.Errorf("Commit: %v", err)
- }
-
- return comment, nil
+ return changeIssueStatus(ctx, issue, doer, isClosed, false)
}
// ChangeIssueTitle changes the title of this issue, as the given user.
@@ -787,16 +772,11 @@ func ChangeIssueRef(issue *Issue, doer *user_model.User, oldRef string) (err err
}
// AddDeletePRBranchComment adds delete branch comment for pull request issue
-func AddDeletePRBranchComment(doer *user_model.User, repo *repo_model.Repository, issueID int64, branchName string) error {
- issue, err := getIssueByID(db.GetEngine(db.DefaultContext), issueID)
- if err != nil {
- return err
- }
- ctx, committer, err := db.TxContext()
+func AddDeletePRBranchComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issueID int64, branchName string) error {
+ issue, err := getIssueByID(db.GetEngine(ctx), issueID)
if err != nil {
return err
}
- defer committer.Close()
opts := &CreateCommentOptions{
Type: CommentTypeDeleteBranch,
Doer: doer,
@@ -804,11 +784,8 @@ func AddDeletePRBranchComment(doer *user_model.User, repo *repo_model.Repository
Issue: issue,
OldRef: branchName,
}
- if _, err = CreateCommentCtx(ctx, opts); err != nil {
- return err
- }
-
- return committer.Commit()
+ _, err = CreateCommentCtx(ctx, opts)
+ return err
}
// UpdateIssueAttachments update attachments by UUIDs for the issue
diff --git a/models/issue_comment.go b/models/issue_comment.go
index 39c2818eed..ceea878662 100644
--- a/models/issue_comment.go
+++ b/models/issue_comment.go
@@ -284,14 +284,15 @@ type PushActionContent struct {
// LoadIssue loads issue from database
func (c *Comment) LoadIssue() (err error) {
- return c.loadIssue(db.GetEngine(db.DefaultContext))
+ return c.LoadIssueCtx(db.DefaultContext)
}
-func (c *Comment) loadIssue(e db.Engine) (err error) {
+// LoadIssueCtx loads issue from database
+func (c *Comment) LoadIssueCtx(ctx context.Context) (err error) {
if c.Issue != nil {
return nil
}
- c.Issue, err = getIssueByID(e, c.IssueID)
+ c.Issue, err = getIssueByID(db.GetEngine(ctx), c.IssueID)
return
}
@@ -1126,7 +1127,7 @@ func UpdateComment(c *Comment, doer *user_model.User) error {
if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
return err
}
- if err := c.loadIssue(sess); err != nil {
+ if err := c.LoadIssueCtx(ctx); err != nil {
return err
}
if err := c.addCrossReferences(ctx, doer, true); err != nil {
diff --git a/models/issue_dependency.go b/models/issue_dependency.go
index d2c5785b90..b292db57e0 100644
--- a/models/issue_dependency.go
+++ b/models/issue_dependency.go
@@ -5,6 +5,8 @@
package models
import (
+ "context"
+
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
@@ -117,12 +119,8 @@ func issueDepExists(e db.Engine, issueID, depID int64) (bool, error) {
}
// IssueNoDependenciesLeft checks if issue can be closed
-func IssueNoDependenciesLeft(issue *Issue) (bool, error) {
- return issueNoDependenciesLeft(db.GetEngine(db.DefaultContext), issue)
-}
-
-func issueNoDependenciesLeft(e db.Engine, issue *Issue) (bool, error) {
- exists, err := e.
+func IssueNoDependenciesLeft(ctx context.Context, issue *Issue) (bool, error) {
+ exists, err := db.GetEngine(ctx).
Table("issue_dependency").
Select("issue.*").
Join("INNER", "issue", "issue.id = issue_dependency.dependency_id").
diff --git a/models/issue_dependency_test.go b/models/issue_dependency_test.go
index 1789de5dc0..345a9077cd 100644
--- a/models/issue_dependency_test.go
+++ b/models/issue_dependency_test.go
@@ -7,6 +7,7 @@ package models
import (
"testing"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
@@ -43,15 +44,15 @@ func TestCreateIssueDependency(t *testing.T) {
_ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeAddDependency, PosterID: user1.ID, IssueID: issue1.ID})
// Check if dependencies left is correct
- left, err := IssueNoDependenciesLeft(issue1)
+ left, err := IssueNoDependenciesLeft(db.DefaultContext, issue1)
assert.NoError(t, err)
assert.False(t, left)
// Close #2 and check again
- _, err = ChangeIssueStatus(issue2, user1, true)
+ _, err = ChangeIssueStatus(db.DefaultContext, issue2, user1, true)
assert.NoError(t, err)
- left, err = IssueNoDependenciesLeft(issue1)
+ left, err = IssueNoDependenciesLeft(db.DefaultContext, issue1)
assert.NoError(t, err)
assert.True(t, left)
diff --git a/models/issue_test.go b/models/issue_test.go
index 5382c04f43..9b82fc80fb 100644
--- a/models/issue_test.go
+++ b/models/issue_test.go
@@ -443,12 +443,12 @@ func TestIssue_DeleteIssue(t *testing.T) {
assert.NoError(t, err)
err = CreateIssueDependency(user, issue1, issue2)
assert.NoError(t, err)
- left, err := IssueNoDependenciesLeft(issue1)
+ left, err := IssueNoDependenciesLeft(db.DefaultContext, issue1)
assert.NoError(t, err)
assert.False(t, left)
err = DeleteIssue(&Issue{ID: 2})
assert.NoError(t, err)
- left, err = IssueNoDependenciesLeft(issue1)
+ left, err = IssueNoDependenciesLeft(db.DefaultContext, issue1)
assert.NoError(t, err)
assert.True(t, left)
}
diff --git a/models/issue_xref.go b/models/issue_xref.go
index 2647f9c656..cd1c122252 100644
--- a/models/issue_xref.go
+++ b/models/issue_xref.go
@@ -249,7 +249,7 @@ func (comment *Comment) addCrossReferences(stdCtx context.Context, doer *user_mo
if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
return nil
}
- if err := comment.loadIssue(db.GetEngine(stdCtx)); err != nil {
+ if err := comment.LoadIssueCtx(stdCtx); err != nil {
return err
}
ctx := &crossReferencesContext{
@@ -340,9 +340,9 @@ func (comment *Comment) RefIssueIdent() string {
// \/ \/ |__| \/ \/
// ResolveCrossReferences will return the list of references to close/reopen by this PR
-func (pr *PullRequest) ResolveCrossReferences() ([]*Comment, error) {
+func (pr *PullRequest) ResolveCrossReferences(ctx context.Context) ([]*Comment, error) {
unfiltered := make([]*Comment, 0, 5)
- if err := db.GetEngine(db.DefaultContext).
+ if err := db.GetEngine(ctx).
Where("ref_repo_id = ? AND ref_issue_id = ?", pr.Issue.RepoID, pr.Issue.ID).
In("ref_action", []references.XRefAction{references.XRefActionCloses, references.XRefActionReopens}).
OrderBy("id").
diff --git a/models/issue_xref_test.go b/models/issue_xref_test.go
index 1ae5fb360f..677caa48ba 100644
--- a/models/issue_xref_test.go
+++ b/models/issue_xref_test.go
@@ -98,7 +98,7 @@ func TestXRef_ResolveCrossReferences(t *testing.T) {
i1 := testCreateIssue(t, 1, 2, "title1", "content1", false)
i2 := testCreateIssue(t, 1, 2, "title2", "content2", false)
i3 := testCreateIssue(t, 1, 2, "title3", "content3", false)
- _, err := ChangeIssueStatus(i3, d, true)
+ _, err := ChangeIssueStatus(db.DefaultContext, i3, d, true)
assert.NoError(t, err)
pr := testCreatePR(t, 1, 2, "titlepr", fmt.Sprintf("closes #%d", i1.Index))
@@ -118,7 +118,7 @@ func TestXRef_ResolveCrossReferences(t *testing.T) {
c4 := testCreateComment(t, 1, 2, pr.Issue.ID, fmt.Sprintf("closes #%d", i3.Index))
r4 := unittest.AssertExistsAndLoadBean(t, &Comment{IssueID: i3.ID, RefIssueID: pr.Issue.ID, RefCommentID: c4.ID}).(*Comment)
- refs, err := pr.ResolveCrossReferences()
+ refs, err := pr.ResolveCrossReferences(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, refs, 3)
assert.Equal(t, rp.ID, refs[0].ID, "bad ref rp: %+v", refs[0])
diff --git a/models/pull.go b/models/pull.go
index 437b202d3f..d056888130 100644
--- a/models/pull.go
+++ b/models/pull.go
@@ -227,23 +227,23 @@ func (pr *PullRequest) LoadProtectedBranchCtx(ctx context.Context) (err error) {
}
// GetDefaultMergeMessage returns default message used when merging pull request
-func (pr *PullRequest) GetDefaultMergeMessage() (string, error) {
+func (pr *PullRequest) GetDefaultMergeMessage(ctx context.Context) (string, error) {
if pr.HeadRepo == nil {
var err error
- pr.HeadRepo, err = repo_model.GetRepositoryByID(pr.HeadRepoID)
+ pr.HeadRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.HeadRepoID)
if err != nil {
return "", fmt.Errorf("GetRepositoryById[%d]: %v", pr.HeadRepoID, err)
}
}
- if err := pr.LoadIssue(); err != nil {
+ if err := pr.LoadIssueCtx(ctx); err != nil {
return "", fmt.Errorf("Cannot load issue %d for PR id %d: Error: %v", pr.IssueID, pr.ID, err)
}
- if err := pr.LoadBaseRepo(); err != nil {
+ if err := pr.LoadBaseRepoCtx(ctx); err != nil {
return "", fmt.Errorf("LoadBaseRepo: %v", err)
}
issueReference := "#"
- if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
+ if pr.BaseRepo.UnitEnabledCtx(ctx, unit.TypeExternalTracker) {
issueReference = "!"
}
@@ -337,14 +337,14 @@ func (pr *PullRequest) getReviewedByLines(writer io.Writer) error {
}
// GetDefaultSquashMessage returns default message used when squash and merging pull request
-func (pr *PullRequest) GetDefaultSquashMessage() (string, error) {
- if err := pr.LoadIssue(); err != nil {
+func (pr *PullRequest) GetDefaultSquashMessage(ctx context.Context) (string, error) {
+ if err := pr.LoadIssueCtx(ctx); err != nil {
return "", fmt.Errorf("LoadIssue: %v", err)
}
- if err := pr.LoadBaseRepo(); err != nil {
+ if err := pr.LoadBaseRepoCtx(ctx); err != nil {
return "", fmt.Errorf("LoadBaseRepo: %v", err)
}
- if pr.BaseRepo.UnitEnabled(unit.TypeExternalTracker) {
+ if pr.BaseRepo.UnitEnabledCtx(ctx, unit.TypeExternalTracker) {
return fmt.Sprintf("%s (!%d)", pr.Issue.Title, pr.Issue.Index), nil
}
return fmt.Sprintf("%s (#%d)", pr.Issue.Title, pr.Issue.Index), nil
@@ -371,7 +371,7 @@ func (pr *PullRequest) IsEmpty() bool {
}
// SetMerged sets a pull request to merged and closes the corresponding issue
-func (pr *PullRequest) SetMerged() (bool, error) {
+func (pr *PullRequest) SetMerged(ctx context.Context) (bool, error) {
if pr.HasMerged {
return false, fmt.Errorf("PullRequest[%d] already merged", pr.Index)
}
@@ -380,12 +380,6 @@ func (pr *PullRequest) SetMerged() (bool, error) {
}
pr.HasMerged = true
-
- ctx, committer, err := db.TxContext()
- if err != nil {
- return false, err
- }
- defer committer.Close()
sess := db.GetEngine(ctx)
if _, err := sess.Exec("UPDATE `issue` SET `repo_id` = `repo_id` WHERE `id` = ?", pr.IssueID); err != nil {
@@ -432,9 +426,6 @@ func (pr *PullRequest) SetMerged() (bool, error) {
return false, fmt.Errorf("Failed to update pr[%d]: %v", pr.ID, err)
}
- if err := committer.Commit(); err != nil {
- return false, fmt.Errorf("Commit: %v", err)
- }
return true, nil
}
diff --git a/models/pull_list.go b/models/pull_list.go
index 055119a1e5..ca09e28a93 100644
--- a/models/pull_list.go
+++ b/models/pull_list.go
@@ -62,8 +62,8 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ
// HasUnmergedPullRequestsByHeadInfo checks if there are open and not merged pull request
// by given head information (repo and branch)
-func HasUnmergedPullRequestsByHeadInfo(repoID int64, branch string) (bool, error) {
- return db.GetEngine(db.DefaultContext).
+func HasUnmergedPullRequestsByHeadInfo(ctx context.Context, repoID int64, branch string) (bool, error) {
+ return db.GetEngine(ctx).
Where("head_repo_id = ? AND head_branch = ? AND has_merged = ? AND issue.is_closed = ? AND flow = ?",
repoID, branch, false, false, PullRequestFlowGithub).
Join("INNER", "issue", "issue.id = pull_request.issue_id").
diff --git a/models/pull_test.go b/models/pull_test.go
index 6194ac0e24..92cf9a6524 100644
--- a/models/pull_test.go
+++ b/models/pull_test.go
@@ -110,11 +110,11 @@ func TestGetUnmergedPullRequest(t *testing.T) {
func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
- exist, err := HasUnmergedPullRequestsByHeadInfo(1, "branch2")
+ exist, err := HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "branch2")
assert.NoError(t, err)
assert.Equal(t, true, exist)
- exist, err = HasUnmergedPullRequestsByHeadInfo(1, "not_exist_branch")
+ exist, err = HasUnmergedPullRequestsByHeadInfo(db.DefaultContext, 1, "not_exist_branch")
assert.NoError(t, err)
assert.Equal(t, false, exist)
}
@@ -261,13 +261,13 @@ func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
- msg, err := pr.GetDefaultMergeMessage()
+ msg, err := pr.GetDefaultMergeMessage(db.DefaultContext)
assert.NoError(t, err)
assert.Equal(t, "Merge pull request 'issue3' (#3) from branch2 into master", msg)
pr.BaseRepoID = 1
pr.HeadRepoID = 2
- msg, err = pr.GetDefaultMergeMessage()
+ msg, err = pr.GetDefaultMergeMessage(db.DefaultContext)
assert.NoError(t, err)
assert.Equal(t, "Merge pull request 'issue3' (#3) from user2/repo1:branch2 into master", msg)
}
@@ -287,13 +287,22 @@ func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) {
pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2, BaseRepo: baseRepo}).(*PullRequest)
- msg, err := pr.GetDefaultMergeMessage()
+ msg, err := pr.GetDefaultMergeMessage(db.DefaultContext)
assert.NoError(t, err)
assert.Equal(t, "Merge pull request 'issue3' (!3) from branch2 into master", msg)
pr.BaseRepoID = 1
pr.HeadRepoID = 2
- msg, err = pr.GetDefaultMergeMessage()
+ msg, err = pr.GetDefaultMergeMessage(db.DefaultContext)
assert.NoError(t, err)
assert.Equal(t, "Merge pull request 'issue3' (!3) from user2/repo1:branch2 into master", msg)
}
+
+func TestPullRequest_GetDefaultSquashMessage(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+ pr := unittest.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest)
+
+ msg, err := pr.GetDefaultSquashMessage(db.DefaultContext)
+ assert.NoError(t, err)
+ assert.Equal(t, "issue3 (#3)", msg)
+}
diff --git a/models/repo.go b/models/repo.go
index 9c2fce8d3c..e934b24fb3 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -455,8 +455,8 @@ func CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_
}
}
- if isAdmin, err := isUserRepoAdmin(ctx, repo, doer); err != nil {
- return fmt.Errorf("isUserRepoAdmin: %v", err)
+ if isAdmin, err := IsUserRepoAdminCtx(ctx, repo, doer); err != nil {
+ return fmt.Errorf("IsUserRepoAdminCtx: %v", err)
} else if !isAdmin {
// Make creator repo admin if it wasn't assigned automatically
if err = addCollaborator(ctx, repo, doer); err != nil {
@@ -1215,7 +1215,7 @@ func DeleteDeployKey(ctx context.Context, doer *user_model.User, id int64) error
if err != nil {
return fmt.Errorf("GetRepositoryByID: %v", err)
}
- has, err := isUserRepoAdmin(ctx, repo, doer)
+ has, err := IsUserRepoAdminCtx(ctx, repo, doer)
if err != nil {
return fmt.Errorf("GetUserRepoPermission: %v", err)
} else if !has {
diff --git a/models/repo/repo.go b/models/repo/repo.go
index fc72d36dac..8af6357bf3 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -302,8 +302,19 @@ func (repo *Repository) LoadUnits(ctx context.Context) (err error) {
}
// UnitEnabled if this repository has the given unit enabled
-func (repo *Repository) UnitEnabled(tp unit.Type) bool {
- if err := repo.LoadUnits(db.DefaultContext); err != nil {
+func (repo *Repository) UnitEnabled(tp unit.Type) (result bool) {
+ if err := db.WithContext(func(ctx *db.Context) error {
+ result = repo.UnitEnabledCtx(ctx, tp)
+ return nil
+ }); err != nil {
+ log.Error("repo.UnitEnabled: %v", err)
+ }
+ return
+}
+
+// UnitEnabled if this repository has the given unit enabled
+func (repo *Repository) UnitEnabledCtx(ctx context.Context, tp unit.Type) bool {
+ if err := repo.LoadUnits(ctx); err != nil {
log.Warn("Error loading repository (ID: %d) units: %s", repo.ID, err.Error())
}
for _, unit := range repo.Units {
diff --git a/models/repo_permission.go b/models/repo_permission.go
index bc31b873f2..8ba6b86145 100644
--- a/models/repo_permission.go
+++ b/models/repo_permission.go
@@ -331,10 +331,11 @@ func IsUserRealRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bo
// IsUserRepoAdmin return true if user has admin right of a repo
func IsUserRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bool, error) {
- return isUserRepoAdmin(db.DefaultContext, repo, user)
+ return IsUserRepoAdminCtx(db.DefaultContext, repo, user)
}
-func isUserRepoAdmin(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (bool, error) {
+// IsUserRepoAdminCtx return true if user has admin right of a repo
+func IsUserRepoAdminCtx(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (bool, error) {
if user == nil || repo == nil {
return false, nil
}