summaryrefslogtreecommitdiffstats
path: root/routers/repo
diff options
context:
space:
mode:
authora1012112796 <1012112796@qq.com>2020-10-21 02:18:25 +0800
committerGitHub <noreply@github.com>2020-10-20 14:18:25 -0400
commitb9850375fca1ed72f5a5ba265d48c241aff2e21e (patch)
treebc5f03623003b95911686e3e472c97f3c8cbe04e /routers/repo
parentb50448b28698b5c9cd1825bf95f2331fa030790f (diff)
downloadgitea-b9850375fca1ed72f5a5ba265d48c241aff2e21e.tar.gz
gitea-b9850375fca1ed72f5a5ba265d48c241aff2e21e.zip
Add review request api (#11355)
* Add review request api * add : POST /repos/{owner}/{repo}/pulls/{index}/requested_reviewers * Remove : DELET /repos/{owner}/{repo}/pulls/{index}/requested_reviewers * fix some request review bug * block delet request review by models/DeleteReview() Signed-off-by: a1012112796 <1012112796@qq.com> * make fmt * fix bug * fix test code * fix typo * Apply suggestion from code review @jonasfranz * fix swagger ref * fix typo Co-authored-by: Lauris BH <lauris@nix.lv> * fix comment * Change response message * chang response so some simplfy * Add ErrIllLegalReviewRequest fix some nits * make fmt * Apply suggestions from code review Co-authored-by: silverwind <me@silverwind.io> * * Add team support * fix test * fix an known bug * fix nit * fix test * Apply suggestions from code review Co-authored-by: zeripath <art27@cantab.net> * update get api and add test Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'routers/repo')
-rw-r--r--routers/repo/issue.go158
1 files changed, 5 insertions, 153 deletions
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index bc65e73f11..835a952e5e 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -1699,154 +1699,6 @@ func UpdateIssueAssignee(ctx *context.Context) {
})
}
-func isValidReviewRequest(reviewer, doer *models.User, isAdd bool, issue *models.Issue) error {
- if reviewer.IsOrganization() {
- return models.ErrNotValidReviewRequest{
- Reason: "Organization can't be added as reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
- if doer.IsOrganization() {
- return models.ErrNotValidReviewRequest{
- Reason: "Organization can't be doer to add reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
-
- permReviewer, err := models.GetUserRepoPermission(issue.Repo, reviewer)
- if err != nil {
- return err
- }
-
- permDoer, err := models.GetUserRepoPermission(issue.Repo, doer)
- if err != nil {
- return err
- }
-
- lastreview, err := models.GetReviewByIssueIDAndUserID(issue.ID, reviewer.ID)
- if err != nil && !models.IsErrReviewNotExist(err) {
- return err
- }
-
- var pemResult bool
- if isAdd {
- pemResult = permReviewer.CanAccessAny(models.AccessModeRead, models.UnitTypePullRequests)
- if !pemResult {
- return models.ErrNotValidReviewRequest{
- Reason: "Reviewer can't read",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
-
- if doer.ID == issue.PosterID && issue.OriginalAuthorID == 0 && lastreview != nil && lastreview.Type != models.ReviewTypeRequest {
- return nil
- }
-
- pemResult = permDoer.CanAccessAny(models.AccessModeWrite, models.UnitTypePullRequests)
- if !pemResult {
- pemResult, err = models.IsOfficialReviewer(issue, doer)
- if err != nil {
- return err
- }
- if !pemResult {
- return models.ErrNotValidReviewRequest{
- Reason: "Doer can't choose reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
- }
-
- if doer.ID == reviewer.ID {
- return models.ErrNotValidReviewRequest{
- Reason: "doer can't be reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
-
- if reviewer.ID == issue.PosterID && issue.OriginalAuthorID == 0 {
- return models.ErrNotValidReviewRequest{
- Reason: "poster of pr can't be reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
- } else {
- if lastreview != nil && lastreview.Type == models.ReviewTypeRequest && lastreview.ReviewerID == doer.ID {
- return nil
- }
-
- pemResult = permDoer.IsAdmin()
- if !pemResult {
- return models.ErrNotValidReviewRequest{
- Reason: "Doer is not admin",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
- }
-
- return nil
-}
-
-func isValidTeamReviewRequest(reviewer *models.Team, doer *models.User, isAdd bool, issue *models.Issue) error {
- if doer.IsOrganization() {
- return models.ErrNotValidReviewRequest{
- Reason: "Organization can't be doer to add reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
-
- permission, err := models.GetUserRepoPermission(issue.Repo, doer)
- 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 {
- hasTeam := models.HasTeamRepo(reviewer.OrgID, reviewer.ID, issue.RepoID)
-
- if !hasTeam {
- return models.ErrNotValidReviewRequest{
- Reason: "Reviewing team can't read repo",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
- }
-
- doerCanWrite := permission.CanAccessAny(models.AccessModeWrite, models.UnitTypePullRequests)
- if !doerCanWrite {
- official, err := models.IsOfficialReviewer(issue, doer)
- if err != nil {
- log.Error("Unable to Check if IsOfficialReviewer for %-v in %-v#%d", doer, issue.Repo, issue.Index)
- return err
- }
- if !official {
- return models.ErrNotValidReviewRequest{
- Reason: "Doer can't choose reviewer",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
- }
- } else if !permission.IsAdmin() {
- return models.ErrNotValidReviewRequest{
- Reason: "Only admin users can remove team requests. Doer is not admin",
- UserID: doer.ID,
- RepoID: issue.Repo.ID,
- }
- }
-
- return nil
-}
-
// UpdatePullReviewRequest add or remove review request
func UpdatePullReviewRequest(ctx *context.Context) {
issues := getActionIssues(ctx)
@@ -1907,7 +1759,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
return
}
- err = isValidTeamReviewRequest(team, ctx.User, action == "attach", issue)
+ err = issue_service.IsValidTeamReviewRequest(team, ctx.User, action == "attach", issue)
if err != nil {
if models.IsErrNotValidReviewRequest(err) {
log.Warn(
@@ -1918,11 +1770,11 @@ func UpdatePullReviewRequest(ctx *context.Context) {
ctx.Status(403)
return
}
- ctx.ServerError("isValidTeamReviewRequest", err)
+ ctx.ServerError("IsValidTeamReviewRequest", err)
return
}
- err = issue_service.TeamReviewRequest(issue, ctx.User, team, action == "attach")
+ _, err = issue_service.TeamReviewRequest(issue, ctx.User, team, action == "attach")
if err != nil {
ctx.ServerError("TeamReviewRequest", err)
return
@@ -1945,7 +1797,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
return
}
- err = isValidReviewRequest(reviewer, ctx.User, action == "attach", issue)
+ err = issue_service.IsValidReviewRequest(reviewer, ctx.User, action == "attach", issue, nil)
if err != nil {
if models.IsErrNotValidReviewRequest(err) {
log.Warn(
@@ -1960,7 +1812,7 @@ func UpdatePullReviewRequest(ctx *context.Context) {
return
}
- err = issue_service.ReviewRequest(issue, ctx.User, reviewer, action == "attach")
+ _, err = issue_service.ReviewRequest(issue, ctx.User, reviewer, action == "attach")
if err != nil {
ctx.ServerError("ReviewRequest", err)
return