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-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
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"
|
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"
|
2019-10-28 07:41:50 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeleteNotPassedAssignee deletes all assignees who aren't passed via the "assignees" array
|
2022-06-13 15:07:59 +05:30
|
|
|
func DeleteNotPassedAssignee(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
|
2019-10-28 07:41:50 +05:30
|
|
|
if _, _, err := ToggleAssignee(issue, doer, assignee.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToggleAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
|
2022-06-13 15:07:59 +05:30
|
|
|
func ToggleAssignee(issue *issues_model.Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *issues_model.Comment, err error) {
|
|
|
|
removed, comment, err = issues_model.ToggleIssueAssignee(issue, doer, assigneeID)
|
2019-10-28 07:41:50 +05:30
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-03 08:18:26 +05:30
|
|
|
assignee, err1 := user_model.GetUserByID(db.DefaultContext, assigneeID)
|
2019-10-28 07:41:50 +05:30
|
|
|
if err1 != nil {
|
|
|
|
err = err1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyIssueChangeAssignee(db.DefaultContext, 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.
|
2022-06-13 15:07:59 +05:30
|
|
|
func ReviewRequest(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 {
|
2022-06-13 15:07:59 +05:30
|
|
|
comment, err = issues_model.AddReviewRequest(issue, reviewer, doer)
|
2020-04-06 22:03:34 +05:30
|
|
|
} else {
|
2022-06-13 15:07:59 +05:30
|
|
|
comment, err = issues_model.RemoveReviewRequest(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 {
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyPullReviewRequest(db.DefaultContext, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
var pemResult bool
|
|
|
|
if isAdd {
|
2021-11-28 17:28:28 +05:30
|
|
|
pemResult = permReviewer.CanAccessAny(perm.AccessModeRead, unit.TypePullRequests)
|
2020-10-20 23:48:25 +05:30
|
|
|
if !pemResult {
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if doer.ID == issue.PosterID && issue.OriginalAuthorID == 0 && lastreview != nil && lastreview.Type != issues_model.ReviewTypeRequest {
|
2020-10-20 23:48:25 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:57:47 +05:30
|
|
|
pemResult = doer.ID == issue.PosterID
|
|
|
|
if !pemResult {
|
|
|
|
pemResult = permDoer.CanAccessAny(perm.AccessModeWrite, unit.TypePullRequests)
|
|
|
|
}
|
2020-10-20 23:48:25 +05:30
|
|
|
if !pemResult {
|
2022-06-13 15:07:59 +05:30
|
|
|
pemResult, err = issues_model.IsOfficialReviewer(ctx, issue, doer)
|
2020-10-20 23:48:25 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !pemResult {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Doer can't choose reviewer",
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-13 15:07:59 +05:30
|
|
|
if lastreview != nil && lastreview.Type == issues_model.ReviewTypeRequest && lastreview.ReviewerID == doer.ID {
|
2020-10-20 23:48:25 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pemResult = permDoer.IsAdmin()
|
|
|
|
if !pemResult {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Doer is not admin",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 15:39:36 +05:30
|
|
|
permission, err := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
|
2020-10-20 23:48:25 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to GetUserRepoPermission for %-v in %-v#%d", doer, issue.Repo, issue.Index)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 17:28:28 +05:30
|
|
|
doerCanWrite := permission.CanAccessAny(perm.AccessModeWrite, unit.TypePullRequests)
|
2022-09-09 22:57:47 +05:30
|
|
|
if !doerCanWrite && doer.ID != issue.PosterID {
|
2022-06-13 15:07:59 +05:30
|
|
|
official, err := issues_model.IsOfficialReviewer(ctx, issue, doer)
|
2020-10-20 23:48:25 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to Check if IsOfficialReviewer for %-v in %-v#%d", doer, issue.Repo, issue.Index)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !official {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Doer can't choose reviewer",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !permission.IsAdmin() {
|
2022-06-13 15:07:59 +05:30
|
|
|
return issues_model.ErrNotValidReviewRequest{
|
2020-10-20 23:48:25 +05:30
|
|
|
Reason: "Only admin users can remove team requests. Doer is not admin",
|
|
|
|
UserID: doer.ID,
|
|
|
|
RepoID: issue.Repo.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:03:34 +05:30
|
|
|
return nil
|
|
|
|
}
|
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.
|
2022-06-13 15:07:59 +05:30
|
|
|
func TeamReviewRequest(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 {
|
2022-06-13 15:07:59 +05:30
|
|
|
comment, err = issues_model.AddTeamReviewRequest(issue, reviewer, doer)
|
2020-10-13 01:25:13 +05:30
|
|
|
} else {
|
2022-06-13 15:07:59 +05:30
|
|
|
comment, err = issues_model.RemoveTeamReviewRequest(issue, reviewer, doer)
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if comment == nil || !isAdd {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify all user in this team
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = comment.LoadIssue(db.DefaultContext); err != nil {
|
2020-10-13 01:25:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
members, err := organization.GetTeamMembers(db.DefaultContext, &organization.SearchMembersOptions{
|
|
|
|
TeamID: reviewer.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-13 01:25:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyPullReviewRequest(db.DefaultContext, doer, issue, member, isAdd, comment)
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return comment, err
|
2020-10-13 01:25:13 +05:30
|
|
|
}
|