diff --git a/internal/forge/gitea.go b/internal/forge/gitea.go index 3e252a01f..3eef869c5 100644 --- a/internal/forge/gitea.go +++ b/internal/forge/gitea.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "log/slog" + "math" "net/http" "strings" "sync" @@ -239,6 +240,12 @@ func (c *GiteaClient) ListReviewComments(ctx context.Context, owner, repo string if cm.ID <= sinceID { continue } + // LineNum is the forge's uint64. A value past the int range is not a + // line this client can name, and wrapping it would put a negative + // line number in front of a reviewer. + if cm.LineNum > math.MaxInt { + return nil, fmt.Errorf("review comment %d on %s/%s#%d has line number %d, which does not fit an int", cm.ID, owner, repo, number, cm.LineNum) + } out = append(out, ReviewComment{ ID: cm.ID, ReviewID: rv.ID, diff --git a/internal/forge/review_methods_test.go b/internal/forge/review_methods_test.go index fc38f7ff1..26776dc42 100644 --- a/internal/forge/review_methods_test.go +++ b/internal/forge/review_methods_test.go @@ -136,6 +136,26 @@ func TestGiteaListReviewComments(t *testing.T) { } } +// TestGiteaListReviewCommentsRejectsLineNumberPastInt pins the bound on the +// forge's line number. Gitea reports position as a uint64; past the int range +// the conversion wraps negative and reaches a reviewer as a line that cannot +// exist. +func TestGiteaListReviewCommentsRejectsLineNumberPastInt(t *testing.T) { + c, mux := newTestGiteaClient(t) + mux.HandleFunc("GET /api/v1/repos/o/r/pulls/7/reviews", func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, []map[string]any{{"id": 3, "state": "COMMENT", "user": map[string]any{"login": "alice"}}}) + }) + mux.HandleFunc("GET /api/v1/repos/o/r/pulls/7/reviews/3/comments", func(w http.ResponseWriter, r *http.Request) { + writeJSON(t, w, []map[string]any{ + {"id": 20, "body": "x", "path": "b.go", "position": uint64(1) << 63, "user": map[string]any{"login": "bob"}}, + }) + }) + + if _, err := c.ListReviewComments(t.Context(), "o", "r", 7, 0); err == nil { + t.Fatal("want an error for a line number that does not fit an int") + } +} + func TestGiteaReplyToReview(t *testing.T) { c, mux := newTestGiteaClient(t) var got map[string]any