Merge pull request '[GITEA] POST /repos/{owner}/{repo}/pulls/{index}/reviews/{id}/comments (squash) do not implicitly create a review' (#2169) from earl-warren/forgejo:wip-api-review into forgejo-dependency

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2169
Reviewed-by: Loïc Dachary <dachary@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-01-18 14:24:04 +00:00
commit 66a2d07ed2
3 changed files with 7 additions and 34 deletions

View file

@ -331,22 +331,14 @@ func CreatePullReviewComment(ctx *context.APIContext) {
line = opts.OldLineNum * -1 line = opts.OldLineNum * -1
} }
comment, err := pull_service.CreateCodeComment(ctx, comment, err := pull_service.CreateCodeCommentKnownReviewID(ctx,
ctx.Doer, ctx.Doer,
ctx.Repo.GitRepo, pr.Issue.Repo,
pr.Issue, pr.Issue,
line,
opts.Body, opts.Body,
opts.Path, opts.Path,
// as of e522e774cae2240279fc48c349fc513c9d3353ee line,
// isPending is not needed because review.ID is always available
// and does not need to be discovered implicitly
false,
review.ID, review.ID,
// as of e522e774cae2240279fc48c349fc513c9d3353ee
// latestCommitID is not needed because it is only used to
// create a new review in case it does not already exist
"",
) )
if err != nil { if err != nil {
ctx.InternalServerError(err) ctx.InternalServerError(err)

View file

@ -95,7 +95,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
return nil, err return nil, err
} }
comment, err := createCodeComment(ctx, comment, err := CreateCodeCommentKnownReviewID(ctx,
doer, doer,
issue.Repo, issue.Repo,
issue, issue,
@ -135,7 +135,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
} }
} }
comment, err := createCodeComment(ctx, comment, err := CreateCodeCommentKnownReviewID(ctx,
doer, doer,
issue.Repo, issue.Repo,
issue, issue,
@ -161,7 +161,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
} }
// createCodeComment creates a plain code comment at the specified line / path // createCodeComment creates a plain code comment at the specified line / path
func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, treePath string, line, reviewID int64) (*issues_model.Comment, error) { func CreateCodeCommentKnownReviewID(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, treePath string, line, reviewID int64) (*issues_model.Comment, error) {
var commitID, patch string var commitID, patch string
if err := issue.LoadPullRequest(ctx); err != nil { if err := issue.LoadPullRequest(ctx); err != nil {
return nil, fmt.Errorf("LoadPullRequest: %w", err) return nil, fmt.Errorf("LoadPullRequest: %w", err)

View file

@ -45,7 +45,6 @@ func TestAPIPullReviewCreateDeleteComment(t *testing.T) {
t.Run("Event_"+string(event), func(t *testing.T) { t.Run("Event_"+string(event), func(t *testing.T) {
path := "README.md" path := "README.md"
var review api.PullReview var review api.PullReview
existingCommentBody := "existing comment body"
var reviewLine int64 = 1 var reviewLine int64 = 1
// cleanup // cleanup
@ -76,18 +75,11 @@ func TestAPIPullReviewCreateDeleteComment(t *testing.T) {
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/pulls/%d/reviews", repo.FullName(), pullIssue.Index), &api.CreatePullReviewOptions{ req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/pulls/%d/reviews", repo.FullName(), pullIssue.Index), &api.CreatePullReviewOptions{
Body: "body1", Body: "body1",
Event: event, Event: event,
Comments: []api.CreatePullReviewComment{
{
Path: path,
Body: existingCommentBody,
OldLineNum: reviewLine,
},
},
}).AddTokenAuth(token) }).AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK) resp := MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &review) DecodeJSON(t, resp, &review)
require.EqualValues(t, string(event), review.State) require.EqualValues(t, string(event), review.State)
require.EqualValues(t, 1, review.CodeCommentsCount) require.EqualValues(t, 0, review.CodeCommentsCount)
} }
{ {
@ -100,17 +92,6 @@ func TestAPIPullReviewCreateDeleteComment(t *testing.T) {
} }
requireReviewCount(1) requireReviewCount(1)
{
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/pulls/%d/reviews/%d/comments", repo.FullName(), pullIssue.Index, review.ID).
AddTokenAuth(token)
resp := MakeRequest(t, req, http.StatusOK)
var reviewComments []*api.PullReviewComment
DecodeJSON(t, resp, &reviewComments)
require.Len(t, reviewComments, 1)
assert.EqualValues(t, username, reviewComments[0].Poster.UserName)
assert.EqualValues(t, existingCommentBody, reviewComments[0].Body)
}
newCommentBody := "first new line" newCommentBody := "first new line"
var reviewComment api.PullReviewComment var reviewComment api.PullReviewComment