Skip to content

Commit 69fb112

Browse files
committed
Always set the merge base used to merge the commit (go-gitea#15352)
Backport go-gitea#15352 The issue is that the TestPatch will reset the PR MergeBase - and it is possible for TestPatch to update the MergeBase whilst a merge is ongoing. The ensuing merge will then complete but it doesn't re-set the MergeBase it used to merge the PR. Fixes the intermittent error in git test. Signed-off-by: Andrew Thornton [email protected]
1 parent 67a12b8 commit 69fb112

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

integrations/api_helper_for_declarative_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,26 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
239239
}
240240
}
241241

242+
func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) (api.PullRequest, error) {
243+
return func(t *testing.T) (api.PullRequest, error) {
244+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s",
245+
owner, repo, index, ctx.Token)
246+
req := NewRequest(t, http.MethodGet, urlStr)
247+
248+
expected := 200
249+
if ctx.ExpectedCode != 0 {
250+
expected = ctx.ExpectedCode
251+
}
252+
resp := ctx.Session.MakeRequest(t, req, expected)
253+
254+
json := jsoniter.ConfigCompatibleWithStandardLibrary
255+
decoder := json.NewDecoder(resp.Body)
256+
pr := api.PullRequest{}
257+
err := decoder.Decode(&pr)
258+
return pr, err
259+
}
260+
}
261+
242262
func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) {
243263
return func(t *testing.T) {
244264
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s",

integrations/git_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integrations
66

77
import (
8+
"encoding/hex"
89
"fmt"
910
"io/ioutil"
1011
"math/rand"
@@ -451,26 +452,34 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
451452

452453
// Then get the diff string
453454
var diffHash string
455+
var diffLength int
454456
t.Run("GetDiff", func(t *testing.T) {
455457
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index))
456458
resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK)
457459
diffHash = string(resp.Hash.Sum(nil))
460+
diffLength = resp.Length
458461
})
459462

460463
// Now: Merge the PR & make sure that doesn't break the PR page or change its diff
461464
t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index))
462465
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
463-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
466+
t.Run("CheckPR", func(t *testing.T) {
467+
oldMergeBase := pr.MergeBase
468+
pr2, err := doAPIGetPullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index)(t)
469+
assert.NoError(t, err)
470+
assert.Equal(t, oldMergeBase, pr2.MergeBase)
471+
})
472+
t.Run("EnsurDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength))
464473

465474
// Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
466475
t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch))
467476
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
468-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
477+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength))
469478

470479
// Delete the head repository & make sure that doesn't break the PR page or change its diff
471480
t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx))
472481
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
473-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
482+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength))
474483
}
475484
}
476485

@@ -514,14 +523,15 @@ func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.
514523
}
515524
}
516525

517-
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffHash string) func(t *testing.T) {
526+
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffHash string, diffLength int) func(t *testing.T) {
518527
return func(t *testing.T) {
519528
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
520529
resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK)
521530
actual := string(resp.Hash.Sum(nil))
531+
actualLength := resp.Length
522532

523533
equal := diffHash == actual
524-
assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s but was actually: %s", diffHash, actual)
534+
assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s size: %d but was actually: %s size: %d", hex.EncodeToString([]byte(diffHash)), diffLength, hex.EncodeToString([]byte(actual)), actualLength)
525535
}
526536
}
527537

models/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ func (pr *PullRequest) SetMerged() (bool, error) {
406406
return false, fmt.Errorf("Issue.changeStatus: %v", err)
407407
}
408408

409-
if _, err := sess.Where("id = ?", pr.ID).Cols("has_merged, status, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
409+
// We need to save all of the data used to compute this merge as it may have already been changed by TestPatch. FIXME: need to set some state to prevent TestPatch from running whilst we are merging.
410+
if _, err := sess.Where("id = ?", pr.ID).Cols("has_merged, status, merge_base, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
410411
return false, fmt.Errorf("Failed to update pr[%d]: %v", pr.ID, err)
411412
}
412413

modules/queue/manager.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func (m *Manager) FlushAll(baseCtx context.Context, timeout time.Duration) error
174174
default:
175175
}
176176
mqs := m.ManagedQueues()
177+
log.Debug("Found %d Managed Queues", len(mqs))
177178
wg := sync.WaitGroup{}
178179
wg.Add(len(mqs))
179180
allEmpty := true
@@ -184,6 +185,7 @@ func (m *Manager) FlushAll(baseCtx context.Context, timeout time.Duration) error
184185
}
185186
allEmpty = false
186187
if flushable, ok := mq.Managed.(Flushable); ok {
188+
log.Debug("Flushing (flushable) queue: %s", mq.Name)
187189
go func(q *ManagedQueue) {
188190
localCtx, localCancel := context.WithCancel(ctx)
189191
pid := q.RegisterWorkers(1, start, hasTimeout, end, localCancel, true)
@@ -196,7 +198,11 @@ func (m *Manager) FlushAll(baseCtx context.Context, timeout time.Duration) error
196198
wg.Done()
197199
}(mq)
198200
} else {
199-
wg.Done()
201+
log.Debug("Queue: %s is non-empty but is not flushable - adding 100 millisecond wait", mq.Name)
202+
go func() {
203+
<-time.After(100 * time.Millisecond)
204+
wg.Done()
205+
}()
200206
}
201207

202208
}

services/pull/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Merge(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repositor
6464
pr.Merger = doer
6565
pr.MergerID = doer.ID
6666

67-
if _, err = pr.SetMerged(); err != nil {
67+
if _, err := pr.SetMerged(); err != nil {
6868
log.Error("setMerged [%d]: %v", pr.ID, err)
6969
}
7070

0 commit comments

Comments
 (0)