|
| 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 | + |
| 5 | +package feed |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "html" |
| 10 | + "net/url" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "code.gitea.io/gitea/models" |
| 15 | + "code.gitea.io/gitea/modules/context" |
| 16 | + "code.gitea.io/gitea/modules/setting" |
| 17 | + "code.gitea.io/gitea/modules/templates" |
| 18 | + |
| 19 | + "github.com/gorilla/feeds" |
| 20 | +) |
| 21 | + |
| 22 | +// feedActionsToFeedItems convert gitea's Action feed to feeds Item |
| 23 | +func feedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item, err error) { |
| 24 | + for _, act := range actions { |
| 25 | + act.LoadActUser() |
| 26 | + |
| 27 | + content, desc, title := "", "", "" |
| 28 | + |
| 29 | + link := &feeds.Link{Href: act.GetCommentLink()} |
| 30 | + |
| 31 | + // title |
| 32 | + title = act.ActUser.DisplayName() + " " |
| 33 | + switch act.OpType { |
| 34 | + case models.ActionCreateRepo: |
| 35 | + title += ctx.Tr("action.create_repo", act.GetRepoLink(), act.ShortRepoPath()) |
| 36 | + case models.ActionRenameRepo: |
| 37 | + title += ctx.Tr("action.rename_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath()) |
| 38 | + case models.ActionCommitRepo: |
| 39 | + branchLink := act.GetBranch() |
| 40 | + if len(act.Content) != 0 { |
| 41 | + title += ctx.Tr("action.commit_repo", act.GetRepoLink(), branchLink, act.GetBranch(), act.ShortRepoPath()) |
| 42 | + } else { |
| 43 | + title += ctx.Tr("action.create_branch", act.GetRepoLink(), branchLink, act.GetBranch(), act.ShortRepoPath()) |
| 44 | + } |
| 45 | + case models.ActionCreateIssue: |
| 46 | + title += ctx.Tr("action.create_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 47 | + case models.ActionCreatePullRequest: |
| 48 | + title += ctx.Tr("action.create_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 49 | + case models.ActionTransferRepo: |
| 50 | + title += ctx.Tr("action.transfer_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath()) |
| 51 | + case models.ActionPushTag: |
| 52 | + title += ctx.Tr("action.push_tag", act.GetRepoLink(), url.QueryEscape(act.GetTag()), act.ShortRepoPath()) |
| 53 | + case models.ActionCommentIssue: |
| 54 | + title += ctx.Tr("action.comment_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 55 | + case models.ActionMergePullRequest: |
| 56 | + title += ctx.Tr("action.merge_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 57 | + case models.ActionCloseIssue: |
| 58 | + title += ctx.Tr("action.close_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 59 | + case models.ActionReopenIssue: |
| 60 | + title += ctx.Tr("action.reopen_issue", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 61 | + case models.ActionClosePullRequest: |
| 62 | + title += ctx.Tr("action.close_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 63 | + case models.ActionReopenPullRequest: |
| 64 | + title += ctx.Tr("action.reopen_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath) |
| 65 | + case models.ActionDeleteTag: |
| 66 | + title += ctx.Tr("action.delete_tag", act.GetRepoLink(), html.EscapeString(act.GetTag()), act.ShortRepoPath()) |
| 67 | + case models.ActionDeleteBranch: |
| 68 | + title += ctx.Tr("action.delete_branch", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) |
| 69 | + case models.ActionMirrorSyncPush: |
| 70 | + title += ctx.Tr("action.mirror_sync_push", act.GetRepoLink(), url.QueryEscape(act.GetBranch()), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) |
| 71 | + case models.ActionMirrorSyncCreate: |
| 72 | + title += ctx.Tr("action.mirror_sync_create", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) |
| 73 | + case models.ActionMirrorSyncDelete: |
| 74 | + title += ctx.Tr("action.mirror_sync_delete", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath()) |
| 75 | + case models.ActionApprovePullRequest: |
| 76 | + title += ctx.Tr("action.approve_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 77 | + case models.ActionRejectPullRequest: |
| 78 | + title += ctx.Tr("action.reject_pull_request", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 79 | + case models.ActionCommentPull: |
| 80 | + title += ctx.Tr("action.comment_pull", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath()) |
| 81 | + case models.ActionPublishRelease: |
| 82 | + title += ctx.Tr("action.publish_release", act.GetRepoLink(), html.EscapeString(act.GetBranch()), act.ShortRepoPath(), act.Content) |
| 83 | + case models.ActionPullReviewDismissed: |
| 84 | + title += ctx.Tr("action.review_dismissed", act.GetRepoLink(), act.GetIssueInfos()[0], act.ShortRepoPath(), act.GetIssueInfos()[1]) |
| 85 | + case models.ActionStarRepo: |
| 86 | + title += ctx.Tr("action.stared_repo", act.GetRepoLink(), act.GetRepoPath()) |
| 87 | + link = &feeds.Link{Href: act.GetRepoLink()} |
| 88 | + case models.ActionWatchRepo: |
| 89 | + title += ctx.Tr("action.watched_repo", act.GetRepoLink(), act.GetRepoPath()) |
| 90 | + link = &feeds.Link{Href: act.GetRepoLink()} |
| 91 | + default: |
| 92 | + return nil, fmt.Errorf("unknown action type: %v", act.OpType) |
| 93 | + } |
| 94 | + |
| 95 | + // description & content |
| 96 | + { |
| 97 | + switch act.OpType { |
| 98 | + case models.ActionCommitRepo, models.ActionMirrorSyncPush: |
| 99 | + push := templates.ActionContent2Commits(act) |
| 100 | + repoLink := act.GetRepoLink() |
| 101 | + |
| 102 | + for _, commit := range push.Commits { |
| 103 | + if len(desc) != 0 { |
| 104 | + desc += "\n\n" |
| 105 | + } |
| 106 | + desc += fmt.Sprintf("<a href=\"%s\">%s</a>\n%s", |
| 107 | + fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), commit.Sha1), |
| 108 | + commit.Sha1, |
| 109 | + templates.RenderCommitMessage(commit.Message, repoLink, nil), |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + if push.Len > 1 { |
| 114 | + link = &feeds.Link{Href: fmt.Sprintf("%s/%s", setting.AppSubURL, push.CompareURL)} |
| 115 | + } else if push.Len == 1 { |
| 116 | + link = &feeds.Link{Href: fmt.Sprintf("%s/commit/%s", act.GetRepoLink(), push.Commits[0].Sha1)} |
| 117 | + } |
| 118 | + |
| 119 | + case models.ActionCreateIssue, models.ActionCreatePullRequest: |
| 120 | + desc = strings.Join(act.GetIssueInfos(), "#") |
| 121 | + content = act.GetIssueContent() |
| 122 | + case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull: |
| 123 | + desc = act.GetIssueTitle() |
| 124 | + comment := act.GetIssueInfos()[1] |
| 125 | + if len(comment) != 0 { |
| 126 | + desc += "\n\n" + comment |
| 127 | + } |
| 128 | + case models.ActionMergePullRequest: |
| 129 | + desc = act.GetIssueInfos()[1] |
| 130 | + case models.ActionCloseIssue, models.ActionReopenIssue, models.ActionClosePullRequest, models.ActionReopenPullRequest: |
| 131 | + desc = act.GetIssueTitle() |
| 132 | + case models.ActionPullReviewDismissed: |
| 133 | + desc = ctx.Tr("action.review_dismissed_reason") + "\n\n" + act.GetIssueInfos()[2] |
| 134 | + } |
| 135 | + } |
| 136 | + if len(content) == 0 { |
| 137 | + content = desc |
| 138 | + } |
| 139 | + |
| 140 | + items = append(items, &feeds.Item{ |
| 141 | + Title: title, |
| 142 | + Link: link, |
| 143 | + Description: desc, |
| 144 | + Author: &feeds.Author{ |
| 145 | + Name: act.ActUser.DisplayName(), |
| 146 | + Email: act.ActUser.GetEmail(), |
| 147 | + }, |
| 148 | + Id: strconv.FormatInt(act.ID, 10), |
| 149 | + Created: act.CreatedUnix.AsTime(), |
| 150 | + Content: content, |
| 151 | + }) |
| 152 | + } |
| 153 | + return |
| 154 | +} |
0 commit comments