Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

convert.go 12KB

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