The poller, the forge webhook intake and the review scan built envelopes without an Org, so every task and reaction keyed on the default org no matter which org its identity served. Resolve the org through the State Store's PrincipalSource (the existing org RPC over the staterpc client) on the single publish paths: - PublishTask stamps the envelope's identity org before encoding, so the idempotency key is org/identity/owner/repo/number; a resolve failure stops the publish instead of keying under a guess. - PublishReaction is the one reaction write path, stamping the root org when the webhook producer could not; the poll scan stamps its task's already-resolved org, so both deliveries of one review key the same. - The webhook receiver's inline publish closure moves onto the daemon method, removing the second reaction write path.
146 lines
5.3 KiB
Go
146 lines
5.3 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,
|
|
Org: task.Org,
|
|
}
|
|
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,
|
|
Org: task.Org,
|
|
}
|
|
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) {
|
|
// The reactions this scan publishes key on the task's org, resolved from
|
|
// the owning identity through the same org RPC the poll uses, so a poll
|
|
// and a webhook delivery of one review collapse under one key. An org-RPC
|
|
// failure skips the task for this scan, cursors unchanged, rather than
|
|
// publishing under the default org.
|
|
if t.Org == "" {
|
|
resolved, err := d.identityOrg(ctx, d.publisherIdentity(t.Identity))
|
|
if err != nil {
|
|
d.Log.Warn("review scan org resolve failed", "task", t.ID, "err", err)
|
|
return
|
|
}
|
|
t.Org = resolved
|
|
}
|
|
// 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)
|
|
}
|
|
}
|