|
| 1 | +// Copyright 2019 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 models |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/git" |
| 13 | + "code.gitea.io/gitea/modules/markup" |
| 14 | + api "code.gitea.io/sdk/gitea" |
| 15 | +) |
| 16 | + |
| 17 | +type ( |
| 18 | + // TelegramPayload represents |
| 19 | + TelegramPayload struct { |
| 20 | + Message string `json:"text"` |
| 21 | + ParseMode string `json:"parse_mode"` |
| 22 | + } |
| 23 | + |
| 24 | + // TelegramMeta contains the telegram metadata |
| 25 | + TelegramMeta struct { |
| 26 | + BotToken string `json:"bot_token"` |
| 27 | + ChatID string `json:"chat_id"` |
| 28 | + } |
| 29 | +) |
| 30 | + |
| 31 | +// SetSecret sets the telegram secret |
| 32 | +func (p *TelegramPayload) SetSecret(_ string) {} |
| 33 | + |
| 34 | +// JSONPayload Marshals the TelegramPayload to json |
| 35 | +func (p *TelegramPayload) JSONPayload() ([]byte, error) { |
| 36 | + p.ParseMode = "HTML" |
| 37 | + p.Message = markup.Sanitize(p.Message) |
| 38 | + data, err := json.MarshalIndent(p, "", " ") |
| 39 | + if err != nil { |
| 40 | + return []byte{}, err |
| 41 | + } |
| 42 | + return data, nil |
| 43 | +} |
| 44 | + |
| 45 | +func getTelegramCreatePayload(p *api.CreatePayload) (*TelegramPayload, error) { |
| 46 | + // created tag/branch |
| 47 | + refName := git.RefEndName(p.Ref) |
| 48 | + title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> created`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType, |
| 49 | + p.Repo.HTMLURL+"/src/"+refName, refName) |
| 50 | + |
| 51 | + return &TelegramPayload{ |
| 52 | + Message: title, |
| 53 | + }, nil |
| 54 | +} |
| 55 | + |
| 56 | +func getTelegramDeletePayload(p *api.DeletePayload) (*TelegramPayload, error) { |
| 57 | + // created tag/branch |
| 58 | + refName := git.RefEndName(p.Ref) |
| 59 | + title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType, |
| 60 | + p.Repo.HTMLURL+"/src/"+refName, refName) |
| 61 | + |
| 62 | + return &TelegramPayload{ |
| 63 | + Message: title, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +func getTelegramForkPayload(p *api.ForkPayload) (*TelegramPayload, error) { |
| 68 | + title := fmt.Sprintf(`%s is forked to <a href="%s">%s</a>`, p.Forkee.FullName, p.Repo.HTMLURL, p.Repo.FullName) |
| 69 | + |
| 70 | + return &TelegramPayload{ |
| 71 | + Message: title, |
| 72 | + }, nil |
| 73 | +} |
| 74 | + |
| 75 | +func getTelegramPushPayload(p *api.PushPayload) (*TelegramPayload, error) { |
| 76 | + var ( |
| 77 | + branchName = git.RefEndName(p.Ref) |
| 78 | + commitDesc string |
| 79 | + ) |
| 80 | + |
| 81 | + var titleLink string |
| 82 | + if len(p.Commits) == 1 { |
| 83 | + commitDesc = "1 new commit" |
| 84 | + titleLink = p.Commits[0].URL |
| 85 | + } else { |
| 86 | + commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) |
| 87 | + titleLink = p.CompareURL |
| 88 | + } |
| 89 | + if titleLink == "" { |
| 90 | + titleLink = p.Repo.HTMLURL + "/src/" + branchName |
| 91 | + } |
| 92 | + title := fmt.Sprintf(`[<a href="%s">%s</a>:<a href="%s">%s</a>] %s`, p.Repo.HTMLURL, p.Repo.FullName, titleLink, branchName, commitDesc) |
| 93 | + |
| 94 | + var text string |
| 95 | + // for each commit, generate attachment text |
| 96 | + for i, commit := range p.Commits { |
| 97 | + var authorName string |
| 98 | + if commit.Author != nil { |
| 99 | + authorName = " - " + commit.Author.Name |
| 100 | + } |
| 101 | + text += fmt.Sprintf(`[<a href="%s">%s</a>] %s`, commit.URL, commit.ID[:7], |
| 102 | + strings.TrimRight(commit.Message, "\r\n")) + authorName |
| 103 | + // add linebreak to each commit but the last |
| 104 | + if i < len(p.Commits)-1 { |
| 105 | + text += "\n" |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return &TelegramPayload{ |
| 110 | + Message: title + "\n" + text, |
| 111 | + }, nil |
| 112 | +} |
| 113 | + |
| 114 | +func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) { |
| 115 | + var text, title string |
| 116 | + switch p.Action { |
| 117 | + case api.HookIssueOpened: |
| 118 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 119 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 120 | + text = p.Issue.Body |
| 121 | + case api.HookIssueClosed: |
| 122 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue closed: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 123 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 124 | + text = p.Issue.Body |
| 125 | + case api.HookIssueReOpened: |
| 126 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue re-opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 127 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 128 | + text = p.Issue.Body |
| 129 | + case api.HookIssueEdited: |
| 130 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue edited: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 131 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 132 | + text = p.Issue.Body |
| 133 | + case api.HookIssueAssigned: |
| 134 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue assigned to %s: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 135 | + p.Issue.Assignee.UserName, p.Issue.URL, p.Index, p.Issue.Title) |
| 136 | + text = p.Issue.Body |
| 137 | + case api.HookIssueUnassigned: |
| 138 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue unassigned: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 139 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 140 | + text = p.Issue.Body |
| 141 | + case api.HookIssueLabelUpdated: |
| 142 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue labels updated: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 143 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 144 | + text = p.Issue.Body |
| 145 | + case api.HookIssueLabelCleared: |
| 146 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue labels cleared: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 147 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 148 | + text = p.Issue.Body |
| 149 | + case api.HookIssueSynchronized: |
| 150 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue synchronized: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 151 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 152 | + text = p.Issue.Body |
| 153 | + case api.HookIssueMilestoned: |
| 154 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 155 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 156 | + text = p.Issue.Body |
| 157 | + case api.HookIssueDemilestoned: |
| 158 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Issue clear milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 159 | + p.Issue.URL, p.Index, p.Issue.Title) |
| 160 | + text = p.Issue.Body |
| 161 | + } |
| 162 | + |
| 163 | + return &TelegramPayload{ |
| 164 | + Message: title + "\n\n" + text, |
| 165 | + }, nil |
| 166 | +} |
| 167 | + |
| 168 | +func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayload, error) { |
| 169 | + url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID)) |
| 170 | + title := fmt.Sprintf(`<a href="%s">#%d %s</a>`, url, p.Issue.Index, p.Issue.Title) |
| 171 | + var text string |
| 172 | + switch p.Action { |
| 173 | + case api.HookIssueCommentCreated: |
| 174 | + text = "New comment: " + title |
| 175 | + text += p.Comment.Body |
| 176 | + case api.HookIssueCommentEdited: |
| 177 | + text = "Comment edited: " + title |
| 178 | + text += p.Comment.Body |
| 179 | + case api.HookIssueCommentDeleted: |
| 180 | + text = "Comment deleted: " + title |
| 181 | + text += p.Comment.Body |
| 182 | + } |
| 183 | + |
| 184 | + return &TelegramPayload{ |
| 185 | + Message: title + "\n" + text, |
| 186 | + }, nil |
| 187 | +} |
| 188 | + |
| 189 | +func getTelegramPullRequestPayload(p *api.PullRequestPayload) (*TelegramPayload, error) { |
| 190 | + var text, title string |
| 191 | + switch p.Action { |
| 192 | + case api.HookIssueOpened: |
| 193 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 194 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 195 | + text = p.PullRequest.Body |
| 196 | + case api.HookIssueClosed: |
| 197 | + if p.PullRequest.HasMerged { |
| 198 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request merged: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 199 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 200 | + } else { |
| 201 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request closed: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 202 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 203 | + } |
| 204 | + text = p.PullRequest.Body |
| 205 | + case api.HookIssueReOpened: |
| 206 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request re-opened: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 207 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 208 | + text = p.PullRequest.Body |
| 209 | + case api.HookIssueEdited: |
| 210 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request edited: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 211 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 212 | + text = p.PullRequest.Body |
| 213 | + case api.HookIssueAssigned: |
| 214 | + list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID}) |
| 215 | + if err != nil { |
| 216 | + return &TelegramPayload{}, err |
| 217 | + } |
| 218 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request assigned to %s: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 219 | + list, p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 220 | + text = p.PullRequest.Body |
| 221 | + case api.HookIssueUnassigned: |
| 222 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request unassigned: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 223 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 224 | + text = p.PullRequest.Body |
| 225 | + case api.HookIssueLabelUpdated: |
| 226 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request labels updated: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 227 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 228 | + text = p.PullRequest.Body |
| 229 | + case api.HookIssueLabelCleared: |
| 230 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request labels cleared: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 231 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 232 | + text = p.PullRequest.Body |
| 233 | + case api.HookIssueSynchronized: |
| 234 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request synchronized: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 235 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 236 | + text = p.PullRequest.Body |
| 237 | + case api.HookIssueMilestoned: |
| 238 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 239 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 240 | + text = p.PullRequest.Body |
| 241 | + case api.HookIssueDemilestoned: |
| 242 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Pull request clear milestone: <a href="%s">#%d %s</a>`, p.Repository.HTMLURL, p.Repository.FullName, |
| 243 | + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) |
| 244 | + text = p.PullRequest.Body |
| 245 | + } |
| 246 | + |
| 247 | + return &TelegramPayload{ |
| 248 | + Message: title + "\n" + text, |
| 249 | + }, nil |
| 250 | +} |
| 251 | + |
| 252 | +func getTelegramRepositoryPayload(p *api.RepositoryPayload) (*TelegramPayload, error) { |
| 253 | + var title string |
| 254 | + switch p.Action { |
| 255 | + case api.HookRepoCreated: |
| 256 | + title = fmt.Sprintf(`[<a href="%s">%s</a>] Repository created`, p.Repository.HTMLURL, p.Repository.FullName) |
| 257 | + return &TelegramPayload{ |
| 258 | + Message: title, |
| 259 | + }, nil |
| 260 | + case api.HookRepoDeleted: |
| 261 | + title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) |
| 262 | + return &TelegramPayload{ |
| 263 | + Message: title, |
| 264 | + }, nil |
| 265 | + } |
| 266 | + return nil, nil |
| 267 | +} |
| 268 | + |
| 269 | +func getTelegramReleasePayload(p *api.ReleasePayload) (*TelegramPayload, error) { |
| 270 | + var title, url string |
| 271 | + switch p.Action { |
| 272 | + case api.HookReleasePublished: |
| 273 | + title = fmt.Sprintf("[%s] Release created", p.Release.TagName) |
| 274 | + url = p.Release.URL |
| 275 | + return &TelegramPayload{ |
| 276 | + Message: title + "\n" + url, |
| 277 | + }, nil |
| 278 | + case api.HookReleaseUpdated: |
| 279 | + title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) |
| 280 | + url = p.Release.URL |
| 281 | + return &TelegramPayload{ |
| 282 | + Message: title + "\n" + url, |
| 283 | + }, nil |
| 284 | + |
| 285 | + case api.HookReleaseDeleted: |
| 286 | + title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) |
| 287 | + url = p.Release.URL |
| 288 | + return &TelegramPayload{ |
| 289 | + Message: title + "\n" + url, |
| 290 | + }, nil |
| 291 | + } |
| 292 | + |
| 293 | + return nil, nil |
| 294 | +} |
| 295 | + |
| 296 | +// GetTelegramPayload converts a telegram webhook into a TelegramPayload |
| 297 | +func GetTelegramPayload(p api.Payloader, event HookEventType, meta string) (*TelegramPayload, error) { |
| 298 | + s := new(TelegramPayload) |
| 299 | + |
| 300 | + switch event { |
| 301 | + case HookEventCreate: |
| 302 | + return getTelegramCreatePayload(p.(*api.CreatePayload)) |
| 303 | + case HookEventDelete: |
| 304 | + return getTelegramDeletePayload(p.(*api.DeletePayload)) |
| 305 | + case HookEventFork: |
| 306 | + return getTelegramForkPayload(p.(*api.ForkPayload)) |
| 307 | + case HookEventIssues: |
| 308 | + return getTelegramIssuesPayload(p.(*api.IssuePayload)) |
| 309 | + case HookEventIssueComment: |
| 310 | + return getTelegramIssueCommentPayload(p.(*api.IssueCommentPayload)) |
| 311 | + case HookEventPush: |
| 312 | + return getTelegramPushPayload(p.(*api.PushPayload)) |
| 313 | + case HookEventPullRequest: |
| 314 | + return getTelegramPullRequestPayload(p.(*api.PullRequestPayload)) |
| 315 | + case HookEventRepository: |
| 316 | + return getTelegramRepositoryPayload(p.(*api.RepositoryPayload)) |
| 317 | + case HookEventRelease: |
| 318 | + return getTelegramReleasePayload(p.(*api.ReleasePayload)) |
| 319 | + } |
| 320 | + |
| 321 | + return s, nil |
| 322 | +} |
0 commit comments