gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "context"
  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 is the doer-provided comment message for the locked issue
  14. // GitHub doesn't support changing the "reasons" by config file, so GitHub has pre-defined "reason" enum values.
  15. // Gitea is not like GitHub, it allows site admin to define customized "reasons" in the config file.
  16. // So the API caller might not know what kind of "reasons" are valid, and the customized reasons are not translatable.
  17. // To make things clear and simple: doer have the chance to use any reason they like, we do not do validation.
  18. Reason string
  19. }
  20. // LockIssue locks an issue. This would limit commenting abilities to
  21. // users with write access to the repo
  22. func LockIssue(ctx context.Context, opts *IssueLockOptions) error {
  23. return updateIssueLock(ctx, opts, true)
  24. }
  25. // UnlockIssue unlocks a previously locked issue.
  26. func UnlockIssue(ctx context.Context, opts *IssueLockOptions) error {
  27. return updateIssueLock(ctx, opts, false)
  28. }
  29. func updateIssueLock(ctx context.Context, opts *IssueLockOptions, lock bool) error {
  30. if opts.Issue.IsLocked == lock {
  31. return nil
  32. }
  33. opts.Issue.IsLocked = lock
  34. var commentType CommentType
  35. if opts.Issue.IsLocked {
  36. commentType = CommentTypeLock
  37. } else {
  38. commentType = CommentTypeUnlock
  39. }
  40. return db.WithTx(ctx, func(ctx context.Context) error {
  41. if err := UpdateIssueCols(ctx, opts.Issue, "is_locked"); err != nil {
  42. return err
  43. }
  44. opt := &CreateCommentOptions{
  45. Doer: opts.Doer,
  46. Issue: opts.Issue,
  47. Repo: opts.Issue.Repo,
  48. Type: commentType,
  49. Content: opts.Reason,
  50. }
  51. _, err := CreateComment(ctx, opt)
  52. return err
  53. })
  54. }