2021-10-16 14:21:16 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package feed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
2022-03-13 16:40:47 +00:00
|
|
|
"net/http"
|
2021-10-16 14:21:16 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2021-10-16 14:21:16 +00:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-03-12 22:05:12 +00:00
|
|
|
"code.gitea.io/gitea/modules/markup"
|
|
|
|
"code.gitea.io/gitea/modules/markup/markdown"
|
2021-10-16 14:21:16 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/templates"
|
2021-11-16 18:18:25 +00:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-10-16 14:21:16 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/feeds"
|
|
|
|
)
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
func toBranchLink(act *activities_model.Action) string {
|
2021-11-16 18:18:25 +00:00
|
|
|
return act.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(act.GetBranch())
|
|
|
|
}
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
func toTagLink(act *activities_model.Action) string {
|
2021-11-16 18:18:25 +00:00
|
|
|
return act.GetRepoLink() + "/src/tag/" + util.PathEscapeSegments(act.GetTag())
|
|
|
|
}
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
func toIssueLink(act *activities_model.Action) string {
|
2021-11-16 18:18:25 +00:00
|
|
|
return act.GetRepoLink() + "/issues/" + url.PathEscape(act.GetIssueInfos()[0])
|
|
|
|
}
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
func toPullLink(act *activities_model.Action) string {
|
2021-11-16 18:18:25 +00:00
|
|
|
return act.GetRepoLink() + "/pulls/" + url.PathEscape(act.GetIssueInfos()[0])
|
|
|
|
}
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
func toSrcLink(act *activities_model.Action) string {
|
2021-11-16 18:18:25 +00:00
|
|
|
return act.GetRepoLink() + "/src/" + util.PathEscapeSegments(act.GetBranch())
|
|
|
|
}
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
func toReleaseLink(act *activities_model.Action) string {
|
2021-11-16 18:18:25 +00:00
|
|
|
return act.GetRepoLink() + "/releases/tag/" + util.PathEscapeSegments(act.GetBranch())
|
|
|
|
}
|
|
|
|
|
2022-03-12 22:05:12 +00:00
|
|
|
// renderMarkdown creates a minimal markdown render context from an action.
|
|
|
|
// If rendering fails, the original markdown text is returned
|
2022-08-25 02:31:57 +00:00
|
|
|
func renderMarkdown(ctx *context.Context, act *activities_model.Action, content string) string {
|
2022-03-12 22:05:12 +00:00
|
|
|
markdownCtx := &markup.RenderContext{
|
|
|
|
Ctx: ctx,
|
|
|
|
URLPrefix: act.GetRepoLink(),
|
|
|
|
Type: markdown.MarkupName,
|
|
|
|
Metas: map[string]string{
|
|
|
|
"user": act.GetRepoUserName(),
|
|
|
|
"repo": act.GetRepoName(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
markdown, err := markdown.RenderString(markdownCtx, content)
|
|
|
|
if err != nil {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
return markdown
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:16 +00:00
|
|
|
// feedActionsToFeedItems convert gitea's Action feed to feeds Item
|
2022-08-25 02:31:57 +00:00
|
|
|
func feedActionsToFeedItems(ctx *context.Context, actions activities_model.ActionList) (items []*feeds.Item, err error) {
|
2021-10-16 14:21:16 +00:00
|
|
|
for _, act := range actions {
|
|
|
|
act.LoadActUser()
|
|
|
|
|
2022-06-20 10:02:49 +00:00
|
|
|
var content, desc, title string
|
2021-10-16 14:21:16 +00:00
|
|
|
|
|
|
|
link := &feeds.Link{Href: act.GetCommentLink()}
|
|
|
|
|
|
|
|
// title
|
|
|
|
title = act.ActUser.DisplayName() + " "
|
|
|
|
switch act.OpType {
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCreateRepo:
|
2021-11-16 18:18:25 +00:00
|
|
|
title += ctx.TrHTMLEscapeArgs("action.create_repo", act.GetRepoLink(), act.ShortRepoPath())
|
|
|
|
link.Href = act.GetRepoLink()
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionRenameRepo:
|
2021-11-16 18:18:25 +00:00
|
|
|
title += ctx.TrHTMLEscapeArgs("action.rename_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath())
|
|
|
|
link.Href = act.GetRepoLink()
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCommitRepo:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = toBranchLink(act)
|
2021-10-16 14:21:16 +00:00
|
|
|
if len(act.Content) != 0 {
|
2021-11-16 18:18:25 +00:00
|
|
|
title += ctx.TrHTMLEscapeArgs("action.commit_repo", act.GetRepoLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
|
2021-10-16 14:21:16 +00:00
|
|
|
} else {
|
2021-11-16 18:18:25 +00:00
|
|
|
title += ctx.TrHTMLEscapeArgs("action.create_branch", act.GetRepoLink(), link.Href, act.GetBranch(), act.ShortRepoPath())
|
2021-10-16 14:21:16 +00:00
|
|
|
}
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCreateIssue:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = toIssueLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.create_issue", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCreatePullRequest:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = toPullLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.create_pull_request", link.Href, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionTransferRepo:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = act.GetRepoLink()
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.transfer_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionPushTag:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = toTagLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.push_tag", act.GetRepoLink(), link.Href, act.GetTag(), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCommentIssue:
|
2021-11-16 18:18:25 +00:00
|
|
|
issueLink := toIssueLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = issueLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.comment_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionMergePullRequest:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = pullLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.merge_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCloseIssue:
|
2021-11-16 18:18:25 +00:00
|
|
|
issueLink := toIssueLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = issueLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.close_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionReopenIssue:
|
2021-11-16 18:18:25 +00:00
|
|
|
issueLink := toIssueLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = issueLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.reopen_issue", issueLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionClosePullRequest:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = pullLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.close_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionReopenPullRequest:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = pullLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.reopen_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionDeleteTag:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = act.GetRepoLink()
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.delete_tag", act.GetRepoLink(), act.GetTag(), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionDeleteBranch:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = act.GetRepoLink()
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.delete_branch", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionMirrorSyncPush:
|
2021-11-16 18:18:25 +00:00
|
|
|
srcLink := toSrcLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = srcLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_push", act.GetRepoLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionMirrorSyncCreate:
|
2021-11-16 18:18:25 +00:00
|
|
|
srcLink := toSrcLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = srcLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_create", act.GetRepoLink(), srcLink, act.GetBranch(), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionMirrorSyncDelete:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = act.GetRepoLink()
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.mirror_sync_delete", act.GetRepoLink(), act.GetBranch(), act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionApprovePullRequest:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.approve_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionRejectPullRequest:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.reject_pull_request", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCommentPull:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.comment_pull", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionPublishRelease:
|
2021-11-16 18:18:25 +00:00
|
|
|
releaseLink := toReleaseLink(act)
|
|
|
|
if link.Href == "#" {
|
|
|
|
link.Href = releaseLink
|
|
|
|
}
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.publish_release", act.GetRepoLink(), releaseLink, act.ShortRepoPath(), act.Content)
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionPullReviewDismissed:
|
2021-11-16 18:18:25 +00:00
|
|
|
pullLink := toPullLink(act)
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.review_dismissed", pullLink, act.GetIssueInfos()[0], act.ShortRepoPath(), act.GetIssueInfos()[1])
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionStarRepo:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = act.GetRepoLink()
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.starred_repo", act.GetRepoLink(), act.GetRepoPath())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionWatchRepo:
|
2021-11-16 18:18:25 +00:00
|
|
|
link.Href = act.GetRepoLink()
|
|
|
|
title += ctx.TrHTMLEscapeArgs("action.watched_repo", act.GetRepoLink(), act.GetRepoPath())
|
2021-10-16 14:21:16 +00:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown action type: %v", act.OpType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// description & content
|
|
|
|
{
|
|
|
|
switch act.OpType {
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCommitRepo, activities_model.ActionMirrorSyncPush:
|
2021-10-16 14:21:16 +00:00
|
|
|
push := templates.ActionContent2Commits(act)
|
|
|
|
repoLink := act.GetRepoLink()
|
|
|
|
|
|
|
|
for _, commit := range push.Commits {
|
|
|
|
if len(desc) != 0 {
|
|
|
|
desc += "\n\n"
|
|
|
|
}
|
|
|
|
desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s",
|
2021-11-16 18:18:25 +00:00
|
|
|
html.EscapeString(fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), commit.Sha1)),
|
2021-10-16 14:21:16 +00:00
|
|
|
commit.Sha1,
|
2022-01-19 23:26:57 +00:00
|
|
|
templates.RenderCommitMessage(ctx, commit.Message, repoLink, nil),
|
2021-10-16 14:21:16 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if push.Len > 1 {
|
|
|
|
link = &feeds.Link{Href: fmt.Sprintf("%s/%s", setting.AppSubURL, push.CompareURL)}
|
|
|
|
} else if push.Len == 1 {
|
|
|
|
link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), push.Commits[0].Sha1)}
|
|
|
|
}
|
|
|
|
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCreateIssue, activities_model.ActionCreatePullRequest:
|
2021-10-16 14:21:16 +00:00
|
|
|
desc = strings.Join(act.GetIssueInfos(), "#")
|
2022-03-12 22:05:12 +00:00
|
|
|
content = renderMarkdown(ctx, act, act.GetIssueContent())
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCommentIssue, activities_model.ActionApprovePullRequest, activities_model.ActionRejectPullRequest, activities_model.ActionCommentPull:
|
2021-10-16 14:21:16 +00:00
|
|
|
desc = act.GetIssueTitle()
|
|
|
|
comment := act.GetIssueInfos()[1]
|
|
|
|
if len(comment) != 0 {
|
2022-03-12 22:05:12 +00:00
|
|
|
desc += "\n\n" + renderMarkdown(ctx, act, comment)
|
2021-10-16 14:21:16 +00:00
|
|
|
}
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionMergePullRequest:
|
2021-10-16 14:21:16 +00:00
|
|
|
desc = act.GetIssueInfos()[1]
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionCloseIssue, activities_model.ActionReopenIssue, activities_model.ActionClosePullRequest, activities_model.ActionReopenPullRequest:
|
2021-10-16 14:21:16 +00:00
|
|
|
desc = act.GetIssueTitle()
|
2022-08-25 02:31:57 +00:00
|
|
|
case activities_model.ActionPullReviewDismissed:
|
2021-10-16 14:21:16 +00:00
|
|
|
desc = ctx.Tr("action.review_dismissed_reason") + "\n\n" + act.GetIssueInfos()[2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(content) == 0 {
|
|
|
|
content = desc
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, &feeds.Item{
|
|
|
|
Title: title,
|
|
|
|
Link: link,
|
|
|
|
Description: desc,
|
|
|
|
Author: &feeds.Author{
|
|
|
|
Name: act.ActUser.DisplayName(),
|
|
|
|
Email: act.ActUser.GetEmail(),
|
|
|
|
},
|
|
|
|
Id: strconv.FormatInt(act.ID, 10),
|
|
|
|
Created: act.CreatedUnix.AsTime(),
|
|
|
|
Content: content,
|
|
|
|
})
|
|
|
|
}
|
2022-06-20 10:02:49 +00:00
|
|
|
return items, err
|
2021-10-16 14:21:16 +00:00
|
|
|
}
|
2022-03-13 16:40:47 +00:00
|
|
|
|
|
|
|
// GetFeedType return if it is a feed request and altered name and feed type.
|
|
|
|
func GetFeedType(name string, req *http.Request) (bool, string, string) {
|
|
|
|
if strings.HasSuffix(name, ".rss") ||
|
|
|
|
strings.Contains(req.Header.Get("Accept"), "application/rss+xml") {
|
|
|
|
return true, strings.TrimSuffix(name, ".rss"), "rss"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(name, ".atom") ||
|
|
|
|
strings.Contains(req.Header.Get("Accept"), "application/atom+xml") {
|
|
|
|
return true, strings.TrimSuffix(name, ".atom"), "atom"
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, name, ""
|
|
|
|
}
|