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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "fmt"
  6. "strings"
  7. webhook_model "code.gitea.io/gitea/models/webhook"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/json"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/markup"
  12. api "code.gitea.io/gitea/modules/structs"
  13. webhook_module "code.gitea.io/gitea/modules/webhook"
  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. ThreadID string `json:"thread_id"`
  27. }
  28. )
  29. // GetTelegramHook returns telegram metadata
  30. func GetTelegramHook(w *webhook_model.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. var _ PayloadConvertor = &TelegramPayload{}
  38. // JSONPayload Marshals the TelegramPayload to json
  39. func (t *TelegramPayload) JSONPayload() ([]byte, error) {
  40. t.ParseMode = "HTML"
  41. t.DisableWebPreview = true
  42. t.Message = markup.Sanitize(t.Message)
  43. data, err := json.MarshalIndent(t, "", " ")
  44. if err != nil {
  45. return []byte{}, err
  46. }
  47. return data, nil
  48. }
  49. // Create implements PayloadConvertor Create method
  50. func (t *TelegramPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
  51. // created tag/branch
  52. refName := git.RefName(p.Ref).ShortName()
  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 createTelegramPayload(title), nil
  56. }
  57. // Delete implements PayloadConvertor Delete method
  58. func (t *TelegramPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
  59. // created tag/branch
  60. refName := git.RefName(p.Ref).ShortName()
  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 createTelegramPayload(title), nil
  64. }
  65. // Fork implements PayloadConvertor Fork method
  66. func (t *TelegramPayload) Fork(p *api.ForkPayload) (api.Payloader, error) {
  67. title := fmt.Sprintf(`%s is forked to <a href="%s">%s</a>`, p.Forkee.FullName, p.Repo.HTMLURL, p.Repo.FullName)
  68. return createTelegramPayload(title), nil
  69. }
  70. // Push implements PayloadConvertor Push method
  71. func (t *TelegramPayload) Push(p *api.PushPayload) (api.Payloader, error) {
  72. var (
  73. branchName = git.RefName(p.Ref).ShortName()
  74. commitDesc string
  75. )
  76. var titleLink string
  77. if p.TotalCommits == 1 {
  78. commitDesc = "1 new commit"
  79. titleLink = p.Commits[0].URL
  80. } else {
  81. commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
  82. titleLink = p.CompareURL
  83. }
  84. if titleLink == "" {
  85. titleLink = p.Repo.HTMLURL + "/src/" + branchName
  86. }
  87. title := fmt.Sprintf(`[<a href="%s">%s</a>:<a href="%s">%s</a>] %s`, p.Repo.HTMLURL, p.Repo.FullName, titleLink, branchName, commitDesc)
  88. var text string
  89. // for each commit, generate attachment text
  90. for i, commit := range p.Commits {
  91. var authorName string
  92. if commit.Author != nil {
  93. authorName = " - " + commit.Author.Name
  94. }
  95. text += fmt.Sprintf(`[<a href="%s">%s</a>] %s`, commit.URL, commit.ID[:7],
  96. strings.TrimRight(commit.Message, "\r\n")) + authorName
  97. // add linebreak to each commit but the last
  98. if i < len(p.Commits)-1 {
  99. text += "\n"
  100. }
  101. }
  102. return createTelegramPayload(title + "\n" + text), nil
  103. }
  104. // Issue implements PayloadConvertor Issue method
  105. func (t *TelegramPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
  106. text, _, attachmentText, _ := getIssuesPayloadInfo(p, htmlLinkFormatter, true)
  107. return createTelegramPayload(text + "\n\n" + attachmentText), nil
  108. }
  109. // IssueComment implements PayloadConvertor IssueComment method
  110. func (t *TelegramPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
  111. text, _, _ := getIssueCommentPayloadInfo(p, htmlLinkFormatter, true)
  112. return createTelegramPayload(text + "\n" + p.Comment.Body), nil
  113. }
  114. // PullRequest implements PayloadConvertor PullRequest method
  115. func (t *TelegramPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
  116. text, _, attachmentText, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter, true)
  117. return createTelegramPayload(text + "\n" + attachmentText), nil
  118. }
  119. // Review implements PayloadConvertor Review method
  120. func (t *TelegramPayload) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (api.Payloader, error) {
  121. var text, attachmentText string
  122. switch p.Action {
  123. case api.HookIssueReviewed:
  124. action, err := parseHookPullRequestEventType(event)
  125. if err != nil {
  126. return nil, err
  127. }
  128. text = fmt.Sprintf("[%s] Pull request review %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
  129. attachmentText = p.Review.Content
  130. }
  131. return createTelegramPayload(text + "\n" + attachmentText), nil
  132. }
  133. // Repository implements PayloadConvertor Repository method
  134. func (t *TelegramPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) {
  135. var title string
  136. switch p.Action {
  137. case api.HookRepoCreated:
  138. title = fmt.Sprintf(`[<a href="%s">%s</a>] Repository created`, p.Repository.HTMLURL, p.Repository.FullName)
  139. return createTelegramPayload(title), nil
  140. case api.HookRepoDeleted:
  141. title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
  142. return createTelegramPayload(title), nil
  143. }
  144. return nil, nil
  145. }
  146. // Wiki implements PayloadConvertor Wiki method
  147. func (t *TelegramPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) {
  148. text, _, _ := getWikiPayloadInfo(p, htmlLinkFormatter, true)
  149. return createTelegramPayload(text), nil
  150. }
  151. // Release implements PayloadConvertor Release method
  152. func (t *TelegramPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
  153. text, _ := getReleasePayloadInfo(p, htmlLinkFormatter, true)
  154. return createTelegramPayload(text), nil
  155. }
  156. // GetTelegramPayload converts a telegram webhook into a TelegramPayload
  157. func GetTelegramPayload(p api.Payloader, event webhook_module.HookEventType, _ string) (api.Payloader, error) {
  158. return convertPayloader(new(TelegramPayload), p, event)
  159. }
  160. func createTelegramPayload(message string) *TelegramPayload {
  161. return &TelegramPayload{
  162. Message: strings.TrimSpace(message),
  163. }
  164. }