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.

telegram.go 12KB

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