From de0bb514e39ffd8ff4636c6d80d244178fc3371a Mon Sep 17 00:00:00 2001 From: fullsend-code <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2026 08:10:19 +0000 Subject: [PATCH 1/2] fix(#8174)!: normalize review-thread position fields across forges ReviewThread exposed only line and original_line, but the two forges filled them with different meanings. GitHub used them for the current and creation-time line on an unfetched diff side, while GitLab put new_line and old_line into them. A GitLab removed-line discussion therefore looked like an outdated GitHub thread, and consumers could not match threads by path and line across forges. Define one position contract on forge.ReviewThread: side, line, start_side, start_line, original_line, original_start_line, old_line, new_line and is_outdated, reusing the entity-context-snapshot reviews vocabulary. The GitHub query now fetches diffSide, startDiffSide, startLine, originalStartLine and isOutdated, and maps the current line to old_line or new_line by side. The GitLab fetcher keeps old_line and new_line as reported, derives side and line from them, reads line_range for multi-line starts, and leaves is_outdated and the original_* fields null because GitLab does not expose them. The fake client deep-copies the new pointer fields, and the CLI internals guide documents the contract and the migration. Pre-commit could not fetch remote hook repositories in the sandbox (TLS verification failure). The pre-commit-hooks checks (v6.0.0), gofmt, go vet, lint-broken-symlinks, lint-docs-links and lychee were run directly and passed. scan-secrets replaced gitleaks. BREAKING CHANGE: fetch-review-threads output on GitLab changed. Read the old-file line from old_line instead of original_line, which is now always null, and the new-file line from new_line instead of line. For removed-line discussions, line now holds the old-file line with side "left" instead of null. GitHub line and original_line keep their meaning. Closes #8174 Co-Authored-By: Claude Opus 5.5 --- docs/guides/dev/cli-internals.md | 37 ++++++ internal/cli/fetchreviewthreads_test.go | 16 ++- internal/forge/fake.go | 26 ++-- internal/forge/fake_test.go | 24 +++- internal/forge/forge.go | 50 +++++++- internal/forge/github/github.go | 53 +++++++- internal/forge/github/review_threads_test.go | 78 +++++++++++- internal/forge/gitlab/methods_test.go | 123 +++++++++++++++++++ internal/forge/gitlab/mr.go | 54 +++++++- 9 files changed, 430 insertions(+), 31 deletions(-) diff --git a/docs/guides/dev/cli-internals.md b/docs/guides/dev/cli-internals.md index 07f261859a..8a6ad99855 100644 --- a/docs/guides/dev/cli-internals.md +++ b/docs/guides/dev/cli-internals.md @@ -229,6 +229,43 @@ also includes `author_role` and `author_role_verified`; resolved threads include `verified: true` for an authoritative non-member result, and `verified: false` when the actor is a bot/unknown or the permission lookup fails. +Each thread carries the same position fields on every forge. A `null` value +means the forge did not report that value. `left` is the old (base) version of +the file and `right` is the new (head) version. Line numbers "on a side" count +lines in that side's version of the file. + +| Field | Meaning | GitHub source | GitLab source | +|-------|---------|---------------|---------------| +| `side` | Diff side that `line` and `original_line` refer to (`left` or `right`) | `diffSide`, lowercased | `right` when `new_line` is set, otherwise `left` when `old_line` is set | +| `line` | Last line of the thread's range on `side` in the current diff; `null` when the thread no longer maps onto the current diff | `line` (`null` for outdated threads) | `new_line` on the right side, `old_line` on the left side | +| `start_side` | Side of the first line of a multi-line range; `null` for single-line threads | `startDiffSide`, lowercased | `line_range.start.type` (`old` is `left`, `new` is `right`), otherwise the same rule as `side` | +| `start_line` | First line of a multi-line range on `start_side`; `null` for single-line threads | `startLine` | `line_range.start` line on `start_side`, when it differs from the end | +| `original_line` | Line on `side` in the diff the thread was created against | `originalLine` | Always `null` (not exposed) | +| `original_start_line` | Start line on `side` in the diff the thread was created against | `originalStartLine` | Always `null` (not exposed) | +| `old_line` | Current line number in the old (left) file | `line` when `side` is `left` | `old_line` | +| `new_line` | Current line number in the new (right) file | `line` when `side` is `right` | `new_line` | +| `is_outdated` | Whether later pushes made the thread outdated | `isOutdated` | Always `null` (not exposed) | + +To match a finding reported against the head revision, compare `path` and +`new_line`. The table distinguishes the cases the two forges previously +conflated: + +- A GitHub outdated thread has `is_outdated: true`, a `null` `line`, and its + creation position in `original_line`. +- A GitLab removed-line discussion has `side: "left"`, `old_line` set, + `new_line: null`, and `is_outdated: null`. A `null` `new_line` alone never + means a thread is outdated. +- A GitLab comment on an unchanged context line has both `old_line` and + `new_line`. GitHub reports only the line on the thread's `side`, so the other + field is `null`. + +**Migration from the earlier model.** Earlier releases emitted only `line` and +`original_line`, with forge-specific meanings. On GitHub, `line` and +`original_line` keep their meaning. On GitLab, read the old-file line from +`old_line` instead of `original_line`, which is now always `null`. Read the +new-file line from `new_line` instead of `line`: for a removed-line discussion, +`line` is now the old-file line on the `left` side rather than `null`. + The `mint`, `inference`, and `github` subcommands decompose setup into role-specific operations for organizations that separate GCP and GitHub responsibilities: | Install Phase | Standalone Command | Required Access | diff --git a/internal/cli/fetchreviewthreads_test.go b/internal/cli/fetchreviewthreads_test.go index 34c76dcba7..51d629fe7f 100644 --- a/internal/cli/fetchreviewthreads_test.go +++ b/internal/cli/fetchreviewthreads_test.go @@ -18,7 +18,7 @@ func TestFetchReviewThreadsCommand(t *testing.T) { srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPost, r.Method) assert.Equal(t, "/api/graphql", r.URL.Path) - _, _ = w.Write([]byte(`{"data":{"repository":{"pullRequest":{"reviewThreads":{"pageInfo":{"hasNextPage":false,"endCursor":""},"nodes":[{"id":"thread-1","isResolved":true,"path":"main.go","line":12,"originalLine":11,"resolvedBy":{"login":"reviewer"},"comments":{"pageInfo":{"hasNextPage":false},"nodes":[]}}]}}}}}`)) + _, _ = w.Write([]byte(`{"data":{"repository":{"pullRequest":{"reviewThreads":{"pageInfo":{"hasNextPage":false,"endCursor":""},"nodes":[{"id":"thread-1","isResolved":true,"path":"main.go","diffSide":"RIGHT","line":12,"originalLine":11,"isOutdated":false,"resolvedBy":{"login":"reviewer"},"comments":{"pageInfo":{"hasNextPage":false},"nodes":[]}}]}}}}}`)) })) defer srv.Close() @@ -43,6 +43,20 @@ func TestFetchReviewThreadsCommand(t *testing.T) { require.Len(t, got.Threads, 1) assert.Equal(t, "thread-1", got.Threads[0].ID) assert.True(t, got.Threads[0].IsResolved) + + var raw struct { + Threads []map[string]any `json:"threads"` + } + require.NoError(t, json.Unmarshal(output.Bytes(), &raw)) + require.Len(t, raw.Threads, 1) + thread := raw.Threads[0] + for _, key := range []string{"side", "line", "start_side", "start_line", "original_line", "original_start_line", "old_line", "new_line", "is_outdated"} { + assert.Contains(t, thread, key, "documented position field %q", key) + } + assert.Equal(t, "right", thread["side"]) + assert.Equal(t, float64(12), thread["new_line"]) + assert.Nil(t, thread["old_line"]) + assert.Equal(t, false, thread["is_outdated"]) } func TestFetchReviewThreadsCommand_DefaultsToGitHubForge(t *testing.T) { diff --git a/internal/forge/fake.go b/internal/forge/fake.go index 0b9c355b3a..00e5e139cd 100644 --- a/internal/forge/fake.go +++ b/internal/forge/fake.go @@ -2076,14 +2076,15 @@ func (f *FakeClient) ListPullRequestReviewThreads(_ context.Context, owner, repo for i := range threads.Threads { t := &threads.Threads[i] t.Comments = slices.Clone(t.Comments) - if t.Line != nil { - l := *t.Line - t.Line = &l - } - if t.OriginalLine != nil { - l := *t.OriginalLine - t.OriginalLine = &l - } + t.Side = clonePtr(t.Side) + t.Line = clonePtr(t.Line) + t.StartSide = clonePtr(t.StartSide) + t.StartLine = clonePtr(t.StartLine) + t.OriginalLine = clonePtr(t.OriginalLine) + t.OriginalStartLine = clonePtr(t.OriginalStartLine) + t.OldLine = clonePtr(t.OldLine) + t.NewLine = clonePtr(t.NewLine) + t.IsOutdated = clonePtr(t.IsOutdated) } return threads, nil } @@ -2091,6 +2092,15 @@ func (f *FakeClient) ListPullRequestReviewThreads(_ context.Context, owner, repo return ReviewThreadPage{}, nil } +// clonePtr returns a pointer to a copy of *p, or nil when p is nil. +func clonePtr[T any](p *T) *T { + if p == nil { + return nil + } + v := *p + return &v +} + func (f *FakeClient) DismissPullRequestReview(_ context.Context, owner, repo string, number, reviewID int, message string) error { f.mu.Lock() defer f.mu.Unlock() diff --git a/internal/forge/fake_test.go b/internal/forge/fake_test.go index 31715c03e7..1c3e376e79 100644 --- a/internal/forge/fake_test.go +++ b/internal/forge/fake_test.go @@ -2482,8 +2482,14 @@ func TestFakeClient_ReadsReturnCopies(t *testing.T) { fc.PRFiles = map[string][]string{"o/r/1": {"a.go"}} fc.PRCommits = map[string][]string{"o/r/1": {"sha"}} fc.PRFileDiffs = map[string][]PullRequestFileDiff{"o/r/1": {{Path: "a.go"}}} - line, originalLine := 3, 4 - fc.PRReviewThreads = map[string]ReviewThreadPage{"o/r/1": {Threads: []ReviewThread{{ID: "t", Line: &line, OriginalLine: &originalLine, Comments: []ReviewThreadComment{{Author: "u"}}}}}} + line, originalLine, startLine, originalStartLine, oldLine, newLine := 3, 4, 1, 2, 5, 6 + side, startSide, outdated := DiffSideRight, DiffSideLeft, true + fc.PRReviewThreads = map[string]ReviewThreadPage{"o/r/1": {Threads: []ReviewThread{{ + ID: "t", Side: &side, Line: &line, StartSide: &startSide, StartLine: &startLine, + OriginalLine: &originalLine, OriginalStartLine: &originalStartLine, + OldLine: &oldLine, NewLine: &newLine, IsOutdated: &outdated, + Comments: []ReviewThreadComment{{Author: "u"}}, + }}}} fc.Installations[0].Permissions = map[string]string{"contents": "read"} fc.FileContents["o/r/f"] = []byte("x") fc.FileContentsRef["o/r/f@main"] = []byte("x") @@ -2548,10 +2554,24 @@ func TestFakeClient_ReadsReturnCopies(t *testing.T) { threads.Threads[0].Comments[0].Author = "changed" assert.Equal(t, "t", fc.PRReviewThreads["o/r/1"].Threads[0].ID) assert.Equal(t, "u", fc.PRReviewThreads["o/r/1"].Threads[0].Comments[0].Author) + *threads.Threads[0].Side = DiffSideLeft *threads.Threads[0].Line = 0 + *threads.Threads[0].StartSide = DiffSideRight + *threads.Threads[0].StartLine = 0 *threads.Threads[0].OriginalLine = 0 + *threads.Threads[0].OriginalStartLine = 0 + *threads.Threads[0].OldLine = 0 + *threads.Threads[0].NewLine = 0 + *threads.Threads[0].IsOutdated = false + assert.Equal(t, DiffSideRight, side) assert.Equal(t, 3, line) + assert.Equal(t, DiffSideLeft, startSide) + assert.Equal(t, 1, startLine) assert.Equal(t, 4, originalLine) + assert.Equal(t, 2, originalStartLine) + assert.Equal(t, 5, oldLine) + assert.Equal(t, 6, newLine) + assert.True(t, outdated) annotations, err := fc.GetWorkflowRunAnnotations(ctx, "o", "r", 1) require.NoError(t, err) diff --git a/internal/forge/forge.go b/internal/forge/forge.go index a4e14f82a6..43dd46ad08 100644 --- a/internal/forge/forge.go +++ b/internal/forge/forge.go @@ -620,15 +620,55 @@ type ReviewThreadComment struct { CreatedAt string `json:"created_at"` } +// DiffSide identifies which side of a diff a review-thread position refers to. +// Left is the old (base) version of the file; right is the new (head) +// version. +type DiffSide string + +const ( + // DiffSideLeft is the old (base) side of the diff. + DiffSideLeft DiffSide = "left" + // DiffSideRight is the new (head) side of the diff. + DiffSideRight DiffSide = "right" +) + // ReviewThread represents the resolution state and comments of a pull-request // review thread. Forges that do not expose review threads return // ErrNotSupported. +// +// Position fields use one contract across forges; a nil value means the forge +// did not report that value. Line numbers on a side are line numbers in that +// side's version of the file. type ReviewThread struct { - ID string `json:"id"` - IsResolved bool `json:"is_resolved"` - Path string `json:"path"` - Line *int `json:"line"` - OriginalLine *int `json:"original_line"` + ID string `json:"id"` + IsResolved bool `json:"is_resolved"` + Path string `json:"path"` + // Side is the diff side Line (and OriginalLine) refers to. Nil when the + // thread has no diff position. + Side *DiffSide `json:"side"` + // Line is the last line of the thread's range on Side in the current + // diff. Nil when the thread no longer maps onto the current diff (for + // example an outdated GitHub thread) or has no line position. + Line *int `json:"line"` + // StartSide and StartLine give the first line of a multi-line range. + // Both are nil for single-line threads. + StartSide *DiffSide `json:"start_side"` + StartLine *int `json:"start_line"` + // OriginalLine and OriginalStartLine are the line and start line on Side + // in the diff the thread was created against. Nil when the forge does + // not report the original position (GitLab). + OriginalLine *int `json:"original_line"` + OriginalStartLine *int `json:"original_start_line"` + // OldLine and NewLine are the current position's line numbers in the + // old (left) and new (right) file. A removed line has only OldLine, an + // added line only NewLine, and an unchanged context line may have both + // when the forge reports them (GitLab). GitHub reports one side only. + OldLine *int `json:"old_line"` + NewLine *int `json:"new_line"` + // IsOutdated reports whether the forge marks the thread as outdated by + // later pushes. Nil when the forge does not expose outdated state + // (GitLab); a nil NewLine alone never implies an outdated thread. + IsOutdated *bool `json:"is_outdated"` ResolvedBy string `json:"resolved_by"` ResolvedByType string `json:"resolved_by_type"` ResolvedByRole normevent.ActorRole `json:"resolved_by_role"` diff --git a/internal/forge/github/github.go b/internal/forge/github/github.go index a75f567496..794f40f0f7 100644 --- a/internal/forge/github/github.go +++ b/internal/forge/github/github.go @@ -3865,8 +3865,13 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re id isResolved path + diffSide line + startDiffSide + startLine originalLine + originalStartLine + isOutdated resolvedBy { login __typename } comments(first: 100) { pageInfo { hasNextPage } @@ -3883,12 +3888,17 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re }` type gqlThread struct { - ID string `json:"id"` - IsResolved bool `json:"isResolved"` - Path string `json:"path"` - Line *int `json:"line"` - OriginalLine *int `json:"originalLine"` - ResolvedBy *struct { + ID string `json:"id"` + IsResolved bool `json:"isResolved"` + Path string `json:"path"` + DiffSide string `json:"diffSide"` + Line *int `json:"line"` + StartDiffSide *string `json:"startDiffSide"` + StartLine *int `json:"startLine"` + OriginalLine *int `json:"originalLine"` + OriginalStartLine *int `json:"originalStartLine"` + IsOutdated bool `json:"isOutdated"` + ResolvedBy *struct { Login string `json:"login"` Type string `json:"__typename"` } `json:"resolvedBy"` @@ -3981,10 +3991,26 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re ResolvedByRole: normevent.RoleNone, IsResolved: thread.IsResolved, Path: thread.Path, + Side: githubDiffSide(thread.DiffSide), Line: thread.Line, + StartLine: thread.StartLine, OriginalLine: thread.OriginalLine, + OriginalStartLine: thread.OriginalStartLine, + IsOutdated: &thread.IsOutdated, CommentsTruncated: thread.Comments.PageInfo.HasNextPage, } + if thread.StartLine != nil && thread.StartDiffSide != nil { + converted.StartSide = githubDiffSide(*thread.StartDiffSide) + } + // GitHub reports the current line on one side only. + if converted.Side != nil && thread.Line != nil { + line := *thread.Line + if *converted.Side == forge.DiffSideLeft { + converted.OldLine = &line + } else { + converted.NewLine = &line + } + } if thread.ResolvedBy != nil { converted.ResolvedBy = thread.ResolvedBy.Login converted.ResolvedByType = thread.ResolvedBy.Type @@ -4019,6 +4045,21 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re return result, nil } +// githubDiffSide maps GitHub's DiffSide enum (LEFT, RIGHT) to the normalized +// forge.DiffSide. Unknown or empty values map to nil. +func githubDiffSide(side string) *forge.DiffSide { + var normalized forge.DiffSide + switch side { + case "LEFT": + normalized = forge.DiffSideLeft + case "RIGHT": + normalized = forge.DiffSideRight + default: + return nil + } + return &normalized +} + // DismissPullRequestReview dismisses a review, changing its state to DISMISSED. func (c *LiveClient) DismissPullRequestReview(ctx context.Context, owner, repo string, number, reviewID int, message string) error { payload := map[string]string{ diff --git a/internal/forge/github/review_threads_test.go b/internal/forge/github/review_threads_test.go index 8187df95ac..76fa926517 100644 --- a/internal/forge/github/review_threads_test.go +++ b/internal/forge/github/review_threads_test.go @@ -37,7 +37,8 @@ func TestListPullRequestReviewThreads(t *testing.T) { "nodes": []any{ map[string]any{ "id": "PRRT_1", "isResolved": true, "path": "main.go", - "line": line, "originalLine": originalLine, + "diffSide": "RIGHT", "line": line, "originalLine": originalLine, + "startDiffSide": nil, "startLine": nil, "originalStartLine": nil, "isOutdated": false, "resolvedBy": map[string]any{"login": "reviewer", "__typename": "User"}, "comments": map[string]any{ "pageInfo": map[string]any{"hasNextPage": true}, @@ -67,9 +68,15 @@ func TestListPullRequestReviewThreads(t *testing.T) { assert.Equal(t, "repo", request["variables"].(map[string]any)["name"]) assert.Equal(t, float64(42), request["variables"].(map[string]any)["number"]) assert.Len(t, got.Threads, 2) + query := request["query"].(string) + for _, field := range []string{"diffSide", "startDiffSide", "startLine", "originalStartLine", "isOutdated"} { + assert.Contains(t, query, field) + } + right, notOutdated := forge.DiffSideRight, false assert.Equal(t, forge.ReviewThread{ - ID: "PRRT_1", IsResolved: true, Path: "main.go", Line: &line, - OriginalLine: &originalLine, ResolvedBy: "reviewer", ResolvedByType: "User", + ID: "PRRT_1", IsResolved: true, Path: "main.go", Side: &right, Line: &line, + OriginalLine: &originalLine, NewLine: &line, IsOutdated: ¬Outdated, + ResolvedBy: "reviewer", ResolvedByType: "User", ResolvedByRole: normevent.RoleWrite, ResolvedByRoleVerified: true, Comments: []forge.ReviewThreadComment{{ Author: "reviewer", AuthorType: "User", AuthorRole: normevent.RoleWrite, AuthorRoleVerified: true, @@ -77,10 +84,75 @@ func TestListPullRequestReviewThreads(t *testing.T) { }}, CommentsTruncated: true, }, got.Threads[0]) assert.Nil(t, got.Threads[1].Line) + assert.Nil(t, got.Threads[1].Side, "a missing diffSide is unknown") assert.Empty(t, got.Threads[1].ResolvedBy) assert.Equal(t, normevent.RoleNone, got.Threads[1].ResolvedByRole) } +func TestListPullRequestReviewThreads_PositionContract(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + thread := func(fields map[string]any) map[string]any { + fields["comments"] = map[string]any{"pageInfo": map[string]any{"hasNextPage": false}, "nodes": []any{}} + return fields + } + json.NewEncoder(w).Encode(map[string]any{"data": map[string]any{"repository": map[string]any{ + "pullRequest": map[string]any{"reviewThreads": map[string]any{ + "pageInfo": map[string]any{"hasNextPage": false, "endCursor": ""}, + "nodes": []any{ + thread(map[string]any{ + "id": "old-side", "path": "main.go", "diffSide": "LEFT", + "line": 8, "originalLine": 8, "isOutdated": false, + }), + thread(map[string]any{ + "id": "outdated", "path": "main.go", "diffSide": "RIGHT", + "line": nil, "originalLine": 21, "isOutdated": true, + }), + thread(map[string]any{ + "id": "multi-line", "path": "main.go", "diffSide": "RIGHT", + "line": 14, "originalLine": 13, "startDiffSide": "LEFT", + "startLine": 10, "originalStartLine": 9, "isOutdated": false, + }), + }, + }}, + }}}) + })) + defer srv.Close() + + got, err := newTestClient(t, srv).ListPullRequestReviewThreads(context.Background(), "owner", "repo", 1) + require.NoError(t, err) + require.Len(t, got.Threads, 3) + + oldSide := got.Threads[0] + require.NotNil(t, oldSide.Side) + assert.Equal(t, forge.DiffSideLeft, *oldSide.Side) + assert.Equal(t, 8, *oldSide.Line) + assert.Equal(t, 8, *oldSide.OldLine, "a LEFT-side line is an old-file line") + assert.Nil(t, oldSide.NewLine) + require.NotNil(t, oldSide.IsOutdated) + assert.False(t, *oldSide.IsOutdated) + assert.Nil(t, oldSide.StartSide) + assert.Nil(t, oldSide.StartLine) + + outdated := got.Threads[1] + assert.Equal(t, forge.DiffSideRight, *outdated.Side) + assert.Nil(t, outdated.Line) + assert.Nil(t, outdated.OldLine) + assert.Nil(t, outdated.NewLine) + assert.Equal(t, 21, *outdated.OriginalLine) + require.NotNil(t, outdated.IsOutdated) + assert.True(t, *outdated.IsOutdated) + + multi := got.Threads[2] + assert.Equal(t, forge.DiffSideRight, *multi.Side) + assert.Equal(t, 14, *multi.Line) + assert.Equal(t, 14, *multi.NewLine) + require.NotNil(t, multi.StartSide) + assert.Equal(t, forge.DiffSideLeft, *multi.StartSide) + assert.Equal(t, 10, *multi.StartLine) + assert.Equal(t, 13, *multi.OriginalLine) + assert.Equal(t, 9, *multi.OriginalStartLine) +} + func TestListPullRequestReviewThreads_GHESGraphQLEndpoint(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/api/graphql", r.URL.Path) diff --git a/internal/forge/gitlab/methods_test.go b/internal/forge/gitlab/methods_test.go index a442a817af..efb25b49fb 100644 --- a/internal/forge/gitlab/methods_test.go +++ b/internal/forge/gitlab/methods_test.go @@ -250,6 +250,129 @@ func TestListPullRequestReviewThreads(t *testing.T) { assert.Equal(t, normevent.RoleNone, got.Threads[7].ResolvedByRole) } +func TestListPullRequestReviewThreads_PositionContract(t *testing.T) { + client, mux := setupTest(t) + note := func(position map[string]any) map[string]any { + n := map[string]any{"id": 1, "body": "comment", "resolvable": true, "resolved": false} + if position != nil { + n["position"] = position + } + return n + } + mux.HandleFunc("/api/v4/projects/myorg%2Fmyrepo/merge_requests/42/discussions", func(w http.ResponseWriter, _ *http.Request) { + writeJSON(t, w, http.StatusOK, []map[string]any{ + {"id": "context-line", "notes": []map[string]any{note(map[string]any{ + "new_path": "main.go", "old_path": "main.go", "new_line": 17, "old_line": 16, + })}}, + {"id": "removed-line", "notes": []map[string]any{note(map[string]any{ + "new_path": "new.go", "old_path": "old.go", "new_line": nil, "old_line": 7, + })}}, + {"id": "multi-line-new", "notes": []map[string]any{note(map[string]any{ + "new_path": "main.go", "old_path": "main.go", "new_line": 12, + "line_range": map[string]any{ + "start": map[string]any{"type": "new", "new_line": 10}, + "end": map[string]any{"type": "new", "new_line": 12}, + }, + })}}, + {"id": "multi-line-mixed", "notes": []map[string]any{note(map[string]any{ + "new_path": "main.go", "old_path": "main.go", "new_line": 6, + "line_range": map[string]any{ + "start": map[string]any{"type": "old", "old_line": 5}, + "end": map[string]any{"type": "new", "new_line": 6}, + }, + })}}, + {"id": "single-line-range", "notes": []map[string]any{note(map[string]any{ + "new_path": "main.go", "old_path": "main.go", "new_line": 4, "old_line": 4, + "line_range": map[string]any{ + "start": map[string]any{"type": nil, "old_line": 4, "new_line": 4}, + "end": map[string]any{"type": nil, "old_line": 4, "new_line": 4}, + }, + })}}, + {"id": "general", "notes": []map[string]any{note(nil)}}, + }) + }) + + got, err := client.ListPullRequestReviewThreads(context.Background(), "myorg", "myrepo", 42) + require.NoError(t, err) + require.Len(t, got.Threads, 6) + for _, thread := range got.Threads { + assert.Nil(t, thread.IsOutdated, "%s: GitLab does not expose outdated state", thread.ID) + assert.Nil(t, thread.OriginalLine, "%s: GitLab does not expose the original position", thread.ID) + assert.Nil(t, thread.OriginalStartLine, "%s", thread.ID) + } + + contextLine := got.Threads[0] + require.NotNil(t, contextLine.Side) + assert.Equal(t, forge.DiffSideRight, *contextLine.Side) + assert.Equal(t, 17, *contextLine.Line) + assert.Equal(t, 16, *contextLine.OldLine, "old_line keeps GitLab's old-file meaning") + assert.Equal(t, 17, *contextLine.NewLine, "new_line keeps GitLab's new-file meaning") + assert.Nil(t, contextLine.StartSide) + assert.Nil(t, contextLine.StartLine) + + removed := got.Threads[1] + assert.Equal(t, "old.go", removed.Path) + require.NotNil(t, removed.Side) + assert.Equal(t, forge.DiffSideLeft, *removed.Side, "a removed-line discussion is on the old side") + assert.Equal(t, 7, *removed.Line) + assert.Equal(t, 7, *removed.OldLine) + assert.Nil(t, removed.NewLine) + + multiNew := got.Threads[2] + assert.Equal(t, forge.DiffSideRight, *multiNew.Side) + assert.Equal(t, 12, *multiNew.Line) + require.NotNil(t, multiNew.StartSide) + assert.Equal(t, forge.DiffSideRight, *multiNew.StartSide) + assert.Equal(t, 10, *multiNew.StartLine) + + multiMixed := got.Threads[3] + assert.Equal(t, forge.DiffSideRight, *multiMixed.Side) + assert.Equal(t, 6, *multiMixed.Line) + require.NotNil(t, multiMixed.StartSide) + assert.Equal(t, forge.DiffSideLeft, *multiMixed.StartSide) + assert.Equal(t, 5, *multiMixed.StartLine) + + singleRange := got.Threads[4] + assert.Equal(t, 4, *singleRange.Line) + assert.Nil(t, singleRange.StartSide, "a range covering one line is not multi-line") + assert.Nil(t, singleRange.StartLine) + + general := got.Threads[5] + assert.Nil(t, general.Side) + assert.Nil(t, general.Line) + assert.Nil(t, general.OldLine) + assert.Nil(t, general.NewLine) + assert.Nil(t, general.StartLine) +} + +func TestGitLabSideLine(t *testing.T) { + one, two := 1, 2 + tests := []struct { + name string + lineType string + oldLine *int + newLine *int + wantSide *forge.DiffSide + wantLine *int + }{ + {name: "context prefers new", oldLine: &one, newLine: &two, wantSide: ptrSide(forge.DiffSideRight), wantLine: &two}, + {name: "old type", lineType: "old", oldLine: &one, newLine: &two, wantSide: ptrSide(forge.DiffSideLeft), wantLine: &one}, + {name: "new type", lineType: "new", newLine: &two, wantSide: ptrSide(forge.DiffSideRight), wantLine: &two}, + {name: "old type without old line falls back", lineType: "old", newLine: &two, wantSide: ptrSide(forge.DiffSideRight), wantLine: &two}, + {name: "removed line", oldLine: &one, wantSide: ptrSide(forge.DiffSideLeft), wantLine: &one}, + {name: "no lines"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + side, line := gitlabSideLine(tt.lineType, tt.oldLine, tt.newLine) + assert.Equal(t, tt.wantSide, side) + assert.Equal(t, tt.wantLine, line) + }) + } +} + +func ptrSide(side forge.DiffSide) *forge.DiffSide { return &side } + func TestListPullRequestReviewThreads_DecodeError(t *testing.T) { client, mux := setupTest(t) mux.HandleFunc("/api/v4/projects/myorg%2Fmyrepo/merge_requests/42/discussions", func(w http.ResponseWriter, _ *http.Request) { diff --git a/internal/forge/gitlab/mr.go b/internal/forge/gitlab/mr.go index fb052012a6..1bd8825ab2 100644 --- a/internal/forge/gitlab/mr.go +++ b/internal/forge/gitlab/mr.go @@ -772,6 +772,37 @@ func (c *LiveClient) ListPullRequestReviews(ctx context.Context, owner, repo str return result, nil } +// gitlabLineRangePoint is one end of a GitLab multi-line position range. +type gitlabLineRangePoint struct { + Type string `json:"type"` + OldLine *int `json:"old_line"` + NewLine *int `json:"new_line"` +} + +// gitlabSideLine maps a GitLab diff position to the normalized side and the +// line number on that side. lineType is a line_range point's "old" or "new" +// type; it is empty for unchanged context lines and for the top-level +// position, in which case the new side wins whenever a new line exists. A +// position with only old_line is a removed line on the left side. +func gitlabSideLine(lineType string, oldLine, newLine *int) (*forge.DiffSide, *int) { + var side forge.DiffSide + var line *int + switch { + case lineType == "old" && oldLine != nil: + side, line = forge.DiffSideLeft, oldLine + case lineType == "new" && newLine != nil: + side, line = forge.DiffSideRight, newLine + case newLine != nil: + side, line = forge.DiffSideRight, newLine + case oldLine != nil: + side, line = forge.DiffSideLeft, oldLine + default: + return nil, nil + } + value := *line + return &side, &value +} + type gitlabDiscussionActor struct { ID int `json:"id"` Username string `json:"username"` @@ -856,10 +887,13 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re Author gitlabDiscussionActor `json:"author"` ResolvedBy *gitlabDiscussionActor `json:"resolved_by"` Position *struct { - NewPath string `json:"new_path"` - OldPath string `json:"old_path"` - NewLine *int `json:"new_line"` - OldLine *int `json:"old_line"` + NewPath string `json:"new_path"` + OldPath string `json:"old_path"` + NewLine *int `json:"new_line"` + OldLine *int `json:"old_line"` + LineRange *struct { + Start *gitlabLineRangePoint `json:"start"` + } `json:"line_range"` } `json:"position"` } `json:"notes"` } @@ -925,8 +959,16 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re } else if thread.Path == "" { thread.Path = note.Position.OldPath } - thread.Line = note.Position.NewLine - thread.OriginalLine = note.Position.OldLine + thread.OldLine = note.Position.OldLine + thread.NewLine = note.Position.NewLine + thread.Side, thread.Line = gitlabSideLine("", note.Position.OldLine, note.Position.NewLine) + thread.StartSide, thread.StartLine = nil, nil + if lr := note.Position.LineRange; lr != nil && lr.Start != nil { + startSide, startLine := gitlabSideLine(lr.Start.Type, lr.Start.OldLine, lr.Start.NewLine) + if startLine != nil && (thread.Line == nil || *startLine != *thread.Line || *startSide != *thread.Side) { + thread.StartSide, thread.StartLine = startSide, startLine + } + } } } result.Threads = append(result.Threads, thread) From 3d721198a9227ef887f38dd29d48496baa90af1d Mon Sep 17 00:00:00 2001 From: fullsend-fix <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2026 08:32:58 +0000 Subject: [PATCH 2/2] fix(#8174): address review feedback on PR #8693 Keep StartSide when GitHub reports startDiffSide even if startLine is null (outdated multi-line threads) and cover it with a test. Document original_start_line as belonging to start_side in the position contract and the ReviewThread field comments. Track the GitLab position-contract test route with handleTracked. Not changed: the entity-context-snapshot vocabulary note (no claim in the code or docs) and the GitLab line-meaning migration (cross-repo coordination). pre-commit could not fetch hook repos (certificate failure); ran gofmt, go vet, interface-doc-sync and whitespace checks directly instead. Addresses #8693 Co-Authored-By: Claude Sonnet 5.5 --- docs/guides/dev/cli-internals.md | 4 ++-- internal/forge/forge.go | 10 ++++++---- internal/forge/github/github.go | 5 ++++- internal/forge/github/review_threads_test.go | 15 ++++++++++++++- internal/forge/gitlab/methods_test.go | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/docs/guides/dev/cli-internals.md b/docs/guides/dev/cli-internals.md index 8a6ad99855..2b5091f0d1 100644 --- a/docs/guides/dev/cli-internals.md +++ b/docs/guides/dev/cli-internals.md @@ -239,9 +239,9 @@ lines in that side's version of the file. | `side` | Diff side that `line` and `original_line` refer to (`left` or `right`) | `diffSide`, lowercased | `right` when `new_line` is set, otherwise `left` when `old_line` is set | | `line` | Last line of the thread's range on `side` in the current diff; `null` when the thread no longer maps onto the current diff | `line` (`null` for outdated threads) | `new_line` on the right side, `old_line` on the left side | | `start_side` | Side of the first line of a multi-line range; `null` for single-line threads | `startDiffSide`, lowercased | `line_range.start.type` (`old` is `left`, `new` is `right`), otherwise the same rule as `side` | -| `start_line` | First line of a multi-line range on `start_side`; `null` for single-line threads | `startLine` | `line_range.start` line on `start_side`, when it differs from the end | +| `start_line` | First line of a multi-line range on `start_side`; `null` for single-line threads and for outdated multi-line GitHub threads (which keep `start_side` and `original_start_line`) | `startLine` | `line_range.start` line on `start_side`, when it differs from the end | | `original_line` | Line on `side` in the diff the thread was created against | `originalLine` | Always `null` (not exposed) | -| `original_start_line` | Start line on `side` in the diff the thread was created against | `originalStartLine` | Always `null` (not exposed) | +| `original_start_line` | Start line on `start_side` in the diff the thread was created against; for cross-side ranges this is a different file version than `original_line` | `originalStartLine` | Always `null` (not exposed) | | `old_line` | Current line number in the old (left) file | `line` when `side` is `left` | `old_line` | | `new_line` | Current line number in the new (right) file | `line` when `side` is `right` | `new_line` | | `is_outdated` | Whether later pushes made the thread outdated | `isOutdated` | Always `null` (not exposed) | diff --git a/internal/forge/forge.go b/internal/forge/forge.go index 43dd46ad08..673d12a77a 100644 --- a/internal/forge/forge.go +++ b/internal/forge/forge.go @@ -651,12 +651,14 @@ type ReviewThread struct { // example an outdated GitHub thread) or has no line position. Line *int `json:"line"` // StartSide and StartLine give the first line of a multi-line range. - // Both are nil for single-line threads. + // Both are nil for single-line threads. StartLine is nil for an outdated + // multi-line thread, while StartSide may still be set. StartSide *DiffSide `json:"start_side"` StartLine *int `json:"start_line"` - // OriginalLine and OriginalStartLine are the line and start line on Side - // in the diff the thread was created against. Nil when the forge does - // not report the original position (GitLab). + // OriginalLine is the line on Side, and OriginalStartLine is the start + // line on StartSide, in the diff the thread was created against. For + // cross-side ranges the two belong to different file versions. Nil when + // the forge does not report the original position (GitLab). OriginalLine *int `json:"original_line"` OriginalStartLine *int `json:"original_start_line"` // OldLine and NewLine are the current position's line numbers in the diff --git a/internal/forge/github/github.go b/internal/forge/github/github.go index 794f40f0f7..6fa3ca4d48 100644 --- a/internal/forge/github/github.go +++ b/internal/forge/github/github.go @@ -3999,7 +3999,10 @@ func (c *LiveClient) ListPullRequestReviewThreads(ctx context.Context, owner, re IsOutdated: &thread.IsOutdated, CommentsTruncated: thread.Comments.PageInfo.HasNextPage, } - if thread.StartLine != nil && thread.StartDiffSide != nil { + // Outdated multi-line threads have a null startLine but may still + // report startDiffSide and originalStartLine, so do not gate on + // startLine. + if thread.StartDiffSide != nil { converted.StartSide = githubDiffSide(*thread.StartDiffSide) } // GitHub reports the current line on one side only. diff --git a/internal/forge/github/review_threads_test.go b/internal/forge/github/review_threads_test.go index 76fa926517..90717a5112 100644 --- a/internal/forge/github/review_threads_test.go +++ b/internal/forge/github/review_threads_test.go @@ -112,6 +112,11 @@ func TestListPullRequestReviewThreads_PositionContract(t *testing.T) { "line": 14, "originalLine": 13, "startDiffSide": "LEFT", "startLine": 10, "originalStartLine": 9, "isOutdated": false, }), + thread(map[string]any{ + "id": "outdated-multi-line", "path": "main.go", "diffSide": "RIGHT", + "line": nil, "originalLine": 25, "startDiffSide": "LEFT", + "startLine": nil, "originalStartLine": 22, "isOutdated": true, + }), }, }}, }}}) @@ -120,7 +125,7 @@ func TestListPullRequestReviewThreads_PositionContract(t *testing.T) { got, err := newTestClient(t, srv).ListPullRequestReviewThreads(context.Background(), "owner", "repo", 1) require.NoError(t, err) - require.Len(t, got.Threads, 3) + require.Len(t, got.Threads, 4) oldSide := got.Threads[0] require.NotNil(t, oldSide.Side) @@ -151,6 +156,14 @@ func TestListPullRequestReviewThreads_PositionContract(t *testing.T) { assert.Equal(t, 10, *multi.StartLine) assert.Equal(t, 13, *multi.OriginalLine) assert.Equal(t, 9, *multi.OriginalStartLine) + + outdatedMulti := got.Threads[3] + assert.Nil(t, outdatedMulti.Line) + assert.Nil(t, outdatedMulti.StartLine) + require.NotNil(t, outdatedMulti.StartSide, "start side survives a null startLine on outdated threads") + assert.Equal(t, forge.DiffSideLeft, *outdatedMulti.StartSide) + assert.Equal(t, 25, *outdatedMulti.OriginalLine) + assert.Equal(t, 22, *outdatedMulti.OriginalStartLine) } func TestListPullRequestReviewThreads_GHESGraphQLEndpoint(t *testing.T) { diff --git a/internal/forge/gitlab/methods_test.go b/internal/forge/gitlab/methods_test.go index efb25b49fb..61cdf59391 100644 --- a/internal/forge/gitlab/methods_test.go +++ b/internal/forge/gitlab/methods_test.go @@ -259,7 +259,7 @@ func TestListPullRequestReviewThreads_PositionContract(t *testing.T) { } return n } - mux.HandleFunc("/api/v4/projects/myorg%2Fmyrepo/merge_requests/42/discussions", func(w http.ResponseWriter, _ *http.Request) { + handleTracked(t, mux, "/api/v4/projects/myorg%2Fmyrepo/merge_requests/42/discussions", func(w http.ResponseWriter, _ *http.Request) { writeJSON(t, w, http.StatusOK, []map[string]any{ {"id": "context-line", "notes": []map[string]any{note(map[string]any{ "new_path": "main.go", "old_path": "main.go", "new_line": 17, "old_line": 16,