gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/util"
  10. "xorm.io/builder"
  11. )
  12. // IssueAssignees saves all issue assignees
  13. type IssueAssignees struct {
  14. ID int64 `xorm:"pk autoincr"`
  15. AssigneeID int64 `xorm:"INDEX"`
  16. IssueID int64 `xorm:"INDEX"`
  17. }
  18. func init() {
  19. db.RegisterModel(new(IssueAssignees))
  20. }
  21. // LoadAssignees load assignees of this issue.
  22. func (issue *Issue) LoadAssignees(ctx context.Context) (err error) {
  23. if issue.isAssigneeLoaded || len(issue.Assignees) > 0 {
  24. return nil
  25. }
  26. // Reset maybe preexisting assignees
  27. issue.Assignees = []*user_model.User{}
  28. issue.Assignee = nil
  29. if err = db.GetEngine(ctx).Table("`user`").
  30. Join("INNER", "issue_assignees", "assignee_id = `user`.id").
  31. Where("issue_assignees.issue_id = ?", issue.ID).
  32. Find(&issue.Assignees); err != nil {
  33. return err
  34. }
  35. issue.isAssigneeLoaded = true
  36. // Check if we have at least one assignee and if yes put it in as `Assignee`
  37. if len(issue.Assignees) > 0 {
  38. issue.Assignee = issue.Assignees[0]
  39. }
  40. return nil
  41. }
  42. // GetAssigneeIDsByIssue returns the IDs of users assigned to an issue
  43. // but skips joining with `user` for performance reasons.
  44. // User permissions must be verified elsewhere if required.
  45. func GetAssigneeIDsByIssue(ctx context.Context, issueID int64) ([]int64, error) {
  46. userIDs := make([]int64, 0, 5)
  47. return userIDs, db.GetEngine(ctx).
  48. Table("issue_assignees").
  49. Cols("assignee_id").
  50. Where("issue_id = ?", issueID).
  51. Distinct("assignee_id").
  52. Find(&userIDs)
  53. }
  54. // IsUserAssignedToIssue returns true when the user is assigned to the issue
  55. func IsUserAssignedToIssue(ctx context.Context, issue *Issue, user *user_model.User) (isAssigned bool, err error) {
  56. return db.Exist[IssueAssignees](ctx, builder.Eq{"assignee_id": user.ID, "issue_id": issue.ID})
  57. }
  58. type AssignedIssuesOptions struct {
  59. db.ListOptions
  60. AssigneeID int64
  61. RepoOwnerID int64
  62. }
  63. func (opts *AssignedIssuesOptions) ToConds() builder.Cond {
  64. cond := builder.NewCond()
  65. if opts.AssigneeID != 0 {
  66. cond = cond.And(builder.In("issue.id", builder.Select("issue_id").From("issue_assignees").Where(builder.Eq{"assignee_id": opts.AssigneeID})))
  67. }
  68. if opts.RepoOwnerID != 0 {
  69. cond = cond.And(builder.In("issue.repo_id", builder.Select("id").From("repository").Where(builder.Eq{"owner_id": opts.RepoOwnerID})))
  70. }
  71. return cond
  72. }
  73. func GetAssignedIssues(ctx context.Context, opts *AssignedIssuesOptions) ([]*Issue, int64, error) {
  74. return db.FindAndCount[Issue](ctx, opts)
  75. }
  76. // ToggleIssueAssignee changes a user between assigned and not assigned for this issue, and make issue comment for it.
  77. func ToggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64) (removed bool, comment *Comment, err error) {
  78. if err := db.WithTx(ctx, func(ctx context.Context) error {
  79. removed, comment, err = toggleIssueAssignee(ctx, issue, doer, assigneeID, false)
  80. return err
  81. }); err != nil {
  82. return false, nil, err
  83. }
  84. return removed, comment, nil
  85. }
  86. func toggleIssueAssignee(ctx context.Context, issue *Issue, doer *user_model.User, assigneeID int64, isCreate bool) (removed bool, comment *Comment, err error) {
  87. removed, err = toggleUserAssignee(ctx, issue, assigneeID)
  88. if err != nil {
  89. return false, nil, fmt.Errorf("UpdateIssueUserByAssignee: %w", err)
  90. }
  91. // Repo infos
  92. if err = issue.LoadRepo(ctx); err != nil {
  93. return false, nil, fmt.Errorf("loadRepo: %w", err)
  94. }
  95. opts := &CreateCommentOptions{
  96. Type: CommentTypeAssignees,
  97. Doer: doer,
  98. Repo: issue.Repo,
  99. Issue: issue,
  100. RemovedAssignee: removed,
  101. AssigneeID: assigneeID,
  102. }
  103. // Comment
  104. comment, err = CreateComment(ctx, opts)
  105. if err != nil {
  106. return false, nil, fmt.Errorf("createComment: %w", err)
  107. }
  108. // if pull request is in the middle of creation - don't call webhook
  109. if isCreate {
  110. return removed, comment, err
  111. }
  112. return removed, comment, nil
  113. }
  114. // toggles user assignee state in database
  115. func toggleUserAssignee(ctx context.Context, issue *Issue, assigneeID int64) (removed bool, err error) {
  116. // Check if the user exists
  117. assignee, err := user_model.GetUserByID(ctx, assigneeID)
  118. if err != nil {
  119. return false, err
  120. }
  121. // Check if the submitted user is already assigned, if yes delete him otherwise add him
  122. found := false
  123. i := 0
  124. for ; i < len(issue.Assignees); i++ {
  125. if issue.Assignees[i].ID == assigneeID {
  126. found = true
  127. break
  128. }
  129. }
  130. assigneeIn := IssueAssignees{AssigneeID: assigneeID, IssueID: issue.ID}
  131. if found {
  132. issue.Assignees = append(issue.Assignees[:i], issue.Assignees[i+1:]...)
  133. _, err = db.DeleteByBean(ctx, &assigneeIn)
  134. if err != nil {
  135. return found, err
  136. }
  137. } else {
  138. issue.Assignees = append(issue.Assignees, assignee)
  139. if err = db.Insert(ctx, &assigneeIn); err != nil {
  140. return found, err
  141. }
  142. }
  143. return found, nil
  144. }
  145. // MakeIDsFromAPIAssigneesToAdd returns an array with all assignee IDs
  146. func MakeIDsFromAPIAssigneesToAdd(ctx context.Context, oneAssignee string, multipleAssignees []string) (assigneeIDs []int64, err error) {
  147. var requestAssignees []string
  148. // Keeping the old assigning method for compatibility reasons
  149. if oneAssignee != "" && !util.SliceContainsString(multipleAssignees, oneAssignee) {
  150. requestAssignees = append(requestAssignees, oneAssignee)
  151. }
  152. // Prevent empty assignees
  153. if len(multipleAssignees) > 0 && multipleAssignees[0] != "" {
  154. requestAssignees = append(requestAssignees, multipleAssignees...)
  155. }
  156. // Get the IDs of all assignees
  157. assigneeIDs, err = user_model.GetUserIDsByNames(ctx, requestAssignees, false)
  158. return assigneeIDs, err
  159. }