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

365 lines
11 KiB

  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. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/references"
  9. "github.com/unknwon/com"
  10. "xorm.io/xorm"
  11. )
  12. type crossReference struct {
  13. Issue *Issue
  14. Action references.XRefAction
  15. }
  16. // crossReferencesContext is context to pass along findCrossReference functions
  17. type crossReferencesContext struct {
  18. Type CommentType
  19. Doer *User
  20. OrigIssue *Issue
  21. OrigComment *Comment
  22. RemoveOld bool
  23. }
  24. func findOldCrossReferences(e Engine, issueID int64, commentID int64) ([]*Comment, error) {
  25. active := make([]*Comment, 0, 10)
  26. return active, e.Where("`ref_action` IN (?, ?, ?)", references.XRefActionNone, references.XRefActionCloses, references.XRefActionReopens).
  27. And("`ref_issue_id` = ?", issueID).
  28. And("`ref_comment_id` = ?", commentID).
  29. Find(&active)
  30. }
  31. func neuterCrossReferences(e Engine, issueID int64, commentID int64) error {
  32. active, err := findOldCrossReferences(e, issueID, commentID)
  33. if err != nil {
  34. return err
  35. }
  36. ids := make([]int64, len(active))
  37. for i, c := range active {
  38. ids[i] = c.ID
  39. }
  40. return neuterCrossReferencesIds(e, ids)
  41. }
  42. func neuterCrossReferencesIds(e Engine, ids []int64) error {
  43. _, err := e.In("id", ids).Cols("`ref_action`").Update(&Comment{RefAction: references.XRefActionNeutered})
  44. return err
  45. }
  46. // .___
  47. // | | ______ ________ __ ____
  48. // | |/ ___// ___/ | \_/ __ \
  49. // | |\___ \ \___ \| | /\ ___/
  50. // |___/____ >____ >____/ \___ >
  51. // \/ \/ \/
  52. //
  53. func (issue *Issue) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
  54. var commentType CommentType
  55. if issue.IsPull {
  56. commentType = CommentTypePullRef
  57. } else {
  58. commentType = CommentTypeIssueRef
  59. }
  60. ctx := &crossReferencesContext{
  61. Type: commentType,
  62. Doer: doer,
  63. OrigIssue: issue,
  64. RemoveOld: removeOld,
  65. }
  66. return issue.createCrossReferences(e, ctx, issue.Title, issue.Content)
  67. }
  68. func (issue *Issue) createCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) error {
  69. xreflist, err := ctx.OrigIssue.getCrossReferences(e, ctx, plaincontent, mdcontent)
  70. if err != nil {
  71. return err
  72. }
  73. if ctx.RemoveOld {
  74. var commentID int64
  75. if ctx.OrigComment != nil {
  76. commentID = ctx.OrigComment.ID
  77. }
  78. active, err := findOldCrossReferences(e, ctx.OrigIssue.ID, commentID)
  79. if err != nil {
  80. return err
  81. }
  82. ids := make([]int64, 0, len(active))
  83. for _, c := range active {
  84. found := false
  85. for i, x := range xreflist {
  86. if x.Issue.ID == c.IssueID && x.Action == c.RefAction {
  87. found = true
  88. xreflist = append(xreflist[:i], xreflist[i+1:]...)
  89. break
  90. }
  91. }
  92. if !found {
  93. ids = append(ids, c.ID)
  94. }
  95. }
  96. if len(ids) > 0 {
  97. if err = neuterCrossReferencesIds(e, ids); err != nil {
  98. return err
  99. }
  100. }
  101. }
  102. for _, xref := range xreflist {
  103. var refCommentID int64
  104. if ctx.OrigComment != nil {
  105. refCommentID = ctx.OrigComment.ID
  106. }
  107. var opts = &CreateCommentOptions{
  108. Type: ctx.Type,
  109. Doer: ctx.Doer,
  110. Repo: xref.Issue.Repo,
  111. Issue: xref.Issue,
  112. RefRepoID: ctx.OrigIssue.RepoID,
  113. RefIssueID: ctx.OrigIssue.ID,
  114. RefCommentID: refCommentID,
  115. RefAction: xref.Action,
  116. RefIsPull: ctx.OrigIssue.IsPull,
  117. }
  118. _, err := createComment(e, opts)
  119. if err != nil {
  120. return err
  121. }
  122. }
  123. return nil
  124. }
  125. func (issue *Issue) getCrossReferences(e *xorm.Session, ctx *crossReferencesContext, plaincontent, mdcontent string) ([]*crossReference, error) {
  126. xreflist := make([]*crossReference, 0, 5)
  127. var (
  128. refRepo *Repository
  129. refIssue *Issue
  130. refAction references.XRefAction
  131. err error
  132. )
  133. allrefs := append(references.FindAllIssueReferences(plaincontent), references.FindAllIssueReferencesMarkdown(mdcontent)...)
  134. for _, ref := range allrefs {
  135. if ref.Owner == "" && ref.Name == "" {
  136. // Issues in the same repository
  137. if err := ctx.OrigIssue.loadRepo(e); err != nil {
  138. return nil, err
  139. }
  140. refRepo = ctx.OrigIssue.Repo
  141. } else {
  142. // Issues in other repositories
  143. refRepo, err = getRepositoryByOwnerAndName(e, ref.Owner, ref.Name)
  144. if err != nil {
  145. if IsErrRepoNotExist(err) {
  146. continue
  147. }
  148. return nil, err
  149. }
  150. }
  151. if refIssue, refAction, err = ctx.OrigIssue.verifyReferencedIssue(e, ctx, refRepo, ref); err != nil {
  152. return nil, err
  153. }
  154. if refIssue != nil {
  155. xreflist = ctx.OrigIssue.updateCrossReferenceList(xreflist, &crossReference{
  156. Issue: refIssue,
  157. Action: refAction,
  158. })
  159. }
  160. }
  161. return xreflist, nil
  162. }
  163. func (issue *Issue) updateCrossReferenceList(list []*crossReference, xref *crossReference) []*crossReference {
  164. if xref.Issue.ID == issue.ID {
  165. return list
  166. }
  167. for i, r := range list {
  168. if r.Issue.ID == xref.Issue.ID {
  169. if xref.Action != references.XRefActionNone {
  170. list[i].Action = xref.Action
  171. }
  172. return list
  173. }
  174. }
  175. return append(list, xref)
  176. }
  177. // verifyReferencedIssue will check if the referenced issue exists, and whether the doer has permission to do what
  178. func (issue *Issue) verifyReferencedIssue(e Engine, ctx *crossReferencesContext, repo *Repository,
  179. ref references.IssueReference) (*Issue, references.XRefAction, error) {
  180. refIssue := &Issue{RepoID: repo.ID, Index: ref.Index}
  181. refAction := ref.Action
  182. if has, _ := e.Get(refIssue); !has {
  183. return nil, references.XRefActionNone, nil
  184. }
  185. if err := refIssue.loadRepo(e); err != nil {
  186. return nil, references.XRefActionNone, err
  187. }
  188. // Close/reopen actions can only be set from pull requests to issues
  189. if refIssue.IsPull || !issue.IsPull {
  190. refAction = references.XRefActionNone
  191. }
  192. // Check doer permissions; set action to None if the doer can't change the destination
  193. if refIssue.RepoID != ctx.OrigIssue.RepoID || ref.Action != references.XRefActionNone {
  194. perm, err := getUserRepoPermission(e, refIssue.Repo, ctx.Doer)
  195. if err != nil {
  196. return nil, references.XRefActionNone, err
  197. }
  198. if !perm.CanReadIssuesOrPulls(refIssue.IsPull) {
  199. return nil, references.XRefActionNone, nil
  200. }
  201. // Accept close/reopening actions only if the poster is able to close the
  202. // referenced issue manually at this moment. The only exception is
  203. // the poster of a new PR referencing an issue on the same repo: then the merger
  204. // should be responsible for checking whether the reference should resolve.
  205. if ref.Action != references.XRefActionNone &&
  206. ctx.Doer.ID != refIssue.PosterID &&
  207. !perm.CanWriteIssuesOrPulls(refIssue.IsPull) &&
  208. (refIssue.RepoID != ctx.OrigIssue.RepoID || ctx.OrigComment != nil) {
  209. refAction = references.XRefActionNone
  210. }
  211. }
  212. return refIssue, refAction, nil
  213. }
  214. // _________ __
  215. // \_ ___ \ ____ _____ _____ ____ _____/ |_
  216. // / \ \/ / _ \ / \ / \_/ __ \ / \ __\
  217. // \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
  218. // \______ /\____/|__|_| /__|_| /\___ >___| /__|
  219. // \/ \/ \/ \/ \/
  220. //
  221. func (comment *Comment) addCrossReferences(e *xorm.Session, doer *User, removeOld bool) error {
  222. if comment.Type != CommentTypeCode && comment.Type != CommentTypeComment {
  223. return nil
  224. }
  225. if err := comment.loadIssue(e); err != nil {
  226. return err
  227. }
  228. ctx := &crossReferencesContext{
  229. Type: CommentTypeCommentRef,
  230. Doer: doer,
  231. OrigIssue: comment.Issue,
  232. OrigComment: comment,
  233. RemoveOld: removeOld,
  234. }
  235. return comment.Issue.createCrossReferences(e, ctx, "", comment.Content)
  236. }
  237. func (comment *Comment) neuterCrossReferences(e Engine) error {
  238. return neuterCrossReferences(e, comment.IssueID, comment.ID)
  239. }
  240. // LoadRefComment loads comment that created this reference from database
  241. func (comment *Comment) LoadRefComment() (err error) {
  242. if comment.RefComment != nil {
  243. return nil
  244. }
  245. comment.RefComment, err = GetCommentByID(comment.RefCommentID)
  246. return
  247. }
  248. // LoadRefIssue loads comment that created this reference from database
  249. func (comment *Comment) LoadRefIssue() (err error) {
  250. if comment.RefIssue != nil {
  251. return nil
  252. }
  253. comment.RefIssue, err = GetIssueByID(comment.RefIssueID)
  254. if err == nil {
  255. err = comment.RefIssue.loadRepo(x)
  256. }
  257. return
  258. }
  259. // CommentTypeIsRef returns true if CommentType is a reference from another issue
  260. func CommentTypeIsRef(t CommentType) bool {
  261. return t == CommentTypeCommentRef || t == CommentTypePullRef || t == CommentTypeIssueRef
  262. }
  263. // RefCommentHTMLURL returns the HTML URL for the comment that created this reference
  264. func (comment *Comment) RefCommentHTMLURL() string {
  265. if err := comment.LoadRefComment(); err != nil { // Silently dropping errors :unamused:
  266. log.Error("LoadRefComment(%d): %v", comment.RefCommentID, err)
  267. return ""
  268. }
  269. return comment.RefComment.HTMLURL()
  270. }
  271. // RefIssueHTMLURL returns the HTML URL of the issue where this reference was created
  272. func (comment *Comment) RefIssueHTMLURL() string {
  273. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  274. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  275. return ""
  276. }
  277. return comment.RefIssue.HTMLURL()
  278. }
  279. // RefIssueTitle returns the title of the issue where this reference was created
  280. func (comment *Comment) RefIssueTitle() string {
  281. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  282. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  283. return ""
  284. }
  285. return comment.RefIssue.Title
  286. }
  287. // RefIssueIdent returns the user friendly identity (e.g. "#1234") of the issue where this reference was created
  288. func (comment *Comment) RefIssueIdent() string {
  289. if err := comment.LoadRefIssue(); err != nil { // Silently dropping errors :unamused:
  290. log.Error("LoadRefIssue(%d): %v", comment.RefCommentID, err)
  291. return ""
  292. }
  293. // FIXME: check this name for cross-repository references (#7901 if it gets merged)
  294. return "#" + com.ToStr(comment.RefIssue.Index)
  295. }
  296. // __________ .__ .__ __________ __
  297. // \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
  298. // | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
  299. // | | | | / |_| |_| | \ ___< <_| | | /\ ___/ \___ \ | |
  300. // |____| |____/|____/____/____|_ /\___ >__ |____/ \___ >____ > |__|
  301. // \/ \/ |__| \/ \/
  302. // ResolveCrossReferences will return the list of references to close/reopen by this PR
  303. func (pr *PullRequest) ResolveCrossReferences() ([]*Comment, error) {
  304. unfiltered := make([]*Comment, 0, 5)
  305. if err := x.
  306. Where("ref_repo_id = ? AND ref_issue_id = ?", pr.Issue.RepoID, pr.Issue.ID).
  307. In("ref_action", []references.XRefAction{references.XRefActionCloses, references.XRefActionReopens}).
  308. OrderBy("id").
  309. Find(&unfiltered); err != nil {
  310. return nil, fmt.Errorf("get reference: %v", err)
  311. }
  312. refs := make([]*Comment, 0, len(unfiltered))
  313. for _, ref := range unfiltered {
  314. found := false
  315. for i, r := range refs {
  316. if r.IssueID == ref.IssueID {
  317. // Keep only the latest
  318. refs[i] = ref
  319. found = true
  320. break
  321. }
  322. }
  323. if !found {
  324. refs = append(refs, ref)
  325. }
  326. }
  327. return refs, nil
  328. }