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.

issue_xref.go 11KB

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