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

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