You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webhook_telegram.go 12KB

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