本站源代码
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.

61 lines
1.3KB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. // IssueLockOptions defines options for locking and/or unlocking an issue/PR
  6. type IssueLockOptions struct {
  7. Doer *User
  8. Issue *Issue
  9. Reason string
  10. }
  11. // LockIssue locks an issue. This would limit commenting abilities to
  12. // users with write access to the repo
  13. func LockIssue(opts *IssueLockOptions) error {
  14. return updateIssueLock(opts, true)
  15. }
  16. // UnlockIssue unlocks a previously locked issue.
  17. func UnlockIssue(opts *IssueLockOptions) error {
  18. return updateIssueLock(opts, false)
  19. }
  20. func updateIssueLock(opts *IssueLockOptions, lock bool) error {
  21. if opts.Issue.IsLocked == lock {
  22. return nil
  23. }
  24. opts.Issue.IsLocked = lock
  25. var commentType CommentType
  26. if opts.Issue.IsLocked {
  27. commentType = CommentTypeLock
  28. } else {
  29. commentType = CommentTypeUnlock
  30. }
  31. sess := x.NewSession()
  32. defer sess.Close()
  33. if err := sess.Begin(); err != nil {
  34. return err
  35. }
  36. if err := updateIssueCols(sess, opts.Issue, "is_locked"); err != nil {
  37. return err
  38. }
  39. _, err := createComment(sess, &CreateCommentOptions{
  40. Doer: opts.Doer,
  41. Issue: opts.Issue,
  42. Repo: opts.Issue.Repo,
  43. Type: commentType,
  44. Content: opts.Reason,
  45. })
  46. if err != nil {
  47. return err
  48. }
  49. return sess.Commit()
  50. }
上海开阖软件有限公司 沪ICP备12045867号-1