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.

general.go 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "fmt"
  6. "html"
  7. "net/url"
  8. "strings"
  9. webhook_model "code.gitea.io/gitea/models/webhook"
  10. "code.gitea.io/gitea/modules/setting"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/modules/util"
  13. webhook_module "code.gitea.io/gitea/modules/webhook"
  14. )
  15. type linkFormatter = func(string, string) string
  16. // noneLinkFormatter does not create a link but just returns the text
  17. func noneLinkFormatter(url, text string) string {
  18. return text
  19. }
  20. // htmlLinkFormatter creates a HTML link
  21. func htmlLinkFormatter(url, text string) string {
  22. return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(url), html.EscapeString(text))
  23. }
  24. // getPullRequestInfo gets the information for a pull request
  25. func getPullRequestInfo(p *api.PullRequestPayload) (title, link, by, operator, operateResult, assignees string) {
  26. title = fmt.Sprintf("[PullRequest-%s #%d]: %s\n%s", p.Repository.FullName, p.PullRequest.Index, p.Action, p.PullRequest.Title)
  27. assignList := p.PullRequest.Assignees
  28. assignStringList := make([]string, len(assignList))
  29. for i, user := range assignList {
  30. assignStringList[i] = user.UserName
  31. }
  32. if p.Action == api.HookIssueAssigned {
  33. operateResult = fmt.Sprintf("%s assign this to %s", p.Sender.UserName, assignList[len(assignList)-1].UserName)
  34. } else if p.Action == api.HookIssueUnassigned {
  35. operateResult = fmt.Sprintf("%s unassigned this for someone", p.Sender.UserName)
  36. } else if p.Action == api.HookIssueMilestoned {
  37. operateResult = fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
  38. }
  39. link = p.PullRequest.HTMLURL
  40. by = fmt.Sprintf("PullRequest by %s", p.PullRequest.Poster.UserName)
  41. if len(assignStringList) > 0 {
  42. assignees = fmt.Sprintf("Assignees: %s", strings.Join(assignStringList, ", "))
  43. }
  44. operator = fmt.Sprintf("Operator: %s", p.Sender.UserName)
  45. return title, link, by, operator, operateResult, assignees
  46. }
  47. // getIssuesInfo gets the information for an issue
  48. func getIssuesInfo(p *api.IssuePayload) (issueTitle, link, by, operator, operateResult, assignees string) {
  49. issueTitle = fmt.Sprintf("[Issue-%s #%d]: %s\n%s", p.Repository.FullName, p.Issue.Index, p.Action, p.Issue.Title)
  50. assignList := p.Issue.Assignees
  51. assignStringList := make([]string, len(assignList))
  52. for i, user := range assignList {
  53. assignStringList[i] = user.UserName
  54. }
  55. if p.Action == api.HookIssueAssigned {
  56. operateResult = fmt.Sprintf("%s assign this to %s", p.Sender.UserName, assignList[len(assignList)-1].UserName)
  57. } else if p.Action == api.HookIssueUnassigned {
  58. operateResult = fmt.Sprintf("%s unassigned this for someone", p.Sender.UserName)
  59. } else if p.Action == api.HookIssueMilestoned {
  60. operateResult = fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
  61. }
  62. link = p.Issue.HTMLURL
  63. by = fmt.Sprintf("Issue by %s", p.Issue.Poster.UserName)
  64. if len(assignStringList) > 0 {
  65. assignees = fmt.Sprintf("Assignees: %s", strings.Join(assignStringList, ", "))
  66. }
  67. operator = fmt.Sprintf("Operator: %s", p.Sender.UserName)
  68. return issueTitle, link, by, operator, operateResult, assignees
  69. }
  70. // getIssuesCommentInfo gets the information for a comment
  71. func getIssuesCommentInfo(p *api.IssueCommentPayload) (title, link, by, operator string) {
  72. title = fmt.Sprintf("[Comment-%s #%d]: %s\n%s", p.Repository.FullName, p.Issue.Index, p.Action, p.Issue.Title)
  73. link = p.Issue.HTMLURL
  74. if p.IsPull {
  75. by = fmt.Sprintf("PullRequest by %s", p.Issue.Poster.UserName)
  76. } else {
  77. by = fmt.Sprintf("Issue by %s", p.Issue.Poster.UserName)
  78. }
  79. operator = fmt.Sprintf("Operator: %s", p.Sender.UserName)
  80. return title, link, by, operator
  81. }
  82. func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
  83. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  84. issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  85. titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
  86. var text string
  87. color := yellowColor
  88. switch p.Action {
  89. case api.HookIssueOpened:
  90. text = fmt.Sprintf("[%s] Issue opened: %s", repoLink, titleLink)
  91. color = orangeColor
  92. case api.HookIssueClosed:
  93. text = fmt.Sprintf("[%s] Issue closed: %s", repoLink, titleLink)
  94. color = redColor
  95. case api.HookIssueReOpened:
  96. text = fmt.Sprintf("[%s] Issue re-opened: %s", repoLink, titleLink)
  97. case api.HookIssueEdited:
  98. text = fmt.Sprintf("[%s] Issue edited: %s", repoLink, titleLink)
  99. case api.HookIssueAssigned:
  100. list := make([]string, len(p.Issue.Assignees))
  101. for i, user := range p.Issue.Assignees {
  102. list[i] = linkFormatter(setting.AppURL+url.PathEscape(user.UserName), user.UserName)
  103. }
  104. text = fmt.Sprintf("[%s] Issue assigned to %s: %s", repoLink, strings.Join(list, ", "), titleLink)
  105. color = greenColor
  106. case api.HookIssueUnassigned:
  107. text = fmt.Sprintf("[%s] Issue unassigned: %s", repoLink, titleLink)
  108. case api.HookIssueLabelUpdated:
  109. text = fmt.Sprintf("[%s] Issue labels updated: %s", repoLink, titleLink)
  110. case api.HookIssueLabelCleared:
  111. text = fmt.Sprintf("[%s] Issue labels cleared: %s", repoLink, titleLink)
  112. case api.HookIssueSynchronized:
  113. text = fmt.Sprintf("[%s] Issue synchronized: %s", repoLink, titleLink)
  114. case api.HookIssueMilestoned:
  115. mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
  116. text = fmt.Sprintf("[%s] Issue milestoned to %s: %s", repoLink,
  117. linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink)
  118. case api.HookIssueDemilestoned:
  119. text = fmt.Sprintf("[%s] Issue milestone cleared: %s", repoLink, titleLink)
  120. }
  121. if withSender {
  122. text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
  123. }
  124. var attachmentText string
  125. if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
  126. attachmentText = p.Issue.Body
  127. }
  128. return text, issueTitle, attachmentText, color
  129. }
  130. func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
  131. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  132. issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  133. titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
  134. var text string
  135. var attachmentText string
  136. color := yellowColor
  137. switch p.Action {
  138. case api.HookIssueOpened:
  139. text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink)
  140. attachmentText = p.PullRequest.Body
  141. color = greenColor
  142. case api.HookIssueClosed:
  143. if p.PullRequest.HasMerged {
  144. text = fmt.Sprintf("[%s] Pull request merged: %s", repoLink, titleLink)
  145. color = purpleColor
  146. } else {
  147. text = fmt.Sprintf("[%s] Pull request closed: %s", repoLink, titleLink)
  148. color = redColor
  149. }
  150. case api.HookIssueReOpened:
  151. text = fmt.Sprintf("[%s] Pull request re-opened: %s", repoLink, titleLink)
  152. case api.HookIssueEdited:
  153. text = fmt.Sprintf("[%s] Pull request edited: %s", repoLink, titleLink)
  154. attachmentText = p.PullRequest.Body
  155. case api.HookIssueAssigned:
  156. list := make([]string, len(p.PullRequest.Assignees))
  157. for i, user := range p.PullRequest.Assignees {
  158. list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName)
  159. }
  160. text = fmt.Sprintf("[%s] Pull request assigned to %s: %s", repoLink,
  161. strings.Join(list, ", "), titleLink)
  162. color = greenColor
  163. case api.HookIssueUnassigned:
  164. text = fmt.Sprintf("[%s] Pull request unassigned: %s", repoLink, titleLink)
  165. case api.HookIssueLabelUpdated:
  166. text = fmt.Sprintf("[%s] Pull request labels updated: %s", repoLink, titleLink)
  167. case api.HookIssueLabelCleared:
  168. text = fmt.Sprintf("[%s] Pull request labels cleared: %s", repoLink, titleLink)
  169. case api.HookIssueSynchronized:
  170. text = fmt.Sprintf("[%s] Pull request synchronized: %s", repoLink, titleLink)
  171. case api.HookIssueMilestoned:
  172. mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
  173. text = fmt.Sprintf("[%s] Pull request milestoned to %s: %s", repoLink,
  174. linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink)
  175. case api.HookIssueDemilestoned:
  176. text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink)
  177. case api.HookIssueReviewed:
  178. text = fmt.Sprintf("[%s] Pull request reviewed: %s", repoLink, titleLink)
  179. attachmentText = p.Review.Content
  180. case api.HookIssueReviewRequested:
  181. text = fmt.Sprintf("[%s] Pull request review requested: %s", repoLink, titleLink)
  182. case api.HookIssueReviewRequestRemoved:
  183. text = fmt.Sprintf("[%s] Pull request review request removed: %s", repoLink, titleLink)
  184. }
  185. if withSender {
  186. text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName))
  187. }
  188. return text, issueTitle, attachmentText, color
  189. }
  190. func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
  191. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  192. refLink := linkFormatter(p.Repository.HTMLURL+"/releases/tag/"+util.PathEscapeSegments(p.Release.TagName), p.Release.TagName)
  193. switch p.Action {
  194. case api.HookReleasePublished:
  195. text = fmt.Sprintf("[%s] Release created: %s", repoLink, refLink)
  196. color = greenColor
  197. case api.HookReleaseUpdated:
  198. text = fmt.Sprintf("[%s] Release updated: %s", repoLink, refLink)
  199. color = yellowColor
  200. case api.HookReleaseDeleted:
  201. text = fmt.Sprintf("[%s] Release deleted: %s", repoLink, refLink)
  202. color = redColor
  203. }
  204. if withSender {
  205. text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
  206. }
  207. return text, color
  208. }
  209. func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSender bool) (string, int, string) {
  210. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  211. pageLink := linkFormatter(p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), p.Page)
  212. var text string
  213. color := greenColor
  214. switch p.Action {
  215. case api.HookWikiCreated:
  216. text = fmt.Sprintf("[%s] New wiki page '%s'", repoLink, pageLink)
  217. case api.HookWikiEdited:
  218. text = fmt.Sprintf("[%s] Wiki page '%s' edited", repoLink, pageLink)
  219. color = yellowColor
  220. case api.HookWikiDeleted:
  221. text = fmt.Sprintf("[%s] Wiki page '%s' deleted", repoLink, pageLink)
  222. color = redColor
  223. }
  224. if p.Action != api.HookWikiDeleted && p.Comment != "" {
  225. text += fmt.Sprintf(" (%s)", p.Comment)
  226. }
  227. if withSender {
  228. text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
  229. }
  230. return text, color, pageLink
  231. }
  232. func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) {
  233. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  234. issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
  235. var text, typ, titleLink string
  236. color := yellowColor
  237. if p.IsPull {
  238. typ = "pull request"
  239. titleLink = linkFormatter(p.Comment.PRURL, issueTitle)
  240. } else {
  241. typ = "issue"
  242. titleLink = linkFormatter(p.Comment.IssueURL, issueTitle)
  243. }
  244. switch p.Action {
  245. case api.HookIssueCommentCreated:
  246. text = fmt.Sprintf("[%s] New comment on %s %s", repoLink, typ, titleLink)
  247. if p.IsPull {
  248. color = greenColorLight
  249. } else {
  250. color = orangeColorLight
  251. }
  252. case api.HookIssueCommentEdited:
  253. text = fmt.Sprintf("[%s] Comment edited on %s %s", repoLink, typ, titleLink)
  254. case api.HookIssueCommentDeleted:
  255. text = fmt.Sprintf("[%s] Comment deleted on %s %s", repoLink, typ, titleLink)
  256. color = redColor
  257. }
  258. if withSender {
  259. text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
  260. }
  261. return text, issueTitle, color
  262. }
  263. // ToHook convert models.Webhook to api.Hook
  264. // This function is not part of the convert package to prevent an import cycle
  265. func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
  266. config := map[string]string{
  267. "url": w.URL,
  268. "content_type": w.ContentType.Name(),
  269. }
  270. if w.Type == webhook_module.SLACK {
  271. s := GetSlackHook(w)
  272. config["channel"] = s.Channel
  273. config["username"] = s.Username
  274. config["icon_url"] = s.IconURL
  275. config["color"] = s.Color
  276. }
  277. authorizationHeader, err := w.HeaderAuthorization()
  278. if err != nil {
  279. return nil, err
  280. }
  281. return &api.Hook{
  282. ID: w.ID,
  283. Type: w.Type,
  284. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  285. Active: w.IsActive,
  286. Config: config,
  287. Events: w.EventsArray(),
  288. AuthorizationHeader: authorizationHeader,
  289. Updated: w.UpdatedUnix.AsTime(),
  290. Created: w.CreatedUnix.AsTime(),
  291. BranchFilter: w.BranchFilter,
  292. }, nil
  293. }