You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

edits.go 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2022 The Gitea Authors.
  2. // All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package pull
  5. import (
  6. "context"
  7. "errors"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. access_model "code.gitea.io/gitea/models/perm/access"
  10. unit_model "code.gitea.io/gitea/models/unit"
  11. user_model "code.gitea.io/gitea/models/user"
  12. )
  13. var ErrUserHasNoPermissionForAction = errors.New("user not allowed to do this action")
  14. // SetAllowEdits allow edits from maintainers to PRs
  15. func SetAllowEdits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, allow bool) error {
  16. if doer == nil || !pr.Issue.IsPoster(doer.ID) {
  17. return ErrUserHasNoPermissionForAction
  18. }
  19. if err := pr.LoadHeadRepo(ctx); err != nil {
  20. return err
  21. }
  22. permission, err := access_model.GetUserRepoPermission(ctx, pr.HeadRepo, doer)
  23. if err != nil {
  24. return err
  25. }
  26. if !permission.CanWrite(unit_model.TypeCode) {
  27. return ErrUserHasNoPermissionForAction
  28. }
  29. pr.AllowMaintainerEdit = allow
  30. return issues_model.UpdateAllowEdits(ctx, pr)
  31. }