diff --git a/changelog.d/fixed-15603-empty-diff-head-age.md b/changelog.d/fixed-15603-empty-diff-head-age.md new file mode 100644 index 0000000000..e3b4e677e0 --- /dev/null +++ b/changelog.d/fixed-15603-empty-diff-head-age.md @@ -0,0 +1 @@ +- Empty-diff autoclose head age is floored at the PR's `created_at`, so a PR just opened from an old branch keeps its just-opened grace period, and a head already on the base is measured from its own commit date rather than the label-bumped `updated_at` ([#15603](https://github.com/hivecommons/hive/issues/15603)) diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose.go b/src/pkg/github/pr_empty_diff_hive_autoclose.go index fa8264ba28..9af329f4e4 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose.go @@ -105,7 +105,9 @@ type HiveEmptyDiffAutoCloseFacts struct { DiffKnown bool DiffFiles int // HeadAge is now minus the head commit's date when the compare - // reported one, else minus the PR's last update (or creation). + // reported one, else minus the PR's last update (or creation). It + // never exceeds the PR's own age: a PR just opened from an old branch + // is still a just-opened PR. HeadAge time.Duration // ReviewerConfirmed: the reviewer verdict for this very head says the // change is already on the base. The head has been read, so the @@ -185,7 +187,8 @@ func PRChangedFilesClaimsEmpty(pr *gh.PullRequest) bool { // prDiffAgainstBase measures the PR's diff as GitHub shows it: the compare // base...head. known is false when the compare failed or the file list // was cut short. headAt is the newest commit's committer date when the -// compare listed commits (zero otherwise). +// compare listed commits; when it listed none the head is already on the +// base, so it is the merge base's date. Zero when neither is readable. func (c *Client) prDiffAgainstBase(ctx context.Context, owner, name string, pr *gh.PullRequest) (files []*gh.CommitFile, headAt time.Time, known bool) { base := strings.TrimSpace(pr.GetBase().GetRef()) head := strings.TrimSpace(pr.GetHead().GetSHA()) @@ -201,6 +204,9 @@ func (c *Client) prDiffAgainstBase(ctx context.Context, owner, name string, pr * headAt = at } } + if mb := comparison.GetMergeBaseCommit(); len(comparison.Commits) == 0 && strings.EqualFold(mb.GetSHA(), head) { + headAt = mb.GetCommit().GetCommitter().GetDate().Time + } if (resp != nil && resp.NextPage != 0) || len(comparison.Files) >= githubCompareFileLimit { return comparison.Files, headAt, false } @@ -302,7 +308,7 @@ func (c *Client) closeHiveRedundantPR(ctx context.Context, repo string, pr *gh.P HiveAuthored: identity.Matches(safeGetLogin(pr.GetUser())), Held: c.isHeld(labels) || c.isExempt(labels), KeptOpen: supersessionHasLabel(pr, SupersessionKeepOpenLabel), - HeadAge: emptyDiffHeadAge(pr, now), + HeadAge: emptyDiffHeadAge(pr, time.Time{}, now), ReviewerConfirmed: reviewed, } if pr.ChangedFiles != nil && *pr.ChangedFiles > 0 && !reviewed { @@ -325,9 +331,7 @@ func (c *Client) closeHiveRedundantPR(ctx context.Context, repo string, pr *gh.P if !facts.DiffKnown { files, headAt, known := c.prDiffAgainstBase(ctx, owner, name, pr) facts.DiffFiles, facts.DiffKnown = len(files), known - if !headAt.IsZero() { - facts.HeadAge = now.Sub(headAt) - } + facts.HeadAge = emptyDiffHeadAge(pr, headAt, now) if known && len(files) > 0 && reviewed { match, matchKnown := c.prFilesIdenticalOnBase(ctx, owner, name, strings.TrimSpace(pr.GetBase().GetRef()), files) facts.TreeMatchesBase = match && matchKnown @@ -419,10 +423,17 @@ func alreadyLandedIssueNote(pr, redundantOf int, base string) string { return fmt.Sprintf("The hive closed its own #%d as superseded: the change it carried is already on `%s`, so this issue is done. Reopen if that's wrong.", pr, base) } -func emptyDiffHeadAge(pr *gh.PullRequest, now time.Time) time.Duration { - at := pr.GetUpdatedAt().Time +// emptyDiffHeadAge measures from the compare's newest commit (headAt) when +// it is known; updated_at, which labels and comments bump, stands in only +// when it is not. The age never exceeds the PR's own age: a PR just opened +// from an old branch is still a just-opened PR whose commits may be arriving. +func emptyDiffHeadAge(pr *gh.PullRequest, headAt, now time.Time) time.Duration { + at := headAt if at.IsZero() { - at = pr.GetCreatedAt().Time + at = pr.GetUpdatedAt().Time + } + if created := pr.GetCreatedAt().Time; !created.IsZero() && created.After(at) { + at = created } if at.IsZero() { return 0 diff --git a/src/pkg/github/pr_empty_diff_hive_autoclose_test.go b/src/pkg/github/pr_empty_diff_hive_autoclose_test.go index 52c5b858fd..de4a71539a 100644 --- a/src/pkg/github/pr_empty_diff_hive_autoclose_test.go +++ b/src/pkg/github/pr_empty_diff_hive_autoclose_test.go @@ -3,6 +3,7 @@ package github import ( "context" "encoding/json" + "log/slog" "net/http" "net/http/httptest" "reflect" @@ -116,6 +117,68 @@ func TestReviewRedundantOfNumber(t *testing.T) { } } +// hivecommons/hive#15603: updated_at is bumped by labels and comments, so a +// recently touched PR with an old head must still count as old — and a PR +// just opened from an old branch must still count as just opened. +func TestEmptyDiffHeadAgeFlooredAtCreatedAt(t *testing.T) { + now := time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC) + pr := &gh.PullRequest{UpdatedAt: &gh.Timestamp{Time: now.Add(-time.Minute)}} + if got := emptyDiffHeadAge(pr, now.Add(-time.Hour), now); got != time.Hour { + t.Fatalf("head age = %v, want 1h from the head commit", got) + } + if got := emptyDiffHeadAge(pr, time.Time{}, now); got != time.Minute { + t.Fatalf("head age = %v, want 1m from updated_at fallback", got) + } + // A PR just opened from an old branch: its head commit predates the PR, + // so the age is the PR's own age and the just-opened grace period holds. + justOpened := &gh.PullRequest{ + CreatedAt: &gh.Timestamp{Time: now.Add(-2 * time.Minute)}, + UpdatedAt: &gh.Timestamp{Time: now.Add(-time.Minute)}, + } + if got := emptyDiffHeadAge(justOpened, now.Add(-time.Hour), now); got != 2*time.Minute { + t.Fatalf("head age = %v, want 2m floored at created_at", got) + } + // A head commit pushed after the PR opened measures from the commit. + if got := emptyDiffHeadAge(justOpened, now.Add(-90*time.Second), now); got != 90*time.Second { + t.Fatalf("head age = %v, want 90s from the head commit", got) + } + // No commit date and no created_at: updated_at still stands in. + if got := emptyDiffHeadAge(&gh.PullRequest{}, time.Time{}, now); got != 0 { + t.Fatalf("head age = %v, want 0 with no timestamps", got) + } +} + +// A head already on the base lists no commits in base...head, so the +// compare's commit dates are empty; the merge base is the head itself and +// its date must stand in, not updated_at (hivecommons/hive#15603). +func TestPRDiffAgainstBaseHeadOnBaseUsesMergeBaseDate(t *testing.T) { + headDate := time.Date(2026, 10, 29, 11, 0, 0, 0, time.UTC) + mux := http.NewServeMux() + mux.HandleFunc("/repos/hivecommons/hive/compare/v5...head999", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{ + "status": "behind", + "commits": []any{}, + "files": []any{}, + "merge_base_commit": map[string]any{ + "sha": "head999", + "commit": map[string]any{"committer": map[string]any{"date": headDate.Format(time.RFC3339)}}, + }, + }) + }) + ts := httptest.NewServer(mux) + defer ts.Close() + + c := NewClientForTest(ts.URL, "hivecommons", []string{"hive"}, slog.Default()) + pr := &gh.PullRequest{Base: &gh.PullRequestBranch{Ref: gh.Ptr("v5")}, Head: &gh.PullRequestBranch{SHA: gh.Ptr("head999")}} + files, headAt, known := c.prDiffAgainstBase(context.Background(), "hivecommons", "hive", pr) + if !known || len(files) != 0 { + t.Fatalf("diff = %d files, known=%v; want empty and known", len(files), known) + } + if !headAt.Equal(headDate) { + t.Fatalf("headAt = %v, want merge-base (head) date %v", headAt, headDate) + } +} + // hivecommons/hive#15689: CloseIssue's gates (here the tracking-issue // refusal) may keep an issue open; the "this issue is done" note must then // not be posted. A close that goes through still gets its note.