@@ -12,13 +12,15 @@ import (
12
12
"testing"
13
13
14
14
"code.gitea.io/gitea/models/db"
15
+ git_model "code.gitea.io/gitea/models/git"
15
16
issues_model "code.gitea.io/gitea/models/issues"
16
17
repo_model "code.gitea.io/gitea/models/repo"
17
18
"code.gitea.io/gitea/models/unittest"
18
19
user_model "code.gitea.io/gitea/models/user"
19
20
"code.gitea.io/gitea/modules/git"
20
21
"code.gitea.io/gitea/modules/test"
21
22
issue_service "code.gitea.io/gitea/services/issue"
23
+ pull_service "code.gitea.io/gitea/services/pull"
22
24
repo_service "code.gitea.io/gitea/services/repository"
23
25
files_service "code.gitea.io/gitea/services/repository/files"
24
26
"code.gitea.io/gitea/tests"
@@ -49,6 +51,8 @@ func TestPullView_ReviewerMissed(t *testing.T) {
49
51
func TestPullView_CodeOwner (t * testing.T ) {
50
52
onGiteaRun (t , func (t * testing.T , u * url.URL ) {
51
53
user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
54
+ user5 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 5 })
55
+ user8 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 8 })
52
56
53
57
// Create the repo.
54
58
repo , err := repo_service .CreateRepositoryDirectly (db .DefaultContext , user2 , user2 , repo_service.CreateRepoOptions {
@@ -60,6 +64,17 @@ func TestPullView_CodeOwner(t *testing.T) {
60
64
}, true )
61
65
assert .NoError (t , err )
62
66
67
+ // create code owner branch protection
68
+ protectBranch := git_model.ProtectedBranch {
69
+ BlockOnCodeownerReviews : true ,
70
+ RepoID : repo .ID ,
71
+ RuleName : "master" ,
72
+ CanPush : true ,
73
+ }
74
+
75
+ err = pull_service .CreateOrUpdateProtectedBranch (db .DefaultContext , repo , & protectBranch , git_model.WhitelistOptions {})
76
+ assert .NoError (t , err )
77
+
63
78
// add CODEOWNERS to default branch
64
79
_ , err = files_service .ChangeRepoFiles (db .DefaultContext , repo , user2 , & files_service.ChangeRepoFilesOptions {
65
80
OldBranch : repo .DefaultBranch ,
@@ -96,7 +111,7 @@ func TestPullView_CodeOwner(t *testing.T) {
96
111
assert .NoError (t , pr .LoadIssue (db .DefaultContext ))
97
112
98
113
// update the file on the pr branch
99
- _ , err = files_service .ChangeRepoFiles (db .DefaultContext , repo , user2 , & files_service.ChangeRepoFilesOptions {
114
+ resp , err : = files_service .ChangeRepoFiles (db .DefaultContext , repo , user2 , & files_service.ChangeRepoFilesOptions {
100
115
OldBranch : "codeowner-basebranch" ,
101
116
Files : []* files_service.ChangeRepoFile {
102
117
{
@@ -124,6 +139,22 @@ func TestPullView_CodeOwner(t *testing.T) {
124
139
prUpdated2 := unittest .AssertExistsAndLoadBean (t , & issues_model.PullRequest {ID : pr .ID })
125
140
assert .NoError (t , prUpdated2 .LoadIssue (db .DefaultContext ))
126
141
assert .Equal (t , "Test Pull Request2" , prUpdated2 .Issue .Title )
142
+
143
+ // ensure it cannot be merged
144
+ hasCodeownerReviews := issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
145
+ assert .False (t , hasCodeownerReviews )
146
+
147
+ issues_model .SubmitReview (db .DefaultContext , user5 , pr .Issue , issues_model .ReviewTypeApprove , "Very good" , resp .Commit .SHA , false , make ([]string , 0 ))
148
+
149
+ // should still fail (we also need user8)
150
+ hasCodeownerReviews = issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
151
+ assert .False (t , hasCodeownerReviews )
152
+
153
+ issues_model .SubmitReview (db .DefaultContext , user8 , pr .Issue , issues_model .ReviewTypeApprove , "Very good" , resp .Commit .SHA , false , make ([]string , 0 ))
154
+
155
+ // now we should be able to merge
156
+ hasCodeownerReviews = issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
157
+ assert .True (t , hasCodeownerReviews )
127
158
})
128
159
129
160
// change the default branch CODEOWNERS file to change README.md's codeowner
@@ -140,7 +171,7 @@ func TestPullView_CodeOwner(t *testing.T) {
140
171
141
172
t .Run ("Second Pull Request" , func (t * testing.T ) {
142
173
// create a new branch to prepare for pull request
143
- _ , err = files_service .ChangeRepoFiles (db .DefaultContext , repo , user2 , & files_service.ChangeRepoFilesOptions {
174
+ resp , err : = files_service .ChangeRepoFiles (db .DefaultContext , repo , user2 , & files_service.ChangeRepoFilesOptions {
144
175
NewBranch : "codeowner-basebranch2" ,
145
176
Files : []* files_service.ChangeRepoFile {
146
177
{
@@ -158,6 +189,15 @@ func TestPullView_CodeOwner(t *testing.T) {
158
189
159
190
pr := unittest .AssertExistsAndLoadBean (t , & issues_model.PullRequest {BaseRepoID : repo .ID , HeadBranch : "codeowner-basebranch2" })
160
191
unittest .AssertExistsAndLoadBean (t , & issues_model.Review {IssueID : pr .IssueID , Type : issues_model .ReviewTypeRequest , ReviewerID : 8 })
192
+
193
+ // should need user8 approval only now
194
+ hasCodeownerReviews := issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
195
+ assert .False (t , hasCodeownerReviews )
196
+
197
+ issues_model .SubmitReview (db .DefaultContext , user8 , pr .Issue , issues_model .ReviewTypeApprove , "Very good" , resp .Commit .SHA , false , make ([]string , 0 ))
198
+
199
+ hasCodeownerReviews = issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
200
+ assert .True (t , hasCodeownerReviews )
161
201
})
162
202
163
203
t .Run ("Forked Repo Pull Request" , func (t * testing.T ) {
@@ -169,7 +209,7 @@ func TestPullView_CodeOwner(t *testing.T) {
169
209
assert .NoError (t , err )
170
210
171
211
// create a new branch to prepare for pull request
172
- _ , err = files_service .ChangeRepoFiles (db .DefaultContext , forkedRepo , user5 , & files_service.ChangeRepoFilesOptions {
212
+ resp , err : = files_service .ChangeRepoFiles (db .DefaultContext , forkedRepo , user5 , & files_service.ChangeRepoFilesOptions {
173
213
NewBranch : "codeowner-basebranch-forked" ,
174
214
Files : []* files_service.ChangeRepoFile {
175
215
{
@@ -194,6 +234,21 @@ func TestPullView_CodeOwner(t *testing.T) {
194
234
195
235
pr = unittest .AssertExistsAndLoadBean (t , & issues_model.PullRequest {BaseRepoID : repo .ID , HeadRepoID : forkedRepo .ID , HeadBranch : "codeowner-basebranch-forked" })
196
236
unittest .AssertExistsAndLoadBean (t , & issues_model.Review {IssueID : pr .IssueID , Type : issues_model .ReviewTypeRequest , ReviewerID : 8 })
237
+
238
+ // will also need user8 for this
239
+ hasCodeownerReviews := issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
240
+ assert .False (t , hasCodeownerReviews )
241
+
242
+ issues_model .SubmitReview (db .DefaultContext , user5 , pr .Issue , issues_model .ReviewTypeApprove , "Very good" , resp .Commit .SHA , false , make ([]string , 0 ))
243
+
244
+ // should still fail (user5 is not a code owner for this PR)
245
+ hasCodeownerReviews = issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
246
+ assert .False (t , hasCodeownerReviews )
247
+
248
+ issues_model .SubmitReview (db .DefaultContext , user8 , pr .Issue , issues_model .ReviewTypeApprove , "Very good" , resp .Commit .SHA , false , make ([]string , 0 ))
249
+
250
+ hasCodeownerReviews = issue_service .HasAllRequiredCodeownerReviews (db .DefaultContext , & protectBranch , pr )
251
+ assert .True (t , hasCodeownerReviews )
197
252
})
198
253
})
199
254
}
0 commit comments