feat(forge): submit one review with event and body on GitHub and Gitea
This commit is contained in:
5 files changed
+81
-30
No files matched your search
@@ -270,9 +270,9 @@ func (c *GiteaClient) ReplyToReview(ctx context.Context, owner, repo string, num
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateReviewComments posts the comments as one COMMENT review on the PR's
|
||||
// head, refused if the head moved past reviewedHeadSHA.
|
||||
func (c *GiteaClient) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, comments []forge.InlineReviewComment) error {
|
||||
// CreateReviewComments submits the review on the PR's head, refused if the
|
||||
// head moved past reviewedHeadSHA.
|
||||
func (c *GiteaClient) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, review forge.ReviewSubmission) error {
|
||||
pr, err := c.GetPullRequest(ctx, owner, repo, number)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -283,8 +283,8 @@ func (c *GiteaClient) CreateReviewComments(ctx context.Context, owner, repo stri
|
||||
if err := forge.ReviewHeadDrift(owner, repo, number, pr.HeadSHA, reviewedHeadSHA); err != nil {
|
||||
return err
|
||||
}
|
||||
reviewComments := make([]gitea.CreatePullReviewComment, 0, len(comments))
|
||||
for _, cm := range comments {
|
||||
reviewComments := make([]gitea.CreatePullReviewComment, 0, len(review.Comments))
|
||||
for _, cm := range review.Comments {
|
||||
reviewComments = append(reviewComments, gitea.CreatePullReviewComment{
|
||||
Path: cm.Path,
|
||||
Body: cm.Body,
|
||||
@@ -292,7 +292,8 @@ func (c *GiteaClient) CreateReviewComments(ctx context.Context, owner, repo stri
|
||||
})
|
||||
}
|
||||
if _, _, err := c.cli.CreatePullReview(owner, repo, int64(number), gitea.CreatePullReviewOptions{
|
||||
State: gitea.ReviewStateComment,
|
||||
State: gitea.ReviewStateType(review.Event),
|
||||
Body: review.Body,
|
||||
CommitID: pr.HeadSHA,
|
||||
Comments: reviewComments,
|
||||
}); err != nil {
|
||||
|
||||
+26
-18
@@ -2,7 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -296,10 +295,9 @@ func (c *GitHubClient) ReplyToReview(ctx context.Context, owner, repo string, nu
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateReviewComments posts each comment on the PR head's new side, refused
|
||||
// if the head moved past reviewedHeadSHA. Failures are joined; the rest are
|
||||
// still posted.
|
||||
func (c *GitHubClient) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, comments []forge.InlineReviewComment) error {
|
||||
// CreateReviewComments submits the review on the PR head's new side, refused
|
||||
// if the head moved past reviewedHeadSHA.
|
||||
func (c *GitHubClient) CreateReviewComments(ctx context.Context, owner, repo string, number int, reviewedHeadSHA string, review forge.ReviewSubmission) error {
|
||||
head, err := c.reviewHeadSHA(ctx, owner, repo, number)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -307,20 +305,30 @@ func (c *GitHubClient) CreateReviewComments(ctx context.Context, owner, repo str
|
||||
if err := forge.ReviewHeadDrift(owner, repo, number, head, reviewedHeadSHA); err != nil {
|
||||
return err
|
||||
}
|
||||
var errs []error
|
||||
for _, cm := range comments {
|
||||
comment := &github.PullRequestComment{
|
||||
Body: new(cm.Body),
|
||||
Path: new(cm.Path),
|
||||
Line: new(cm.Line),
|
||||
Side: new("RIGHT"),
|
||||
CommitID: new(head),
|
||||
}
|
||||
if _, _, err := c.gh.PullRequests.CreateComment(ctx, owner, repo, number, comment); err != nil {
|
||||
errs = append(errs, fmt.Errorf("create review comment %s:%d on %s/%s#%d: %w", cm.Path, cm.Line, owner, repo, number, err))
|
||||
}
|
||||
comments := make([]*github.DraftReviewComment, 0, len(review.Comments))
|
||||
for _, cm := range review.Comments {
|
||||
comments = append(comments, &github.DraftReviewComment{
|
||||
Body: new(cm.Body),
|
||||
Path: new(cm.Path),
|
||||
Line: new(cm.Line),
|
||||
Side: new("RIGHT"),
|
||||
})
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
body := review.Body
|
||||
// GitHub rejects a REQUEST_CHANGES review with an empty body.
|
||||
if body == "" && review.Event == forge.ReviewEventRequestChanges {
|
||||
body = "Changes requested."
|
||||
}
|
||||
req := &github.PullRequestReviewRequest{
|
||||
CommitID: new(head),
|
||||
Body: new(body),
|
||||
Event: new(review.Event),
|
||||
Comments: comments,
|
||||
}
|
||||
if _, _, err := c.gh.PullRequests.CreateReview(ctx, owner, repo, number, req); err != nil {
|
||||
return fmt.Errorf("create review on %s/%s#%d: %w", owner, repo, number, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// reviewHeadSHA resolves the commit a PR's line numbers are anchored to. A
|
||||
|
||||
+15
-1
@@ -113,8 +113,22 @@ type InlineReviewComment struct {
|
||||
Body string
|
||||
}
|
||||
|
||||
// Review events a submission may carry.
|
||||
const (
|
||||
ReviewEventComment = "COMMENT"
|
||||
ReviewEventRequestChanges = "REQUEST_CHANGES"
|
||||
)
|
||||
|
||||
// ReviewSubmission is one submitted PR review. Body lists findings that have
|
||||
// no line anchor.
|
||||
type ReviewSubmission struct {
|
||||
Event string
|
||||
Body string
|
||||
Comments []InlineReviewComment
|
||||
}
|
||||
|
||||
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 a decoded delivery; at most one field is set.
|
||||
|
||||
+9
-1
@@ -238,12 +238,20 @@ func (s *server) CreateReviewComments(ctx context.Context, r *forgev1.CreateRevi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event := r.GetEvent()
|
||||
switch event {
|
||||
case "":
|
||||
event = ReviewEventComment
|
||||
case ReviewEventComment, ReviewEventRequestChanges:
|
||||
default:
|
||||
return nil, status.Errorf(codes.InvalidArgument, "review event %q is not COMMENT or REQUEST_CHANGES", event)
|
||||
}
|
||||
pr := r.GetPullRequest()
|
||||
comments := make([]InlineReviewComment, 0, len(r.GetComments()))
|
||||
for _, cm := range r.GetComments() {
|
||||
comments = append(comments, InlineReviewComment{Path: cm.GetPath(), Line: int(cm.GetLine()), Body: cm.GetBody()})
|
||||
}
|
||||
return &forgev1.CreateReviewCommentsResponse{}, fail(c.CreateReviewComments(ctx, pr.GetRepo().GetOwner(), pr.GetRepo().GetRepo(), int(pr.GetNumber()), r.GetReviewedHeadSha(), comments))
|
||||
return &forgev1.CreateReviewCommentsResponse{}, fail(c.CreateReviewComments(ctx, pr.GetRepo().GetOwner(), pr.GetRepo().GetRepo(), int(pr.GetNumber()), r.GetReviewedHeadSha(), ReviewSubmission{Event: event, Body: r.GetBody(), Comments: comments}))
|
||||
}
|
||||
|
||||
func (s *server) AcceptInvitations(ctx context.Context, _ *forgev1.AcceptInvitationsRequest) (*forgev1.AcceptInvitationsResponse, error) {
|
||||
|
||||
@@ -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" +
|
||||
|
||||
Reference in new issue
Block a user