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.
139 lines
5.0 KiB
Go
139 lines
5.0 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
|
"github.com/samcharles93/archie-core/internal/forge"
|
|
)
|
|
|
|
type fakeReviewReader struct {
|
|
reviews []forge.Review
|
|
comments []forge.ReviewComment
|
|
listErr error
|
|
}
|
|
|
|
func (f *fakeReviewReader) ListReviews(ctx context.Context, owner, repo string, number int, sinceID int64) ([]forge.Review, error) {
|
|
// The real implementations filter on the forge; the fake models the
|
|
// contract so "records at or below the cursor never come back" is
|
|
// actually exercised.
|
|
var out []forge.Review
|
|
for _, r := range f.reviews {
|
|
if r.ID > sinceID {
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return out, f.listErr
|
|
}
|
|
|
|
func (f *fakeReviewReader) ListReviewComments(ctx context.Context, owner, repo string, number int, sinceID int64) ([]forge.ReviewComment, error) {
|
|
var out []forge.ReviewComment
|
|
for _, c := range f.comments {
|
|
if c.ID > sinceID {
|
|
out = append(out, c)
|
|
}
|
|
}
|
|
return out, f.listErr
|
|
}
|
|
|
|
func (f *fakeReviewReader) ReplyToReview(ctx context.Context, owner, repo string, number int, inReplyTo int64, body string) error {
|
|
return nil
|
|
}
|
|
|
|
type recordingPublisher struct {
|
|
reactions []workintake.ReviewCommentEnvelope
|
|
err error
|
|
}
|
|
|
|
func (p *recordingPublisher) publish(ctx context.Context, e workintake.ReviewCommentEnvelope) error {
|
|
if p.err != nil {
|
|
return p.err
|
|
}
|
|
p.reactions = append(p.reactions, e)
|
|
return nil
|
|
}
|
|
|
|
func openTask(prNumber int, reviewCursor, commentCursor int64) *workflowtask.Task {
|
|
return &workflowtask.Task{ID: 7, Owner: "acme", Repo: "widgets", PRNumber: prNumber, Status: "pr_open", ReviewCursor: reviewCursor, WatchCommentID: commentCursor}
|
|
}
|
|
|
|
func TestScanPRReviewReactionsPublishesNewRecordsAndSkipsOwn(t *testing.T) {
|
|
reader := &fakeReviewReader{
|
|
reviews: []forge.Review{
|
|
{ID: 5, Author: "archie-bot", State: "requested_changes", Body: "mine"},
|
|
{ID: 7, Author: "alice", State: "requested_changes", Body: "please fix"},
|
|
},
|
|
comments: []forge.ReviewComment{
|
|
{ID: 9, ReviewID: 7, Author: "archie-bot", Body: "my reply"},
|
|
{ID: 11, ReviewID: 7, Author: "bob", Body: "also this", Path: "x.go", Line: 3},
|
|
},
|
|
}
|
|
publisher := &recordingPublisher{}
|
|
task := openTask(42, 0, 0)
|
|
|
|
reviewCursor, commentCursor, err := scanPRReviewReactions(
|
|
context.Background(), reader, task, "archie-bot", 0, 0, publisher.publish)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// The own review and own reply are excluded; alice's review and bob's
|
|
// comment are the two actionable records.
|
|
if len(publisher.reactions) != 2 {
|
|
t.Fatalf("published = %d, want 2", len(publisher.reactions))
|
|
}
|
|
if publisher.reactions[0].Kind != workintake.ReviewReactionReview || publisher.reactions[0].ReviewID != 7 {
|
|
t.Errorf("first reaction = %+v, want alice's review 7", publisher.reactions[0])
|
|
}
|
|
if publisher.reactions[1].Kind != workintake.ReviewReactionComment || publisher.reactions[1].CommentID != 11 {
|
|
t.Errorf("second reaction = %+v, want bob's comment 11", publisher.reactions[1])
|
|
}
|
|
if publisher.reactions[1].ReviewID != 7 {
|
|
t.Errorf("comment reaction ReviewID = %d, want 7 (collected under its review)", publisher.reactions[1].ReviewID)
|
|
}
|
|
if publisher.reactions[1].Body != "also this" || publisher.reactions[1].Path != "x.go" || publisher.reactions[1].Line != 3 {
|
|
t.Errorf("comment payload = %+v, want the comment's own content", publisher.reactions[1])
|
|
}
|
|
// Cursors advance past every record seen, own or not: a cursor that
|
|
// stalls on a skipped record would re-list it every scan forever.
|
|
if reviewCursor != 7 || commentCursor != 11 {
|
|
t.Errorf("cursors = (%d, %d), want (7, 11)", reviewCursor, commentCursor)
|
|
}
|
|
}
|
|
|
|
func TestScanPRReviewReactionsSkipRecordsAtOrBelowTheCursors(t *testing.T) {
|
|
reader := &fakeReviewReader{
|
|
reviews: []forge.Review{{ID: 5, Author: "alice", State: "requested_changes", Body: "old"}},
|
|
comments: []forge.ReviewComment{{ID: 6, ReviewID: 5, Author: "bob", Body: "old comment"}},
|
|
}
|
|
publisher := &recordingPublisher{}
|
|
|
|
reviewCursor, commentCursor, err := scanPRReviewReactions(
|
|
context.Background(), reader, openTask(42, 5, 6), "archie-bot", 5, 6, publisher.publish)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(publisher.reactions) != 0 {
|
|
t.Errorf("published %d reactions for records at or below the cursors", len(publisher.reactions))
|
|
}
|
|
if reviewCursor != 5 || commentCursor != 6 {
|
|
t.Errorf("cursors = (%d, %d), want unchanged (5, 6)", reviewCursor, commentCursor)
|
|
}
|
|
}
|
|
|
|
func TestScanPRReviewReactionsKeepsCursorsOnPublishFailure(t *testing.T) {
|
|
reader := &fakeReviewReader{
|
|
reviews: []forge.Review{{ID: 7, Author: "alice", State: "requested_changes", Body: "fix"}},
|
|
}
|
|
publisher := &recordingPublisher{err: errors.New("bus unavailable")}
|
|
|
|
_, _, err := scanPRReviewReactions(
|
|
context.Background(), reader, openTask(42, 0, 0), "archie-bot", 0, 0, publisher.publish)
|
|
if err == nil {
|
|
t.Fatal("publish error was swallowed; the cursors must not advance past unpublished records")
|
|
}
|
|
}
|