Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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-tracker-adoption-lineage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Stage tracker rows and decision previews now identify the original contributor PR when Hive takes it over, including when the issue already links directly to the adopted copy ([#15396](https://github.com/hivecommons/hive/issues/15396)).
19 changes: 19 additions & 0 deletions src/pkg/dashboard/stage_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ type StageTrackerItem struct {
// closed, unmerged, and never a tracker row; this is the only trace of
// it the tracker draws — a small chip on the live PR.
Supersedes *StageTrackerSupersedes `json:"supersedes,omitempty"`
Lineage *StageTrackerLineage `json:"lineage,omitempty"`
// Adoption is, on a nudged PR at stage/072, whether adoption is due,
// what holds it, and the author's live presence, with the plain-words
// line the row shows instead of a bare "Returned to contributor"
Expand Down Expand Up @@ -647,6 +648,7 @@ type StageTrackerLinkedPR struct {
Takeover *StageTrackerTakeover `json:"takeover,omitempty"`
// Supersedes mirrors the nested PR's own row field.
Supersedes *StageTrackerSupersedes `json:"supersedes,omitempty"`
Lineage *StageTrackerLineage `json:"lineage,omitempty"`
}

// StageTrackerSupersedes is the closed original a live PR replaced
Expand Down Expand Up @@ -2082,6 +2084,8 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto
prState := s.stageTrackerPRStateResolver(status, resp.RecentlyMerged)
prOpen := func(repo string, number int) bool { return prState(repo, number) == blockedOnPRStateOpen }
supersedes := stageTrackerSupersedesIndex(store, status.GitHubBaseURL)
lineage := stageTrackerLineageIndex(store, status.GitHubBaseURL)
lineageAuthors := map[string]github.PullRequest{}
issueFilter := s.issueFilterConfig()
addIssue := func(repoName string, issue github.Issue, bucket, bucketReason string, progress *github.IssueChildren) {
// A closed issue has no stage; a stage label it closed wearing is
Expand Down Expand Up @@ -2155,6 +2159,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto
}
addPR := func(repoName string, pr github.PullRequest, verdict *github.MergeVerdict, bucket, bucketReason string) {
pr.Repo = nonEmpty(pr.Repo, repoName)
lineageAuthors[stagelabels.Key(repoName, pr.Number)] = pr
rec, ok := lookup(repoName, pr.Number)
if ok && rec.Superseded() {
// hivecommons/hive#14471: the hive's adoption of this PR merged
Expand All @@ -2166,6 +2171,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto
Labels: append([]string(nil), pr.Labels...), CreatedAt: timePtr(pr.CreatedAt), UpdatedAt: timePtr(pr.UpdatedAt),
Bucket: bucket, BucketReason: bucketReason,
URL: overviewItemURL(pr.URL, pr.Repo, pr.Number, "pr", status.GitHubBaseURL)}
it.Lineage = stageTrackerPRLineage(lineage, repoName, pr, status.GitHubBaseURL)
it.BaseRef = pr.BaseRef
it.AuthorKind, it.AuthorKindSource = stageTrackerAuthorKind(pr.Labels, pr.Author, pr.AppAuthored, pr.HiveAttributed)
if it.AuthorKindSource == authorKindSrcLabel {
Expand Down Expand Up @@ -2227,6 +2233,9 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto
it.Caption = joinClause(it.Caption, clause)
}
}
if it.Lineage != nil {
it.Caption += fmt.Sprintf(" (adopted from #%d)", it.Lineage.FromNumber)
}
it.DecisionNeeded = stageTrackerDecisionNeededWith(it, agg, sentinelLabel, now, bctx)
it.StageActor = stageTrackerStageActor(it, workers, now)
add(it)
Expand Down Expand Up @@ -2256,6 +2265,15 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto
}
}
}
for i := range resp.Items {
it := &resp.Items[i]
if l := it.Lineage; l != nil {
if original, ok := lineageAuthors[stagelabels.Key(it.Repo, l.FromNumber)]; ok {
l.FromAuthor = nonEmpty(original.Author, l.FromAuthor)
l.FromAuthorKind, _ = stageTrackerAuthorKind(original.Labels, original.Author, original.AppAuthored, original.HiveAttributed)
}
}
}
resp.OpenIssues, resp.OpenPRs = stageTrackerOpenCounts(resp.Items)
stageTrackerMergeTrainPostPass(resp.Items)
s.applyOperatorActions(resp.Items, now)
Expand Down Expand Up @@ -3005,6 +3023,7 @@ func stageTrackerResolveLinkedPRs(items []StageTrackerItem, open func(repo strin
lp.Rework = pr.Rework
lp.Takeover = pr.Takeover
lp.Supersedes = pr.Supersedes
lp.Lineage = pr.Lineage
lp.HumanApprover, lp.ManualMerge = pr.HumanApprover, pr.ManualMerge
lp.HumanMergeReason, lp.HumanMergeReasonText = pr.HumanMergeReason, pr.HumanMergeReasonText
lp.AdmittedBy, lp.DCOUnsigned, lp.DCOTotal = pr.AdmittedBy, pr.DCOUnsigned, pr.DCOTotal
Expand Down
69 changes: 69 additions & 0 deletions src/pkg/dashboard/stage_tracker_lineage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dashboard

import (
"strconv"
"strings"
"time"

"github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/stagelabels"
)

// StageTrackerLineage describes the provenance of the live copy, independently
// of whether the original is still linked, closed, or its copy has merged.
type StageTrackerLineage struct {
Kind string `json:"kind"`
FromNumber int `json:"from_number"`
FromURL string `json:"from_url"`
FromAuthor string `json:"from_author,omitempty"`
FromAuthorKind string `json:"from_author_kind,omitempty"`
ToNumber int `json:"to_number"`
ToURL string `json:"to_url"`
At *time.Time `json:"at,omitempty"`
Reason string `json:"reason,omitempty"`
}

func stageTrackerLineageIndex(store *stagelabels.Store, baseURL string) map[string]StageTrackerLineage {
out := map[string]StageTrackerLineage{}
if store == nil {
return out
}
for _, key := range store.Keys() {
rec, ok := store.Get(key)
if !ok || rec.AdoptedBy <= 0 {
continue
}
repo, text, found := strings.Cut(key, "#")
number, err := strconv.Atoi(text)
if !found || err != nil || repo == "" || number <= 0 || number == rec.AdoptedBy {
continue
}
by := stagelabels.Key(repo, rec.AdoptedBy)
if prev, exists := out[by]; exists && prev.FromNumber <= number {
continue
}
l := StageTrackerLineage{Kind: "adopted", FromNumber: number, FromURL: overviewItemURL("", repo, number, "pr", baseURL), FromAuthor: rec.Author, ToNumber: rec.AdoptedBy, ToURL: overviewItemURL("", repo, rec.AdoptedBy, "pr", baseURL), Reason: rec.AdoptReason}
if !rec.AdoptedAt.IsZero() {
at := rec.AdoptedAt
l.At = &at
}
out[by] = l
}
return out
}

// No network lookup: an App-owned adoption branch is enough to retain the
// original reference when the stage record has expired. Unknown facts stay empty.
func stageTrackerPRLineage(index map[string]StageTrackerLineage, repo string, pr github.PullRequest, baseURL string) *StageTrackerLineage {
if l, ok := index[stagelabels.Key(repo, pr.Number)]; ok {
return &l
}
if !pr.AppAuthored || !strings.HasPrefix(pr.HeadRef, "hive/adopt-") {
return nil
}
number, err := strconv.Atoi(strings.TrimPrefix(pr.HeadRef, "hive/adopt-"))
if err != nil || number <= 0 || number == pr.Number {
return nil
}
return &StageTrackerLineage{Kind: "adopted", FromNumber: number, FromURL: overviewItemURL("", repo, number, "pr", baseURL), ToNumber: pr.Number, ToURL: overviewItemURL(pr.URL, repo, pr.Number, "pr", baseURL)}
}
153 changes: 153 additions & 0 deletions src/pkg/dashboard/stage_tracker_lineage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package dashboard

import (
"encoding/json"
"strings"
"testing"
"time"

"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/stagelabels"
)

func TestStageTrackerLiveAdoptionLineage(t *testing.T) {
now := time.Date(2026, 10, 29, 10, 56, 0, 0, time.UTC)
for _, closeFirst := range []bool{false, true} {
t.Run(map[bool]string{false: "original-open", true: "close-first"}[closeFirst], func(t *testing.T) {
s := newTestServer()
store := stagelabels.NewStore("", func() time.Time { return now })
s.stageStore = store
s.deps = &Dependencies{Config: &config.Config{}}
key := stagelabels.Key("hivecommons/hive", 15226)
store.Observe(key, stagelabels.AdoptedByHive, "contributor-lane:adopted")
store.NoteAuthor(key, "thatguywob")
store.MarkAdopted(key, "oldhead", 15308, now.Add(-6*time.Hour))
store.NoteAdoptReason(key, "contributor-offline")
if closeFirst {
store.MarkSupersededClosedFirst(key, 15308, false, now.Add(-6*time.Hour))
}
status := minimalPayload()
status.Repos = []FrontendRepo{{Name: "hive", Full: "hivecommons/hive",
ActionableIssues: []any{github.Issue{Number: 15198, Title: "Fix this", Labels: []string{"stage/040-covered-by-pr"}, LinkedPRs: []github.IssueLinkedPR{{Number: 15308, State: "open", Closing: true}}}},
OpenPrs: []any{FrontendPR{PullRequest: github.PullRequest{Number: 15308, Author: "hivecommons-hive[bot]", AppAuthored: true, Title: "Adopted fix", Labels: []string{"stage/070-fixing"}, LinkedIssues: []github.PRLinkedIssue{{Number: 15198, State: "open"}}}}},
}}
if !closeFirst {
status.Repos[0].OpenPrs = append(status.Repos[0].OpenPrs, FrontendPR{PullRequest: github.PullRequest{Number: 15226, Author: "thatguywob", Labels: []string{"author/clanker", "stage/074-adopted-by-hive"}}})
}
s.UpdateStatus(status)
resp := s.buildStageTracker(status, store, now)
pr := trackerItem(t, resp, "pr", 15308)
issue := trackerItem(t, resp, "issue", 15198)
l := pr.Lineage
if l == nil || l.Kind != "adopted" || l.FromNumber != 15226 || l.FromAuthor != "thatguywob" || l.FromURL != "https://github.com/hivecommons/hive/pull/15226" || l.ToNumber != 15308 || l.ToURL != "https://github.com/hivecommons/hive/pull/15308" || l.At == nil || !l.At.Equal(now.Add(-6*time.Hour)) || l.Reason != "contributor-offline" {
t.Fatalf("lineage = %+v", l)
}
if !closeFirst && l.FromAuthorKind != "clanker" {
t.Fatalf("cached original kind = %q", l.FromAuthorKind)
}
if len(issue.LinkedPRs) != 1 || issue.LinkedPRs[0].Lineage != l {
t.Fatalf("linked lineage = %+v", issue.LinkedPRs)
}
if !strings.Contains(pr.Caption, "(adopted from #15226)") || !strings.Contains(issue.Caption, "(adopted from #15226)") {
t.Fatalf("captions: %q / %q", pr.Caption, issue.Caption)
}
raw, err := json.Marshal(resp)
if err != nil {
t.Fatal(err)
}
if strings.Count(string(raw), `"lineage":{"kind":"adopted"`) != 2 {
t.Fatalf("missing top-level or linked lineage: %s", raw)
}
})
}
}

func TestStageTrackerLineageRecordWinsAndIsRepoScoped(t *testing.T) {
now := time.Date(2026, 10, 29, 4, 56, 0, 0, time.UTC)
store := stagelabels.NewStore("", func() time.Time { return now })
key := stagelabels.Key("acme/repo", 7)
store.Observe(key, stagelabels.AdoptedByHive, "adopted")
store.MarkAdopted(key, "head", 9, now)
index := stageTrackerLineageIndex(store, "")
pr := github.PullRequest{Number: 9, AppAuthored: true, HeadRef: "hive/adopt-8"}
if l := stageTrackerPRLineage(index, "acme/repo", pr, ""); l == nil || l.FromNumber != 7 || l.At == nil {
t.Fatalf("record did not win: %+v", l)
}
pr.HeadRef = "ordinary"
if l := stageTrackerPRLineage(index, "another/repo", pr, ""); l != nil {
t.Fatalf("cross-repo lineage: %+v", l)
}
}

func TestStageTrackerLineageBranchFallback(t *testing.T) {
for _, tc := range []struct {
branch string
app bool
want bool
}{
{"hive/adopt-15226", true, true}, {"hive/adopt-15226", false, false},
{"hive/adopt-0", true, false}, {"hive/adopt-abc", true, false},
{"hive/adopt-15226-extra", true, false}, {"hive/adopt-15308", true, false},
{"ordinary", true, false},
} {
pr := github.PullRequest{Number: 15308, AppAuthored: tc.app, HeadRef: tc.branch}
l := stageTrackerPRLineage(nil, "acme/repo", pr, "https://git.example")
if (l != nil) != tc.want {
t.Fatalf("%+v: %+v", tc, l)
}
if l != nil && (l.FromURL != "https://git.example/acme/repo/pull/15226" || l.FromAuthor != "" || l.At != nil || l.Reason != "") {
t.Fatalf("fallback fabricated facts: %+v", l)
}
}
if len(stageTrackerLineageIndex(nil, "")) != 0 {
t.Fatal("nil store")
}
}

func TestStageTrackerLineageJS(t *testing.T) {
html := indexHTML(t)
script := strings.Join([]string{
"const assert = require('assert');",
jsFunc(t, html, "esc"), jsFunc(t, html, "stageTrackerAgoText"),
jsFunc(t, html, "stageTrackerLineageClause"), jsFunc(t, html, "stageTrackerLineageChipHtml"),
jsFunc(t, html, "stageTrackerSupersedesTip"), jsFunc(t, html, "stageTrackerSupersedesChipHtml"),
jsFunc(t, html, "stageTrackerLinkedPRItem"), jsFunc(t, html, "stageTrackerLinkedPROpen"),
jsFunc(t, html, "stageTrackerFetchDelegatePlan"),
"function stageTrackerActionRequest() { return {url:'/plan', body:{}}; }",
"function stageTrackerRefOf(it) { return '#' + it.number; }",
"const STAGE_TRACKER_TERMINAL = 100;",
`
const now = Date.parse('2026-10-29T10:56:00Z');
const l = {kind:'adopted', from_number:15226, from_url:'https://github.com/hivecommons/hive/pull/15226', from_author:'thatguywob', at:'2026-10-29T04:56:00Z', reason:'contributor-offline'};
const pr = {kind:'pr', number:15308, stage:70, lineage:l};
assert.strictEqual(stageTrackerLineageClause(pr), '(adopted from #15226)');
assert.strictEqual(stageTrackerLineageClause({}), '');
assert.strictEqual(stageTrackerLineageChipHtml({}, now), '');
const chip = stageTrackerLineageChipHtml(pr, now);
assert.ok(chip.includes("adopted from @thatguywob&#39;s #15226 · 6h ago"), chip);
assert.ok(chip.includes('href="' + l.from_url + '"'), chip);
assert.ok(chip.includes('Original commits kept.'), chip);
assert.ok(chip.includes('after CI went red'), chip);
assert.strictEqual(stageTrackerSupersedesChipHtml(Object.assign({supersedes:{number:15226}}, pr), now), '');
const sub = stageTrackerLinkedPRItem({repo:'hivecommons/hive', number:15198, linked_prs:[pr]}, pr);
assert.strictEqual(sub.lineage, l);
assert.strictEqual(stageTrackerLinkedPRItem({number:1}, {number:2}).lineage, null);
const unknown = stageTrackerLineageChipHtml({lineage:{kind:'adopted', from_number:7}}, now);
assert.ok(!unknown.includes('undefined') && !unknown.includes('after CI went red'), unknown);
const hostile = stageTrackerLineageChipHtml({lineage:Object.assign({}, l, {from_author:'<img>', from_url:'https://example.org/"x'})}, now);
assert.ok(!hostile.includes('<img>') && hostile.includes('&lt;img&gt;'), hostile);
global.fetch = async () => ({ok:true, json:async () => ({ref:'server-ref', number:15308})});
(async () => {
const plan = await stageTrackerFetchDelegatePlan(pr);
assert.strictEqual(plan.ref, 'server-ref (adopted from #15226)');
})().catch(e => { console.error(e); process.exit(1); });
`,
}, "\n")
runStageTrackerRowActionsScript(t, script)
for _, want := range []string{"stageTrackerLineageChipHtml(pr, nowMs)", "stageTrackerLineageChipHtml(item, nowMs)", "stageTrackerLineageChipHtml(lp, nowMs)", "(lineage ? ' ' + lineage : '')"} {
if !strings.Contains(html, want) {
t.Errorf("lineage not wired: %s", want)
}
}
}
Loading
Loading