diff --git a/internal/domain/workintake/review_envelope.go b/internal/domain/workintake/review_envelope.go new file mode 100644 index 000000000..4dce1f52b --- /dev/null +++ b/internal/domain/workintake/review_envelope.go @@ -0,0 +1,101 @@ +package workintake + +import ( + "encoding/json" + "errors" + "fmt" + "strconv" +) + +// ErrInvalidReviewReaction reports a review reaction that cannot be routed. +var ErrInvalidReviewReaction = errors.New("workintake: invalid review reaction") + +// ReviewReactionKind distinguishes whole reviews from their comment records. +type ReviewReactionKind string + +const ( + ReviewReactionReview ReviewReactionKind = "review" + ReviewReactionComment ReviewReactionKind = "comment" + + // SubjectReviewReaction carries forge review activity for Archie-owned PRs. + SubjectReviewReaction = "archie.reaction.review_comment" +) + +// ReviewCommentEnvelope is the forge-neutral reaction payload for a whole +// review or one review comment. +type ReviewCommentEnvelope struct { + Owner string `json:"owner"` + Repo string `json:"repo"` + PRNumber int `json:"pr_number"` + Kind ReviewReactionKind `json:"kind"` + CommentID int64 `json:"comment_id,omitempty"` + ReviewID int64 `json:"review_id,omitempty"` + Author string `json:"author,omitempty"` + State string `json:"state,omitempty"` + Body string `json:"body,omitempty"` + Path string `json:"path,omitempty"` + Line int `json:"line,omitempty"` +} + +// Validate reports whether the envelope has the coordinates and external ID +// its reaction kind requires. +func (e ReviewCommentEnvelope) Validate() error { + if e.Owner == "" || e.Repo == "" || e.PRNumber <= 0 { + return fmt.Errorf("%w: owner, repo and positive PR number are required", ErrInvalidReviewReaction) + } + switch e.Kind { + case ReviewReactionReview: + if e.ReviewID <= 0 { + return fmt.Errorf("%w: review id is required for kind %q", ErrInvalidReviewReaction, e.Kind) + } + case ReviewReactionComment: + if e.CommentID <= 0 { + return fmt.Errorf("%w: comment id is required for kind %q", ErrInvalidReviewReaction, e.Kind) + } + default: + return fmt.Errorf("%w: unknown kind %q", ErrInvalidReviewReaction, e.Kind) + } + return nil +} + +// Encode renders the envelope for transport. +func (e ReviewCommentEnvelope) Encode() ([]byte, error) { + if err := e.Validate(); err != nil { + return nil, err + } + data, err := json.Marshal(e) + if err != nil { + return nil, fmt.Errorf("encode review reaction: %w", err) + } + return data, nil +} + +// DecodeReviewComment parses a transported review envelope. +func DecodeReviewComment(data []byte) (ReviewCommentEnvelope, error) { + var e ReviewCommentEnvelope + if err := json.Unmarshal(data, &e); err != nil { + return ReviewCommentEnvelope{}, fmt.Errorf("decode review reaction: %w", err) + } + if err := e.Validate(); err != nil { + return ReviewCommentEnvelope{}, err + } + return e, nil +} + +// Ref identifies the pull request receiving the reaction. +func (e ReviewCommentEnvelope) Ref() string { + return e.Owner + "/" + e.Repo + "#" + strconv.Itoa(e.PRNumber) +} + +// IdempotencyKey identifies one forge review record independently of delivery +// source. +func (e ReviewCommentEnvelope) IdempotencyKey() string { + id := e.ReviewID + if e.Kind == ReviewReactionComment { + id = e.CommentID + } + return dedupKeyPrefix + "review/" + e.Owner + "/" + e.Repo + "/" + strconv.Itoa(e.PRNumber) + "/" + string(e.Kind) + "/" + strconv.FormatInt(id, 10) +} + +// Subject returns the reaction address for this envelope. +func (e ReviewCommentEnvelope) Subject() string { return SubjectReviewReaction } diff --git a/internal/domain/workintake/review_envelope_test.go b/internal/domain/workintake/review_envelope_test.go new file mode 100644 index 000000000..cb0d51a24 --- /dev/null +++ b/internal/domain/workintake/review_envelope_test.go @@ -0,0 +1,80 @@ +package workintake + +import ( + "errors" + "reflect" + "testing" +) + +func TestReviewCommentEnvelopeRoundTrip(t *testing.T) { + tests := []ReviewCommentEnvelope{ + { + Owner: "acme", Repo: "widgets", PRNumber: 42, + Kind: ReviewReactionReview, ReviewID: 17, Author: "reviewer", + State: "requested_changes", Body: "Please handle nil input.", + }, + { + Owner: "acme", Repo: "widgets", PRNumber: 42, + Kind: ReviewReactionComment, CommentID: 17, ReviewID: 9, + Author: "reviewer", Body: "This branch leaks.", Path: "worker.go", Line: 73, + }, + } + for _, want := range tests { + data, err := want.Encode() + if err != nil { + t.Fatalf("Encode(%q) = %v", want.Kind, err) + } + got, err := DecodeReviewComment(data) + if err != nil { + t.Fatalf("DecodeReviewComment(%q) = %v", want.Kind, err) + } + if !reflect.DeepEqual(got, want) { + t.Errorf("round trip %q = %+v, want %+v", want.Kind, got, want) + } + } +} + +func TestReviewCommentEnvelopeKindSeparatesIdempotencyKeys(t *testing.T) { + review := ReviewCommentEnvelope{Owner: "acme", Repo: "widgets", PRNumber: 42, Kind: ReviewReactionReview, ReviewID: 17} + comment := ReviewCommentEnvelope{Owner: "acme", Repo: "widgets", PRNumber: 42, Kind: ReviewReactionComment, CommentID: 17} + + if review.IdempotencyKey() == comment.IdempotencyKey() { + t.Fatalf("review and comment idempotency keys both %q; independent ID sequences collided", review.IdempotencyKey()) + } + if got, want := review.IdempotencyKey(), "archie:review/acme/widgets/42/review/17"; got != want { + t.Errorf("review key = %q, want %q", got, want) + } + if got, want := comment.IdempotencyKey(), "archie:review/acme/widgets/42/comment/17"; got != want { + t.Errorf("comment key = %q, want %q", got, want) + } +} + +func TestReviewCommentEnvelopeValidation(t *testing.T) { + tests := []struct { + name string + env ReviewCommentEnvelope + }{ + {name: "missing coordinates", env: ReviewCommentEnvelope{Kind: ReviewReactionReview, ReviewID: 1}}, + {name: "unknown kind", env: ReviewCommentEnvelope{Owner: "o", Repo: "r", PRNumber: 1, Kind: "reaction", ReviewID: 1}}, + {name: "review missing review id", env: ReviewCommentEnvelope{Owner: "o", Repo: "r", PRNumber: 1, Kind: ReviewReactionReview}}, + {name: "comment missing comment id", env: ReviewCommentEnvelope{Owner: "o", Repo: "r", PRNumber: 1, Kind: ReviewReactionComment}}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + if err := tc.env.Validate(); !errors.Is(err, ErrInvalidReviewReaction) { + t.Fatalf("Validate() = %v, want ErrInvalidReviewReaction", err) + } + }) + } + + valid := ReviewCommentEnvelope{Owner: "o", Repo: "r", PRNumber: 1, Kind: ReviewReactionComment, CommentID: 2} + if err := valid.Validate(); err != nil { + t.Fatalf("valid envelope rejected: %v", err) + } + if got := valid.Subject(); got != SubjectReviewReaction { + t.Errorf("Subject() = %q, want %q", got, SubjectReviewReaction) + } + if got := valid.Ref(); got != "o/r#1" { + t.Errorf("Ref() = %q, want o/r#1", got) + } +}