-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Fix bug when pushing to a pull request which enabled dismiss approval automatically #25882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf32895
Fix bug when pushing to a pull request which enabled dismiss approval…
lunny 8ee2976
Fix test
lunny 12dff88
remove unused function.
lunny b670264
Merge branch 'main' into lunny/fix_dismiss_when_new_push
lunny 54b501a
Merge branch 'main' into lunny/fix_dismiss_when_new_push
GiteaBot 2ce367f
Merge branch 'main' into lunny/fix_dismiss_when_new_push
GiteaBot 820ec56
Merge branch 'main' into lunny/fix_dismiss_when_new_push
GiteaBot 4924d68
Merge branch 'main' into lunny/fix_dismiss_when_new_push
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package issues | ||
|
||
import ( | ||
"context" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/container" | ||
"code.gitea.io/gitea/modules/util" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
type ReviewList []*Review | ||
|
||
// LoadReviewers loads reviewers | ||
func (reviews ReviewList) LoadReviewers(ctx context.Context) error { | ||
reviewerIds := make([]int64, len(reviews)) | ||
for i := 0; i < len(reviews); i++ { | ||
reviewerIds[i] = reviews[i].ReviewerID | ||
} | ||
reviewers, err := user_model.GetPossibleUserByIDs(ctx, reviewerIds) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userMap := make(map[int64]*user_model.User, len(reviewers)) | ||
for _, reviewer := range reviewers { | ||
userMap[reviewer.ID] = reviewer | ||
} | ||
for _, review := range reviews { | ||
review.Reviewer = userMap[review.ReviewerID] | ||
} | ||
return nil | ||
} | ||
|
||
func (reviews ReviewList) LoadIssues(ctx context.Context) error { | ||
issueIds := container.Set[int64]{} | ||
for i := 0; i < len(reviews); i++ { | ||
issueIds.Add(reviews[i].IssueID) | ||
} | ||
|
||
issues, err := GetIssuesByIDs(ctx, issueIds.Values()) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := issues.LoadRepositories(ctx); err != nil { | ||
return err | ||
} | ||
issueMap := make(map[int64]*Issue, len(issues)) | ||
for _, issue := range issues { | ||
issueMap[issue.ID] = issue | ||
} | ||
|
||
for _, review := range reviews { | ||
review.Issue = issueMap[review.IssueID] | ||
} | ||
return nil | ||
} | ||
|
||
// FindReviewOptions represent possible filters to find reviews | ||
type FindReviewOptions struct { | ||
db.ListOptions | ||
Type ReviewType | ||
IssueID int64 | ||
ReviewerID int64 | ||
OfficialOnly bool | ||
Dismissed util.OptionalBool | ||
} | ||
|
||
func (opts *FindReviewOptions) toCond() builder.Cond { | ||
cond := builder.NewCond() | ||
if opts.IssueID > 0 { | ||
cond = cond.And(builder.Eq{"issue_id": opts.IssueID}) | ||
} | ||
if opts.ReviewerID > 0 { | ||
cond = cond.And(builder.Eq{"reviewer_id": opts.ReviewerID}) | ||
} | ||
if opts.Type != ReviewTypeUnknown { | ||
cond = cond.And(builder.Eq{"type": opts.Type}) | ||
} | ||
if opts.OfficialOnly { | ||
cond = cond.And(builder.Eq{"official": true}) | ||
} | ||
if !opts.Dismissed.IsNone() { | ||
cond = cond.And(builder.Eq{"dismissed": opts.Dismissed.IsTrue()}) | ||
} | ||
return cond | ||
} | ||
|
||
// FindReviews returns reviews passing FindReviewOptions | ||
func FindReviews(ctx context.Context, opts FindReviewOptions) (ReviewList, error) { | ||
reviews := make([]*Review, 0, 10) | ||
sess := db.GetEngine(ctx).Where(opts.toCond()) | ||
if opts.Page > 0 && !opts.IsListAll() { | ||
sess = db.SetSessionPagination(sess, &opts) | ||
} | ||
return reviews, sess. | ||
Asc("created_unix"). | ||
Asc("id"). | ||
Find(&reviews) | ||
} | ||
|
||
// FindLatestReviews returns only latest reviews per user, passing FindReviewOptions | ||
func FindLatestReviews(ctx context.Context, opts FindReviewOptions) (ReviewList, error) { | ||
reviews := make([]*Review, 0, 10) | ||
cond := opts.toCond() | ||
sess := db.GetEngine(ctx).Where(cond) | ||
if opts.Page > 0 { | ||
sess = db.SetSessionPagination(sess, &opts) | ||
} | ||
|
||
sess.In("id", builder. | ||
Select("max ( id ) "). | ||
From("review"). | ||
Where(cond). | ||
GroupBy("reviewer_id")) | ||
|
||
return reviews, sess. | ||
Asc("created_unix"). | ||
Asc("id"). | ||
Find(&reviews) | ||
} | ||
|
||
// CountReviews returns count of reviews passing FindReviewOptions | ||
func CountReviews(opts FindReviewOptions) (int64, error) { | ||
return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&Review{}) | ||
} | ||
|
||
// GetReviewersFromOriginalAuthorsByIssueID gets the latest review of each original authors for a pull request | ||
func GetReviewersFromOriginalAuthorsByIssueID(issueID int64) (ReviewList, error) { | ||
reviews := make([]*Review, 0, 10) | ||
|
||
// Get latest review of each reviewer, sorted in order they were made | ||
if err := db.GetEngine(db.DefaultContext).SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND original_author_id <> 0 GROUP BY issue_id, original_author_id) ORDER BY review.updated_unix ASC", | ||
issueID, ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest). | ||
Find(&reviews); err != nil { | ||
return nil, err | ||
} | ||
|
||
return reviews, nil | ||
} | ||
|
||
// GetReviewsByIssueID gets the latest review of each reviewer for a pull request | ||
func GetReviewsByIssueID(issueID int64) (ReviewList, error) { | ||
reviews := make([]*Review, 0, 10) | ||
|
||
sess := db.GetEngine(db.DefaultContext) | ||
|
||
// Get latest review of each reviewer, sorted in order they were made | ||
if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND dismissed = ? AND original_author_id = 0 GROUP BY issue_id, reviewer_id) ORDER BY review.updated_unix ASC", | ||
issueID, ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest, false). | ||
Find(&reviews); err != nil { | ||
return nil, err | ||
} | ||
|
||
teamReviewRequests := make([]*Review, 0, 5) | ||
if err := sess.SQL("SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id <> 0 AND original_author_id = 0 GROUP BY issue_id, reviewer_team_id) ORDER BY review.updated_unix ASC", | ||
issueID). | ||
Find(&teamReviewRequests); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(teamReviewRequests) > 0 { | ||
reviews = append(reviews, teamReviewRequests...) | ||
} | ||
|
||
return reviews, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.