|
| 1 | +package rss |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "code.gitea.io/gitea/models" |
| 9 | + "code.gitea.io/gitea/modules/context" |
| 10 | + |
| 11 | + "github.com/gorilla/feeds" |
| 12 | +) |
| 13 | + |
| 14 | +func FeedActionsToFeedItems(ctx *context.Context, actions []*models.Action) (items []*feeds.Item) { |
| 15 | + for _, act := range actions { |
| 16 | + act.LoadActUser() |
| 17 | + |
| 18 | + content, desc, title := "", "", "" |
| 19 | + |
| 20 | + link := &feeds.Link{Href: act.GetCommentLink()} |
| 21 | + |
| 22 | + title = tmpTypeName(act.OpType) |
| 23 | + |
| 24 | + // title |
| 25 | + { |
| 26 | + switch act.OpType { |
| 27 | + |
| 28 | + case models.ActionCreateRepo: |
| 29 | + title = ctx.Tr("action.create_repo", act.GetRepoLink(), act.ShortRepoPath()) |
| 30 | + case models.ActionRenameRepo: |
| 31 | + title = ctx.Tr("action.rename_repo", act.GetContent(), act.GetRepoLink(), act.ShortRepoPath()) |
| 32 | + case models.ActionCommitRepo: |
| 33 | + branchLink := act.GetBranch() |
| 34 | + if len(act.Content) != 0 { |
| 35 | + title = ctx.Tr("action.commit_repo", act.GetRepoLink(), branchLink, act.GetBranch(), act.ShortRepoPath()) |
| 36 | + } else { |
| 37 | + title = ctx.Tr("action.create_branch", act.GetRepoLink(), branchLink, act.GetBranch(), act.ShortRepoPath()) |
| 38 | + } |
| 39 | + case models.ActionCreateIssue: |
| 40 | + title = ` {{ $index := index .GetIssueInfos 0}} |
| 41 | + {{$.i18n.Tr "action.create_issue" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 42 | + case models.ActionCreatePullRequest: |
| 43 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 44 | + {{$.i18n.Tr "action.create_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 45 | + case models.ActionTransferRepo: |
| 46 | + title = `{{$.i18n.Tr "action.transfer_repo" .GetContent .GetRepoLink .ShortRepoPath | Str2html}}` |
| 47 | + case models.ActionPushTag: |
| 48 | + title = `{{ $tagLink := .GetTag | EscapePound | Escape}} |
| 49 | + {{$.i18n.Tr "action.push_tag" .GetRepoLink $tagLink .ShortRepoPath | Str2html}}` |
| 50 | + case models.ActionCommentIssue: |
| 51 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 52 | + {{$.i18n.Tr "action.comment_issue" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 53 | + case models.ActionMergePullRequest: |
| 54 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 55 | + {{$.i18n.Tr "action.merge_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 56 | + case models.ActionCloseIssue: |
| 57 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 58 | + {{$.i18n.Tr "action.close_issue" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 59 | + case models.ActionReopenIssue: |
| 60 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 61 | + {{$.i18n.Tr "action.reopen_issue" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 62 | + case models.ActionClosePullRequest: |
| 63 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 64 | + {{$.i18n.Tr "action.close_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 65 | + case models.ActionReopenPullRequest: |
| 66 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 67 | + {{$.i18n.Tr "action.reopen_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 68 | + case models.ActionDeleteTag: |
| 69 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 70 | + {{$.i18n.Tr "action.delete_tag" .GetRepoLink (.GetTag|Escape) .ShortRepoPath | Str2html}}` |
| 71 | + case models.ActionDeleteBranch: |
| 72 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 73 | + {{$.i18n.Tr "action.delete_branch" .GetRepoLink (.GetBranch|Escape) .ShortRepoPath | Str2html}}` |
| 74 | + case models.ActionMirrorSyncPush: |
| 75 | + title = `{{ $branchLink := .GetBranch | EscapePound}} |
| 76 | + {{$.i18n.Tr "action.mirror_sync_push" .GetRepoLink $branchLink (.GetBranch|Escape) .ShortRepoPath | Str2html}}` |
| 77 | + case models.ActionMirrorSyncCreate: |
| 78 | + title = `{{$.i18n.Tr "action.mirror_sync_create" .GetRepoLink (.GetBranch|Escape) .ShortRepoPath | Str2html}}` |
| 79 | + case models.ActionMirrorSyncDelete: |
| 80 | + title = `{{$.i18n.Tr "action.mirror_sync_delete" .GetRepoLink (.GetBranch|Escape) .ShortRepoPath | Str2html}}` |
| 81 | + case models.ActionApprovePullRequest: |
| 82 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 83 | + {{$.i18n.Tr "action.approve_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 84 | + case models.ActionRejectPullRequest: |
| 85 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 86 | + {{$.i18n.Tr "action.reject_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 87 | + case models.ActionCommentPull: |
| 88 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 89 | + {{$.i18n.Tr "action.comment_pull" .GetRepoLink $index .ShortRepoPath | Str2html}}` |
| 90 | + case models.ActionPublishRelease: |
| 91 | + title = `{{ $branchLink := .GetBranch | EscapePound | Escape}} |
| 92 | + {{ $linkText := .Content | RenderEmoji }} |
| 93 | + {{$.i18n.Tr "action.publish_release" .GetRepoLink $branchLink .ShortRepoPath $linkText | Str2html}}` |
| 94 | + case models.ActionPullReviewDismissed: |
| 95 | + title = `{{ $index := index .GetIssueInfos 0}} |
| 96 | + {{ $reviewer := index .GetIssueInfos 1}} |
| 97 | + {{$.i18n.Tr "action.review_dismissed" .GetRepoLink $index .ShortRepoPath $reviewer | Str2html}}` |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // description & content |
| 102 | + { |
| 103 | + switch act.OpType { |
| 104 | + case models.ActionCommitRepo, models.ActionMirrorSyncPush: |
| 105 | + desc = `<div class="content"> |
| 106 | + <ul> |
| 107 | + {{ $push := ActionContent2Commits .}} |
| 108 | + {{ $repoLink := .GetRepoLink}} |
| 109 | + {{if $push.Commits}} |
| 110 | + {{range $push.Commits}} |
| 111 | + {{ $commitLink := printf "%s/commit/%s" $repoLink .Sha1}} |
| 112 | + <li> |
| 113 | + {{avatarHTML ($push.AvatarLink .AuthorEmail) 16 "mr-2" .AuthorName}} |
| 114 | + <a class="commit-id mr-2" href="{{$commitLink}}">{{ShortSha .Sha1}}</a> |
| 115 | + <span class="text truncate light grey"> |
| 116 | + {{RenderCommitMessage .Message $repoLink $.ComposeMetas}} |
| 117 | + </span> |
| 118 | + </li> |
| 119 | + {{end}} |
| 120 | + {{end}} |
| 121 | + {{if and (gt $push.Len 1) $push.CompareURL}}<li><a href="{{AppSubUrl}}/{{$push.CompareURL}}">{{$.i18n.Tr "action.compare_commits" $push.Len}} »</a></li>{{end}} |
| 122 | + </ul> |
| 123 | + </div>` |
| 124 | + |
| 125 | + case models.ActionCreateIssue, models.ActionCreatePullRequest: |
| 126 | + desc = strings.Join(act.GetIssueInfos(), "#") // index .GetIssueInfos 1 | RenderEmoji |
| 127 | + content = act.GetIssueContent() |
| 128 | + case models.ActionCommentIssue, models.ActionApprovePullRequest, models.ActionRejectPullRequest, models.ActionCommentPull: |
| 129 | + desc = act.GetIssueTitle() // class="text truncate issue title" | RenderEmoji |
| 130 | + comment := act.GetIssueInfos()[1] // class="text light grey" | RenderEmoji |
| 131 | + if len(comment) != 0 { |
| 132 | + desc += "\n\n" + comment |
| 133 | + } |
| 134 | + case models.ActionMergePullRequest: |
| 135 | + desc = act.GetIssueInfos()[1] // class="text light grey" |
| 136 | + case models.ActionCloseIssue, models.ActionReopenIssue, models.ActionClosePullRequest, models.ActionReopenPullRequest: |
| 137 | + desc = `<span class="text truncate issue title">{{.GetIssueTitle | RenderEmoji}}</span>` |
| 138 | + case models.ActionPullReviewDismissed: |
| 139 | + desc = `<p class="text light grey">{{$.i18n.Tr "action.review_dismissed_reason"}}</p> |
| 140 | + <p class="text light grey">{{index .GetIssueInfos 2 | RenderEmoji}}</p>` |
| 141 | + } |
| 142 | + } |
| 143 | + if len(content) == 0 { |
| 144 | + content = desc |
| 145 | + } |
| 146 | + |
| 147 | + // img := templates.ActionIcon(act.OpType) |
| 148 | + |
| 149 | + items = append(items, &feeds.Item{ |
| 150 | + Title: title, |
| 151 | + Link: link, |
| 152 | + Description: desc, |
| 153 | + Author: feedsAuthor(act.ActUser), |
| 154 | + Id: fmt.Sprint(act.ID), |
| 155 | + Created: time.Now(), // Created: act.CreatedUnix.AsTime(), |
| 156 | + Content: content, |
| 157 | + }) |
| 158 | + } |
| 159 | + return |
| 160 | +} |
| 161 | + |
| 162 | +func feedsAuthor(user *models.User) *feeds.Author { |
| 163 | + return &feeds.Author{ |
| 164 | + Name: user.DisplayName(), |
| 165 | + Email: user.GetEmail(), |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func tmpTypeName(t models.ActionType) string { |
| 170 | + switch t { |
| 171 | + case models.ActionCreateRepo: |
| 172 | + return "ActionCreateRepo" |
| 173 | + case models.ActionRenameRepo: |
| 174 | + return "ActionRenameRepo" |
| 175 | + case models.ActionStarRepo: |
| 176 | + return "ActionStarRepo" |
| 177 | + case models.ActionWatchRepo: |
| 178 | + return "ActionWatchRepo" |
| 179 | + case models.ActionCommitRepo: |
| 180 | + return "ActionCommitRepo" |
| 181 | + case models.ActionCreateIssue: |
| 182 | + return "ActionCreateIssue" |
| 183 | + case models.ActionCreatePullRequest: |
| 184 | + return "ActionCreatePullRequest" |
| 185 | + case models.ActionTransferRepo: |
| 186 | + return "ActionTransferRepo" |
| 187 | + case models.ActionPushTag: |
| 188 | + return "ActionPushTag" |
| 189 | + case models.ActionCommentIssue: |
| 190 | + return "ActionCommentIssue" |
| 191 | + case models.ActionMergePullRequest: |
| 192 | + return "ActionMergePullRequest" |
| 193 | + case models.ActionCloseIssue: |
| 194 | + return "ActionCloseIssue" |
| 195 | + case models.ActionReopenIssue: |
| 196 | + return "ActionReopenIssue" |
| 197 | + case models.ActionClosePullRequest: |
| 198 | + return "ActionClosePullRequest" |
| 199 | + case models.ActionReopenPullRequest: |
| 200 | + return "ActionReopenPullRequest" |
| 201 | + case models.ActionDeleteTag: |
| 202 | + return "ActionDeleteTag" |
| 203 | + case models.ActionDeleteBranch: |
| 204 | + return "ActionDeleteBranch" |
| 205 | + case models.ActionMirrorSyncPush: |
| 206 | + return "ActionMirrorSyncPush" |
| 207 | + case models.ActionMirrorSyncCreate: |
| 208 | + return "ActionMirrorSyncCreate" |
| 209 | + case models.ActionMirrorSyncDelete: |
| 210 | + return "ActionMirrorSyncDelete" |
| 211 | + case models.ActionApprovePullRequest: |
| 212 | + return "ActionApprovePullRequest" |
| 213 | + case models.ActionRejectPullRequest: |
| 214 | + return "ActionRejectPullRequest" |
| 215 | + case models.ActionCommentPull: |
| 216 | + return "ActionCommentPull" |
| 217 | + case models.ActionPublishRelease: |
| 218 | + return "ActionPublishRelease" |
| 219 | + case models.ActionPullReviewDismissed: |
| 220 | + return "ActionPullReviewDismissed" |
| 221 | + } |
| 222 | + return "" |
| 223 | +} |
| 224 | + |
| 225 | +/* |
| 226 | +
|
| 227 | + */ |
0 commit comments