Skip to content

Commit 84f5a0b

Browse files
authored
Always set the merge base used to merge the commit (#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 e375cbf commit 84f5a0b

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"
@@ -452,26 +453,34 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
452453

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

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

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

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

@@ -515,14 +524,15 @@ func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.
515524
}
516525
}
517526

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

524534
equal := diffHash == actual
525-
assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s but was actually: %s", diffHash, actual)
535+
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)
526536
}
527537
}
528538

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)