Skip to content
Merged
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
37 changes: 37 additions & 0 deletions docs/guides/dev/cli-internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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 `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) |

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 |
Expand Down
16 changes: 15 additions & 1 deletion internal/cli/fetchreviewthreads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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) {
Expand Down
26 changes: 18 additions & 8 deletions internal/forge/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2076,21 +2076,31 @@ 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
}
}
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()
Expand Down
24 changes: 22 additions & 2 deletions internal/forge/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
52 changes: 47 additions & 5 deletions internal/forge/forge.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,57 @@ 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.
Comment thread
csoceanu marked this conversation as resolved.
Resolved
Comment thread
csoceanu marked this conversation as resolved.
Resolved
// 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 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
// 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"`
Expand Down
56 changes: 50 additions & 6 deletions internal/forge/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"`
Expand Down Expand Up @@ -3981,10 +3991,29 @@ 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,
}
// 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.
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
Expand Down Expand Up @@ -4019,6 +4048,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{
Expand Down
Loading
Loading