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

webhook_telegram.go 12 kB

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