Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0221b14
🐛 fix: measure empty-diff head age from the head commit, not updated_at
hivecommons-hive[bot] Oct 30, 2026
2b513fd
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
560dd75
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
1e73950
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
7018ace
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
d8ee9bb
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
febcac6
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
bb7a3ac
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
ae4130d
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
442c134
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
37a1289
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
ca0243c
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
77a7b80
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
b949d99
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
e1f5201
fix: floor empty-diff head age at the PR's created_at
hivecommons-hive[bot] Oct 30, 2026
7720fd4
Merge remote-tracking branch 'origin/v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
ba2a98a
🐛 fix: floor the empty-diff head age at created_at; drop the extra Ge…
hivecommons-hive[bot] Oct 30, 2026
81b4157
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
a00e646
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
2328431
🐛 fix: measure an on-base head's age from the merge-base commit
hivecommons-hive[bot] Oct 30, 2026
8570ea1
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
8d4da9c
Merge remote-tracking branch 'origin/v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
2b0f61e
Merge branch 'v5' into scanner/fix-15603
hivecommons-hive[bot] Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-15603-empty-diff-head-age.md
Original file line number Diff line number Diff line change
@@ -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))
29 changes: 20 additions & 9 deletions src/pkg/github/pr_empty_diff_hive_autoclose.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package github

import (
Expand Down Expand Up @@ -105,7 +105,9 @@
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
Expand Down Expand Up @@ -185,7 +187,8 @@
// 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())
Expand All @@ -201,6 +204,9 @@
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
}
Expand Down Expand Up @@ -302,7 +308,7 @@
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 {
Expand All @@ -325,9 +331,7 @@
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
Expand Down Expand Up @@ -419,10 +423,17 @@
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
Expand Down
63 changes: 63 additions & 0 deletions src/pkg/github/pr_empty_diff_hive_autoclose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package github
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -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.
Expand Down
Loading