From 823518f8ae073ed9b8760ceb5e8ab6a2ab32da90 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Wed, 22 Jan 2025 16:18:26 +0100 Subject: [PATCH] filter dismissed via sql more & make it possible to have review requests falsly stayed in DB deleted --- models/issues/pull.go | 2 ++ models/issues/review.go | 18 +++++++++++++----- models/issues/review_list.go | 4 ++-- services/pull/pull.go | 2 ++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/models/issues/pull.go b/models/issues/pull.go index e3af00224de..0f3ddeaac3a 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -20,6 +20,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -401,6 +402,7 @@ func (pr *PullRequest) getReviewedByLines(ctx context.Context, writer io.Writer) Types: []ReviewType{ReviewTypeApprove}, IssueID: pr.IssueID, OfficialOnly: setting.Repository.PullRequest.DefaultMergeMessageOfficialApproversOnly, + Dismissed: optional.Some(true), }) if err != nil { log.Error("Unable to FindReviews for PR ID %d: %v", pr.ID, err) diff --git a/models/issues/review.go b/models/issues/review.go index 3e787273be8..577d93052ed 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -16,6 +16,7 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/optional" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/util" @@ -392,6 +393,7 @@ func GetCurrentReview(ctx context.Context, reviewer *user_model.User, issue *Iss Types: []ReviewType{ReviewTypePending}, IssueID: issue.ID, ReviewerID: reviewer.ID, + Dismissed: optional.Some(true), }) if err != nil { return nil, err @@ -534,12 +536,18 @@ func SubmitReview(ctx context.Context, doer *user_model.User, issue *Issue, revi } // GetReviewByIssueIDAndUserID get the latest review of reviewer for a pull request -func GetReviewByIssueIDAndUserID(ctx context.Context, issueID, userID int64) (*Review, error) { +func GetReviewByIssueIDAndUserID(ctx context.Context, issueID, userID int64, dismissed ...bool) (*Review, error) { review := new(Review) - has, err := db.GetEngine(ctx).Where( - builder.In("type", ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest). - And(builder.Eq{"issue_id": issueID, "reviewer_id": userID, "original_author_id": 0})). + cond := builder.In("type", ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest). + And(builder.Eq{"issue_id": issueID, "reviewer_id": userID, "original_author_id": 0}) + + // apply optional filter for dismissed + if len(dismissed) != 0 { + cond = cond.And(builder.Eq{"dismissed": dismissed[0]}) + } + + has, err := db.GetEngine(ctx).Where(cond). Desc("id"). Get(review) if err != nil { @@ -731,7 +739,7 @@ func RemoveReviewRequest(ctx context.Context, issue *Issue, reviewer, doer *user } defer committer.Close() - review, err := GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID) + review, err := GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID, false) if err != nil && !IsErrReviewNotExist(err) { return nil, err } diff --git a/models/issues/review_list.go b/models/issues/review_list.go index 928f24fb2dc..3f03347ac96 100644 --- a/models/issues/review_list.go +++ b/models/issues/review_list.go @@ -163,7 +163,7 @@ func GetReviewsByIssueID(ctx context.Context, issueID int64) (latestReviews, mig reviews := make([]*Review, 0, 10) // Get all reviews for the issue id - if err := db.GetEngine(ctx).Where("issue_id=?", issueID).OrderBy("updated_unix ASC").Find(&reviews); err != nil { + if err := db.GetEngine(ctx).Where("issue_id=? AND dismissed=?", issueID, false).OrderBy("updated_unix ASC").Find(&reviews); err != nil { return nil, nil, err } @@ -175,7 +175,7 @@ func GetReviewsByIssueID(ctx context.Context, issueID int64) (latestReviews, mig reviewTeamsMap := make(map[int64][]*Review) // key is reviewer team id countedReivewTypes := []ReviewType{ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest} for _, review := range reviews { - if review.ReviewerTeamID == 0 && slices.Contains(countedReivewTypes, review.Type) && !review.Dismissed { + if review.ReviewerTeamID == 0 && slices.Contains(countedReivewTypes, review.Type) { if review.OriginalAuthorID != 0 { originalReviewersMap[review.OriginalAuthorID] = append(originalReviewersMap[review.OriginalAuthorID], review) } else { diff --git a/services/pull/pull.go b/services/pull/pull.go index 5d3758eca6d..7d89e503fe8 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -30,6 +30,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/optional" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -1102,6 +1103,7 @@ func GetPullCommits(ctx *gitea_context.Context, issue *issues_model.Issue) ([]Co issues_model.ReviewTypeComment, issues_model.ReviewTypeReject, }, + Dismissed: optional.Some(false), }) if err != nil && !issues_model.IsErrReviewNotExist(err) {