@@ -13,9 +13,13 @@ import (
1313
1414 auth_model "code.gitea.io/gitea/models/auth"
1515 git_model "code.gitea.io/gitea/models/git"
16+ "code.gitea.io/gitea/models/issues"
1617 repo_model "code.gitea.io/gitea/models/repo"
1718 "code.gitea.io/gitea/models/unittest"
19+ "code.gitea.io/gitea/modules/setting"
1820 api "code.gitea.io/gitea/modules/structs"
21+ "code.gitea.io/gitea/modules/test"
22+ "code.gitea.io/gitea/services/pull"
1923
2024 "github.com/stretchr/testify/assert"
2125)
@@ -165,3 +169,75 @@ func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
165169 assert .Contains (t , text , "This branch is already included in the target branch. There is nothing to merge." )
166170 })
167171}
172+
173+ func TestPullStatusDelayCheck (t * testing.T ) {
174+ onGiteaRun (t , func (t * testing.T , u * url.URL ) {
175+ defer test .MockVariableValue (& setting .IsProd )()
176+ defer test .MockVariableValue (& setting .Repository .PullRequest .DelayCheckForInactiveDays , 1 )()
177+ defer test .MockVariableValue (& pull .AddPullRequestToCheckQueue )()
178+
179+ session := loginUser (t , "user2" )
180+
181+ run := func (t * testing.T , fn func (* testing.T )) (issue3 * issues.Issue , checkedPrID int64 ) {
182+ pull .AddPullRequestToCheckQueue = func (prID int64 ) {
183+ checkedPrID = prID
184+ }
185+ fn (t )
186+ issue3 = unittest .AssertExistsAndLoadBean (t , & issues.Issue {RepoID : 1 , Index : 3 })
187+ _ = issue3 .LoadPullRequest (t .Context ())
188+ return issue3 , checkedPrID
189+ }
190+
191+ assertReloadingInterval := func (t * testing.T , interval string ) {
192+ req := NewRequest (t , "GET" , "/user2/repo1/pulls/3" )
193+ resp := session .MakeRequest (t , req , http .StatusOK )
194+ attr := "data-pull-merge-box-reloading-interval"
195+ if interval == "" {
196+ assert .NotContains (t , resp .Body .String (), attr )
197+ } else {
198+ assert .Contains (t , resp .Body .String (), fmt .Sprintf (`%s="%v"` , attr , interval ))
199+ }
200+ }
201+
202+ // PR issue3 is merageable at the beginning
203+ issue3 , checkedPrID := run (t , func (t * testing.T ) {})
204+ assert .Equal (t , issues .PullRequestStatusMergeable , issue3 .PullRequest .Status )
205+ assert .Zero (t , checkedPrID )
206+ setting .IsProd = true
207+ assertReloadingInterval (t , "" ) // the PR is mergeable, so no need to reload the merge box
208+ setting .IsProd = false
209+ assertReloadingInterval (t , "1" ) // make sure dev mode always do merge box reloading, to make sure the UI logic won't break
210+ setting .IsProd = true
211+
212+ // when base branch changes, PR status should be updated, but it is inactive for long time, so no real check
213+ issue3 , checkedPrID = run (t , func (t * testing.T ) {
214+ testEditFile (t , session , "user2" , "repo1" , "master" , "README.md" , "new content" )
215+ })
216+ assert .Equal (t , issues .PullRequestStatusChecking , issue3 .PullRequest .Status )
217+ assert .Zero (t , checkedPrID )
218+ assertReloadingInterval (t , "2000" ) // the PR status is "checking", so try to reload the merge box
219+
220+ // view a PR with status=checking, it starts the real check
221+ issue3 , checkedPrID = run (t , func (t * testing.T ) {
222+ req := NewRequest (t , "GET" , "/user2/repo1/pulls/3" )
223+ session .MakeRequest (t , req , http .StatusOK )
224+ })
225+ assert .Equal (t , issues .PullRequestStatusChecking , issue3 .PullRequest .Status )
226+ assert .Equal (t , issue3 .PullRequest .ID , checkedPrID )
227+
228+ // when base branch changes, still so no real check
229+ issue3 , checkedPrID = run (t , func (t * testing.T ) {
230+ testEditFile (t , session , "user2" , "repo1" , "master" , "README.md" , "new content" )
231+ })
232+ assert .Equal (t , issues .PullRequestStatusChecking , issue3 .PullRequest .Status )
233+ assert .Zero (t , 0 )
234+
235+ // the allow to check PRs without delay
236+ setting .Repository .PullRequest .DelayCheckForInactiveDays = - 1
237+ issue3 , checkedPrID = run (t , func (t * testing.T ) {
238+ testEditFile (t , session , "user2" , "repo1" , "master" , "README.md" , "new content" )
239+ })
240+ assert .Equal (t , issues .PullRequestStatusChecking , issue3 .PullRequest .Status )
241+ assert .Equal (t , issue3 .PullRequest .ID , checkedPrID )
242+ })
243+ }
0 commit comments