diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-12-10 09:27:50 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-10 09:27:50 +0800 |
commit | 719bddcd76610a63dadc8555760072957a11cf30 (patch) | |
tree | 0df26092fba7e3e21444fe493e6b349473b6b0cb /models/pull.go | |
parent | fb8166c6c6b652a0e6fa98681780a6a71090faf3 (diff) | |
download | gitea-719bddcd76610a63dadc8555760072957a11cf30.tar.gz gitea-719bddcd76610a63dadc8555760072957a11cf30.zip |
Move repository model into models/repo (#17933)
* Some refactors related repository model
* Move more methods out of repository
* Move repository into models/repo
* Fix test
* Fix test
* some improvements
* Remove unnecessary function
Diffstat (limited to 'models/pull.go')
-rw-r--r-- | models/pull.go | 62 |
1 files changed, 23 insertions, 39 deletions
diff --git a/models/pull.go b/models/pull.go index aa58322bce..243d40b1fa 100644 --- a/models/pull.go +++ b/models/pull.go @@ -6,11 +6,13 @@ package models import ( + "context" "fmt" "io" "strings" "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" @@ -67,10 +69,10 @@ type PullRequest struct { Issue *Issue `xorm:"-"` Index int64 - HeadRepoID int64 `xorm:"INDEX"` - HeadRepo *Repository `xorm:"-"` - BaseRepoID int64 `xorm:"INDEX"` - BaseRepo *Repository `xorm:"-"` + HeadRepoID int64 `xorm:"INDEX"` + HeadRepo *repo_model.Repository `xorm:"-"` + BaseRepoID int64 `xorm:"INDEX"` + BaseRepo *repo_model.Repository `xorm:"-"` HeadBranch string HeadCommitID string `xorm:"-"` BaseBranch string @@ -95,7 +97,7 @@ func init() { // MustHeadUserName returns the HeadRepo's username if failed return blank func (pr *PullRequest) MustHeadUserName() string { if err := pr.LoadHeadRepo(); err != nil { - if !IsErrRepoNotExist(err) { + if !repo_model.IsErrRepoNotExist(err) { log.Error("LoadHeadRepo: %v", err) } else { log.Warn("LoadHeadRepo %d but repository does not exist: %v", pr.HeadRepoID, err) @@ -128,7 +130,7 @@ func (pr *PullRequest) LoadAttributes() error { return pr.loadAttributes(db.GetEngine(db.DefaultContext)) } -func (pr *PullRequest) loadHeadRepo(e db.Engine) (err error) { +func (pr *PullRequest) loadHeadRepo(ctx context.Context) (err error) { if !pr.isHeadRepoLoaded && pr.HeadRepo == nil && pr.HeadRepoID > 0 { if pr.HeadRepoID == pr.BaseRepoID { if pr.BaseRepo != nil { @@ -140,8 +142,8 @@ func (pr *PullRequest) loadHeadRepo(e db.Engine) (err error) { } } - pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID) - if err != nil && !IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work + pr.HeadRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.HeadRepoID) + if err != nil && !repo_model.IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work return fmt.Errorf("getRepositoryByID(head): %v", err) } pr.isHeadRepoLoaded = true @@ -151,15 +153,15 @@ func (pr *PullRequest) loadHeadRepo(e db.Engine) (err error) { // LoadHeadRepo loads the head repository func (pr *PullRequest) LoadHeadRepo() error { - return pr.loadHeadRepo(db.GetEngine(db.DefaultContext)) + return pr.loadHeadRepo(db.DefaultContext) } // LoadBaseRepo loads the target repository func (pr *PullRequest) LoadBaseRepo() error { - return pr.loadBaseRepo(db.GetEngine(db.DefaultContext)) + return pr.loadBaseRepo(db.DefaultContext) } -func (pr *PullRequest) loadBaseRepo(e db.Engine) (err error) { +func (pr *PullRequest) loadBaseRepo(ctx context.Context) (err error) { if pr.BaseRepo != nil { return nil } @@ -174,9 +176,9 @@ func (pr *PullRequest) loadBaseRepo(e db.Engine) (err error) { return nil } - pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID) + pr.BaseRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.BaseRepoID) if err != nil { - return fmt.Errorf("GetRepositoryByID(base): %v", err) + return fmt.Errorf("repo_model.GetRepositoryByID(base): %v", err) } return nil } @@ -200,21 +202,21 @@ func (pr *PullRequest) loadIssue(e db.Engine) (err error) { // LoadProtectedBranch loads the protected branch of the base branch func (pr *PullRequest) LoadProtectedBranch() (err error) { - return pr.loadProtectedBranch(db.GetEngine(db.DefaultContext)) + return pr.loadProtectedBranch(db.DefaultContext) } -func (pr *PullRequest) loadProtectedBranch(e db.Engine) (err error) { +func (pr *PullRequest) loadProtectedBranch(ctx context.Context) (err error) { if pr.ProtectedBranch == nil { if pr.BaseRepo == nil { if pr.BaseRepoID == 0 { return nil } - pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID) + pr.BaseRepo, err = repo_model.GetRepositoryByIDCtx(ctx, pr.BaseRepoID) if err != nil { return } } - pr.ProtectedBranch, err = getProtectedBranchBy(e, pr.BaseRepo.ID, pr.BaseBranch) + pr.ProtectedBranch, err = getProtectedBranchBy(db.GetEngine(ctx), pr.BaseRepo.ID, pr.BaseBranch) } return } @@ -223,7 +225,7 @@ func (pr *PullRequest) loadProtectedBranch(e db.Engine) (err error) { func (pr *PullRequest) GetDefaultMergeMessage() string { if pr.HeadRepo == nil { var err error - pr.HeadRepo, err = GetRepositoryByID(pr.HeadRepoID) + pr.HeadRepo, err = repo_model.GetRepositoryByID(pr.HeadRepoID) if err != nil { log.Error("GetRepositoryById[%d]: %v", pr.HeadRepoID, err) return "" @@ -368,24 +370,6 @@ func (pr *PullRequest) IsEmpty() bool { return pr.Status == PullRequestStatusEmpty } -// MergeStyle represents the approach to merge commits into base branch. -type MergeStyle string - -const ( - // MergeStyleMerge create merge commit - MergeStyleMerge MergeStyle = "merge" - // MergeStyleRebase rebase before merging - MergeStyleRebase MergeStyle = "rebase" - // MergeStyleRebaseMerge rebase before merging with merge commit (--no-ff) - MergeStyleRebaseMerge MergeStyle = "rebase-merge" - // MergeStyleSquash squash commits into single commit before merging - MergeStyleSquash MergeStyle = "squash" - // MergeStyleManuallyMerged pr has been merged manually, just mark it as merged directly - MergeStyleManuallyMerged MergeStyle = "manually-merged" - // MergeStyleRebaseUpdate not a merge style, used to update pull head by rebase - MergeStyleRebaseUpdate MergeStyle = "rebase-update-only" -) - // SetMerged sets a pull request to merged and closes the corresponding issue func (pr *PullRequest) SetMerged() (bool, error) { if pr.HasMerged { @@ -428,11 +412,11 @@ func (pr *PullRequest) SetMerged() (bool, error) { return false, fmt.Errorf("PullRequest[%d] already closed", pr.Index) } - if err := pr.Issue.loadRepo(sess); err != nil { + if err := pr.Issue.loadRepo(ctx); err != nil { return false, err } - if err := pr.Issue.Repo.getOwner(sess); err != nil { + if err := pr.Issue.Repo.GetOwner(ctx); err != nil { return false, err } @@ -452,7 +436,7 @@ func (pr *PullRequest) SetMerged() (bool, error) { } // NewPullRequest creates new pull request with labels for repository. -func NewPullRequest(repo *Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { +func NewPullRequest(repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) { idx, err := db.GetNextResourceIndex("issue_index", repo.ID) if err != nil { return fmt.Errorf("generate pull request index failed: %v", err) |