Skip to content

Commit ecb2d74

Browse files
committed
add tests
1 parent d1f8ad1 commit ecb2d74

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

services/pull/check.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func markPullRequestStatusAsChecking(ctx context.Context, pr *issues_model.PullR
6161
return pr.Status == issues_model.PullRequestStatusChecking
6262
}
6363

64-
func addPullRequestToCheckQueue(prID int64) {
64+
var AddPullRequestToCheckQueue = realAddPullRequestToCheckQueue
65+
66+
func realAddPullRequestToCheckQueue(prID int64) {
6567
err := prPatchCheckerQueue.Push(strconv.FormatInt(prID, 10))
6668
if err != nil && !errors.Is(err, queue.ErrAlreadyInQueue) {
6769
log.Error("Error adding %v to the pull requests check queue: %v", prID, err)
@@ -72,7 +74,7 @@ func StartPullRequestCheckImmediately(ctx context.Context, pr *issues_model.Pull
7274
if !markPullRequestStatusAsChecking(ctx, pr) {
7375
return
7476
}
75-
addPullRequestToCheckQueue(pr.ID)
77+
AddPullRequestToCheckQueue(pr.ID)
7678
}
7779

7880
// StartPullRequestCheckDelayable will delay the check if the pull request is not update recently.
@@ -95,15 +97,15 @@ func StartPullRequestCheckDelayable(ctx context.Context, pr *issues_model.PullRe
9597
}
9698
}
9799

98-
addPullRequestToCheckQueue(pr.ID)
100+
AddPullRequestToCheckQueue(pr.ID)
99101
}
100102

101103
func StartPullRequestCheckOnView(_ context.Context, pr *issues_model.PullRequest) {
102104
// TODO: its correctness totally depends on the "unique queue" feature.
103105
// So duplicate "start" requests will be ignored if there is already a task in the queue
104106
// Ideally in the future we should decouple the "unique queue" feature from the "start" request
105107
if pr.Status == issues_model.PullRequestStatusChecking {
106-
addPullRequestToCheckQueue(pr.ID)
108+
AddPullRequestToCheckQueue(pr.ID)
107109
}
108110
}
109111

@@ -373,7 +375,7 @@ func InitializePullRequests(ctx context.Context) {
373375
case <-ctx.Done():
374376
return
375377
default:
376-
addPullRequestToCheckQueue(prID)
378+
AddPullRequestToCheckQueue(prID)
377379
}
378380
}
379381
}

tests/integration/pull_status_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)