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.
142 lines
5.2 KiB
Go
142 lines
5.2 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
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"
|
|
)
|
|
|
|
// ReactionPublisher delivers one reaction to the reaction stream. The
|
|
// composition root wires it to the task bus's PublishUnique against the
|
|
// reaction subject, so webhook and poll deliveries of the same record dedup
|
|
// on the envelope's source-independent key.
|
|
type ReactionPublisher func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error
|
|
|
|
// scanPRReviewReactions is the poll backstop's core for one PR (decision 2):
|
|
// list review activity since the task's persisted cursors, publish every
|
|
// non-own record as a reaction, and return the advanced cursors. The cursors
|
|
// advance past every record seen -- skipped own comments included -- so a
|
|
// record is listed once and never re-listed on a later scan. On a publish
|
|
// failure the error carries: the caller keeps the old cursors, and the next
|
|
// scan re-fetches the records it did not get to (PublishUnique's dedup
|
|
// absorbs any re-delivery).
|
|
func scanPRReviewReactions(
|
|
ctx context.Context,
|
|
reader forge.PullRequestReviewReader,
|
|
task *workflowtask.Task,
|
|
botUser string,
|
|
reviewCursor, commentCursor int64,
|
|
publish ReactionPublisher,
|
|
) (int64, int64, error) {
|
|
newReviewCursor, newCommentCursor := reviewCursor, commentCursor
|
|
|
|
reviews, err := reader.ListReviews(ctx, task.Owner, task.Repo, task.PRNumber, reviewCursor)
|
|
if err != nil {
|
|
return reviewCursor, commentCursor, err
|
|
}
|
|
for _, r := range reviews {
|
|
if r.ID > newReviewCursor {
|
|
newReviewCursor = r.ID
|
|
}
|
|
if botUser != "" && r.Author == botUser {
|
|
continue
|
|
}
|
|
reaction := workintake.ReviewCommentEnvelope{
|
|
Owner: task.Owner, Repo: task.Repo, PRNumber: task.PRNumber,
|
|
Kind: workintake.ReviewReactionReview,
|
|
ReviewID: r.ID, Author: r.Author, State: r.State, Body: r.Body,
|
|
}
|
|
if err := publish(ctx, reaction); err != nil {
|
|
return reviewCursor, commentCursor, fmt.Errorf("publish review %d: %w", r.ID, err)
|
|
}
|
|
}
|
|
|
|
comments, err := reader.ListReviewComments(ctx, task.Owner, task.Repo, task.PRNumber, commentCursor)
|
|
if err != nil {
|
|
return reviewCursor, commentCursor, err
|
|
}
|
|
for _, cm := range comments {
|
|
if cm.ID > newCommentCursor {
|
|
newCommentCursor = cm.ID
|
|
}
|
|
if botUser != "" && cm.Author == botUser {
|
|
continue
|
|
}
|
|
reaction := workintake.ReviewCommentEnvelope{
|
|
Owner: task.Owner, Repo: task.Repo, PRNumber: task.PRNumber,
|
|
Kind: workintake.ReviewReactionComment,
|
|
ReviewID: cm.ReviewID,
|
|
CommentID: cm.ID,
|
|
Author: cm.Author, Body: cm.Body, Path: cm.Path, Line: cm.Line,
|
|
}
|
|
if err := publish(ctx, reaction); err != nil {
|
|
return reviewCursor, commentCursor, fmt.Errorf("publish comment %d: %w", cm.ID, err)
|
|
}
|
|
}
|
|
|
|
return newReviewCursor, newCommentCursor, nil
|
|
}
|
|
|
|
// scanPRReviews runs the shared per-PR review scan over every open PR task
|
|
// (reconcilePRs's shape): one pass over all pr_open tasks, each scanned with
|
|
// the forge client its own identity owns. A forge without the review-reader
|
|
// capability is skipped, not an error. Wired after reconcilePRs so a PR
|
|
// that just left pr_open is not scanned for reactions in the same pass.
|
|
func (d *Daemon) scanPRReviews(ctx context.Context) {
|
|
if d.Store == nil || d.Tasks == nil {
|
|
return
|
|
}
|
|
tasks, err := d.Store.OpenPRs(ctx)
|
|
if err != nil {
|
|
d.Log.Error("review scan query failed", "err", err)
|
|
return
|
|
}
|
|
if d.reactionPublisher == nil {
|
|
d.reactionPublisher = d.publishReaction
|
|
}
|
|
for i := range tasks {
|
|
t := &tasks[i]
|
|
reader, ok := d.forgeFor(t).(forge.PullRequestReviewReader)
|
|
if !ok {
|
|
continue // the noop forge and any forge without the capability
|
|
}
|
|
d.scanOnePR(ctx, reader, t)
|
|
}
|
|
}
|
|
|
|
func (d *Daemon) scanOnePR(ctx context.Context, reader forge.PullRequestReviewReader, t *workflowtask.Task) {
|
|
// A forge read failure skips the task for this scan with its cursors
|
|
// unchanged: the next scan re-fetches, and PublishUnique's dedup covers
|
|
// anything the previous scan already published.
|
|
reviewCursor, commentCursor, err := scanPRReviewReactions(
|
|
ctx, reader, t, d.botUserForTask(t),
|
|
t.ReviewCursor, t.WatchCommentID, d.reactionPublisher)
|
|
if err != nil {
|
|
d.Log.Warn("review scan failed", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber, "err", err)
|
|
return
|
|
}
|
|
if reviewCursor == t.ReviewCursor && commentCursor == t.WatchCommentID {
|
|
return
|
|
}
|
|
if err := d.Store.SetReviewCursors(ctx, t.ID, reviewCursor, commentCursor); err != nil {
|
|
// A stale guard here means the task was claimed between the scan and
|
|
// the write; the next scan re-reads and re-lists from the old cursor,
|
|
// and PublishUnique plus the consumer's dedup absorb the repeats.
|
|
d.Log.Warn("review cursor persist failed", "task", t.ID, "err", err)
|
|
}
|
|
}
|
|
|
|
// publishReaction encodes one reaction and publishes it on the reaction
|
|
// subject with its source-independent idempotency key, so a webhook delivery
|
|
// and a poll record of the same review collapse into one.
|
|
func (d *Daemon) publishReaction(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error {
|
|
payload, err := reaction.Encode()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return d.Tasks.PublishUnique(ctx, reaction.Subject(), reaction.IdempotencyKey(), payload)
|
|
}
|