|
|
|
@@ -7,6 +7,7 @@ import (
|
|
|
|
|
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/identity"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
|
|
|
|
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
|
|
|
@@ -20,6 +21,12 @@ import (
|
|
|
|
|
// on the envelope's source-independent key.
|
|
|
|
|
type ReactionPublisher func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error
|
|
|
|
|
|
|
|
|
|
// botUserSource names the bot login that owns a task, so the reaction consumer
|
|
|
|
|
// can drop Archie's own comments.
|
|
|
|
|
type botUserSource interface {
|
|
|
|
|
botUserForTask(task *workflowtask.Task) string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writerGate decides which review activity on one PR may reach agents: never
|
|
|
|
|
// the bot's own, and only from users with write access, which the forge is
|
|
|
|
|
// asked once per user for the lifetime of one scan.
|
|
|
|
@@ -122,26 +129,64 @@ func scanPRReviewReactions(
|
|
|
|
|
return newReviewCursor, newCommentCursor, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReviewScanner runs the pull request maintenance: it reconciles open PRs with
|
|
|
|
|
// forge state, scans their reviews into the reaction stream, and drains that
|
|
|
|
|
// stream into remediate runs.
|
|
|
|
|
type ReviewScanner struct {
|
|
|
|
|
store storecontract.TaskStore
|
|
|
|
|
reactions ReactionSource
|
|
|
|
|
publishTask func(ctx context.Context, subject, idempotencyKey string, payload []byte) error
|
|
|
|
|
bus *events.Bus
|
|
|
|
|
repos repoHub
|
|
|
|
|
orgs orgResolver
|
|
|
|
|
bots botUserSource
|
|
|
|
|
log *slog.Logger
|
|
|
|
|
consumer *reactionConsumer
|
|
|
|
|
publish ReactionPublisher
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewReviewScanner wires the scanner to its dependencies. The reaction consumer
|
|
|
|
|
// and publisher are built here, so the consumer's drop counter spans the
|
|
|
|
|
// process lifetime.
|
|
|
|
|
func NewReviewScanner(
|
|
|
|
|
store storecontract.TaskStore,
|
|
|
|
|
reactions ReactionSource,
|
|
|
|
|
publishTask func(ctx context.Context, subject, idempotencyKey string, payload []byte) error,
|
|
|
|
|
bus *events.Bus,
|
|
|
|
|
repos repoHub,
|
|
|
|
|
orgs orgResolver,
|
|
|
|
|
bots botUserSource,
|
|
|
|
|
log *slog.Logger,
|
|
|
|
|
) *ReviewScanner {
|
|
|
|
|
s := &ReviewScanner{
|
|
|
|
|
store: store, reactions: reactions, publishTask: publishTask, bus: bus,
|
|
|
|
|
repos: repos, orgs: orgs, bots: bots, log: log,
|
|
|
|
|
}
|
|
|
|
|
s.consumer = newReactionConsumer(store, store, store, bots.botUserForTask, log)
|
|
|
|
|
s.publish = s.PublishReaction
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// scanPRReviews scans every open PR task for review activity with its
|
|
|
|
|
// identity's forge. Forges without review reading are skipped.
|
|
|
|
|
func (d *Daemon) scanPRReviews(ctx context.Context) error {
|
|
|
|
|
tasks, err := d.Store.OpenPRs(ctx)
|
|
|
|
|
func (s *ReviewScanner) scanPRReviews(ctx context.Context) error {
|
|
|
|
|
tasks, err := s.store.OpenPRs(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("review scan query: %w", err)
|
|
|
|
|
}
|
|
|
|
|
var failed []string
|
|
|
|
|
for i := range tasks {
|
|
|
|
|
t := &tasks[i]
|
|
|
|
|
reader, ok := d.forgeFor(ctx, t).(forge.PullRequestReviewReader)
|
|
|
|
|
reader, ok := s.repos.forgeFor(ctx, t).(forge.PullRequestReviewReader)
|
|
|
|
|
if !ok {
|
|
|
|
|
continue // the noop forge and any forge without the capability
|
|
|
|
|
}
|
|
|
|
|
checker, ok := d.forgeFor(ctx, t).(forge.WriteAccessChecker)
|
|
|
|
|
checker, ok := s.repos.forgeFor(ctx, t).(forge.WriteAccessChecker)
|
|
|
|
|
if !ok {
|
|
|
|
|
d.Log.WarnContext(ctx, "review scan skipped: forge cannot verify write access", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
|
|
|
|
|
s.log.WarnContext(ctx, "review scan skipped: forge cannot verify write access", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !d.scanOnePR(ctx, reader, checker, t) {
|
|
|
|
|
if !s.scanOnePR(ctx, reader, checker, t) {
|
|
|
|
|
failed = append(failed, fmt.Sprintf("%s/%s#%d", t.Owner, t.Repo, t.PRNumber))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -149,12 +194,12 @@ func (d *Daemon) scanPRReviews(ctx context.Context) error {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// scanOnePR reports false when the scan failed; the cause is logged here.
|
|
|
|
|
func (d *Daemon) scanOnePR(ctx context.Context, reader forge.PullRequestReviewReader, checker forge.WriteAccessChecker, t *workflowtask.Task) bool {
|
|
|
|
|
func (s *ReviewScanner) scanOnePR(ctx context.Context, reader forge.PullRequestReviewReader, checker forge.WriteAccessChecker, t *workflowtask.Task) bool {
|
|
|
|
|
// Resolve the org; on failure skip this task for now.
|
|
|
|
|
if t.Org == "" {
|
|
|
|
|
resolved, err := d.identityOrg(ctx, identity.IdentityID(t.Identity))
|
|
|
|
|
resolved, err := s.orgs.identityOrg(ctx, identity.IdentityID(t.Identity))
|
|
|
|
|
if err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "review scan org resolve failed", "task", t.ID, "err", err)
|
|
|
|
|
s.log.WarnContext(ctx, "review scan org resolve failed", "task", t.ID, "err", err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
t.Org = resolved
|
|
|
|
@@ -163,20 +208,20 @@ func (d *Daemon) scanOnePR(ctx context.Context, reader forge.PullRequestReviewRe
|
|
|
|
|
// unchanged: the next scan re-fetches, and PublishUnique's dedup covers
|
|
|
|
|
// anything the previous scan already published.
|
|
|
|
|
reviewCursor, commentCursor, err := scanPRReviewReactions(
|
|
|
|
|
ctx, d.Log, reader, checker, t, d.botUserForTask(t),
|
|
|
|
|
t.ReviewCursor, t.WatchCommentID, d.reactionPublisher)
|
|
|
|
|
ctx, s.log, reader, checker, t, s.bots.botUserForTask(t),
|
|
|
|
|
t.ReviewCursor, t.WatchCommentID, s.publish)
|
|
|
|
|
if err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "review scan failed", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber, "err", err)
|
|
|
|
|
s.log.WarnContext(ctx, "review scan failed", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber, "err", err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if reviewCursor == t.ReviewCursor && commentCursor == t.WatchCommentID {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if err := d.Store.SetReviewCursors(ctx, t.ID, reviewCursor, commentCursor); err != nil {
|
|
|
|
|
if err := s.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.WarnContext(ctx, "review cursor persist failed", "task", t.ID, "err", err)
|
|
|
|
|
s.log.WarnContext(ctx, "review cursor persist failed", "task", t.ID, "err", err)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
@@ -184,19 +229,18 @@ func (d *Daemon) scanOnePR(ctx context.Context, reader forge.PullRequestReviewRe
|
|
|
|
|
|
|
|
|
|
// drainReactions turns available review reactions into queued remediate
|
|
|
|
|
// runs. It runs before the task drain so a reaction queued in this pass can
|
|
|
|
|
// be claimed by it. The consumer is built by Startup, so its drop counter
|
|
|
|
|
// spans the process lifetime.
|
|
|
|
|
func (d *Daemon) drainReactions(ctx context.Context) {
|
|
|
|
|
if _, err := d.reactionConsumer.drain(ctx, d.Reactions, maxReactionsPerCycle); err != nil {
|
|
|
|
|
d.Log.ErrorContext(ctx, "reaction drain failed", "err", err)
|
|
|
|
|
// be claimed by it.
|
|
|
|
|
func (s *ReviewScanner) drainReactions(ctx context.Context) {
|
|
|
|
|
if _, err := s.consumer.drain(ctx, s.reactions, maxReactionsPerCycle); err != nil {
|
|
|
|
|
s.log.ErrorContext(ctx, "reaction drain failed", "err", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PublishReaction stamps the org on a review reaction, defaulting to the
|
|
|
|
|
// root identity's, and publishes it.
|
|
|
|
|
func (d *Daemon) PublishReaction(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error {
|
|
|
|
|
func (s *ReviewScanner) PublishReaction(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error {
|
|
|
|
|
if reaction.Org == "" {
|
|
|
|
|
resolved, err := d.identityOrg(ctx, identity.SystemID)
|
|
|
|
|
resolved, err := s.orgs.identityOrg(ctx, identity.SystemID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("publish reaction %s: resolve org: %w", reaction.Ref(), err)
|
|
|
|
|
}
|
|
|
|
@@ -206,65 +250,65 @@ func (d *Daemon) PublishReaction(ctx context.Context, reaction workintake.Review
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return d.Tasks.PublishUnique(ctx, reaction.Subject(), reaction.IdempotencyKey(), payload)
|
|
|
|
|
return s.publishTask(ctx, reaction.Subject(), reaction.IdempotencyKey(), payload)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reconcilePRs moves pr_open tasks to merged/rejected from GitHub state
|
|
|
|
|
// and cleans up their worktrees.
|
|
|
|
|
func (d *Daemon) reconcilePRs(ctx context.Context) error {
|
|
|
|
|
tasks, err := d.Store.OpenPRs(ctx)
|
|
|
|
|
func (s *ReviewScanner) reconcilePRs(ctx context.Context) error {
|
|
|
|
|
tasks, err := s.store.OpenPRs(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("reconcile query: %w", err)
|
|
|
|
|
}
|
|
|
|
|
var failed []string
|
|
|
|
|
for _, t := range tasks {
|
|
|
|
|
fg := d.forgeFor(ctx, &t)
|
|
|
|
|
fg := s.repos.forgeFor(ctx, &t)
|
|
|
|
|
if _, unavailable := fg.(*forge.NoopForge); unavailable {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
trees := d.treesFor(&t)
|
|
|
|
|
trees := s.repos.treesFor(&t)
|
|
|
|
|
// PRState/PR reconciliation is valid regardless of Source -- a
|
|
|
|
|
// chat-sourced task can still open a real PR against a real
|
|
|
|
|
// branch. Only the issue-label call (tied to the synthetic
|
|
|
|
|
// issue number) is forge-only.
|
|
|
|
|
state, err := fg.PRState(ctx, t.Owner, t.Repo, t.PRNumber)
|
|
|
|
|
if err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "PR state check failed", "pr", t.PRNumber, "err", err)
|
|
|
|
|
s.log.WarnContext(ctx, "PR state check failed", "pr", t.PRNumber, "err", err)
|
|
|
|
|
failed = append(failed, fmt.Sprintf("%s/%s#%d", t.Owner, t.Repo, t.PRNumber))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
attempt := t.Attempt
|
|
|
|
|
switch state {
|
|
|
|
|
case "merged":
|
|
|
|
|
if err := d.Store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusMerged, ""); err != nil {
|
|
|
|
|
d.Log.ErrorContext(ctx, "PR merged but task transition failed", "task", t.ID, "pr", t.PRNumber, "err", err)
|
|
|
|
|
if err := s.store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusMerged, ""); err != nil {
|
|
|
|
|
s.log.ErrorContext(ctx, "PR merged but task transition failed", "task", t.ID, "pr", t.PRNumber, "err", err)
|
|
|
|
|
failed = append(failed, fmt.Sprintf("%s/%s#%d", t.Owner, t.Repo, t.PRNumber))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err := trees.Cleanup(t.Owner, t.Repo, t.IssueNumber); err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "worktree cleanup failed", "task", t.ID, "err", err)
|
|
|
|
|
s.log.WarnContext(ctx, "worktree cleanup failed", "task", t.ID, "err", err)
|
|
|
|
|
}
|
|
|
|
|
// Close the issue explicitly; nothing else does.
|
|
|
|
|
d.closeResolvedIssue(ctx, &t, fmt.Sprintf(
|
|
|
|
|
s.closeResolvedIssue(ctx, &t, fmt.Sprintf(
|
|
|
|
|
"Closed by #%d, merged by archie.", t.PRNumber,
|
|
|
|
|
))
|
|
|
|
|
d.Log.InfoContext(ctx, "PR merged", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
|
|
|
|
|
d.emit(ctx, events.Event{
|
|
|
|
|
s.log.InfoContext(ctx, "PR merged", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
|
|
|
|
|
s.bus.Publish(ctx, events.Event{
|
|
|
|
|
Kind: events.KindPRMerged, TaskID: t.ID, Attempt: attempt,
|
|
|
|
|
Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber,
|
|
|
|
|
Data: map[string]any{"pr": t.PRNumber},
|
|
|
|
|
})
|
|
|
|
|
case "closed":
|
|
|
|
|
if err := d.Store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusRejected, "PR closed without merge"); err != nil {
|
|
|
|
|
d.Log.ErrorContext(ctx, "PR rejected but task transition failed", "task", t.ID, "pr", t.PRNumber, "err", err)
|
|
|
|
|
if err := s.store.Transition(ctx, t.ID, workflow.StatusPROpen, workflow.StatusRejected, "PR closed without merge"); err != nil {
|
|
|
|
|
s.log.ErrorContext(ctx, "PR rejected but task transition failed", "task", t.ID, "pr", t.PRNumber, "err", err)
|
|
|
|
|
failed = append(failed, fmt.Sprintf("%s/%s#%d", t.Owner, t.Repo, t.PRNumber))
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err := trees.Cleanup(t.Owner, t.Repo, t.IssueNumber); err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "worktree cleanup failed", "task", t.ID, "err", err)
|
|
|
|
|
s.log.WarnContext(ctx, "worktree cleanup failed", "task", t.ID, "err", err)
|
|
|
|
|
}
|
|
|
|
|
d.Log.InfoContext(ctx, "PR rejected", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
|
|
|
|
|
d.emit(ctx, events.Event{
|
|
|
|
|
s.log.InfoContext(ctx, "PR rejected", "repo", t.Owner+"/"+t.Repo, "pr", t.PRNumber)
|
|
|
|
|
s.bus.Publish(ctx, events.Event{
|
|
|
|
|
Kind: events.KindPRRejected, TaskID: t.ID, Attempt: attempt,
|
|
|
|
|
Repo: t.Owner + "/" + t.Repo, Issue: t.IssueNumber,
|
|
|
|
|
Data: map[string]any{"pr": t.PRNumber},
|
|
|
|
@@ -276,17 +320,17 @@ func (d *Daemon) reconcilePRs(ctx context.Context) error {
|
|
|
|
|
|
|
|
|
|
// closeResolvedIssue closes a finished task's forge issue, skipping chat
|
|
|
|
|
// tasks. Failures are logged.
|
|
|
|
|
func (d *Daemon) closeResolvedIssue(ctx context.Context, task *workflow.Task, comment string) {
|
|
|
|
|
func (s *ReviewScanner) closeResolvedIssue(ctx context.Context, task *workflow.Task, comment string) {
|
|
|
|
|
if !task.IsForgeBacked() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
resolved, err := d.Repository(ctx, task, config.OperationPR)
|
|
|
|
|
resolved, err := s.repos.Repository(ctx, task, config.OperationPR)
|
|
|
|
|
if err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "issue closure not permitted", "task", task.ID)
|
|
|
|
|
s.log.WarnContext(ctx, "issue closure not permitted", "task", task.ID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := resolved.Forge.CloseIssue(ctx, task.Owner, task.Repo, task.IssueNumber, comment); err != nil {
|
|
|
|
|
d.Log.WarnContext(ctx, "closing resolved issue failed; it stays open and will be re-polled",
|
|
|
|
|
s.log.WarnContext(ctx, "closing resolved issue failed; it stays open and will be re-polled",
|
|
|
|
|
"repo", task.Owner+"/"+task.Repo, "issue", task.IssueNumber, "err", err)
|
|
|
|
|
}
|
|
|
|
|
}
|