diff --git a/changelog.d/fixed-14966-prlink-cross-reference-mentions.md b/changelog.d/fixed-14966-prlink-cross-reference-mentions.md new file mode 100644 index 0000000000..6e3276c432 --- /dev/null +++ b/changelog.d/fixed-14966-prlink-cross-reference-mentions.md @@ -0,0 +1 @@ +- The dashboard no longer marks an issue as Reviewing just because an open pull request mentions it: a PR found by search or by a timeline cross-reference counts as the linked PR only when its title or body closes the issue (Closes/Fixes/Resolves); a PR that names the issue only in a PR comment is still linked ([#14966](https://github.com/hivecommons/hive/issues/14966)). diff --git a/src/pkg/dashboard/contribute_prlink.go b/src/pkg/dashboard/contribute_prlink.go index a963f3715e..9fceb6e0f8 100644 --- a/src/pkg/dashboard/contribute_prlink.go +++ b/src/pkg/dashboard/contribute_prlink.go @@ -8,6 +8,7 @@ import ( "time" gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/github" ) const ( @@ -127,15 +128,18 @@ func (r *prLinkResolver) resolve(ctx context.Context, repo string, number int) * return status } -// searchLinkedPR asks GitHub which PR (if any) references this issue. It runs one +// searchLinkedPR asks GitHub which PR (if any) fixes this issue. It runs one // Search.Issues query scoped to the issue's repo, matching PRs that mention the -// issue number — GitHub indexes the Fixes/Closes/Resolves cross-reference as well -// as any plain "#N" body/title mention, so the closing PR is included. A merged -// PR wins over a merely-open one (the fix landed), so results are scanned for a -// merged hit first, then an open one. When the search yields no merged PR it also -// reads the issue's timeline, whose cross-referenced events catch a PR that names -// the issue in its body or a comment. A nil client or empty result is -// "no linked PR" (nil, nil) — never an error the caller must handle. +// issue number — GitHub indexes any plain "#N" body/title mention, so each hit +// is then checked for a closing keyword (Closes/Fixes/Resolves #N) in its +// title or body; a passing "see also #N" is not a linked PR. A merged PR wins +// over a merely-open one (the fix landed), so results are scanned for a merged +// hit first, then an open one. When the search yields no merged PR it also +// reads the issue's timeline, whose cross-referenced events catch a PR that +// names the issue only in a PR comment; a timeline PR whose title/body does +// mention the issue is held to the same closing-keyword rule (prClosesIssue). +// A nil client or empty result is "no linked PR" (nil, nil) — never an error +// the caller must handle. func searchLinkedPR(ctx context.Context, gc *gh.Client, repo string, number int) (*prLinkStatus, error) { if gc == nil || repo == "" || number <= 0 { return nil, nil @@ -159,7 +163,7 @@ func searchLinkedPR(ctx context.Context, gc *gh.Client, repo string, number int) if iss == nil || iss.PullRequestLinks == nil { continue // search can return issues too; keep only PRs } - if !prReferencesIssue(iss, number) { + if !searchHitClosesIssue(iss, repo, number) { continue } st, merged := prLinkCandidate(iss) @@ -172,13 +176,16 @@ func searchLinkedPR(ctx context.Context, gc *gh.Client, repo string, number int) } } - // The title/body check above cannot see a PR that names the issue only in + // The search above cannot see a PR that names the issue only in // a PR comment (hivecommons/hive#14859). GitHub records such a mention // as a cross-referenced event on the issue's timeline, so one timeline // read covers body and comment references. Commit-message references // arrive as referenced events and are not matched here. A // timeline error keeps whatever the search already found. for _, iss := range crossReferencingPRs(ctx, gc, repo, number) { + if !prClosesIssue(iss, repo, number) { + continue + } st, merged := prLinkCandidate(iss) if merged { return st, nil @@ -256,11 +263,70 @@ func crossReferencingPRs(ctx context.Context, gc *gh.Client, repo string, number // cheap and within a single request. const prLinkSearchPerPage = 10 -// prReferencesIssue guards against a coincidental number collision: the Search -// "#N" match is broad, so confirm the PR actually names the issue as a reference -// (Fixes/Closes/Resolves #N, or a bare #N mention) in its title or body before -// treating it as the fix-PR. Missing body/title (search omits them on some -// responses) is treated as a match, since the number qualifier already scoped it. +// searchHitClosesIssue reports whether a Search.Issues hit should count as +// the issue's linked PR. The "#N" search qualifier indexes bare mentions, so +// the hit must name the issue with a closing keyword (Closes/Fixes/Resolves) +// in its title or body; a passing "see also #N" is not promoted. A hit with +// no title or body (omitted by the API on some responses) is accepted, since +// the number qualifier already scoped it. +func searchHitClosesIssue(iss *gh.Issue, repo string, number int) bool { + title, body := iss.GetTitle(), iss.GetBody() + if title == "" && body == "" { + return true + } + _, ok := github.FindClosingKeywordReference(title+"\n"+body, repo, repo, number) + return ok +} + +// prClosesIssue reports whether a timeline cross-referencing PR should count +// as the issue's linked PR. A PR whose title/body mentions the issue must name +// it as a fix (Closes/Fixes/Resolves), so a passing "see also #N" is not +// promoted. A PR whose title/body does not mention the issue at all reached +// the timeline through a PR comment (hivecommons/hive#14859), which is the +// case the timeline read exists for, so it is kept. A source with no title or +// body (omitted by the API) is accepted, as in searchHitClosesIssue. +func prClosesIssue(iss *gh.Issue, repo string, number int) bool { + title, body := iss.GetTitle(), iss.GetBody() + if title == "" && body == "" { + return true + } + text := title + "\n" + body + if !prMentionsIssue(text, repo, number) { + return true + } + _, ok := github.FindClosingKeywordReference(text, repo, repo, number) + return ok +} + +// prMentionsIssue reports whether text names repo#number in any of the forms +// GitHub turns into a cross-reference: a bare "#N", "owner/repo#N", or the +// issue's URL. +func prMentionsIssue(text, repo string, number int) bool { + lower := strings.ToLower(text) + for _, form := range []string{ + strings.ToLower(fmt.Sprintf("%s#%d", repo, number)), + strings.ToLower(fmt.Sprintf("github.com/%s/issues/%d", repo, number)), + } { + for i := strings.Index(lower, form); i >= 0; { + end := i + len(form) + if end == len(lower) || lower[end] < '0' || lower[end] > '9' { + return true + } + next := strings.Index(lower[end:], form) + if next < 0 { + break + } + i = end + next + } + } + return prReferencesIssue(&gh.Issue{Body: &text}, number) +} + +// prReferencesIssue reports whether the PR's title or body names the issue +// with a bare "#N" mention (any keyword or none). It backs prMentionsIssue, +// which decides whether a timeline PR reached the issue through its own text +// or only through a PR comment. Missing body/title (the API omits them on +// some responses) is treated as a match. func prReferencesIssue(iss *gh.Issue, number int) bool { needle := fmt.Sprintf("#%d", number) body := iss.GetBody() @@ -270,8 +336,8 @@ func prReferencesIssue(iss *gh.Issue, number int) bool { } hay := title + "\n" + body for _, tok := range strings.Fields(hay) { - // Trim trailing punctuation so "#123." / "#123," still match. - tok = strings.TrimRight(tok, ".,);:") + // Trim surrounding punctuation so "#123." / "#123," / "(#123)" still match. + tok = strings.TrimRight(strings.TrimLeft(tok, "([<"), ".,);:]>") if tok == needle { return true } diff --git a/src/pkg/dashboard/contribute_prlink_test.go b/src/pkg/dashboard/contribute_prlink_test.go index ee43519366..291afbe4b6 100644 --- a/src/pkg/dashboard/contribute_prlink_test.go +++ b/src/pkg/dashboard/contribute_prlink_test.go @@ -45,6 +45,8 @@ func TestPrReferencesIssue_TrailingPunctuation(t *testing.T) { {"Fixes #20,", 20}, {"See #30)", 30}, {"refs #50:", 50}, + {"follow-up (#60)", 60}, + {"see [#70]", 70}, } for _, tt := range tests { iss := &gh.Issue{Title: strPtr(""), Body: strPtr(tt.body)} @@ -396,6 +398,12 @@ func TestSearchLinkedPR_CountsCommitCommentAndTimelineReferences(t *testing.T) { openPR := `{"number":14656,"state":"open","html_url":"https://github.com/o/r/pull/14656","repository":{"full_name":"o/r"},"pull_request":{"url":"x"}}` mergedPR := `{"number":14656,"state":"closed","html_url":"https://github.com/o/r/pull/14656","repository":{"full_name":"o/r"},"pull_request":{"url":"x","merged_at":"2026-10-28T00:00:00Z"}}` otherRepoPR := `{"number":77,"state":"open","html_url":"https://github.com/x/y/pull/77","repository":{"full_name":"x/y"},"pull_request":{"url":"x"}}` + // A PR whose title/body is populated but never names the issue: it is on + // the timeline only because a PR comment mentioned the issue (#14859). + commentOnlyPR := `{"number":14656,"state":"open","title":"keep the header chips from overlapping the title","body":"Hive Agent","html_url":"https://github.com/o/r/pull/14656","repository":{"full_name":"o/r"},"pull_request":{"url":"x"}}` + commentOnlyMergedPR := `{"number":14656,"state":"closed","title":"keep the header chips from overlapping the title","body":"Hive Agent","html_url":"https://github.com/o/r/pull/14656","repository":{"full_name":"o/r"},"pull_request":{"url":"x","merged_at":"2026-10-28T00:00:00Z"}}` + mentionPR := `{"number":14700,"state":"open","title":"unrelated work","body":"see also #13135 for context","html_url":"https://github.com/o/r/pull/14700","repository":{"full_name":"o/r"},"pull_request":{"url":"x"}}` + closingPR := `{"number":14701,"state":"open","title":"fix it","body":"Closes #13135","html_url":"https://github.com/o/r/pull/14701","repository":{"full_name":"o/r"},"pull_request":{"url":"x"}}` plainIssue := `{"number":14859,"state":"open","html_url":"https://github.com/o/r/issues/14859","repository":{"full_name":"o/r"}}` cases := []struct { @@ -426,6 +434,58 @@ func TestSearchLinkedPR_CountsCommitCommentAndTimelineReferences(t *testing.T) { wantNumber: 14656, wantState: prLinkStateMerged, }, + { + name: "PR comment reference only (title/body populated, not naming the issue)", + search: `[` + searchHit + `]`, + timeline: `[` + crossRef(commentOnlyPR) + `]`, + wantNumber: 14656, + wantState: prLinkStateOpen, + }, + { + name: "PR comment reference only from a merged PR", + search: `[]`, + timeline: `[` + crossRef(commentOnlyMergedPR) + `]`, + wantNumber: 14656, + wantState: prLinkStateMerged, + }, + { + name: "comment-only PR beats a later mention-only PR", + search: `[]`, + timeline: `[` + crossRef(commentOnlyPR) + `,` + crossRef(mentionPR) + `]`, + wantNumber: 14656, + wantState: prLinkStateOpen, + }, + { + name: "cross-reference from a PR that only mentions the issue", + search: `[]`, + timeline: `[` + crossRef(mentionPR) + `]`, + }, + { + name: "search hit from a PR that only mentions the issue", + search: `[` + mentionPR + `]`, + timeline: `[]`, + }, + { + name: "search hit with a closing keyword", + search: `[` + closingPR + `]`, + timeline: `[]`, + wantNumber: 14701, + wantState: prLinkStateOpen, + }, + { + name: "mention-only search hit is skipped, closing search hit is chosen", + search: `[` + mentionPR + `,` + closingPR + `]`, + timeline: `[]`, + wantNumber: 14701, + wantState: prLinkStateOpen, + }, + { + name: "mention-only PR is skipped, closing PR is chosen", + search: `[]`, + timeline: `[` + crossRef(mentionPR) + `,` + crossRef(closingPR) + `]`, + wantNumber: 14701, + wantState: prLinkStateOpen, + }, { name: "no reference anywhere", search: `[` + searchHit + `]`, @@ -470,6 +530,43 @@ func TestSearchLinkedPR_CountsCommitCommentAndTimelineReferences(t *testing.T) { } } +// TestPrClosesIssue pins the timeline gate: a PR whose title/body names the +// issue must do so with a closing keyword, while a PR whose title/body never +// names the issue reached the timeline via a PR comment and is kept (#14859). +func TestPrClosesIssue(t *testing.T) { + cases := []struct { + name string + title, body string + want bool + }{ + {"empty title and body", "", "", true}, + {"comment-only: title/body never name the issue", "keep the header chips from overlapping", "Hive Agent", true}, + {"closing keyword", "fix it", "Closes #13135", true}, + {"closing keyword, repo-qualified", "fix it", "Fixes o/r#13135", true}, + {"bare mention without keyword", "unrelated", "see also #13135 for context", false}, + {"parenthesised mention without keyword", "unrelated", "follow-up (#13135)", false}, + {"repo-qualified mention without keyword", "unrelated", "see also o/r#13135", false}, + {"URL mention without keyword", "unrelated", "see https://github.com/o/r/issues/13135", false}, + {"mention in title without keyword", "follow-up to #13135", "", false}, + {"different issue number", "unrelated", "Closes #131350 and see #1313", true}, + {"URL for a different issue number", "unrelated", "see https://github.com/o/r/issues/131350", true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + iss := &gh.Issue{} + if tc.title != "" { + iss.Title = gh.Ptr(tc.title) + } + if tc.body != "" { + iss.Body = gh.Ptr(tc.body) + } + if got := prClosesIssue(iss, "o/r", 13135); got != tc.want { + t.Fatalf("prClosesIssue(%q, %q) = %v, want %v", tc.title, tc.body, got, tc.want) + } + }) + } +} + // TestCrossReferencingPRs_PageCap pins the prLinkTimelineMaxPages bound: a // timeline that keeps advertising a next page is read at most that many // times, and the lookup keeps what it has read instead of walking to