mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
feat(api): add API to update a review (#39865)
Adds `PUT /repos/{owner}/{repo}/pulls/{index}/reviews/{id}` to update
the body of a submitted review, like GitHub's [update a
review](https://docs.github.com/en/rest/pulls/reviews#update-a-review-for-a-pull-request).
Assisted-by: Claude Code:claude-opus-5-5
---------
Co-authored-by: Claude (Opus 5.5) <noreply@anthropic.com>
Co-authored-by: Zettat123 <zettat123@gmail.com>
This commit is contained in:
7 files changed
+268
-2
No files matched your search
@@ -106,6 +106,12 @@ type SubmitPullReviewOptions struct {
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// UpdatePullReviewOptions are options to update the body of a submitted pull request review
|
||||
type UpdatePullReviewOptions struct {
|
||||
// required: true
|
||||
Body string `json:"body" binding:"Required"`
|
||||
}
|
||||
|
||||
// DismissPullReviewOptions are options to dismiss a pull request review
|
||||
type DismissPullReviewOptions struct {
|
||||
Message string `json:"message"`
|
||||
|
||||
@@ -1483,7 +1483,8 @@ func Routes() *web.Router {
|
||||
m.Combo("").
|
||||
Get(repo.GetPullReview).
|
||||
Delete(reqToken(), repo.DeletePullReview).
|
||||
Post(reqToken(), bind(api.SubmitPullReviewOptions{}), repo.SubmitPullReview)
|
||||
Post(reqToken(), bind(api.SubmitPullReviewOptions{}), repo.SubmitPullReview).
|
||||
Put(mustNotBeArchived, reqToken(), bind(api.UpdatePullReviewOptions{}), repo.UpdatePullReview)
|
||||
m.Combo("/comments").
|
||||
Get(repo.GetPullReviewComments)
|
||||
m.Post("/dismissals", reqToken(), bind(api.DismissPullReviewOptions{}), repo.DismissPullReview)
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
audit_model "gitea.dev/models/audit"
|
||||
issues_model "gitea.dev/models/issues"
|
||||
"gitea.dev/models/organization"
|
||||
"gitea.dev/models/perm"
|
||||
access_model "gitea.dev/models/perm/access"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/git"
|
||||
@@ -674,6 +675,93 @@ func SubmitPullReview(ctx *context.APIContext) {
|
||||
ctx.JSON(http.StatusOK, apiReview)
|
||||
}
|
||||
|
||||
// UpdatePullReview updates the body of a submitted review
|
||||
func UpdatePullReview(ctx *context.APIContext) {
|
||||
// swagger:operation PUT /repos/{owner}/{repo}/pulls/{index}/reviews/{id} repository repoUpdatePullReview
|
||||
// ---
|
||||
// summary: Update the body of a submitted review
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: owner
|
||||
// in: path
|
||||
// description: owner of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: repo
|
||||
// in: path
|
||||
// description: name of the repo
|
||||
// type: string
|
||||
// required: true
|
||||
// - name: index
|
||||
// in: path
|
||||
// description: index of the pull request
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the review
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// - name: body
|
||||
// in: body
|
||||
// required: true
|
||||
// schema:
|
||||
// "$ref": "#/definitions/UpdatePullReviewOptions"
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/PullReview"
|
||||
// default:
|
||||
// "$ref": "#/responses/error"
|
||||
|
||||
opts := web.GetForm[*api.UpdatePullReviewOptions](ctx)
|
||||
review, _, isWrong := prepareSingleReview(ctx)
|
||||
if isWrong {
|
||||
return
|
||||
}
|
||||
|
||||
if !issue_service.CanSelfChangeOrAccessContent(ctx.Doer, ctx.Repo.Permission, review.Issue, review.ReviewerID, perm.AccessModeWrite) {
|
||||
ctx.APIError(http.StatusForbidden, "no permission to update review")
|
||||
return
|
||||
}
|
||||
|
||||
comments, err := issues_model.FindComments(ctx, &issues_model.FindCommentsOptions{
|
||||
Type: issues_model.CommentTypeReview,
|
||||
IssueID: review.IssueID,
|
||||
ReviewID: review.ID,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
if review.Type == issues_model.ReviewTypePending || len(comments) == 0 {
|
||||
ctx.APIError(http.StatusUnprocessableEntity, "only a submitted review can be updated")
|
||||
return
|
||||
}
|
||||
comment := comments[len(comments)-1] // older reviews created pending via the API also carry an earlier one
|
||||
|
||||
if opts.Body != comment.Content || opts.Body != review.Content {
|
||||
oldContent := comment.Content
|
||||
comment.Content = opts.Body
|
||||
if err := issue_service.UpdateComment(ctx, comment, comment.ContentVersion, ctx.Doer, oldContent); err != nil {
|
||||
ctx.APIErrorAuto(err)
|
||||
return
|
||||
}
|
||||
review.Content = opts.Body
|
||||
}
|
||||
|
||||
apiReview, err := convert.ToPullReview(ctx, review, ctx.Doer)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, apiReview)
|
||||
}
|
||||
|
||||
func isIssueLockedForDoer(ctx *context.APIContext, issue *issues_model.Issue) bool {
|
||||
if issue_service.IsLockedForDoer(issue, ctx.Repo.Permission) {
|
||||
ctx.APIError(http.StatusForbidden, "cannot comment on a locked issue")
|
||||
|
||||
@@ -183,6 +183,9 @@ type swaggerParameterBodies struct {
|
||||
// in:body
|
||||
SubmitPullReviewOptions api.SubmitPullReviewOptions
|
||||
|
||||
// in:body
|
||||
UpdatePullReviewOptions api.UpdatePullReviewOptions
|
||||
|
||||
// in:body
|
||||
DismissPullReviewOptions api.DismissPullReviewOptions
|
||||
|
||||
|
||||
+80
@@ -11276,6 +11276,20 @@
|
||||
"type": "object",
|
||||
"x-go-package": "gitea.dev/modules/structs"
|
||||
},
|
||||
"UpdatePullReviewOptions": {
|
||||
"description": "UpdatePullReviewOptions are options to update the body of a submitted pull request review",
|
||||
"properties": {
|
||||
"body": {
|
||||
"type": "string",
|
||||
"x-go-name": "Body"
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"body"
|
||||
],
|
||||
"type": "object",
|
||||
"x-go-package": "gitea.dev/modules/structs"
|
||||
},
|
||||
"UpdateRepoAvatarOption": {
|
||||
"description": "UpdateRepoAvatarUserOption options when updating the repo avatar",
|
||||
"properties": {
|
||||
@@ -30892,6 +30906,72 @@
|
||||
"tags": [
|
||||
"repository"
|
||||
]
|
||||
},
|
||||
"put": {
|
||||
"operationId": "repoUpdatePullReview",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "owner of the repo",
|
||||
"in": "path",
|
||||
"name": "owner",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "name of the repo",
|
||||
"in": "path",
|
||||
"name": "repo",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "index of the pull request",
|
||||
"in": "path",
|
||||
"name": "index",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"format": "int64",
|
||||
"type": "integer"
|
||||
}
|
||||
},
|
||||
{
|
||||
"description": "id of the review",
|
||||
"in": "path",
|
||||
"name": "id",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"format": "int64",
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
],
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/UpdatePullReviewOptions"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true,
|
||||
"x-originalParamName": "body"
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"$ref": "#/components/responses/PullReview"
|
||||
},
|
||||
"default": {
|
||||
"$ref": "#/components/responses/error"
|
||||
}
|
||||
},
|
||||
"summary": "Update the body of a submitted review",
|
||||
"tags": [
|
||||
"repository"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/repos/{owner}/{repo}/pulls/{index}/reviews/{id}/comments": {
|
||||
|
||||
+75
@@ -17528,6 +17528,67 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"put": {
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"repository"
|
||||
],
|
||||
"summary": "Update the body of a submitted review",
|
||||
"operationId": "repoUpdatePullReview",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "owner of the repo",
|
||||
"name": "owner",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "name of the repo",
|
||||
"name": "repo",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"description": "index of the pull request",
|
||||
"name": "index",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"description": "id of the review",
|
||||
"name": "id",
|
||||
"in": "path",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"name": "body",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/UpdatePullReviewOptions"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"$ref": "#/responses/PullReview"
|
||||
},
|
||||
"default": {
|
||||
"$ref": "#/responses/error"
|
||||
}
|
||||
}
|
||||
},
|
||||
"post": {
|
||||
"produces": [
|
||||
"application/json"
|
||||
@@ -34108,6 +34169,20 @@
|
||||
},
|
||||
"x-go-package": "gitea.dev/modules/structs"
|
||||
},
|
||||
"UpdatePullReviewOptions": {
|
||||
"description": "UpdatePullReviewOptions are options to update the body of a submitted pull request review",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"body"
|
||||
],
|
||||
"properties": {
|
||||
"body": {
|
||||
"type": "string",
|
||||
"x-go-name": "Body"
|
||||
}
|
||||
},
|
||||
"x-go-package": "gitea.dev/modules/structs"
|
||||
},
|
||||
"UpdateRepoAvatarOption": {
|
||||
"description": "UpdateRepoAvatarUserOption options when updating the repo avatar",
|
||||
"type": "object",
|
||||
|
||||
@@ -156,6 +156,10 @@ func testAPIPullReviewGeneral(t *testing.T) {
|
||||
assert.EqualValues(t, "PENDING", review.State)
|
||||
assert.Equal(t, 3, review.CodeCommentsCount)
|
||||
|
||||
updateReviewURL := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, review.ID)
|
||||
req = NewRequestWithJSON(t, http.MethodPut, updateReviewURL, &api.UpdatePullReviewOptions{Body: "just one nit"}).AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
||||
|
||||
// test SubmitPullReview
|
||||
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, review.ID), &api.SubmitPullReviewOptions{
|
||||
Event: "APPROVED",
|
||||
@@ -171,9 +175,18 @@ func testAPIPullReviewGeneral(t *testing.T) {
|
||||
reviewComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ReviewID: review.ID, Type: issues_model.CommentTypeReview})
|
||||
req = NewRequestWithJSON(t, http.MethodPatch, fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d", repo.OwnerName, repo.Name, reviewComment.ID), &api.EditIssueCommentOption{Body: "just one nit"}).AddTokenAuth(getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue))
|
||||
MakeRequest(t, req, http.StatusOK)
|
||||
req = NewRequest(t, http.MethodGet, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, review.ID)).AddTokenAuth(token)
|
||||
req = NewRequest(t, http.MethodGet, updateReviewURL).AddTokenAuth(token)
|
||||
assert.Equal(t, "just one nit", DecodeJSON(t, MakeRequest(t, req, http.StatusOK), &api.PullReview{}).Body)
|
||||
|
||||
for range 2 { // repeating the same body must not count as another edit
|
||||
req = NewRequestWithJSON(t, http.MethodPut, updateReviewURL, &api.UpdatePullReviewOptions{Body: "no nits"}).AddTokenAuth(token)
|
||||
review = DecodeJSON(t, MakeRequest(t, req, http.StatusOK), &api.PullReview{})
|
||||
assert.Equal(t, "no nits", review.Body)
|
||||
}
|
||||
reviewComment = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: reviewComment.ID})
|
||||
assert.Equal(t, "no nits", reviewComment.Content)
|
||||
assert.Equal(t, 2, reviewComment.ContentVersion)
|
||||
|
||||
// test dismiss review
|
||||
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d/dismissals", repo.OwnerName, repo.Name, pullIssue.Index, review.ID), &api.DismissPullReviewOptions{
|
||||
Message: "test",
|
||||
|
||||
Reference in new issue
Block a user