2019-10-28 07:41:50 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-28 07:41:50 +05:30
|
|
|
|
|
|
|
package issue
|
|
|
|
|
|
|
|
import (
|
2022-04-28 17:18:48 +05:30
|
|
|
"context"
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2024-02-24 18:08:43 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-10-20 23:48:25 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service "code.gitea.io/gitea/services/notify"
|
2019-10-28 07:41:50 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array
|
2023-04-14 23:48:28 +05:30
|
|
|
func DeleteNotPassedAssignee(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assignees []*user_model.User) (err error) {
|
2019-10-28 07:41:50 +05:30
|
|
|
var found bool
|
2022-05-20 19:38:52 +05:30
|
|
|
oriAssignes := make([]*user_model.User, len(issue.Assignees))
|
|
|
|
_ = copy(oriAssignes, issue.Assignees)
|
2019-10-28 07:41:50 +05:30
|
|
|
|
2022-05-20 19:38:52 +05:30
|
|
|
for _, assignee := range oriAssignes {
|
2019-10-28 07:41:50 +05:30
|
|
|
found = false
|
|
|
|
for _, alreadyAssignee := range assignees {
|
|
|
|
if assignee.ID == alreadyAssignee.ID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2021-07-08 17:08:13 +05:30
|
|
|
// This function also does comments and hooks, which is why we call it separately instead of directly removing the assignees here
|
2023-08-10 08:09:21 +05:30
|
|
|
if _, _, err := ToggleAssigneeWithNotify(ctx, issue, doer, assignee.ID); err != nil {
|
2019-10-28 07:41:50 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 08:09:21 +05:30
|
|
|
// ToggleAssigneeWithNoNotify changes a user between assigned and not assigned for this issue, and make issue comment for it.
|
|
|
|
func ToggleAssigneeWithNotify(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *issues_model.Comment, err error) {
|
2023-04-14 23:48:28 +05:30
|
|
|
removed, comment, err = issues_model.ToggleIssueAssignee(ctx, issue, doer, assigneeID)
|
2019-10-28 07:41:50 +05:30
|
|
|
if err != nil {
|
2023-07-07 11:01:56 +05:30
|
|
|
return false, nil, err
|
2019-10-28 07:41:50 +05:30
|
|
|
}
|
|
|
|
|
2023-07-07 11:01:56 +05:30
|
|
|
assignee, err := user_model.GetUserByID(ctx, assigneeID)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
2019-10-28 07:41:50 +05:30
|
|
|
}
|
|
|
|
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service.IssueChangeAssignee(ctx, doer, issue, assignee, removed, comment)
|
2019-10-28 07:41:50 +05:30
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return removed, comment, err
|
2019-10-28 07:41:50 +05:30
|
|
|
}
|
2020-04-06 22:03:34 +05:30
|
|
|
|
2020-10-13 01:25:13 +05:30
|
|
|
// ReviewRequest add or remove a review request from a user for this PR, and make comment for it.
|
2023-04-14 23:48:28 +05:30
|
|
|
func ReviewRequest(ctx context.Context, issue *issues_model.Issue, doer, reviewer *user_model.User, isAdd bool) (comment *issues_model.Comment, err error) {
|
2020-04-06 22:03:34 +05:30
|
|
|
if isAdd {
|
2023-08-10 08:09:21 +05:30
|
|
|
comment, err = issues_model.AddReviewRequest(ctx, issue, reviewer, doer)
|
2020-04-06 22:03:34 +05:30
|
|
|
} else {
|
2023-08-10 08:09:21 +05:30
|
|
|
comment, err = issues_model.RemoveReviewRequest(ctx, issue, reviewer, doer)
|
2020-04-06 22:03:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2020-10-20 23:48:25 +05:30
|
|
|
return nil, err
|
2020-04-06 22:03:34 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if comment != nil {
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service.PullRequestReviewRequest(ctx, doer, issue, reviewer, isAdd, comment)
|
2020-04-06 22:03:34 +05:30
|
|
|
}
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return comment, err
|
2020-10-20 23:48:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidReviewRequest Check permission for ReviewRequest
|
2022-06-13 15:07:59 +05:30
|
|
|
func IsValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User, isAdd bool, issue *issues_model.Issue, permDoer *access_model.Permission) error {
|
2020-10-20 23:48:25 +05:30
|
|
|
if reviewer.IsOrganization() {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Organization can't be added as reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if doer.IsOrganization() {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Organization can't be doer to add reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
permReviewer, err := access_model.GetUserRepoPermission(ctx, issue.Repo, reviewer)
|
2020-10-20 23:48:25 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if permDoer == nil {
|
2022-05-11 15:39:36 +05:30
|
|
|
permDoer = new(access_model.Permission)
|
|
|
|
*permDoer, err = access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
|
2020-10-20 23:48:25 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
lastreview, err := issues_model.GetReviewByIssueIDAndUserID(ctx, issue.ID, reviewer.ID)
|
|
|
|
if err != nil && !issues_model.IsErrReviewNotExist(err) {
|
2020-10-20 23:48:25 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:08:43 +05:30
|
|
|
canDoerChangeReviewRequests := CanDoerChangeReviewRequests(ctx, doer, issue.Repo, issue)
|
|
|
|
|
2020-10-20 23:48:25 +05:30
|
|
|
if isAdd {
|
2024-02-24 18:08:43 +05:30
|
|
|
if !permReviewer.CanAccessAny(perm.AccessModeRead, unit.TypePullRequests) {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Reviewer can't read",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if reviewer.ID == issue.PosterID && issue.OriginalAuthorID == 0 {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "poster of pr can't be reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 18:08:43 +05:30
|
|
|
|
|
|
|
if canDoerChangeReviewRequests {
|
2020-10-20 23:48:25 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:08:43 +05:30
|
|
|
if doer.ID == issue.PosterID && issue.OriginalAuthorID == 0 && lastreview != nil && lastreview.Type != issues_model.ReviewTypeRequest {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
|
|
|
Reason: "Doer can't choose reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
2020-10-20 23:48:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:08:43 +05:30
|
|
|
if canDoerChangeReviewRequests {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if lastreview != nil && lastreview.Type == issues_model.ReviewTypeRequest && lastreview.ReviewerID == doer.ID {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
|
|
|
Reason: "Doer can't remove reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
2020-10-20 23:48:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IsValidTeamReviewRequest Check permission for ReviewRequest Team
|
2022-06-13 15:07:59 +05:30
|
|
|
func IsValidTeamReviewRequest(ctx context.Context, reviewer *organization.Team, doer *user_model.User, isAdd bool, issue *issues_model.Issue) error {
|
2020-10-20 23:48:25 +05:30
|
|
|
if doer.IsOrganization() {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Organization can't be doer to add reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:08:43 +05:30
|
|
|
canDoerChangeReviewRequests := CanDoerChangeReviewRequests(ctx, doer, issue.Repo, issue)
|
2020-10-20 23:48:25 +05:30
|
|
|
|
|
|
|
if isAdd {
|
|
|
|
if issue.Repo.IsPrivate {
|
2022-04-28 17:18:48 +05:30
|
|
|
hasTeam := organization.HasTeamRepo(ctx, reviewer.OrgID, reviewer.ID, issue.RepoID)
|
2020-10-20 23:48:25 +05:30
|
|
|
|
|
|
|
if !hasTeam {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Reviewing team can't read repo",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:08:43 +05:30
|
|
|
if canDoerChangeReviewRequests {
|
|
|
|
return nil
|
2020-10-20 23:48:25 +05:30
|
|
|
}
|
2024-02-24 18:08:43 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2024-02-24 18:08:43 +05:30
|
|
|
Reason: "Doer can't choose reviewer",
|
2020-10-20 23:48:25 +05:30
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 18:08:43 +05:30
|
|
|
if canDoerChangeReviewRequests {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
|
|
|
Reason: "Doer can't remove reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
2020-04-06 22:03:34 +05:30
|
|
|
}
|
2020-10-13 01:25:13 +05:30
|
|
|
|
|
|
|
// TeamReviewRequest add or remove a review request from a team for this PR, and make comment for it.
|
2023-04-14 23:48:28 +05:30
|
|
|
func TeamReviewRequest(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool) (comment *issues_model.Comment, err error) {
|
2020-10-13 01:25:13 +05:30
|
|
|
if isAdd {
|
2023-08-10 08:09:21 +05:30
|
|
|
comment, err = issues_model.AddTeamReviewRequest(ctx, issue, reviewer, doer)
|
2020-10-13 01:25:13 +05:30
|
|
|
} else {
|
2023-08-10 08:09:21 +05:30
|
|
|
comment, err = issues_model.RemoveTeamReviewRequest(ctx, issue, reviewer, doer)
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-07-07 11:01:56 +05:30
|
|
|
return nil, err
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if comment == nil || !isAdd {
|
2023-07-07 11:01:56 +05:30
|
|
|
return nil, nil
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
2024-03-19 10:58:43 +05:30
|
|
|
return comment, teamReviewRequestNotify(ctx, issue, doer, reviewer, isAdd, comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReviewRequestNotify(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, reviewNotifers []*ReviewRequestNotifier) {
|
|
|
|
for _, reviewNotifer := range reviewNotifers {
|
2024-05-09 19:19:37 +05:30
|
|
|
if reviewNotifer.Reviewer != nil {
|
|
|
|
notify_service.PullRequestReviewRequest(ctx, issue.Poster, issue, reviewNotifer.Reviewer, reviewNotifer.IsAdd, reviewNotifer.Comment)
|
2024-03-19 10:58:43 +05:30
|
|
|
} else if reviewNotifer.ReviewTeam != nil {
|
|
|
|
if err := teamReviewRequestNotify(ctx, issue, issue.Poster, reviewNotifer.ReviewTeam, reviewNotifer.IsAdd, reviewNotifer.Comment); err != nil {
|
|
|
|
log.Error("teamReviewRequestNotify: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// teamReviewRequestNotify notify all user in this team
|
|
|
|
func teamReviewRequestNotify(ctx context.Context, issue *issues_model.Issue, doer *user_model.User, reviewer *organization.Team, isAdd bool, comment *issues_model.Comment) error {
|
2020-10-13 01:25:13 +05:30
|
|
|
// notify all user in this team
|
2023-07-07 11:01:56 +05:30
|
|
|
if err := comment.LoadIssue(ctx); err != nil {
|
2024-03-19 10:58:43 +05:30
|
|
|
return err
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
2023-04-14 23:48:28 +05:30
|
|
|
members, err := organization.GetTeamMembers(ctx, &organization.SearchMembersOptions{
|
2022-03-29 11:59:02 +05:30
|
|
|
TeamID: reviewer.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-03-19 10:58:43 +05:30
|
|
|
return err
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
for _, member := range members {
|
2020-10-13 01:25:13 +05:30
|
|
|
if member.ID == comment.Issue.PosterID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
comment.AssigneeID = member.ID
|
2023-09-06 00:07:47 +05:30
|
|
|
notify_service.PullRequestReviewRequest(ctx, doer, issue, member, isAdd, comment)
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
2024-03-19 10:58:43 +05:30
|
|
|
return err
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
2024-02-24 18:08:43 +05:30
|
|
|
|
|
|
|
// CanDoerChangeReviewRequests returns if the doer can add/remove review requests of a PR
|
|
|
|
func CanDoerChangeReviewRequests(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue) bool {
|
|
|
|
// The poster of the PR can change the reviewers
|
|
|
|
if doer.ID == issue.PosterID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// The owner of the repo can change the reviewers
|
|
|
|
if doer.ID == repo.OwnerID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collaborators of the repo can change the reviewers
|
|
|
|
isCollaborator, err := repo_model.IsCollaborator(ctx, repo.ID, doer.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("IsCollaborator: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if isCollaborator {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the repo's owner is an organization, members of teams with read permission on pull requests can change reviewers
|
|
|
|
if repo.Owner.IsOrganization() {
|
|
|
|
teams, err := organization.GetTeamsWithAccessToRepo(ctx, repo.OwnerID, repo.ID, perm.AccessModeRead)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("GetTeamsWithAccessToRepo: %v", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, team := range teams {
|
|
|
|
if !team.UnitEnabled(ctx, unit.TypePullRequests) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
isMember, err := organization.IsTeamMember(ctx, repo.OwnerID, team.ID, doer.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("IsTeamMember: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if isMember {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|