You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_xref.go 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. "regexp"
  7. "strconv"
  8. "code.gitea.io/gitea/modules/log"
  9. "github.com/go-xorm/xorm"
  10. "github.com/unknwon/com"
  11. )
  12. var (
  13. // TODO: Unify all regexp treatment of cross references in one place
  14. // issueNumericPattern matches string that references to a numeric issue, e.g. #1287
  15. issueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)(?:#)([0-9]+)(?:\s|$|\)|\]|:|\.(\s|$))`)
  16. // crossReferenceIssueNumericPattern matches string that references a numeric issue in a different repository
  17. // e.g. gogits/gogs#12345
  18. crossReferenceIssueNumericPattern = regexp.MustCompile(`(?:\s|^|\(|\[)([0-9a-zA-Z-_\.]+)/([0-9a-zA-Z-_\.]+)#([0-9]+)(?:\s|$|\)|\]|\.(\s|$))`)
  19. )
  20. // XRefAction represents the kind of effect a cross reference has once is resolved
  21. type XRefAction int64
  22. const (
  23. // XRefActionNone means the cross-reference is a mention (commit, etc.)
  24. XRefActionNone XRefAction = iota // 0
  25. // XRefActionCloses means the cross-reference should close an issue if it is resolved
  26. XRefActionCloses // 1 - not implemented yet
  27. // XRefActionReopens means the cross-reference should reopen an issue if it is resolved
  28. XRefActionReopens // 2 - Not implemented yet
  29. // XRefActionNeutered means the cross-reference will no longer affect the source
  30. XRefActionNeutered // 3
  31. )
  32. type crossReference struct {
  33. Issue *Issue
  34. Action XRefAction
  35. }
  36. // crossReferencesContext is context to pass along findCrossReference functions
  37. type crossReferencesContext struct {
  38. Type CommentType
  39. Doer *User
  40. OrigIssue *Issue
  41. OrigComment *Comment
  42. }
  43. func newCrossReference(e *xorm.Session, ctx *crossReferencesContext, xref *crossReference) error {
  44. var refCommentID int64
  45. if ctx.OrigComment != nil {
  46. refCommentID = ctx.OrigComment.ID
  47. }
  48. _, err := createComment(e, &CreateCommentOptions{
  49. Type: ctx.Type,
  50. Doer: ctx.Doer,
  51. Repo: xref.Issue.Repo,
  52. Issue: xref.Issue,
  53. RefRepoID: ctx.OrigIssue.RepoID,
  54. RefIssueID: ctx.OrigIssue.ID,
  55. RefCommentID: refCommentID,
  56. RefAction: xref.Action,
  57. RefIsPull: xref.Issue.IsPull,
  58. })
  59. return err
  60. }
  61. func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
  62. active := make([]*Comment, 0, 10)
  63. sess := e.Where("`ref_action` IN (?, ?, ?)", XRefActionNone, XRefActionCloses, XRefActionReopens)
  64. if issueID != 0 {
  65. sess = sess.And("`ref_issue_id` = ?", issueID)
  66. }
  67. if commentID != 0 {
  68. sess = sess.And("`ref_comment_id` = ?", commentID)
  69. }
  70. if err := sess.Find(&active); err != nil || len(active) == 0 {
  71. return err
  72. }
  73. ids := make([]int64, len(active))
  74. for i, c := range active {
  75. ids[i] = c.ID
  76. }
  77. _, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: XRefActionNeutered})
  78. return err
  79. }
  80. // .___
  81. // | | ______ ________ __ ____
  82. // | |/ ___// ___/ | \_/ __ \
  83. // | |\___ \ \___ \| | /\ ___/
  84. // |___/____ >____ >____/ \___ >
  85. // \/ \/ \/
  86. //
  87. func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User) error {
  88. var commentType CommentType
  89. if issue.IsPull {
  90. commentType = CommentTypePullRef
  91. } else {
  92. commentType = CommentTypeIssueRef
  93. }
  94. ctx := &crossReferencesContext{
  95. Type: commentType,
  96. Doer: doer,
  97. OrigIssue: issue,
  98. }
  99. return issue.createCrossReferences(e, ctx, issue.Title+"\n"+issue.Content)
  100. }
  101. func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) error {
  102. xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, content)
  103. if err != nil {
  104. return err
  105. }
  106. for _, xref := range xreflist {
  107. if err = newCrossReference(e, ctx, xref); err != nil {
  108. return err
  109. }
  110. }
  111. return nil
  112. }
  113. func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, content string) ([]*crossReference, error) {
  114. xreflist := make([]*crossReference, 0, 5)
  115. var xref *crossReference
  116. // Issues in the same repository
  117. // FIXME: Should we support IssueNameStyleAlphanumeric?
  118. matches := issueNumericPattern.FindAllStringSubmatch(content, -1)
  119. for _, match := range matches {
  120. if index, err := strconv.ParseInt(match[1], 10, 64); err == nil {
  121. if err = ctx.OrigIssue.loadRepo(e); err != nil {
  122. return nil, err
  123. }
  124. if xref, err = ctx.OrigIssue.isValidCommentReference(e, ctx, issue.Repo, index); err != nil {
  125. return nil, err
  126. }
  127. if xref != nil {
  128. xreflist = ctx.OrigIssue.updateCrossReferenceList(xreflist, xref)
  129. }
  130. }
  131. }
  132. // Issues in other repositories
  133. matches = crossReferenceIssueNumericPattern.FindAllStringSubmatch(content, -1)
  134. for _, match := range matches {
  135. if index, err := strconv.ParseInt(match[3], 10, 64); err == nil {
  136. repo, err := getRepositoryByOwnerAndName(e, match[1], match[2])
  137. if err != nil {
  138. if IsErrRepoNotExist(err) {
  139. continue
  140. }
  141. return nil, err
  142. }
  143. if err = ctx.OrigIssue.loadRepo(e); err != nil {
  144. return nil, err
  145. }
  146. if xref, err = issue.isValidCommentReference(e, ctx, repo, index); err != nil {
  147. return nil, err
  148. }
  149. if xref != nil {
  150. xreflist = issue.updateCrossReferenceList(xreflist, xref)
  151. }
  152. }
  153. }
  154. return xreflist, nil
  155. }
  156. func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *crossReference) []*crossReference {
  157. if xref.Issue.ID == issue.ID {
  158. return list
  159. }
  160. for i, r := range list {
  161. if r.Issue.ID == xref.Issue.ID {
  162. if xref.Action != XRefActionNone {
  163. list[i].Action = xref.Action
  164. }
  165. return list
  166. }
  167. }
  168. return append(list, xref)
  169. }
  170. func (issue *Issue) isValidCommentReference(e Engine, ctx *crossReferencesContext, repo *Repository, index int64) (*crossReference, error) {
  171. refIssue := &Issue{RepoID: repo.ID, Index: index}
  172. if has, _ := e.Get(refIssue); !has {
  173. return nil, nil
  174. }
  175. if err := refIssue.loadRepo(e); err != nil {
  176. return nil, err
  177. }
  178. // Check user permissions
  179. if refIssue.RepoID != ctx.OrigIssue.RepoID {
  180. perm, err := getUserRepoPermission(e, refIssue.Repo, ctx.Doer)
  181. if err != nil {
  182. return nil, err
  183. }
  184. if !perm.CanReadIssuesOrPulls(refIssue.IsPull) {
  185. return nil, nil
  186. }
  187. }
  188. return &crossReference{
  189. Issue: refIssue,
  190. Action: XRefActionNone,
  191. }, nil
  192. }
  193. func (issue *Issue) neuterCrossReferences(e Engine) error {
  194. return neuterCrossReferences(e, issue.ID, 0)
  195. }
  196. // _________ __
  197. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  198. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  199. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  200. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  201. // \/ \/ \/ \/ \/
  202. //
  203. func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User) error {
  204. if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
  205. return nil
  206. }
  207. if err := comment.loadIssue(e); err != nil {
  208. return err
  209. }
  210. ctx := &crossReferencesContext{
  211. Type: CommentTypeCommentRef,
  212. Doer: doer,
  213. OrigIssue: comment.Issue,
  214. OrigComment: comment,
  215. }
  216. return comment.Issue.createCrossReferences(e, ctx, comment.Content)
  217. }
  218. func (comment *Comment) neuterCrossReferences(e Engine) error {
  219. return neuterCrossReferences(e, 0, comment.ID)
  220. }
  221. // LoadRefComment loads comment that created this reference from database
  222. func (comment *Comment) LoadRefComment() (err error) {
  223. if comment.RefComment != nil {
  224. return nil
  225. }
  226. comment.RefComment, err = GetCommentByID(comment.RefCommentID)
  227. return
  228. }
  229. // LoadRefIssue loads comment that created this reference from database
  230. func (comment *Comment) LoadRefIssue() (err error) {
  231. if comment.RefIssue != nil {
  232. return nil
  233. }
  234. comment.RefIssue, err = GetIssueByID(comment.RefIssueID)
  235. if err == nil {
  236. err = comment.RefIssue.loadRepo(x)
  237. }
  238. return
  239. }
  240. // CommentTypeIsRef returns true if CommentType is a reference from another issue
  241. func CommentTypeIsRef(t CommentType) bool {
  242. return t == CommentTypeCommentRef || t == CommentTypePullRef || t == CommentTypeIssueRef
  243. }
  244. // RefCommentHTMLURL returns the HTML URL for the comment that created this reference
  245. func (comment *Comment) RefCommentHTMLURL() string {
  246. if err := comment.LoadRefComment(); err != nil { // Silently dropping errors :unamused:
  247. log.Error("LoadRefComment(%d): %v", comment.RefCommentID, err)
  248. return ""
  249. }
  250. return comment.RefComment.HTMLURL()
  251. }
  252. // RefIssueHTMLURL returns the HTML URL of the issue where this reference was created
  253. func (comment *Comment) RefIssueHTMLURL() string {
  254. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  255. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  256. return ""
  257. }
  258. return comment.RefIssue.HTMLURL()
  259. }
  260. // RefIssueTitle returns the title of the issue where this reference was created
  261. func (comment *Comment) RefIssueTitle() string {
  262. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  263. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  264. return ""
  265. }
  266. return comment.RefIssue.Title
  267. }
  268. // RefIssueIdent returns the user friendly identity (e.g. "#1234") of the issue where this reference was created
  269. func (comment *Comment) RefIssueIdent() string {
  270. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  271. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  272. return ""
  273. }
  274. // FIXME: check this name for cross-repository references (#7901 if it gets merged)
  275. return "#" + com.ToStr(comment.RefIssue.Index)
  276. }