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 12KB

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