gitea源码

general.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. user_model "code.gitea.io/gitea/models/user"
  10. webhook_model "code.gitea.io/gitea/models/webhook"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/util"
  15. webhook_module "code.gitea.io/gitea/modules/webhook"
  16. )
  17. type linkFormatter = func(string, string) string
  18. // noneLinkFormatter does not create a link but just returns the text
  19. func noneLinkFormatter(url, text string) string {
  20. return text
  21. }
  22. // htmlLinkFormatter creates a HTML link
  23. func htmlLinkFormatter(url, text string) string {
  24. return fmt.Sprintf(`<a href="%s">%s</a>`, html.EscapeString(url), html.EscapeString(text))
  25. }
  26. // getPullRequestInfo gets the information for a pull request
  27. func getPullRequestInfo(p *api.PullRequestPayload) (title, link, by, operator, operateResult, assignees string) {
  28. title = fmt.Sprintf("[PullRequest-%s #%d]: %s\n%s", p.Repository.FullName, p.PullRequest.Index, p.Action, p.PullRequest.Title)
  29. assignList := p.PullRequest.Assignees
  30. assignStringList := make([]string, len(assignList))
  31. for i, user := range assignList {
  32. assignStringList[i] = user.UserName
  33. }
  34. switch p.Action {
  35. case api.HookIssueAssigned:
  36. operateResult = fmt.Sprintf("%s assign this to %s", p.Sender.UserName, assignList[len(assignList)-1].UserName)
  37. case api.HookIssueUnassigned:
  38. operateResult = p.Sender.UserName + " unassigned this for someone"
  39. case api.HookIssueMilestoned:
  40. operateResult = fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID)
  41. }
  42. link = p.PullRequest.HTMLURL
  43. by = "PullRequest by " + p.PullRequest.Poster.UserName
  44. if len(assignStringList) > 0 {
  45. assignees = "Assignees: " + strings.Join(assignStringList, ", ")
  46. }
  47. operator = "Operator: " + p.Sender.UserName
  48. return title, link, by, operator, operateResult, assignees
  49. }
  50. // getIssuesInfo gets the information for an issue
  51. func getIssuesInfo(p *api.IssuePayload) (issueTitle, link, by, operator, operateResult, assignees string) {
  52. issueTitle = fmt.Sprintf("[Issue-%s #%d]: %s\n%s", p.Repository.FullName, p.Issue.Index, p.Action, p.Issue.Title)
  53. assignList := p.Issue.Assignees
  54. assignStringList := make([]string, len(assignList))
  55. for i, user := range assignList {
  56. assignStringList[i] = user.UserName
  57. }
  58. switch p.Action {
  59. case api.HookIssueAssigned:
  60. operateResult = fmt.Sprintf("%s assign this to %s", p.Sender.UserName, assignList[len(assignList)-1].UserName)
  61. case api.HookIssueUnassigned:
  62. operateResult = p.Sender.UserName + " unassigned this for someone"
  63. case api.HookIssueMilestoned:
  64. operateResult = fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
  65. }
  66. link = p.Issue.HTMLURL
  67. by = "Issue by " + p.Issue.Poster.UserName
  68. if len(assignStringList) > 0 {
  69. assignees = "Assignees: " + strings.Join(assignStringList, ", ")
  70. }
  71. operator = "Operator: " + p.Sender.UserName
  72. return issueTitle, link, by, operator, operateResult, assignees
  73. }
  74. // getIssuesCommentInfo gets the information for a comment
  75. func getIssuesCommentInfo(p *api.IssueCommentPayload) (title, link, by, operator string) {
  76. title = fmt.Sprintf("[Comment-%s #%d]: %s\n%s", p.Repository.FullName, p.Issue.Index, p.Action, p.Issue.Title)
  77. link = p.Issue.HTMLURL
  78. if p.IsPull {
  79. by = "PullRequest by " + p.Issue.Poster.UserName
  80. } else {
  81. by = "Issue by " + p.Issue.Poster.UserName
  82. }
  83. operator = "Operator: " + p.Sender.UserName
  84. return title, link, by, operator
  85. }
  86. func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (text, issueTitle, extraMarkdown string, color int) {
  87. color = yellowColor
  88. issueTitle = fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
  89. titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
  90. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  91. switch p.Action {
  92. case api.HookIssueOpened:
  93. text = fmt.Sprintf("[%s] Issue opened: %s", repoLink, titleLink)
  94. color = orangeColor
  95. case api.HookIssueClosed:
  96. text = fmt.Sprintf("[%s] Issue closed: %s", repoLink, titleLink)
  97. color = redColor
  98. case api.HookIssueReOpened:
  99. text = fmt.Sprintf("[%s] Issue re-opened: %s", repoLink, titleLink)
  100. case api.HookIssueEdited:
  101. text = fmt.Sprintf("[%s] Issue edited: %s", repoLink, titleLink)
  102. case api.HookIssueAssigned:
  103. list := make([]string, len(p.Issue.Assignees))
  104. for i, user := range p.Issue.Assignees {
  105. list[i] = linkFormatter(setting.AppURL+url.PathEscape(user.UserName), user.UserName)
  106. }
  107. text = fmt.Sprintf("[%s] Issue assigned to %s: %s", repoLink, strings.Join(list, ", "), titleLink)
  108. color = greenColor
  109. case api.HookIssueUnassigned:
  110. text = fmt.Sprintf("[%s] Issue unassigned: %s", repoLink, titleLink)
  111. case api.HookIssueLabelUpdated:
  112. text = fmt.Sprintf("[%s] Issue labels updated: %s", repoLink, titleLink)
  113. case api.HookIssueLabelCleared:
  114. text = fmt.Sprintf("[%s] Issue labels cleared: %s", repoLink, titleLink)
  115. case api.HookIssueSynchronized:
  116. text = fmt.Sprintf("[%s] Issue synchronized: %s", repoLink, titleLink)
  117. case api.HookIssueMilestoned:
  118. mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID)
  119. text = fmt.Sprintf("[%s] Issue milestoned to %s: %s", repoLink,
  120. linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink)
  121. case api.HookIssueDemilestoned:
  122. text = fmt.Sprintf("[%s] Issue milestone cleared: %s", repoLink, titleLink)
  123. }
  124. if withSender {
  125. text += " by " + linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
  126. }
  127. if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
  128. extraMarkdown = p.Issue.Body
  129. }
  130. return text, issueTitle, extraMarkdown, color
  131. }
  132. func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (text, issueTitle, extraMarkdown string, color int) {
  133. color = yellowColor
  134. issueTitle = fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
  135. titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
  136. repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
  137. switch p.Action {
  138. case api.HookIssueOpened:
  139. text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink)
  140. extraMarkdown = 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. extraMarkdown = 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. extraMarkdown = 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 += " by " + linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
  187. }
  188. return text, issueTitle, extraMarkdown, 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 += " by " + 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 += " by " + 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 += " by " + linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
  260. }
  261. return text, issueTitle, color
  262. }
  263. func getPackagePayloadInfo(p *api.PackagePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
  264. refLink := linkFormatter(p.Package.HTMLURL, p.Package.Name+":"+p.Package.Version)
  265. switch p.Action {
  266. case api.HookPackageCreated:
  267. text = "Package created: " + refLink
  268. color = greenColor
  269. case api.HookPackageDeleted:
  270. text = "Package deleted: " + refLink
  271. color = redColor
  272. }
  273. if withSender {
  274. text += " by " + linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
  275. }
  276. return text, color
  277. }
  278. func getStatusPayloadInfo(p *api.CommitStatusPayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
  279. refLink := linkFormatter(p.TargetURL, fmt.Sprintf("%s [%s]", p.Context, base.ShortSha(p.SHA)))
  280. text = fmt.Sprintf("Commit Status changed: %s - %s", refLink, p.Description)
  281. color = greenColor
  282. if withSender {
  283. if user_model.IsGiteaActionsUserName(p.Sender.UserName) {
  284. text += " by " + p.Sender.FullName
  285. } else {
  286. text += " by " + linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
  287. }
  288. }
  289. return text, color
  290. }
  291. func getWorkflowRunPayloadInfo(p *api.WorkflowRunPayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
  292. description := p.WorkflowRun.Conclusion
  293. if description == "" {
  294. description = p.WorkflowRun.Status
  295. }
  296. refLink := linkFormatter(p.WorkflowRun.HTMLURL, fmt.Sprintf("%s(#%d)", p.WorkflowRun.DisplayTitle, p.WorkflowRun.ID)+"["+base.ShortSha(p.WorkflowRun.HeadSha)+"]:"+description)
  297. text = fmt.Sprintf("Workflow Run %s: %s", p.Action, refLink)
  298. switch description {
  299. case "waiting":
  300. color = orangeColor
  301. case "queued":
  302. color = orangeColorLight
  303. case "success":
  304. color = greenColor
  305. case "failure":
  306. color = redColor
  307. case "cancelled":
  308. color = yellowColor
  309. case "skipped":
  310. color = purpleColor
  311. default:
  312. color = greyColor
  313. }
  314. if withSender {
  315. text += " by " + linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
  316. }
  317. return text, color
  318. }
  319. func getWorkflowJobPayloadInfo(p *api.WorkflowJobPayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
  320. description := p.WorkflowJob.Conclusion
  321. if description == "" {
  322. description = p.WorkflowJob.Status
  323. }
  324. refLink := linkFormatter(p.WorkflowJob.HTMLURL, fmt.Sprintf("%s(#%d)", p.WorkflowJob.Name, p.WorkflowJob.RunID)+"["+base.ShortSha(p.WorkflowJob.HeadSha)+"]:"+description)
  325. text = fmt.Sprintf("Workflow Job %s: %s", p.Action, refLink)
  326. switch description {
  327. case "waiting":
  328. color = orangeColor
  329. case "queued":
  330. color = orangeColorLight
  331. case "success":
  332. color = greenColor
  333. case "failure":
  334. color = redColor
  335. case "cancelled":
  336. color = yellowColor
  337. case "skipped":
  338. color = purpleColor
  339. default:
  340. color = greyColor
  341. }
  342. if withSender {
  343. text += " by " + linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)
  344. }
  345. return text, color
  346. }
  347. // ToHook convert models.Webhook to api.Hook
  348. // This function is not part of the convert package to prevent an import cycle
  349. func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
  350. config := map[string]string{
  351. "url": w.URL,
  352. "content_type": w.ContentType.Name(),
  353. }
  354. if w.Type == webhook_module.SLACK {
  355. s := GetSlackHook(w)
  356. config["channel"] = s.Channel
  357. config["username"] = s.Username
  358. config["icon_url"] = s.IconURL
  359. config["color"] = s.Color
  360. }
  361. authorizationHeader, err := w.HeaderAuthorization()
  362. if err != nil {
  363. return nil, err
  364. }
  365. return &api.Hook{
  366. ID: w.ID,
  367. Type: w.Type,
  368. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  369. Active: w.IsActive,
  370. Config: config,
  371. Events: w.EventsArray(),
  372. AuthorizationHeader: authorizationHeader,
  373. Updated: w.UpdatedUnix.AsTime(),
  374. Created: w.CreatedUnix.AsTime(),
  375. BranchFilter: w.BranchFilter,
  376. }, nil
  377. }