test(forge): capture the submitted review object on GitHub and Gitea (#1166)
publish / publish (push) Failing after 36s

This commit is contained in:
sam committed 2026-10-31 14:34:55 +11:00
1 parent 176a74831f
commit d03062cad8
2 files changed
+219

No files matched your search

+113
View File
@@ -1,6 +1,11 @@
package main
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"slices"
"testing"
@@ -82,3 +87,111 @@ func TestSplitInvitations(t *testing.T) {
})
}
}
// A fake GitHub API captures the review object CreateReviewComments submits:
// exactly one review carrying the event, the body and the inline comments.
func TestCreateReviewCommentsSubmitsOneReview(t *testing.T) {
type draftComment struct {
Path string `json:"path"`
Line int `json:"line"`
Side string `json:"side"`
Body string `json:"body"`
}
type reviewRequest struct {
CommitID string `json:"commit_id"`
Body string `json:"body"`
Event string `json:"event"`
Comments []draftComment `json:"comments"`
}
const headSHA = "deadbeefcafe"
var got reviewRequest
posts := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/api/v3/repos/o/r/pulls/6":
_, _ = w.Write([]byte(`{"head":{"sha":"` + headSHA + `"},"user":{"login":"alice"}}`))
case r.Method == http.MethodGet && r.URL.Path == "/api/v3/user":
_, _ = w.Write([]byte(`{"login":"archie-bot"}`))
case r.Method == http.MethodPost && r.URL.Path == "/api/v3/repos/o/r/pulls/6/reviews":
posts++
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
t.Errorf("decode submitted review: %v", err)
w.WriteHeader(http.StatusBadRequest)
return
}
_, _ = w.Write([]byte(`{"id":1}`))
default:
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
w.WriteHeader(http.StatusNotFound)
}
}))
defer srv.Close()
f, err := newGitHub("token", srv.URL, slog.New(slog.NewTextHandler(io.Discard, nil)))
if err != nil {
t.Fatalf("newGitHub: %v", err)
}
writer, ok := f.(forge.ReviewCommentWriter)
if !ok {
t.Fatal("the GitHub extension is not a ReviewCommentWriter")
}
for _, tc := range []struct {
name string
review forge.ReviewSubmission
wantEvent string
wantBody string
wantComments []draftComment
}{
{
name: "comment event carries the body and the inline comments",
review: forge.ReviewSubmission{
Event: forge.ReviewEventComment,
Body: "looks good overall",
Comments: []forge.InlineReviewComment{{Path: "main.go", Line: 12, Body: "rename this"}},
},
wantEvent: forge.ReviewEventComment,
wantBody: "looks good overall",
wantComments: []draftComment{{Path: "main.go", Line: 12, Side: "RIGHT", Body: "rename this"}},
},
{
name: "request changes lists the unanchored finding in the review body",
review: forge.ReviewSubmission{
Event: forge.ReviewEventRequestChanges,
Body: "Findings outside the diff:\n\n- `main.go`: blocking finding",
},
wantEvent: forge.ReviewEventRequestChanges,
wantBody: "Findings outside the diff:\n\n- `main.go`: blocking finding",
},
{
name: "request changes without a body still posts one",
review: forge.ReviewSubmission{
Event: forge.ReviewEventRequestChanges,
Comments: []forge.InlineReviewComment{{Path: "main.go", Line: 3, Body: "blocking"}},
},
wantEvent: forge.ReviewEventRequestChanges,
wantBody: "Changes requested.",
wantComments: []draftComment{{Path: "main.go", Line: 3, Side: "RIGHT", Body: "blocking"}},
},
} {
t.Run(tc.name, func(t *testing.T) {
got, posts = reviewRequest{}, 0
if err := writer.CreateReviewComments(t.Context(), "o", "r", 6, headSHA, tc.review); err != nil {
t.Fatalf("CreateReviewComments: %v", err)
}
if posts != 1 {
t.Fatalf("submitted %d review objects, want exactly 1", posts)
}
if got.Event != tc.wantEvent {
t.Errorf("event = %q, want %q", got.Event, tc.wantEvent)
}
if got.Body != tc.wantBody {
t.Errorf("body = %q, want %q", got.Body, tc.wantBody)
}
if got.CommitID != headSHA {
t.Errorf("commit_id = %q, want the reviewed head %q", got.CommitID, headSHA)
}
if !slices.Equal(got.Comments, tc.wantComments) {
t.Errorf("comments = %+v, want %+v", got.Comments, tc.wantComments)
}
})
}
}