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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2021 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 feed
  5. import (
  6. "fmt"
  7. "html"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/context"
  14. "code.gitea.io/gitea/modules/markup"
  15. "code.gitea.io/gitea/modules/markup/markdown"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/templates"
  18. "code.gitea.io/gitea/modules/util"
  19. "github.com/gorilla/feeds"
  20. )
  21. func toBranchLink(act *models.Action) string {
  22. return act.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(act.GetBranch())
  23. }
  24. func toTagLink(act *models.Action) string {
  25. return act.GetRepoLink() + "/src/tag/" + util.PathEscapeSegments(act.GetTag())
  26. }
  27. func toIssueLink(act *models.Action) string {
  28. return act.GetRepoLink() + "/issues/" + url.PathEscape(act.GetIssueInfos()[0])
  29. }
  30. func toPullLink(act *models.Action) string {
  31. return act.GetRepoLink() + "/pulls/" + url.PathEscape(act.GetIssueInfos()[0])
  32. }
  33. func toSrcLink(act *models.Action) string {
  34. return act.GetRepoLink() + "/src/" + util.PathEscapeSegments(act.GetBranch())
  35. }
  36. func toReleaseLink(act *models.Action) string {
  37. return act.GetRepoLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
  38. }
  39. // renderMarkdown creates a minimal markdown render context from an action.
  40. // If rendering fails, the original markdown text is returned
  41. func renderMarkdown(ctx *context.Context, act *models.Action, content string) string {
  42. markdownCtx := &markup.RenderContext{
  43. Ctx: ctx,
  44. URLPrefix: act.GetRepoLink(),
  45. Type: markdown.MarkupName,
  46. Metas: map[string]string{
  47. "user": act.GetRepoUserName(),
  48. "repo": act.GetRepoName(),
  49. },
  50. }
  51. markdown, err := markdown.RenderString(markdownCtx, content)
  52. if err != nil {
  53. return content
  54. }
  55. return markdown
  56. }
  57. // feedActionsToFeedItems convert gitea's Action feed to feeds Item
  58. func feedActionsToFeedItems(ctx *context.Context, actions models.ActionList) (items []*feeds.Item, err error) {
  59. for _, act := range actions {
  60. act.LoadActUser()
  61. content, desc, title := "", "", ""
  62. link := &feeds.Link{Href: act.GetCommentLink()}
  63. // title
  64. title = act.ActUser.DisplayName() + " "
  65. switch act.OpType {
  66. case models.ActionCreateRepo:
  67. title += ctx.TrHTMLEscapeArgs("action.create_repo", act.GetRepoLink(), act.ShortRepoPath())
  68. link.Href = act.GetRepoLink()
  69. case models.ActionRenameRepo:
  70. title += ctx.TrHTMLEscapeArgs("action.rename_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath())
  71. link.Href = act.GetRepoLink()
  72. case models.ActionCommitRepo:
  73. link.Href = toBranchLink(act)
  74. if len(act.Content) != 0 {
  75. title += ctx.TrHTMLEscapeArgs("action.commit_repo", act.GetRepoLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
  76. } else {
  77. title += ctx.TrHTMLEscapeArgs("action.create_branch", act.GetRepoLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
  78. }
  79. case models.ActionCreateIssue:
  80. link.Href = toIssueLink(act)
  81. title += ctx.TrHTMLEscapeArgs("action.create_issue", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath())
  82. case models.ActionCreatePullRequest:
  83. link.Href = toPullLink(act)
  84. title += ctx.TrHTMLEscapeArgs("action.create_pull_request", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath())
  85. case models.ActionTransferRepo:
  86. link.Href = act.GetRepoLink()
  87. title += ctx.TrHTMLEscapeArgs("action.transfer_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath())
  88. case models.ActionPushTag:
  89. link.Href = toTagLink(act)
  90. title += ctx.TrHTMLEscapeArgs("action.push_tag", act.GetRepoLink(), link.Href, act.GetTag(), act.ShortRepoPath())
  91. case models.ActionCommentIssue:
  92. issueLink := toIssueLink(act)
  93. if link.Href == "#" {
  94. link.Href = issueLink
  95. }
  96. title += ctx.TrHTMLEscapeArgs("action.comment_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  97. case models.ActionMergePullRequest:
  98. pullLink := toPullLink(act)
  99. if link.Href == "#" {
  100. link.Href = pullLink
  101. }
  102. title += ctx.TrHTMLEscapeArgs("action.merge_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  103. case models.ActionCloseIssue:
  104. issueLink := toIssueLink(act)
  105. if link.Href == "#" {
  106. link.Href = issueLink
  107. }
  108. title += ctx.TrHTMLEscapeArgs("action.close_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  109. case models.ActionReopenIssue:
  110. issueLink := toIssueLink(act)
  111. if link.Href == "#" {
  112. link.Href = issueLink
  113. }
  114. title += ctx.TrHTMLEscapeArgs("action.reopen_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  115. case models.ActionClosePullRequest:
  116. pullLink := toPullLink(act)
  117. if link.Href == "#" {
  118. link.Href = pullLink
  119. }
  120. title += ctx.TrHTMLEscapeArgs("action.close_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  121. case models.ActionReopenPullRequest:
  122. pullLink := toPullLink(act)
  123. if link.Href == "#" {
  124. link.Href = pullLink
  125. }
  126. title += ctx.TrHTMLEscapeArgs("action.reopen_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  127. case models.ActionDeleteTag:
  128. link.Href = act.GetRepoLink()
  129. title += ctx.TrHTMLEscapeArgs("action.delete_tag", act.GetRepoLink(), act.GetTag(), act.ShortRepoPath())
  130. case models.ActionDeleteBranch:
  131. link.Href = act.GetRepoLink()
  132. title += ctx.TrHTMLEscapeArgs("action.delete_branch", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath())
  133. case models.ActionMirrorSyncPush:
  134. srcLink := toSrcLink(act)
  135. if link.Href == "#" {
  136. link.Href = srcLink
  137. }
  138. title += ctx.TrHTMLEscapeArgs("action.mirror_sync_push", act.GetRepoLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
  139. case models.ActionMirrorSyncCreate:
  140. srcLink := toSrcLink(act)
  141. if link.Href == "#" {
  142. link.Href = srcLink
  143. }
  144. title += ctx.TrHTMLEscapeArgs("action.mirror_sync_create", act.GetRepoLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
  145. case models.ActionMirrorSyncDelete:
  146. link.Href = act.GetRepoLink()
  147. title += ctx.TrHTMLEscapeArgs("action.mirror_sync_delete", act.GetRepoLink(), act.GetBranch(), act.ShortRepoPath())
  148. case models.ActionApprovePullRequest:
  149. pullLink := toPullLink(act)
  150. title += ctx.TrHTMLEscapeArgs("action.approve_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  151. case models.ActionRejectPullRequest:
  152. pullLink := toPullLink(act)
  153. title += ctx.TrHTMLEscapeArgs("action.reject_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  154. case models.ActionCommentPull:
  155. pullLink := toPullLink(act)
  156. title += ctx.TrHTMLEscapeArgs("action.comment_pull", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
  157. case models.ActionPublishRelease:
  158. releaseLink := toReleaseLink(act)
  159. if link.Href == "#" {
  160. link.Href = releaseLink
  161. }
  162. title += ctx.TrHTMLEscapeArgs("action.publish_release", act.GetRepoLink(), releaseLink, act.ShortRepoPath(), act.Content)
  163. case models.ActionPullReviewDismissed:
  164. pullLink := toPullLink(act)
  165. title += ctx.TrHTMLEscapeArgs("action.review_dismissed", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(), act.GetIssueInfos()[1])
  166. case models.ActionStarRepo:
  167. link.Href = act.GetRepoLink()
  168. title += ctx.TrHTMLEscapeArgs("action.starred_repo", act.GetRepoLink(), act.GetRepoPath())
  169. case models.ActionWatchRepo:
  170. link.Href = act.GetRepoLink()
  171. title += ctx.TrHTMLEscapeArgs("action.watched_repo", act.GetRepoLink(), act.GetRepoPath())
  172. default:
  173. return nil, fmt.Errorf("unknown action type: %v", act.OpType)
  174. }
  175. // description & content
  176. {
  177. switch act.OpType {
  178. case models.ActionCommitRepo, models.ActionMirrorSyncPush:
  179. push := templates.ActionContent2Commits(act)
  180. repoLink := act.GetRepoLink()
  181. for _, commit := range push.Commits {
  182. if len(desc) != 0 {
  183. desc += "\n\n"
  184. }
  185. desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s",
  186. html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), commit.Sha1)),
  187. commit.Sha1,
  188. templates.RenderCommitMessage(ctx, commit.Message, repoLink, nil),
  189. )
  190. }
  191. if push.Len > 1 {
  192. link = &feeds.Link{Href: fmt.Sprintf("%s/%s", setting.AppSubURL, push.CompareURL)}
  193. } else if push.Len == 1 {
  194. link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), push.Commits[0].Sha1)}
  195. }
  196. case models.ActionCreateIssue, models.ActionCreatePullRequest:
  197. desc = strings.Join(act.GetIssueInfos(), "#")
  198. content = renderMarkdown(ctx, act, act.GetIssueContent())
  199. case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull:
  200. desc = act.GetIssueTitle()
  201. comment := act.GetIssueInfos()[1]
  202. if len(comment) != 0 {
  203. desc += "\n\n" + renderMarkdown(ctx, act, comment)
  204. }
  205. case models.ActionMergePullRequest:
  206. desc = act.GetIssueInfos()[1]
  207. case models.ActionCloseIssue, models.ActionReopenIssue, models.ActionClosePullRequest, models.ActionReopenPullRequest:
  208. desc = act.GetIssueTitle()
  209. case models.ActionPullReviewDismissed:
  210. desc = ctx.Tr("action.review_dismissed_reason") + "\n\n" + act.GetIssueInfos()[2]
  211. }
  212. }
  213. if len(content) == 0 {
  214. content = desc
  215. }
  216. items = append(items, &feeds.Item{
  217. Title: title,
  218. Link: link,
  219. Description: desc,
  220. Author: &feeds.Author{
  221. Name: act.ActUser.DisplayName(),
  222. Email: act.ActUser.GetEmail(),
  223. },
  224. Id: strconv.FormatInt(act.ID, 10),
  225. Created: act.CreatedUnix.AsTime(),
  226. Content: content,
  227. })
  228. }
  229. return
  230. }
  231. // GetFeedType return if it is a feed request and altered name and feed type.
  232. func GetFeedType(name string, req *http.Request) (bool, string, string) {
  233. if strings.HasSuffix(name, ".rss") ||
  234. strings.Contains(req.Header.Get("Accept"), "application/rss+xml") {
  235. return true, strings.TrimSuffix(name, ".rss"), "rss"
  236. }
  237. if strings.HasSuffix(name, ".atom") ||
  238. strings.Contains(req.Header.Get("Accept"), "application/atom+xml") {
  239. return true, strings.TrimSuffix(name, ".atom"), "atom"
  240. }
  241. return false, name, ""
  242. }