From a414509a94562e484f8801af613f3c61af9cf6b4 Mon Sep 17 00:00:00 2001 From: Rodin Date: Sat, 9 May 2026 18:08:06 -0700 Subject: [PATCH] fix: skip posting review when HEAD moves during evaluation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a new push arrives while review-bot is processing, the review would be posted against a stale commit. This causes noise in the PR timeline with findings that reference code that no longer exists. Before posting, re-fetch PR metadata and compare HEAD SHA with the commit we evaluated against. If they differ, log a warning and exit successfully — a new workflow run should already be processing the new HEAD. Fixes #52 --- cmd/review-bot/main.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmd/review-bot/main.go b/cmd/review-bot/main.go index 021ccc3..82e4575 100644 --- a/cmd/review-bot/main.go +++ b/cmd/review-bot/main.go @@ -315,6 +315,23 @@ func main() { sentinel := fmt.Sprintf("", *reviewerName) + // Stale check: verify HEAD hasn't moved since we started + // Re-fetch PR metadata to get the current HEAD SHA + evaluatedSHA := pr.Head.Sha + currentPR, err := giteaClient.GetPullRequest(ctx, owner, repoName, prNumber) + if err != nil { + slog.Warn("could not re-fetch PR for stale check", "pr", prNumber, "error", err) + // Continue anyway — better to post a potentially stale review than fail + } else if currentPR.Head.Sha != evaluatedSHA { + slog.Warn("HEAD moved during review — skipping stale review", + "evaluated", evaluatedSHA, + "current", currentPR.Head.Sha, + "pr", prNumber) + // Exit successfully — this isn't an error, just outdated work + // A new workflow run should already be in progress for the new HEAD + return + } + // Map findings to inline comments for lines present in the diff diffRanges := gitea.ParseDiffNewLines(diff) var inlineComments []gitea.ReviewComment