From 3d697c0f52c728b3bd1f0b279aab7ddbb7d212f8 Mon Sep 17 00:00:00 2001 From: Jahon A Date: Wed, 3 Sep 2025 11:53:11 -0700 Subject: [PATCH] Add delay between enabling auto merge and approving the PR --- pullrequest/review/reviewer.go | 2 ++ pullrequest/review/reviewer_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/pullrequest/review/reviewer.go b/pullrequest/review/reviewer.go index a547daf0..67e9f110 100644 --- a/pullrequest/review/reviewer.go +++ b/pullrequest/review/reviewer.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "strings" + "time" "github.com/go-chi/httplog" pe "github.com/marqeta/pr-bot/errors" @@ -44,6 +45,7 @@ func (r *reviewer) Approve(ctx context.Context, id id.PR, body string, opts Appr return r.handleAutoMergeError(ctx, id, err) } oplog.Info().Msgf("enabled auto merge on PR") + time.Sleep(500 * time.Millisecond) err = r.api.AddReview(ctx, id, body, gh.Approve) if err != nil { oplog.Err(err).Msgf("error approving PR") diff --git a/pullrequest/review/reviewer_test.go b/pullrequest/review/reviewer_test.go index 3b508e3e..5fd69bdb 100644 --- a/pullrequest/review/reviewer_test.go +++ b/pullrequest/review/reviewer_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "testing" + "time" gh "github.com/marqeta/pr-bot/github" "github.com/marqeta/pr-bot/id" @@ -176,6 +177,32 @@ func Test_reviewer_Approve(t *testing.T) { } } +func Test_reviewer_Approve_Sleep(t *testing.T) { + ctx := context.Background() + mockAPI := gh.NewMockAPI(t) + metrics := metrics.NewNoopEmitter() + r := review.NewReviewer(mockAPI, metrics) + + // Set up expectations + mockAPI.EXPECT().EnableAutoMerge(ctx, sampleID(), githubv4.PullRequestMergeMethodMerge).Return(nil) + mockAPI.EXPECT().AddReview(ctx, sampleID(), "LGTM", gh.Approve).Return(nil) + + // Measure the time to verify sleep + start := time.Now() + err := r.Approve(ctx, sampleID(), "LGTM", review.ApproveOptions{ + MergeMethod: githubv4.PullRequestMergeMethodMerge, + }) + duration := time.Since(start) + + // Verify no error and sleep duration is at least 500ms + if err != nil { + t.Errorf("Approve() error = %v, want nil", err) + } + if duration < 500*time.Millisecond { + t.Errorf("Sleep duration = %v, want at least 500ms", duration) + } +} + func Test_reviewer_Comment(t *testing.T) { ctx := context.Background()