diff --git a/models/issues/pull_list.go b/models/issues/pull_list.go index 9155ea0834685..418cd13e370cd 100644 --- a/models/issues/pull_list.go +++ b/models/issues/pull_list.go @@ -16,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" + "xorm.io/builder" "xorm.io/xorm" ) @@ -240,6 +241,65 @@ func (prs PullRequestList) GetIssueIDs() []int64 { }) } +func (prs PullRequestList) LoadReviewCommentsCounts(ctx context.Context) (map[int64]int, error) { + issueIDs := prs.GetIssueIDs() + countsMap := make(map[int64]int, len(issueIDs)) + counts := make([]struct { + IssueID int64 + Count int + }, 0, len(issueIDs)) + if err := db.GetEngine(ctx).Select("issue_id, count(*) as count"). + Table("comment").In("issue_id", issueIDs).And("type = ?", CommentTypeReview). + GroupBy("issue_id").Find(&counts); err != nil { + return nil, err + } + for _, c := range counts { + countsMap[c.IssueID] = c.Count + } + return countsMap, nil +} + +func (prs PullRequestList) LoadReviews(ctx context.Context) (ReviewList, error) { + issueIDs := prs.GetIssueIDs() + reviews := make([]*Review, 0, len(issueIDs)) + + subQuery := builder.Select("max(id) as id"). + From("review"). + Where(builder.In("issue_id", issueIDs)). + And(builder.In("`type`", ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest)). + And(builder.Eq{ + "dismissed": false, + "original_author_id": 0, + "reviewer_team_id": 0, + }). + GroupBy("issue_id, reviewer_id") + // Get latest review of each reviewer, sorted in order they were made + if err := db.GetEngine(ctx).In("id", subQuery).OrderBy("review.updated_unix ASC").Find(&reviews); err != nil { + return nil, err + } + + teamReviewRequests := make([]*Review, 0, 5) + subQueryTeam := builder.Select("max(id) as id"). + From("review"). + Where(builder.In("issue_id", issueIDs)). + And(builder.Eq{ + "original_author_id": 0, + }).And(builder.Neq{ + "reviewer_team_id": 0, + }). + GroupBy("issue_id, reviewer_team_id"). + OrderBy("review.updated_unix ASC") + if err := db.GetEngine(ctx).In("id", subQueryTeam).Find(&teamReviewRequests); err != nil { + return nil, err + } + + if len(teamReviewRequests) > 0 { + reviews = append(reviews, teamReviewRequests...) + } + + return reviews, nil +} + // HasMergedPullRequestInRepo returns whether the user(poster) has merged pull-request in the repo func HasMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (bool, error) { return db.GetEngine(ctx). diff --git a/models/issues/review_list.go b/models/issues/review_list.go index a5ceb2179116c..bc7d7ec0f0168 100644 --- a/models/issues/review_list.go +++ b/models/issues/review_list.go @@ -47,14 +47,9 @@ func (reviews ReviewList) LoadReviewersTeams(ctx context.Context) error { } } - teamsMap := make(map[int64]*organization_model.Team, 0) - for _, teamID := range reviewersTeamsIDs { - team, err := organization_model.GetTeamByID(ctx, teamID) - if err != nil { - return err - } - - teamsMap[teamID] = team + teamsMap, err := organization_model.GetTeamsByIDs(ctx, reviewersTeamsIDs) + if err != nil { + return err } for _, review := range reviews { diff --git a/models/organization/team_list.go b/models/organization/team_list.go index cc2a50236a447..4ceb405e31004 100644 --- a/models/organization/team_list.go +++ b/models/organization/team_list.go @@ -131,3 +131,8 @@ func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) { teams := make([]*Team, 0, 10) return teams, db.GetEngine(ctx).Where(builder.In("org_id", orgIDs)).Find(&teams) } + +func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) { + teams := make(map[int64]*Team, len(teamIDs)) + return teams, db.GetEngine(ctx).Where(builder.In("`id`", teamIDs)).Find(&teams) +} diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index 28d7379f07b8a..d8af69a905770 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -139,42 +139,11 @@ func ListPullRequests(ctx *context.APIContext) { return } - apiPrs := make([]*api.PullRequest, len(prs)) - // NOTE: load repository first, so that issue.Repo will be filled with pr.BaseRepo - if err := prs.LoadRepositories(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadRepositories", err) - return - } - issueList, err := prs.LoadIssues(ctx) + apiPrs, err := convert.ToAPIPullRequests(ctx, ctx.Repo.Repository, prs, ctx.Doer) if err != nil { - ctx.Error(http.StatusInternalServerError, "LoadIssues", err) - return - } - - if err := issueList.LoadLabels(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadLabels", err) - return - } - if err := issueList.LoadPosters(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadPoster", err) + ctx.Error(http.StatusInternalServerError, "ToAPIPullRequests", err) return } - if err := issueList.LoadAttachments(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadAttachments", err) - return - } - if err := issueList.LoadMilestones(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadMilestones", err) - return - } - if err := issueList.LoadAssignees(ctx); err != nil { - ctx.Error(http.StatusInternalServerError, "LoadAssignees", err) - return - } - - for i := range prs { - apiPrs[i] = convert.ToAPIPullRequest(ctx, prs[i], ctx.Doer) - } ctx.SetLinkHeader(int(maxResults), listOptions.PageSize) ctx.SetTotalCountHeader(maxResults) diff --git a/services/convert/pull.go b/services/convert/pull.go index 4ec24a8276a13..30028b9144bc5 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -7,9 +7,11 @@ import ( "context" "fmt" + git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" + repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" @@ -259,3 +261,261 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u return apiPullRequest } + +func ToAPIPullRequests(ctx context.Context, baseRepo *repo_model.Repository, prs issues_model.PullRequestList, doer *user_model.User) ([]*api.PullRequest, error) { + for _, pr := range prs { + pr.BaseRepo = baseRepo + if pr.BaseRepoID == pr.HeadRepoID { + pr.HeadRepo = baseRepo + } + } + + // NOTE: load head repositories + if err := prs.LoadRepositories(ctx); err != nil { + return nil, err + } + issueList, err := prs.LoadIssues(ctx) + if err != nil { + return nil, err + } + + if err := issueList.LoadLabels(ctx); err != nil { + return nil, err + } + if err := issueList.LoadPosters(ctx); err != nil { + return nil, err + } + if err := issueList.LoadAttachments(ctx); err != nil { + return nil, err + } + if err := issueList.LoadMilestones(ctx); err != nil { + return nil, err + } + if err := issueList.LoadAssignees(ctx); err != nil { + return nil, err + } + + reviews, err := prs.LoadReviews(ctx) + if err != nil { + return nil, err + } + if err = reviews.LoadReviewers(ctx); err != nil { + return nil, err + } + + reviewersMap := make(map[int64][]*user_model.User) + for _, review := range reviews { + if review.ReviewerID != 0 { + reviewersMap[review.IssueID] = append(reviewersMap[review.IssueID], review.Reviewer) + } + } + + reviewCounts, err := prs.LoadReviewCommentsCounts(ctx) + if err != nil { + return nil, err + } + + gitRepo, err := gitrepo.OpenRepository(ctx, baseRepo) + if err != nil { + return nil, err + } + defer gitRepo.Close() + + var doerID int64 + if doer != nil { + doerID = doer.ID + } + + const repoDoerPermCacheKey = "repo_doer_perm_cache" + p, err := cache.GetWithContextCache(ctx, repoDoerPermCacheKey, fmt.Sprintf("%d_%d", baseRepo.ID, doerID), + func() (access_model.Permission, error) { + return access_model.GetUserRepoPermission(ctx, baseRepo, doer) + }) + if err != nil { + log.Error("GetUserRepoPermission[%d]: %v", baseRepo.ID, err) + p.AccessMode = perm.AccessModeNone + } + + apiRepo := ToRepo(ctx, baseRepo, p) + baseBranchCache := make(map[string]*git_model.Branch) + apiPullRequests := make([]*api.PullRequest, 0, len(prs)) + for _, pr := range prs { + apiIssue := ToAPIIssue(ctx, doer, pr.Issue) + + apiPullRequest := &api.PullRequest{ + ID: pr.ID, + URL: pr.Issue.HTMLURL(), + Index: pr.Index, + Poster: apiIssue.Poster, + Title: apiIssue.Title, + Body: apiIssue.Body, + Labels: apiIssue.Labels, + Milestone: apiIssue.Milestone, + Assignee: apiIssue.Assignee, + Assignees: apiIssue.Assignees, + State: apiIssue.State, + Draft: pr.IsWorkInProgress(ctx), + IsLocked: apiIssue.IsLocked, + Comments: apiIssue.Comments, + ReviewComments: reviewCounts[pr.IssueID], + HTMLURL: pr.Issue.HTMLURL(), + DiffURL: pr.Issue.DiffURL(), + PatchURL: pr.Issue.PatchURL(), + HasMerged: pr.HasMerged, + MergeBase: pr.MergeBase, + Mergeable: pr.Mergeable(ctx), + Deadline: apiIssue.Deadline, + Created: pr.Issue.CreatedUnix.AsTimePtr(), + Updated: pr.Issue.UpdatedUnix.AsTimePtr(), + PinOrder: apiIssue.PinOrder, + + AllowMaintainerEdit: pr.AllowMaintainerEdit, + + Base: &api.PRBranchInfo{ + Name: pr.BaseBranch, + Ref: pr.BaseBranch, + RepoID: pr.BaseRepoID, + Repository: apiRepo, + }, + Head: &api.PRBranchInfo{ + Name: pr.HeadBranch, + Ref: fmt.Sprintf("%s%d/head", git.PullPrefix, pr.Index), + RepoID: -1, + }, + } + + pr.RequestedReviewers = reviewersMap[pr.IssueID] + for _, reviewer := range pr.RequestedReviewers { + apiPullRequest.RequestedReviewers = append(apiPullRequest.RequestedReviewers, ToUser(ctx, reviewer, nil)) + } + + for _, reviewerTeam := range pr.RequestedReviewersTeams { + convertedTeam, err := ToTeam(ctx, reviewerTeam, true) + if err != nil { + log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err) + return nil, err + } + + apiPullRequest.RequestedReviewersTeams = append(apiPullRequest.RequestedReviewersTeams, convertedTeam) + } + + if pr.Issue.ClosedUnix != 0 { + apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr() + } + + baseBranch, ok := baseBranchCache[pr.BaseBranch] + if !ok { + baseBranch, err = git_model.GetBranch(ctx, baseRepo.ID, pr.BaseBranch) + if err == nil { + baseBranchCache[pr.BaseBranch] = baseBranch + } else if !git_model.IsErrBranchNotExist(err) { + return nil, err + } + } + + if baseBranch != nil { + apiPullRequest.Base.Sha = baseBranch.CommitID + } + + if pr.Flow == issues_model.PullRequestFlowAGit { + apiPullRequest.Head.Sha, err = gitRepo.GetRefCommitID(pr.GetGitRefName()) + if err != nil { + log.Error("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err) + return nil, err + } + apiPullRequest.Head.RepoID = pr.BaseRepoID + apiPullRequest.Head.Repository = apiPullRequest.Base.Repository + apiPullRequest.Head.Name = "" + } + + var headGitRepo *git.Repository + if pr.HeadRepo != nil && pr.Flow == issues_model.PullRequestFlowGithub { + if pr.HeadRepoID == pr.BaseRepoID { + apiPullRequest.Head.RepoID = pr.HeadRepo.ID + apiPullRequest.Head.Repository = apiRepo + headGitRepo = gitRepo + } else { + p, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer) + if err != nil { + log.Error("GetUserRepoPermission[%d]: %v", pr.HeadRepoID, err) + p.AccessMode = perm.AccessModeNone + } + + apiPullRequest.Head.RepoID = pr.HeadRepo.ID + apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p) + + headGitRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo) + if err != nil { + log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err) + return nil, err + } + defer headGitRepo.Close() + } + + headBranch, err := headGitRepo.GetBranch(pr.HeadBranch) + if err != nil && !git.IsErrBranchNotExist(err) { + log.Error("GetBranch[%s]: %v", pr.HeadBranch, err) + return nil, err + } + + // Outer scope variables to be used in diff calculation + var ( + startCommitID string + endCommitID string + ) + + if git.IsErrBranchNotExist(err) { + headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref) + if err != nil && !git.IsErrNotExist(err) { + log.Error("GetCommit[%s]: %v", pr.HeadBranch, err) + return nil, err + } + if err == nil { + apiPullRequest.Head.Sha = headCommitID + endCommitID = headCommitID + } + } else { + commit, err := headBranch.GetCommit() + if err != nil && !git.IsErrNotExist(err) { + log.Error("GetCommit[%s]: %v", headBranch.Name, err) + return nil, err + } + if err == nil { + apiPullRequest.Head.Ref = pr.HeadBranch + apiPullRequest.Head.Sha = commit.ID.String() + endCommitID = commit.ID.String() + } + } + + // Calculate diff + startCommitID = pr.MergeBase + + apiPullRequest.ChangedFiles, apiPullRequest.Additions, apiPullRequest.Deletions, err = gitRepo.GetDiffShortStat(startCommitID, endCommitID) + if err != nil { + log.Error("GetDiffShortStat: %v", err) + } + } + + if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 { + refs, err := gitRepo.GetRefsFiltered(apiPullRequest.Head.Ref) + if err != nil { + log.Error("GetRefsFiltered[%s]: %v", apiPullRequest.Head.Ref, err) + return nil, err + } else if len(refs) == 0 { + log.Error("unable to resolve PR head ref") + } else { + apiPullRequest.Head.Sha = refs[0].Object.String() + } + } + + if pr.HasMerged { + apiPullRequest.Merged = pr.MergedUnix.AsTimePtr() + apiPullRequest.MergedCommitID = &pr.MergedCommitID + apiPullRequest.MergedBy = ToUser(ctx, pr.Merger, nil) + } + + apiPullRequests = append(apiPullRequests, apiPullRequest) + } + + return apiPullRequests, nil +}