aboutsummaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-03-03 06:31:55 +0800
committerGitHub <noreply@github.com>2020-03-02 22:31:55 +0000
commit5abe1c52de533b52725b1e584db9fea6fb1b39ab (patch)
tree1cd49bc1a609bb919c70d6bc30727c524c3e6105 /models
parent22b750702408a39a524e011b729d0e0befb30e16 (diff)
downloadgitea-5abe1c52de533b52725b1e584db9fea6fb1b39ab.tar.gz
gitea-5abe1c52de533b52725b1e584db9fea6fb1b39ab.zip
Display pull request head branch even the branch deleted or repository deleted (#10413)
* Display pull request head branch even the branch deleted or repository deleted * Merge getHeadRepo/loadHeadRepo and getBaseRepo/loadBaseRepo on pull and fill repo when pr.Issue.Repo is available * retrieve sha from pull head when pull request branch deleted and fix tests * Fix test * Ensure MustHeadRepoName returns empty string if no head repo Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models')
-rw-r--r--models/pull.go104
-rw-r--r--models/pull_sign.go2
-rw-r--r--models/pull_test.go10
3 files changed, 56 insertions, 60 deletions
diff --git a/models/pull.go b/models/pull.go
index 46c50986b9..3e177ea5e9 100644
--- a/models/pull.go
+++ b/models/pull.go
@@ -62,6 +62,8 @@ type PullRequest struct {
MergerID int64 `xorm:"INDEX"`
Merger *User `xorm:"-"`
MergedUnix timeutil.TimeStamp `xorm:"updated INDEX"`
+
+ isHeadRepoLoaded bool `xorm:"-"`
}
// MustHeadUserName returns the HeadRepo's username if failed return blank
@@ -74,6 +76,9 @@ func (pr *PullRequest) MustHeadUserName() string {
}
return ""
}
+ if pr.HeadRepo == nil {
+ return ""
+ }
return pr.HeadRepo.OwnerName
}
@@ -97,38 +102,55 @@ func (pr *PullRequest) LoadAttributes() error {
return pr.loadAttributes(x)
}
-// LoadBaseRepo loads pull request base repository from database
-func (pr *PullRequest) LoadBaseRepo() error {
- if pr.BaseRepo == nil {
- if pr.HeadRepoID == pr.BaseRepoID && pr.HeadRepo != nil {
- pr.BaseRepo = pr.HeadRepo
- return nil
+func (pr *PullRequest) loadHeadRepo(e Engine) (err error) {
+ if !pr.isHeadRepoLoaded && pr.HeadRepo == nil && pr.HeadRepoID > 0 {
+ if pr.HeadRepoID == pr.BaseRepoID {
+ if pr.BaseRepo != nil {
+ pr.HeadRepo = pr.BaseRepo
+ return nil
+ } else if pr.Issue != nil && pr.Issue.Repo != nil {
+ pr.HeadRepo = pr.Issue.Repo
+ return nil
+ }
}
- var repo Repository
- if has, err := x.ID(pr.BaseRepoID).Get(&repo); err != nil {
- return err
- } else if !has {
- return ErrRepoNotExist{ID: pr.BaseRepoID}
+
+ pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
+ if err != nil && !IsErrRepoNotExist(err) { // Head repo maybe deleted, but it should still work
+ return fmt.Errorf("getRepositoryByID(head): %v", err)
}
- pr.BaseRepo = &repo
+ pr.isHeadRepoLoaded = true
}
return nil
}
-// LoadHeadRepo loads pull request head repository from database
+// LoadHeadRepo loads the head repository
func (pr *PullRequest) LoadHeadRepo() error {
- if pr.HeadRepo == nil {
- if pr.HeadRepoID == pr.BaseRepoID && pr.BaseRepo != nil {
- pr.HeadRepo = pr.BaseRepo
- return nil
- }
- var repo Repository
- if has, err := x.ID(pr.HeadRepoID).Get(&repo); err != nil {
- return err
- } else if !has {
- return ErrRepoNotExist{ID: pr.HeadRepoID}
- }
- pr.HeadRepo = &repo
+ return pr.loadHeadRepo(x)
+}
+
+// LoadBaseRepo loads the target repository
+func (pr *PullRequest) LoadBaseRepo() error {
+ return pr.loadBaseRepo(x)
+}
+
+func (pr *PullRequest) loadBaseRepo(e Engine) (err error) {
+ if pr.BaseRepo != nil {
+ return nil
+ }
+
+ if pr.HeadRepoID == pr.BaseRepoID && pr.HeadRepo != nil {
+ pr.BaseRepo = pr.HeadRepo
+ return nil
+ }
+
+ if pr.Issue != nil && pr.Issue.Repo != nil {
+ pr.BaseRepo = pr.Issue.Repo
+ return nil
+ }
+
+ pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
+ if err != nil {
+ return fmt.Errorf("GetRepositoryByID(base): %v", err)
}
return nil
}
@@ -414,32 +436,6 @@ func (pr *PullRequest) GetGitRefName() string {
return fmt.Sprintf("refs/pull/%d/head", pr.Index)
}
-func (pr *PullRequest) getHeadRepo(e Engine) (err error) {
- pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
- if err != nil && !IsErrRepoNotExist(err) {
- return fmt.Errorf("getRepositoryByID(head): %v", err)
- }
- return nil
-}
-
-// GetHeadRepo loads the head repository
-func (pr *PullRequest) GetHeadRepo() error {
- return pr.getHeadRepo(x)
-}
-
-// GetBaseRepo loads the target repository
-func (pr *PullRequest) GetBaseRepo() (err error) {
- if pr.BaseRepo != nil {
- return nil
- }
-
- pr.BaseRepo, err = GetRepositoryByID(pr.BaseRepoID)
- if err != nil {
- return fmt.Errorf("GetRepositoryByID(base): %v", err)
- }
- return nil
-}
-
// IsChecking returns true if this pull request is still checking conflict.
func (pr *PullRequest) IsChecking() bool {
return pr.Status == PullRequestStatusChecking
@@ -452,7 +448,7 @@ func (pr *PullRequest) CanAutoMerge() bool {
// GetLastCommitStatus returns the last commit status for this pull request.
func (pr *PullRequest) GetLastCommitStatus() (status *CommitStatus, err error) {
- if err = pr.GetHeadRepo(); err != nil {
+ if err = pr.LoadHeadRepo(); err != nil {
return nil, err
}
@@ -774,7 +770,7 @@ func (pr *PullRequest) GetWorkInProgressPrefix() string {
// IsHeadEqualWithBranch returns if the commits of branchName are available in pull request head
func (pr *PullRequest) IsHeadEqualWithBranch(branchName string) (bool, error) {
var err error
- if err = pr.GetBaseRepo(); err != nil {
+ if err = pr.LoadBaseRepo(); err != nil {
return false, err
}
baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
@@ -786,7 +782,7 @@ func (pr *PullRequest) IsHeadEqualWithBranch(branchName string) (bool, error) {
return false, err
}
- if err = pr.GetHeadRepo(); err != nil {
+ if err = pr.LoadHeadRepo(); err != nil {
return false, err
}
headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
diff --git a/models/pull_sign.go b/models/pull_sign.go
index 5b26b4bdc9..91b8defa53 100644
--- a/models/pull_sign.go
+++ b/models/pull_sign.go
@@ -12,7 +12,7 @@ import (
// SignMerge determines if we should sign a PR merge commit to the base repository
func (pr *PullRequest) SignMerge(u *User, tmpBasePath, baseCommit, headCommit string) (bool, string, error) {
- if err := pr.GetBaseRepo(); err != nil {
+ if err := pr.LoadBaseRepo(); err != nil {
log.Error("Unable to get Base Repo for pull request")
return false, "", err
}
diff --git a/models/pull_test.go b/models/pull_test.go
index 0dc3bb86d7..3cc6abfec7 100644
--- a/models/pull_test.go
+++ b/models/pull_test.go
@@ -29,21 +29,21 @@ func TestPullRequest_LoadIssue(t *testing.T) {
assert.Equal(t, int64(2), pr.Issue.ID)
}
-func TestPullRequest_GetBaseRepo(t *testing.T) {
+func TestPullRequest_LoadBaseRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
- assert.NoError(t, pr.GetBaseRepo())
+ assert.NoError(t, pr.LoadBaseRepo())
assert.NotNil(t, pr.BaseRepo)
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
- assert.NoError(t, pr.GetBaseRepo())
+ assert.NoError(t, pr.LoadBaseRepo())
assert.NotNil(t, pr.BaseRepo)
assert.Equal(t, pr.BaseRepoID, pr.BaseRepo.ID)
}
-func TestPullRequest_GetHeadRepo(t *testing.T) {
+func TestPullRequest_LoadHeadRepo(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
pr := AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest)
- assert.NoError(t, pr.GetHeadRepo())
+ assert.NoError(t, pr.LoadHeadRepo())
assert.NotNil(t, pr.HeadRepo)
assert.Equal(t, pr.HeadRepoID, pr.HeadRepo.ID)
}