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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/markup"
  13. api "code.gitea.io/gitea/modules/structs"
  14. )
  15. type (
  16. // TelegramPayload represents
  17. TelegramPayload struct {
  18. Message string `json:"text"`
  19. ParseMode string `json:"parse_mode"`
  20. DisableWebPreview bool `json:"disable_web_page_preview"`
  21. }
  22. // TelegramMeta contains the telegram metadata
  23. TelegramMeta struct {
  24. BotToken string `json:"bot_token"`
  25. ChatID string `json:"chat_id"`
  26. }
  27. )
  28. // GetTelegramHook returns telegram metadata
  29. func GetTelegramHook(w *models.Webhook) *TelegramMeta {
  30. s := &TelegramMeta{}
  31. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  32. log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err)
  33. }
  34. return s
  35. }
  36. // SetSecret sets the telegram secret
  37. func (p *TelegramPayload) SetSecret(_ string) {}
  38. // JSONPayload Marshals the TelegramPayload to json
  39. func (p *TelegramPayload) JSONPayload() ([]byte, error) {
  40. p.ParseMode = "HTML"
  41. p.DisableWebPreview = true
  42. p.Message = markup.Sanitize(p.Message)
  43. data, err := json.MarshalIndent(p, "", " ")
  44. if err != nil {
  45. return []byte{}, err
  46. }
  47. return data, nil
  48. }
  49. func getTelegramCreatePayload(p *api.CreatePayload) (*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> created`, 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 getTelegramDeletePayload(p *api.DeletePayload) (*TelegramPayload, error) {
  59. // created tag/branch
  60. refName := git.RefEndName(p.Ref)
  61. title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
  62. p.Repo.HTMLURL+"/src/"+refName, refName)
  63. return &TelegramPayload{
  64. Message: title,
  65. }, nil
  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. return &TelegramPayload{
  70. Message: title,
  71. }, nil
  72. }
  73. func getTelegramPushPayload(p *api.PushPayload) (*TelegramPayload, error) {
  74. var (
  75. branchName = git.RefEndName(p.Ref)
  76. commitDesc string
  77. )
  78. var titleLink string
  79. if len(p.Commits) == 1 {
  80. commitDesc = "1 new commit"
  81. titleLink = p.Commits[0].URL
  82. } else {
  83. commitDesc = fmt.Sprintf("%d new commits", len(p.Commits))
  84. titleLink = p.CompareURL
  85. }
  86. if titleLink == "" {
  87. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  88. }
  89. title := fmt.Sprintf(`[<a href="%s">%s</a>:<a href="%s">%s</a>] %s`, p.Repo.HTMLURL, p.Repo.FullName, titleLink, branchName, commitDesc)
  90. var text string
  91. // for each commit, generate attachment text
  92. for i, commit := range p.Commits {
  93. var authorName string
  94. if commit.Author != nil {
  95. authorName = " - " + commit.Author.Name
  96. }
  97. text += fmt.Sprintf(`[<a href="%s">%s</a>] %s`, commit.URL, commit.ID[:7],
  98. strings.TrimRight(commit.Message, "\r\n")) + authorName
  99. // add linebreak to each commit but the last
  100. if i < len(p.Commits)-1 {
  101. text += "\n"
  102. }
  103. }
  104. return &TelegramPayload{
  105. Message: title + "\n" + text,
  106. }, nil
  107. }
  108. func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) {
  109. text, _, attachmentText, _ := getIssuesPayloadInfo(p, htmlLinkFormatter)
  110. return &TelegramPayload{
  111. Message: text + "\n\n" + attachmentText,
  112. }, nil
  113. }
  114. func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayload, error) {
  115. text, _, _ := getIssueCommentPayloadInfo(p, htmlLinkFormatter)
  116. return &TelegramPayload{
  117. Message: text + "\n" + p.Comment.Body,
  118. }, nil
  119. }
  120. func getTelegramPullRequestPayload(p *api.PullRequestPayload) (*TelegramPayload, error) {
  121. text, _, attachmentText, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter)
  122. return &TelegramPayload{
  123. Message: text + "\n" + attachmentText,
  124. }, nil
  125. }
  126. func getTelegramRepositoryPayload(p *api.RepositoryPayload) (*TelegramPayload, error) {
  127. var title string
  128. switch p.Action {
  129. case api.HookRepoCreated:
  130. title = fmt.Sprintf(`[<a href="%s">%s</a>] Repository created`, p.Repository.HTMLURL, p.Repository.FullName)
  131. return &TelegramPayload{
  132. Message: title,
  133. }, nil
  134. case api.HookRepoDeleted:
  135. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  136. return &TelegramPayload{
  137. Message: title,
  138. }, nil
  139. }
  140. return nil, nil
  141. }
  142. func getTelegramReleasePayload(p *api.ReleasePayload) (*TelegramPayload, error) {
  143. text, _ := getReleasePayloadInfo(p, htmlLinkFormatter)
  144. return &TelegramPayload{
  145. Message: text + "\n",
  146. }, nil
  147. }
  148. // GetTelegramPayload converts a telegram webhook into a TelegramPayload
  149. func GetTelegramPayload(p api.Payloader, event models.HookEventType, meta string) (*TelegramPayload, error) {
  150. s := new(TelegramPayload)
  151. switch event {
  152. case models.HookEventCreate:
  153. return getTelegramCreatePayload(p.(*api.CreatePayload))
  154. case models.HookEventDelete:
  155. return getTelegramDeletePayload(p.(*api.DeletePayload))
  156. case models.HookEventFork:
  157. return getTelegramForkPayload(p.(*api.ForkPayload))
  158. case models.HookEventIssues:
  159. return getTelegramIssuesPayload(p.(*api.IssuePayload))
  160. case models.HookEventIssueComment:
  161. return getTelegramIssueCommentPayload(p.(*api.IssueCommentPayload))
  162. case models.HookEventPush:
  163. return getTelegramPushPayload(p.(*api.PushPayload))
  164. case models.HookEventPullRequest:
  165. return getTelegramPullRequestPayload(p.(*api.PullRequestPayload))
  166. case models.HookEventRepository:
  167. return getTelegramRepositoryPayload(p.(*api.RepositoryPayload))
  168. case models.HookEventRelease:
  169. return getTelegramReleasePayload(p.(*api.ReleasePayload))
  170. }
  171. return s, nil
  172. }