Skip to content

Commit

Permalink
simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
a1012112796 committed Nov 22, 2020
1 parent a060513 commit 26a5999
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 78 deletions.
21 changes: 4 additions & 17 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,26 +559,13 @@ func MarkReviewsAsNotStale(issueID int64, commitID string) (err error) {
return
}

// MarkReviewAsDismissed marks existing reviews as stale
func MarkReviewAsDismissed(review *Review) (err error) {
if review.Dismissed || (review.Type != ReviewTypeApprove && review.Type != ReviewTypeReject) {
// DismissReview change the dismiss status of a review
func DismissReview(review *Review, isDismiss bool) (err error) {
if review.Dismissed == isDismiss || (review.Type != ReviewTypeApprove && review.Type != ReviewTypeReject) {
return nil
}

review.Dismissed = true

_, err = x.Cols("dismissed").Update(review)

return
}

// MarkReviewAsUnDismissed marks dismissed reviews as not dismissed
func MarkReviewAsUnDismissed(review *Review) (err error) {
if !review.Dismissed || (review.Type != ReviewTypeApprove && review.Type != ReviewTypeReject) {
return nil
}

review.Dismissed = false
review.Dismissed = isDismiss

_, err = x.Cols("dismissed").Update(review)

Expand Down
10 changes: 6 additions & 4 deletions models/review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,12 @@ func TestGetReviewersByIssueID(t *testing.T) {
}
}

func TestMarkReviewAsDismissed(t *testing.T) {
func TestDismissReview(t *testing.T) {
review1 := AssertExistsAndLoadBean(t, &Review{ID: 9}).(*Review)
review2 := AssertExistsAndLoadBean(t, &Review{ID: 11}).(*Review)
assert.NoError(t, MarkReviewAsDismissed(review1))
assert.NoError(t, MarkReviewAsDismissed(review2))
assert.NoError(t, MarkReviewAsUnDismissed(review2))
assert.NoError(t, DismissReview(review1, true))
assert.NoError(t, DismissReview(review2, true))
assert.NoError(t, DismissReview(review2, true))
assert.NoError(t, DismissReview(review2, false))
assert.NoError(t, DismissReview(review2, false))
}
46 changes: 7 additions & 39 deletions routers/api/v1/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,43 +794,7 @@ func DismissPullReview(ctx *context.APIContext, opts api.DismissPullReviewOption
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
if !ctx.Repo.IsAdmin() {
ctx.Error(http.StatusForbidden, "", "Must be repo admin")
return
}
review, pr, isWrong := prepareSingleReview(ctx)
if isWrong {
return
}

if review.Type != models.ReviewTypeApprove && review.Type != models.ReviewTypeReject {
ctx.Error(http.StatusForbidden, "", "not need to dismiss this review because it's type is not Approve or change request")
return
}

if pr.Issue.IsClosed {
ctx.Error(http.StatusForbidden, "", "not need to dismiss this review because this pr is closed")
return
}

_, err := pull_service.DismissReview(review.ID, opts.Message, ctx.User)
if err != nil {
ctx.Error(http.StatusInternalServerError, "pull_service.DismissReview", err)
return
}

if review, err = models.GetReviewByID(review.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "GetReviewByID", err)
return
}

// convert response
apiReview, err := convert.ToPullReview(review, ctx.User)
if err != nil {
ctx.Error(http.StatusInternalServerError, "convertToPullReview", err)
return
}
ctx.JSON(http.StatusOK, apiReview)
disMissReview(ctx, true)
}

// UnDismissPullReview cancel to dismiss a review for a pull request
Expand Down Expand Up @@ -870,6 +834,10 @@ func UnDismissPullReview(ctx *context.APIContext) {
// "$ref": "#/responses/forbidden"
// "422":
// "$ref": "#/responses/validationError"
disMissReview(ctx, false)
}

func disMissReview(ctx *context.APIContext, isDismiss bool) {
if !ctx.Repo.IsAdmin() {
ctx.Error(http.StatusForbidden, "", "Must be repo admin")
return
Expand All @@ -889,12 +857,12 @@ func UnDismissPullReview(ctx *context.APIContext) {
return
}

if err := pull_service.UnDismissReview(review.ID); err != nil {
_, err := pull_service.DismissReview(review.ID, opts.Message, ctx.User, isDismiss)
if err != nil {
ctx.Error(http.StatusInternalServerError, "pull_service.DismissReview", err)
return
}

var err error
if review, err = models.GetReviewByID(review.ID); err != nil {
ctx.Error(http.StatusInternalServerError, "GetReviewByID", err)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func SubmitReview(ctx *context.Context, form auth.SubmitReviewForm) {

// DismissReview dismissing stale review by repo admin
func DismissReview(ctx *context.Context, form auth.DismissReviewForm) {
comm, err := pull_service.DismissReview(form.ReviewID, form.Message, ctx.User)
comm, err := pull_service.DismissReview(form.ReviewID, form.Message, ctx.User, true)
if err != nil {
ctx.ServerError("pull_service.DismissReview", err)
return
Expand Down
23 changes: 6 additions & 17 deletions services/pull/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func SubmitReview(doer *models.User, gitRepo *git.Repository, issue *models.Issu
}

// DismissReview dismissing stale review by repo admin
func DismissReview(reviewID int64, message string, doer *models.User) (comment *models.Comment, err error) {
func DismissReview(reviewID int64, message string, doer *models.User, isDismiss bool) (comment *models.Comment, err error) {
review, err := models.GetReviewByID(reviewID)
if err != nil {
return
Expand All @@ -242,10 +242,14 @@ func DismissReview(reviewID int64, message string, doer *models.User) (comment *
return nil, fmt.Errorf("Wrong using")
}

if err = models.MarkReviewAsDismissed(review); err != nil {
if err = models.DismissReview(review, isDismiss); err != nil {
return
}

if !isDismiss {
return nil, nil
}

// load data for notify
if err = review.LoadAttributes(); err != nil {
return
Expand Down Expand Up @@ -277,18 +281,3 @@ func DismissReview(reviewID int64, message string, doer *models.User) (comment *

return
}

// UnDismissReview cancel dismissed stale review by repo admin
func UnDismissReview(reviewID int64) (err error) {
review, err := models.GetReviewByID(reviewID)
if err != nil {
return
}

if review.Type != models.ReviewTypeApprove && review.Type != models.ReviewTypeReject {
return fmt.Errorf("Wrong using")
}

err = models.MarkReviewAsUnDismissed(review)
return
}

0 comments on commit 26a5999

Please sign in to comment.