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.

issue_lock.go 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "code.gitea.io/gitea/models/db"
  6. user_model "code.gitea.io/gitea/models/user"
  7. )
  8. // IssueLockOptions defines options for locking and/or unlocking an issue/PR
  9. type IssueLockOptions struct {
  10. Doer *user_model.User
  11. Issue *Issue
  12. Reason string
  13. }
  14. // LockIssue locks an issue. This would limit commenting abilities to
  15. // users with write access to the repo
  16. func LockIssue(opts *IssueLockOptions) error {
  17. return updateIssueLock(opts, true)
  18. }
  19. // UnlockIssue unlocks a previously locked issue.
  20. func UnlockIssue(opts *IssueLockOptions) error {
  21. return updateIssueLock(opts, false)
  22. }
  23. func updateIssueLock(opts *IssueLockOptions, lock bool) error {
  24. if opts.Issue.IsLocked == lock {
  25. return nil
  26. }
  27. opts.Issue.IsLocked = lock
  28. var commentType CommentType
  29. if opts.Issue.IsLocked {
  30. commentType = CommentTypeLock
  31. } else {
  32. commentType = CommentTypeUnlock
  33. }
  34. ctx, committer, err := db.TxContext(db.DefaultContext)
  35. if err != nil {
  36. return err
  37. }
  38. defer committer.Close()
  39. if err := UpdateIssueCols(ctx, opts.Issue, "is_locked"); err != nil {
  40. return err
  41. }
  42. opt := &CreateCommentOptions{
  43. Doer: opts.Doer,
  44. Issue: opts.Issue,
  45. Repo: opts.Issue.Repo,
  46. Type: commentType,
  47. Content: opts.Reason,
  48. }
  49. if _, err := CreateComment(ctx, opt); err != nil {
  50. return err
  51. }
  52. return committer.Commit()
  53. }