feat(workflow): pr-review submits REQUEST_CHANGES on blocking findings and lists unanchored ones in the review body
This commit is contained in:
9 files changed
+87
-42
No files matched your search
@@ -2106,8 +2106,12 @@ type CreateReviewCommentsRequest struct {
|
||||
PullRequest *PullRequestRef `protobuf:"bytes,1,opt,name=pull_request,json=pullRequest,proto3" json:"pull_request,omitempty"`
|
||||
ReviewedHeadSha string `protobuf:"bytes,2,opt,name=reviewed_head_sha,json=reviewedHeadSha,proto3" json:"reviewed_head_sha,omitempty"`
|
||||
Comments []*InlineReviewComment `protobuf:"bytes,3,rep,name=comments,proto3" json:"comments,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
// COMMENT or REQUEST_CHANGES; empty means COMMENT.
|
||||
Event string `protobuf:"bytes,4,opt,name=event,proto3" json:"event,omitempty"`
|
||||
// Review body, listing findings that have no line anchor.
|
||||
Body string `protobuf:"bytes,5,opt,name=body,proto3" json:"body,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *CreateReviewCommentsRequest) Reset() {
|
||||
@@ -2161,6 +2165,20 @@ func (x *CreateReviewCommentsRequest) GetComments() []*InlineReviewComment {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *CreateReviewCommentsRequest) GetEvent() string {
|
||||
if x != nil {
|
||||
return x.Event
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CreateReviewCommentsRequest) GetBody() string {
|
||||
if x != nil {
|
||||
return x.Body
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CreateReviewCommentsResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
@@ -3131,11 +3149,13 @@ const file_forge_v1_forge_proto_rawDesc = "" +
|
||||
"\x13InlineReviewComment\x12\x12\n" +
|
||||
"\x04path\x18\x01 \x01(\tR\x04path\x12\x12\n" +
|
||||
"\x04line\x18\x02 \x01(\x05R\x04line\x12\x12\n" +
|
||||
"\x04body\x18\x03 \x01(\tR\x04body\"\xc1\x01\n" +
|
||||
"\x04body\x18\x03 \x01(\tR\x04body\"\xeb\x01\n" +
|
||||
"\x1bCreateReviewCommentsRequest\x12;\n" +
|
||||
"\fpull_request\x18\x01 \x01(\v2\x18.forge.v1.PullRequestRefR\vpullRequest\x12*\n" +
|
||||
"\x11reviewed_head_sha\x18\x02 \x01(\tR\x0freviewedHeadSha\x129\n" +
|
||||
"\bcomments\x18\x03 \x03(\v2\x1d.forge.v1.InlineReviewCommentR\bcomments\"\x1e\n" +
|
||||
"\bcomments\x18\x03 \x03(\v2\x1d.forge.v1.InlineReviewCommentR\bcomments\x12\x14\n" +
|
||||
"\x05event\x18\x04 \x01(\tR\x05event\x12\x12\n" +
|
||||
"\x04body\x18\x05 \x01(\tR\x04body\"\x1e\n" +
|
||||
"\x1cCreateReviewCommentsResponse\"u\n" +
|
||||
"\x11LinkBranchRequest\x12%\n" +
|
||||
"\x04repo\x18\x01 \x01(\v2\x11.forge.v1.RepoRefR\x04repo\x12!\n" +
|
||||
|
||||
@@ -685,39 +685,41 @@ func polishFinding(ctx context.Context, tc *TaskContext, f prreview.ScoredFindin
|
||||
// posts.
|
||||
func postApprovedReview(ctx context.Context, tc *TaskContext, gate task.ReviewGate) (string, error) {
|
||||
posted := prreview.PostedFindings(gate.Posted())
|
||||
comments := reviewComments(posted)
|
||||
if len(comments) == 0 {
|
||||
return fmt.Sprintf("operator approved %d finding(s); none to post with a line anchor", len(posted)), nil
|
||||
if len(posted) == 0 {
|
||||
return "operator approved no findings; nothing posted", nil
|
||||
}
|
||||
if err := tc.Forge.CreateReviewComments(ctx, gate.Owner, gate.Repo, gate.PRNumber, gate.HeadSHA, comments); err != nil {
|
||||
if err := tc.Forge.CreateReviewComments(ctx, gate.Owner, gate.Repo, gate.PRNumber, gate.HeadSHA, forgeReview(posted)); err != nil {
|
||||
return "", fmt.Errorf("post approved review: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("posted %d finding(s) approved by the operator", len(comments)), nil
|
||||
return fmt.Sprintf("posted %d finding(s) approved by the operator as %s", len(posted), prreview.ReviewEventFor(posted)), nil
|
||||
}
|
||||
|
||||
// reviewComments renders every line-anchored finding as one forge review
|
||||
// comment. Findings with no line are dropped.
|
||||
func reviewComments(findings []prreview.ScoredFinding) []ReviewComment {
|
||||
comments := make([]ReviewComment, 0, len(findings))
|
||||
// forgeReview renders findings as one forge review: line-anchored findings
|
||||
// as inline comments, whole-file findings as a list in the review body, and
|
||||
// REQUEST_CHANGES when any finding blocks.
|
||||
func forgeReview(findings []prreview.ScoredFinding) Review {
|
||||
review := Review{Event: string(prreview.ReviewEventFor(findings))}
|
||||
var unanchored []string
|
||||
for _, f := range findings {
|
||||
if f.LineStart <= 0 {
|
||||
unanchored = append(unanchored, fmt.Sprintf("- `%s`: %s", f.File, f.Body))
|
||||
continue
|
||||
}
|
||||
comments = append(comments, ReviewComment{Path: f.File, Line: f.LineStart, Body: f.Body})
|
||||
review.Comments = append(review.Comments, ReviewComment{Path: f.File, Line: f.LineStart, Body: f.Body})
|
||||
}
|
||||
return comments
|
||||
if len(unanchored) > 0 {
|
||||
review.Body = "Findings without a line anchor:\n\n" + strings.Join(unanchored, "\n")
|
||||
}
|
||||
return review
|
||||
}
|
||||
|
||||
// postPRReview posts every line-anchored finding as one forge review. A
|
||||
// finding with no line (LineStart <= 0) is dropped rather than posted
|
||||
// anywhere (see reviewComments).
|
||||
// postPRReview posts every finding as one forge review.
|
||||
func postPRReview(ctx context.Context, tc *TaskContext) error {
|
||||
comments := reviewComments(tc.prReview.scored)
|
||||
if len(comments) == 0 {
|
||||
if len(tc.prReview.scored) == 0 {
|
||||
return nil
|
||||
}
|
||||
return tc.Forge.CreateReviewComments(
|
||||
ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber, tc.prReview.headSHA, comments,
|
||||
ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber, tc.prReview.headSHA, forgeReview(tc.prReview.scored),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ func (f suppressedForge) LinkBranch(_ context.Context, owner, repo string, numbe
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f suppressedForge) CreateReviewComments(_ context.Context, owner, repo string, number int, _ string, comments []ReviewComment) error {
|
||||
f.tc.suppress(fmt.Sprintf("Would post %d review comments on %s/%s#%d", len(comments), owner, repo, number))
|
||||
func (f suppressedForge) CreateReviewComments(_ context.Context, owner, repo string, number int, _ string, review Review) error {
|
||||
f.tc.suppress(fmt.Sprintf("Would post a %s review with %d comments on %s/%s#%d", review.Event, len(review.Comments), owner, repo, number))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestSuppressForgeWritesSkipsEveryWrite(t *testing.T) {
|
||||
"close": func() error { return tc.Forge.CloseIssue(ctx, "o", "r", 1, "") },
|
||||
"pr": func() error { _, err := tc.Forge.CreatePR(ctx, "o", "r", "t", "h", "b", ""); return err },
|
||||
"link": func() error { return tc.Forge.LinkBranch(ctx, "o", "r", 1, "br") },
|
||||
"review": func() error { return tc.Forge.CreateReviewComments(ctx, "o", "r", 1, "", nil) },
|
||||
"review": func() error { return tc.Forge.CreateReviewComments(ctx, "o", "r", 1, "", Review{}) },
|
||||
"comment": func() error { _, err := tc.Forge.Comment(ctx, "o", "r", 1, "hi"); return err },
|
||||
"reply": func() error { return tc.Forge.ReplyToReview(ctx, "o", "r", 1, 2, "hi") },
|
||||
"push": func() error { return tc.Trees.Push(ctx, "dir", "br") },
|
||||
|
||||
@@ -30,10 +30,10 @@ type Forger interface {
|
||||
CloseIssue(ctx context.Context, owner, repo string, number int, comment string) error
|
||||
CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error)
|
||||
LinkBranch(ctx context.Context, owner, repo string, issueNumber int, branch string) error
|
||||
// CreateReviewComments posts line-anchored review comments on an open pull
|
||||
// request in one call. It posts only while the PR head is still
|
||||
// reviewedHeadSHA; empty skips that check.
|
||||
CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, comments []ReviewComment) error
|
||||
// CreateReviewComments submits one review on an open pull request. It
|
||||
// posts only while the PR head is still reviewedHeadSHA; empty skips that
|
||||
// check.
|
||||
CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, review Review) error
|
||||
// Comment posts a plain, non-anchored PR comment and returns its ID.
|
||||
// The remediate workflow's round-cap stage uses this to tell an
|
||||
// operator why it stopped remediating, and a whole-review reply (no
|
||||
@@ -45,6 +45,14 @@ type Forger interface {
|
||||
ReplyToReview(ctx context.Context, owner, repo string, number int, commentID int64, body string) error
|
||||
}
|
||||
|
||||
// Review is one submitted pull request review. Event is COMMENT or
|
||||
// REQUEST_CHANGES; empty means COMMENT.
|
||||
type Review struct {
|
||||
Event string
|
||||
Body string
|
||||
Comments []ReviewComment
|
||||
}
|
||||
|
||||
// ReviewComment is one line-anchored review comment: path, line and body.
|
||||
type ReviewComment struct {
|
||||
Path string
|
||||
|
||||
+10
-2
@@ -169,11 +169,19 @@ type InlineReviewComment struct {
|
||||
Body string
|
||||
}
|
||||
|
||||
// ReviewCommentWriter posts line-anchored review comments on a PR, refusing
|
||||
// ReviewSubmission is one submitted PR review. Event is COMMENT or REQUEST_CHANGES;
|
||||
// empty means COMMENT. Body carries findings that have no line anchor.
|
||||
type ReviewSubmission struct {
|
||||
Event string
|
||||
Body string
|
||||
Comments []InlineReviewComment
|
||||
}
|
||||
|
||||
// ReviewCommentWriter submits one review on a PR, refusing
|
||||
// when the head has moved past reviewedHeadSHA. Empty reviewedHeadSHA skips
|
||||
// the check.
|
||||
type ReviewCommentWriter interface {
|
||||
CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, comments []InlineReviewComment) error
|
||||
CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, review ReviewSubmission) error
|
||||
}
|
||||
|
||||
// WebhookEvent is one decoded webhook delivery. At most one field is set; none
|
||||
|
||||
@@ -109,12 +109,15 @@ type SetStateLabelRequest struct {
|
||||
KnownLabels []string
|
||||
}
|
||||
|
||||
// CreateReviewCommentsRequest is one review's line-anchored comments.
|
||||
// CreateReviewCommentsRequest is one review: its event, body and
|
||||
// line-anchored comments.
|
||||
type CreateReviewCommentsRequest struct {
|
||||
Target
|
||||
Number int
|
||||
// ReviewedHeadSHA is the revision the line numbers were measured on.
|
||||
ReviewedHeadSHA string
|
||||
Event string
|
||||
Body string
|
||||
Comments []InlineReviewCommentPayload
|
||||
}
|
||||
|
||||
@@ -311,7 +314,8 @@ func (s *Server) handleCreateReviewComments(identity string, msg *nats.Msg) {
|
||||
for _, cm := range req.Comments {
|
||||
comments = append(comments, forge.InlineReviewComment{Path: cm.Path, Line: cm.Line, Body: cm.Body})
|
||||
}
|
||||
err = writer.CreateReviewComments(context.Background(), req.Owner, req.Repo, req.Number, req.ReviewedHeadSHA, comments)
|
||||
review := forge.ReviewSubmission{Event: req.Event, Body: req.Body, Comments: comments}
|
||||
err = writer.CreateReviewComments(context.Background(), req.Owner, req.Repo, req.Number, req.ReviewedHeadSHA, review)
|
||||
s.respond(msg, Response{Envelope: natsrpc.NewEnvelope(err)})
|
||||
}
|
||||
|
||||
@@ -425,16 +429,15 @@ func (c *Client) SetStateLabel(ctx context.Context, owner, repo string, number i
|
||||
_, _ = natsrpc.Call[Response](ctx, c.rpc(), c.subject(SubjectSetStateLabel), req)
|
||||
}
|
||||
|
||||
// CreateReviewComments posts line-anchored comments on the PR through the
|
||||
// daemon.
|
||||
func (c *Client) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, comments []workflow.ReviewComment) error {
|
||||
payload := make([]InlineReviewCommentPayload, 0, len(comments))
|
||||
for _, cm := range comments {
|
||||
// CreateReviewComments submits one review on the PR through the daemon.
|
||||
func (c *Client) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, review workflow.Review) error {
|
||||
payload := make([]InlineReviewCommentPayload, 0, len(review.Comments))
|
||||
for _, cm := range review.Comments {
|
||||
payload = append(payload, InlineReviewCommentPayload{Path: cm.Path, Line: cm.Line, Body: cm.Body})
|
||||
}
|
||||
req := CreateReviewCommentsRequest{
|
||||
Target: c.target(owner, repo), Number: number,
|
||||
ReviewedHeadSHA: reviewedHeadSHA, Comments: payload,
|
||||
ReviewedHeadSHA: reviewedHeadSHA, Event: review.Event, Body: review.Body, Comments: payload,
|
||||
}
|
||||
resp, err := natsrpc.Call[Response](ctx, c.rpc(), c.subject(SubjectCreateReviewComment), req)
|
||||
if err != nil {
|
||||
|
||||
@@ -287,13 +287,13 @@ func (f *Forge) ReplyToReview(ctx context.Context, owner, repo string, number in
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *Forge) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, comments []forge.InlineReviewComment) error {
|
||||
in := make([]*forgev1.InlineReviewComment, 0, len(comments))
|
||||
for _, c := range comments {
|
||||
func (f *Forge) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, review forge.ReviewSubmission) error {
|
||||
in := make([]*forgev1.InlineReviewComment, 0, len(review.Comments))
|
||||
for _, c := range review.Comments {
|
||||
in = append(in, &forgev1.InlineReviewComment{Path: c.Path, Line: int32(c.Line), Body: c.Body}) // line numbers fit int32
|
||||
}
|
||||
_, err := call(ctx, f, "create review comments", func(c forgev1.ForgeServiceClient) (*forgev1.CreateReviewCommentsResponse, error) {
|
||||
return c.CreateReviewComments(ctx, &forgev1.CreateReviewCommentsRequest{PullRequest: prRef(owner, repo, number), ReviewedHeadSha: reviewedHeadSHA, Comments: in})
|
||||
return c.CreateReviewComments(ctx, &forgev1.CreateReviewCommentsRequest{PullRequest: prRef(owner, repo, number), ReviewedHeadSha: reviewedHeadSHA, Event: review.Event, Body: review.Body, Comments: in})
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -208,6 +208,10 @@ message CreateReviewCommentsRequest {
|
||||
PullRequestRef pull_request = 1;
|
||||
string reviewed_head_sha = 2;
|
||||
repeated InlineReviewComment comments = 3;
|
||||
// COMMENT or REQUEST_CHANGES; empty means COMMENT.
|
||||
string event = 4;
|
||||
// Review body, listing findings that have no line anchor.
|
||||
string body = 5;
|
||||
}
|
||||
message CreateReviewCommentsResponse {}
|
||||
|
||||
|
||||
Reference in new issue
Block a user