Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

issue_lock.go 1.5KB

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