本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

276 行
8.0KB

  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/modules/log"
  7. "code.gitea.io/gitea/modules/references"
  8. "github.com/unknwon/com"
  9. "xorm.io/xorm"
  10. )
  11. type crossReference struct {
  12. Issue *Issue
  13. Action references.XRefAction
  14. }
  15. // crossReferencesContext is context to pass along findCrossReference functions
  16. type crossReferencesContext struct {
  17. Type CommentType
  18. Doer *User
  19. OrigIssue *Issue
  20. OrigComment *Comment
  21. }
  22. func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error {
  23. var refCommentID int64
  24. if ctx.OrigComment != nil {
  25. refCommentID = ctx.OrigComment.ID
  26. }
  27. _, err := createComment(e, &CreateCommentOptions{
  28. Type: ctx.Type,
  29. Doer: ctx.Doer,
  30. Repo: xref.Issue.Repo,
  31. Issue: xref.Issue,
  32. RefRepoID: ctx.OrigIssue.RepoID,
  33. RefIssueID: ctx.OrigIssue.ID,
  34. RefCommentID: refCommentID,
  35. RefAction: xref.Action,
  36. RefIsPull: xref.Issue.IsPull,
  37. })
  38. return err
  39. }
  40. func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
  41. active := make([]*Comment, 0, 10)
  42. sess := e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens)
  43. if issueID != 0 {
  44. sess = sess.And("`ref_issue_id` = ?", issueID)
  45. }
  46. if commentID != 0 {
  47. sess = sess.And("`ref_comment_id` = ?", commentID)
  48. }
  49. if err := sess.Find(&active); err != nil || len(active) == 0 {
  50. return err
  51. }
  52. ids := make([]int64, len(active))
  53. for i, c := range active {
  54. ids[i] = c.ID
  55. }
  56. _, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: references.XRefActionNeutered})
  57. return err
  58. }
  59. // .___
  60. // | | ______ ________ __ ____
  61. // | |/ ___// ___/ | \_/ __ \
  62. // | |\___ \ \___ \| | /\ ___/
  63. // |___/____ >____ >____/ \___ >
  64. // \/ \/ \/
  65. //
  66. func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
  67. var commentType CommentType
  68. if issue.IsPull {
  69. commentType = CommentTypePullRef
  70. } else {
  71. commentType = CommentTypeIssueRef
  72. }
  73. ctx := &crossReferencesContext{
  74. Type: commentType,
  75. Doer: doer,
  76. OrigIssue: issue,
  77. }
  78. return issue.createCrossReferences(e, ctx, issue.Title, issue.Content)
  79. }
  80. func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
  81. xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, plaincontent, mdcontent)
  82. if err != nil {
  83. return err
  84. }
  85. for _, xref := range xreflist {
  86. if err = newCrossReference(e, ctx, xref); err != nil {
  87. return err
  88. }
  89. }
  90. return nil
  91. }
  92. func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
  93. xreflist := make([]*crossReference, 0, 5)
  94. var (
  95. refRepo *Repository
  96. refIssue *Issue
  97. err error
  98. )
  99. allrefs := append(references.FindAllIssueReferences(plaincontent), references.FindAllIssueReferencesMarkdown(mdcontent)...)
  100. for _, ref := range allrefs {
  101. if ref.Owner == "" && ref.Name == "" {
  102. // Issues in the same repository
  103. if err := ctx.OrigIssue.loadRepo(e); err != nil {
  104. return nil, err
  105. }
  106. refRepo = ctx.OrigIssue.Repo
  107. } else {
  108. // Issues in other repositories
  109. refRepo, err = getRepositoryByOwnerAndName(e, ref.Owner, ref.Name)
  110. if err != nil {
  111. if IsErrRepoNotExist(err) {
  112. continue
  113. }
  114. return nil, err
  115. }
  116. }
  117. if refIssue, err = ctx.OrigIssue.findReferencedIssue(e, ctx, refRepo, ref.Index); err != nil {
  118. return nil, err
  119. }
  120. if refIssue != nil {
  121. xreflist = ctx.OrigIssue.updateCrossReferenceList(xreflist, &crossReference{
  122. Issue: refIssue,
  123. // FIXME: currently ignore keywords
  124. // Action: ref.Action,
  125. Action: references.XRefActionNone,
  126. })
  127. }
  128. }
  129. return xreflist, nil
  130. }
  131. func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *crossReference) []*crossReference {
  132. if xref.Issue.ID == issue.ID {
  133. return list
  134. }
  135. for i, r := range list {
  136. if r.Issue.ID == xref.Issue.ID {
  137. if xref.Action != references.XRefActionNone {
  138. list[i].Action = xref.Action
  139. }
  140. return list
  141. }
  142. }
  143. return append(list, xref)
  144. }
  145. func (issue *Issue) findReferencedIssue(e Engine, ctx *crossReferencesContext, repo *Repository, index int64) (*Issue, error) {
  146. refIssue := &Issue{RepoID: repo.ID, Index: index}
  147. if has, _ := e.Get(refIssue); !has {
  148. return nil, nil
  149. }
  150. if err := refIssue.loadRepo(e); err != nil {
  151. return nil, err
  152. }
  153. // Check user permissions
  154. if refIssue.RepoID != ctx.OrigIssue.RepoID {
  155. perm, err := getUserRepoPermission(e, refIssue.Repo, ctx.Doer)
  156. if err != nil {
  157. return nil, err
  158. }
  159. if !perm.CanReadIssuesOrPulls(refIssue.IsPull) {
  160. return nil, nil
  161. }
  162. }
  163. return refIssue, nil
  164. }
  165. func (issue *Issue) neuterCrossReferences(e Engine) error {
  166. return neuterCrossReferences(e, issue.ID, 0)
  167. }
  168. // _________ __
  169. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  170. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  171. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  172. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  173. // \/ \/ \/ \/ \/
  174. //
  175. func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
  176. if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
  177. return nil
  178. }
  179. if err := comment.loadIssue(e); err != nil {
  180. return err
  181. }
  182. ctx := &crossReferencesContext{
  183. Type: CommentTypeCommentRef,
  184. Doer: doer,
  185. OrigIssue: comment.Issue,
  186. OrigComment: comment,
  187. }
  188. return comment.Issue.createCrossReferences(e, ctx, "", comment.Content)
  189. }
  190. func (comment *Comment) neuterCrossReferences(e Engine) error {
  191. return neuterCrossReferences(e, 0, comment.ID)
  192. }
  193. // LoadRefComment loads comment that created this reference from database
  194. func (comment *Comment) LoadRefComment() (err error) {
  195. if comment.RefComment != nil {
  196. return nil
  197. }
  198. comment.RefComment, err = GetCommentByID(comment.RefCommentID)
  199. return
  200. }
  201. // LoadRefIssue loads comment that created this reference from database
  202. func (comment *Comment) LoadRefIssue() (err error) {
  203. if comment.RefIssue != nil {
  204. return nil
  205. }
  206. comment.RefIssue, err = GetIssueByID(comment.RefIssueID)
  207. if err == nil {
  208. err = comment.RefIssue.loadRepo(x)
  209. }
  210. return
  211. }
  212. // CommentTypeIsRef returns true if CommentType is a reference from another issue
  213. func CommentTypeIsRef(t CommentType) bool {
  214. return t == CommentTypeCommentRef || t == CommentTypePullRef || t == CommentTypeIssueRef
  215. }
  216. // RefCommentHTMLURL returns the HTML URL for the comment that created this reference
  217. func (comment *Comment) RefCommentHTMLURL() string {
  218. if err := comment.LoadRefComment(); err != nil { // Silently dropping errors :unamused:
  219. log.Error("LoadRefComment(%d): %v", comment.RefCommentID, err)
  220. return ""
  221. }
  222. return comment.RefComment.HTMLURL()
  223. }
  224. // RefIssueHTMLURL returns the HTML URL of the issue where this reference was created
  225. func (comment *Comment) RefIssueHTMLURL() string {
  226. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  227. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  228. return ""
  229. }
  230. return comment.RefIssue.HTMLURL()
  231. }
  232. // RefIssueTitle returns the title of the issue where this reference was created
  233. func (comment *Comment) RefIssueTitle() string {
  234. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  235. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  236. return ""
  237. }
  238. return comment.RefIssue.Title
  239. }
  240. // RefIssueIdent returns the user friendly identity (e.g. "#1234") of the issue where this reference was created
  241. func (comment *Comment) RefIssueIdent() string {
  242. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  243. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  244. return ""
  245. }
  246. // FIXME: check this name for cross-repository references (#7901 if it gets merged)
  247. return "#" + com.ToStr(comment.RefIssue.Index)
  248. }
上海开阖软件有限公司 沪ICP备12045867号-1