diff --git a/changelog.d/fixed-closed-by-cross-repo-pr-ref.md b/changelog.d/fixed-closed-by-cross-repo-pr-ref.md new file mode 100644 index 0000000000..b69cf477f4 --- /dev/null +++ b/changelog.d/fixed-closed-by-cross-repo-pr-ref.md @@ -0,0 +1 @@ +- A merged pull request from another repository listed under `closedByPullRequestsReferences` is no longer reported as a same-repo closer, so tracker chips cannot link an issue to an unrelated PR ([#15494](https://github.com/hivecommons/hive/issues/15494)). diff --git a/src/pkg/dashboard/api_repo_item_decision_test.go b/src/pkg/dashboard/api_repo_item_decision_test.go index 7309a518c5..15a1151288 100644 --- a/src/pkg/dashboard/api_repo_item_decision_test.go +++ b/src/pkg/dashboard/api_repo_item_decision_test.go @@ -103,7 +103,7 @@ func newRepoItemDecisionServer(t *testing.T, m *repoItemDecisionMock) *Server { http.NotFound(w, r) return } - _ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"repository": map[string]any{"issue": map[string]any{"closedByPullRequestsReferences": map[string]any{"nodes": []map[string]any{{"number": m.fixedIssueClosedBy, "url": "https://example.test/pr/" + strconv.Itoa(m.fixedIssueClosedBy), "merged": true, "mergedAt": "2026-10-28T11:00:00Z"}}}}}}}) + _ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"repository": map[string]any{"issue": map[string]any{"closedByPullRequestsReferences": map[string]any{"nodes": []map[string]any{{"number": m.fixedIssueClosedBy, "url": "https://github.com/testorg/testrepo/pull/" + strconv.Itoa(m.fixedIssueClosedBy), "merged": true, "mergedAt": "2026-10-28T11:00:00Z"}}}}}}}) case r.Method == http.MethodGet && m.fixedIssueClosedBy > 0 && strings.HasSuffix(path, "/pulls/"+strconv.Itoa(m.fixedIssueClosedBy)): _ = json.NewEncoder(w).Encode(map[string]any{"number": m.fixedIssueClosedBy, "state": "closed", "merged": true, "merged_at": "2026-10-28T11:00:00Z", "merge_commit_sha": "25de3ba24fa0c0ffee0000000000000000000000", "user": map[string]any{"login": "hive[bot]", "type": "Bot"}, "head": map[string]any{"sha": "cafecafecafecafecafecafecafecafecafecafe"}, "base": map[string]any{"ref": "main"}}) case r.Method == http.MethodGet && strings.HasSuffix(path, "/pulls/90"): diff --git a/src/pkg/github/issue_link_state_test.go b/src/pkg/github/issue_link_state_test.go index fbc567c958..bda8211501 100644 --- a/src/pkg/github/issue_link_state_test.go +++ b/src/pkg/github/issue_link_state_test.go @@ -85,3 +85,30 @@ func TestIssueLinkStateCloserLookupBestEffort(t *testing.T) { t.Fatalf("got %+v, want title/state kept and ClosedBy 0", got) } } + +// closedByPullRequestsReferences can include a merged PR from another +// repository; its number must never be reported as a same-repo closer. +func TestIssueLinkStateIgnoresCrossRepoCloser(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/graphql") { + fmt.Fprint(w, `{"data":{"repository":{"issue":{"closedByPullRequestsReferences":{"nodes":[`+ + `{"number":55,"url":"https://github.com/other/repo/pull/55","merged":true,"mergedAt":"2026-10-29T16:38:39Z"},`+ + `{"number":99,"url":"https://github.com/O/R/pull/99","merged":true,"mergedAt":"2026-10-29T16:38:39Z"}]}}}}}`) + return + } + if r.URL.Path == "/repos/o/r/issues/7" { + fmt.Fprint(w, `{"number":7,"title":"Closed one","state":"closed","state_reason":"completed"}`) + return + } + http.NotFound(w, r) + })) + defer ts.Close() + c := NewClientForTest(ts.URL, "o", []string{"r"}, slog.Default()) + got, err := c.IssueLinkState(context.Background(), "o", "r", 7, 0) + if err != nil { + t.Fatal(err) + } + if got.ClosedBy != 99 { + t.Fatalf("ClosedBy = %d, want 99 (cross-repo #55 ignored)", got.ClosedBy) + } +} diff --git a/src/pkg/github/issue_stage_labels_test.go b/src/pkg/github/issue_stage_labels_test.go index 5bbdca1ed6..cf230ca6b0 100644 --- a/src/pkg/github/issue_stage_labels_test.go +++ b/src/pkg/github/issue_stage_labels_test.go @@ -106,7 +106,7 @@ func newIssueStageServer(t *testing.T) *issueStageServer { "state": "OPEN", "baseRefName": base, "body": s.prBodies[pr], - "url": "https://x/pr/" + strconv.Itoa(pr), + "url": "https://github.com/acme/widgets/pull/" + strconv.Itoa(pr), "author": map[string]any{"login": author}, }, }}}) @@ -117,7 +117,7 @@ func newIssueStageServer(t *testing.T) *issueStageServer { s.mu.Unlock() nodes := []map[string]any{} if pr > 0 { - nodes = append(nodes, map[string]any{"number": pr, "url": "https://x/pr/" + strconv.Itoa(pr), "merged": true}) + nodes = append(nodes, map[string]any{"number": pr, "url": "https://github.com/acme/widgets/pull/" + strconv.Itoa(pr), "merged": true}) } _ = json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"repository": map[string]any{"issue": map[string]any{"closedByPullRequestsReferences": map[string]any{"nodes": nodes}}}}}) return diff --git a/src/pkg/github/pr_supersession_sweep.go b/src/pkg/github/pr_supersession_sweep.go index 55c34c130b..d69bd95bc8 100644 --- a/src/pkg/github/pr_supersession_sweep.go +++ b/src/pkg/github/pr_supersession_sweep.go @@ -3,6 +3,8 @@ package github import ( "context" "fmt" + "net/url" + "strconv" "strings" "time" @@ -470,11 +472,27 @@ func (c *Client) closedByPullRequestReference(ctx context.Context, owner, repo s if node.Number == currentPR || !node.Merged { continue } + if !sameRepoPullURL(node.URL, owner, repo, node.Number) { + continue + } return supersessionCloser{Repo: displayRepo, Number: node.Number, URL: node.URL, MergedAt: node.MergedAt}, true } return supersessionCloser{}, false } +// sameRepoPullURL reports whether a PR URL from closedByPullRequestsReferences +// names pull request number in owner/repo. GitHub can list a merged PR from +// another repository there, so only a same-repo PR may be named by number. +// The host is ignored so GitHub Enterprise URLs compare the same way. +func sameRepoPullURL(rawURL, owner, repo string, number int) bool { + u, err := url.Parse(rawURL) + if err != nil { + return false + } + want := "/" + owner + "/" + repo + "/pull/" + strconv.Itoa(number) + return strings.EqualFold(strings.TrimSuffix(u.Path, "/"), want) +} + func (c *Client) issueClosingCommit(ctx context.Context, owner, repo string, issue int) (string, error) { opts := &gh.ListOptions{PerPage: 100} latest := ""