Sequencing steps 6 and 7 of pr-review-remediation.md: the consumer landed in052e350b, and these are the two sources that feed it. Webhook (decision 2, low latency): the GitHub receiver decodes pull_request_review and pull_request_review_comment events behind the existing HMAC verification and publishes the same typed reaction the poller produces, keyed source-independently so PublishUnique collapses the two sources. Only submitted/created carry a verdict to remediate; edited and dismissed are acknowledged. Reaction deliveries are rate-limited per source (decision 6's 'add when the receiver gains a second event family'), and the reaction path carries no dispatch predicate -- eligibility is the consumer's owned-PR lookup. Poll (both forges, the no-webhook backstop): one shared scan over all pr_open tasks, each with the forge client its own identity owns (reconcilePRs's shape), listing reviews and comments since per-task persisted high-water marks and skipping the owning identity's bot user (decision 5's own-comment exclusion, which the poll is positioned to make identity-correct). The cursors are two, not one: review IDs and comment IDs are independent forge sequences, and a combined high-water mark would silently skip reviews once a larger comment ID landed. watch_comment_id -- dead since the comment-watch design it was created for never shipped -- carries the comment cursor; review_cursor is a new presence-gated migrator column, and SetReviewCursors is its guarded RPC (pr_open only, so cursors never move under a run that owns the task). forge.ReviewComment gains ReviewID so a comment is collectable into its review's unit (decision 5) on both forges, and forge.Review gains Body so poll-delivered reviews carry their summary. Note: the store/proto/facade half of this work landed early ina65eae24under a release session's message -- a shared-checkout collision, kept rather than rewritten; that commit carries the review_cursor migration and the SetReviewCursors contract, this one carries the producers.
202 lines
6.3 KiB
Go
202 lines
6.3 KiB
Go
package webhook
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/google/go-github/v78/github"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
|
)
|
|
|
|
// reviewEvent builds a signed pull_request_review delivery, GitHub's
|
|
// low-latency path for "a human reacted to archie's PR" (decision 2).
|
|
func reviewEvent(t *testing.T, action string, mutate func(*github.PullRequestReviewEvent)) *http.Request {
|
|
t.Helper()
|
|
ev := &github.PullRequestReviewEvent{
|
|
Action: new(action),
|
|
Review: &github.PullRequestReview{
|
|
ID: new(int64(7)),
|
|
State: new("requested_changes"),
|
|
Body: new("please fix the loop"),
|
|
User: &github.User{Login: new("alice")},
|
|
},
|
|
PullRequest: &github.PullRequest{Number: new(42)},
|
|
Repo: &github.Repository{
|
|
Name: new("widgets"),
|
|
Owner: &github.User{Login: new("acme")},
|
|
},
|
|
}
|
|
if mutate != nil {
|
|
mutate(ev)
|
|
}
|
|
payload, err := json.Marshal(ev)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return signedRequest(t, testSecret, "pull_request_review", payload)
|
|
}
|
|
|
|
func reviewCommentEvent(t *testing.T, action string, mutate func(*github.PullRequestReviewCommentEvent)) *http.Request {
|
|
t.Helper()
|
|
ev := &github.PullRequestReviewCommentEvent{
|
|
Action: new(action),
|
|
Comment: &github.PullRequestComment{
|
|
ID: new(int64(11)),
|
|
PullRequestReviewID: new(int64(7)),
|
|
Body: new("also this"),
|
|
Path: new("x.go"),
|
|
Line: new(3),
|
|
User: &github.User{Login: new("bob")},
|
|
},
|
|
PullRequest: &github.PullRequest{Number: new(42)},
|
|
Repo: &github.Repository{
|
|
Name: new("widgets"),
|
|
Owner: &github.User{Login: new("acme")},
|
|
},
|
|
}
|
|
if mutate != nil {
|
|
mutate(ev)
|
|
}
|
|
payload, err := json.Marshal(ev)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return signedRequest(t, testSecret, "pull_request_review_comment", payload)
|
|
}
|
|
|
|
func serveReaction(t *testing.T, r *Receiver, req *http.Request) (*httptest.ResponseRecorder, []workintake.ReviewCommentEnvelope) {
|
|
t.Helper()
|
|
var published []workintake.ReviewCommentEnvelope
|
|
r.reactionPublish = func(_ context.Context, e workintake.ReviewCommentEnvelope) error {
|
|
published = append(published, e)
|
|
return nil
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
r.ServeHTTP(rec, req)
|
|
return rec, published
|
|
}
|
|
|
|
func newReactionReceiver() *Receiver {
|
|
return New(testSecret, "label", testLabel, testBot, nil, nil, nil)
|
|
}
|
|
|
|
func TestReceiverPublishesASubmittedReviewAsAReaction(t *testing.T) {
|
|
r := newReactionReceiver()
|
|
rec, published := serveReaction(t, r, reviewEvent(t, "submitted", nil))
|
|
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202", rec.Code)
|
|
}
|
|
if len(published) != 1 {
|
|
t.Fatalf("published = %d, want 1", len(published))
|
|
}
|
|
e := published[0]
|
|
if e.Kind != workintake.ReviewReactionReview || e.ReviewID != 7 || e.Owner != "acme" || e.Repo != "widgets" || e.PRNumber != 42 {
|
|
t.Errorf("reaction = %+v, want review 7 on acme/widgets#42", e)
|
|
}
|
|
if e.State != "requested_changes" || e.Body != "please fix the loop" || e.Author != "alice" {
|
|
t.Errorf("reaction content = %+v, want the review's own state/body/author", e)
|
|
}
|
|
// The idempotency key is kinded (decision 3): a review ID and a comment
|
|
// ID come from independent forge sequences and must never collide.
|
|
if e.IdempotencyKey() == "" {
|
|
t.Error("reaction carried no idempotency key")
|
|
}
|
|
}
|
|
|
|
func TestReceiverPublishesACreatedCommentAsAReaction(t *testing.T) {
|
|
r := newReactionReceiver()
|
|
rec, published := serveReaction(t, r, reviewCommentEvent(t, "created", nil))
|
|
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202", rec.Code)
|
|
}
|
|
if len(published) != 1 {
|
|
t.Fatalf("published = %d, want 1", len(published))
|
|
}
|
|
e := published[0]
|
|
if e.Kind != workintake.ReviewReactionComment || e.CommentID != 11 || e.ReviewID != 7 {
|
|
t.Errorf("reaction = %+v, want comment 11 collected under review 7", e)
|
|
}
|
|
}
|
|
|
|
func TestReceiverIgnoresNonActionReviewActivity(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
kind string
|
|
action string
|
|
}{
|
|
{"dismissed review", "review", "dismissed"},
|
|
{"edited review", "review", "edited"},
|
|
{"deleted review", "review", "deleted"},
|
|
{"edited comment", "comment", "edited"},
|
|
{"deleted comment", "comment", "deleted"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
r := newReactionReceiver()
|
|
var req *http.Request
|
|
if tt.kind == "review" {
|
|
req = reviewEvent(t, tt.action, nil)
|
|
} else {
|
|
req = reviewCommentEvent(t, tt.action, nil)
|
|
}
|
|
rec, published := serveReaction(t, r, req)
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202", rec.Code)
|
|
}
|
|
if len(published) != 0 {
|
|
t.Errorf("published %d reactions for action %q", len(published), tt.action)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReceiverReactionWithoutTheCapabilityIsUntouched(t *testing.T) {
|
|
// A receiver composed without the reaction publisher (nil) must not
|
|
// panic or publish when a review event arrives: the reaction path is an
|
|
// optional capability, the issue path is the receiver's core contract.
|
|
r := New(testSecret, "label", testLabel, testBot, nil, nil, nil)
|
|
rec := httptest.NewRecorder()
|
|
r.ServeHTTP(rec, reviewEvent(t, "submitted", nil))
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d, want 202", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestReceiverRateLimitsReactionDeliveries(t *testing.T) {
|
|
r := newReactionReceiver()
|
|
// Drive the limiter to its per-window ceiling with one remote, then
|
|
// assert the next reaction from that remote is refused with 429 while a
|
|
// different remote still passes.
|
|
remote := "10.0.0.1:1234"
|
|
for i := range reactionRateLimit {
|
|
req := reviewEvent(t, "submitted", nil)
|
|
req.RemoteAddr = remote
|
|
rec, _ := serveReaction(t, r, req)
|
|
if rec.Code != http.StatusAccepted {
|
|
t.Fatalf("delivery %d within the window: status = %d, want 202", i+1, rec.Code)
|
|
}
|
|
}
|
|
req := reviewEvent(t, "submitted", nil)
|
|
req.RemoteAddr = remote
|
|
rec, published := serveReaction(t, r, req)
|
|
if rec.Code != http.StatusTooManyRequests {
|
|
t.Fatalf("over-limit status = %d, want 429", rec.Code)
|
|
}
|
|
if len(published) != 0 {
|
|
t.Errorf("over-limit delivery published %d reactions", len(published))
|
|
}
|
|
|
|
otherReq := reviewEvent(t, "submitted", nil)
|
|
otherReq.RemoteAddr = "10.0.0.2:1234"
|
|
rec, published = serveReaction(t, r, otherReq)
|
|
if rec.Code != http.StatusAccepted || len(published) != 1 {
|
|
t.Errorf("different remote blocked: status = %d, published = %d", rec.Code, len(published))
|
|
}
|
|
}
|