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.1KB

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