summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authora1012112796 <1012112796@qq.com>2022-01-04 03:45:58 +0800
committerGitHub <noreply@github.com>2022-01-03 20:45:58 +0100
commit637c3ec5d82f9d727724cc362aea37eeb2f64b40 (patch)
tree2b3af5ec2e6595d037a78b8a45e0a95210ab7d14 /models
parent650a50a7ba8efcd8d02c4c93e5abc19f27e6c4b5 (diff)
downloadgitea-637c3ec5d82f9d727724cc362aea37eeb2f64b40.tar.gz
gitea-637c3ec5d82f9d727724cc362aea37eeb2f64b40.zip
Don't delete branch if other PRs with this branch are open (#18164)
fix #18149 Signed-off-by: a1012112796 <1012112796@qq.com>
Diffstat (limited to 'models')
-rw-r--r--models/pull_list.go10
-rw-r--r--models/pull_test.go12
2 files changed, 22 insertions, 0 deletions
diff --git a/models/pull_list.go b/models/pull_list.go
index 7c5b83ae5f..9d4d428928 100644
--- a/models/pull_list.go
+++ b/models/pull_list.go
@@ -59,6 +59,16 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ
Find(&prs)
}
+// 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).
+ 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").
+ Exist(&PullRequest{})
+}
+
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
// by given base information (repo and branch).
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
diff --git a/models/pull_test.go b/models/pull_test.go
index f5e9d486ff..2567984cc1 100644
--- a/models/pull_test.go
+++ b/models/pull_test.go
@@ -107,6 +107,18 @@ func TestGetUnmergedPullRequest(t *testing.T) {
assert.True(t, IsErrPullRequestNotExist(err))
}
+func TestHasUnmergedPullRequestsByHeadInfo(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ exist, err := HasUnmergedPullRequestsByHeadInfo(1, "branch2")
+ assert.NoError(t, err)
+ assert.Equal(t, true, exist)
+
+ exist, err = HasUnmergedPullRequestsByHeadInfo(1, "not_exist_branch")
+ assert.NoError(t, err)
+ assert.Equal(t, false, exist)
+}
+
func TestGetUnmergedPullRequestsByHeadInfo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
prs, err := GetUnmergedPullRequestsByHeadInfo(1, "branch2")