webhook.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. // Copyright 2015 The Gogs 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 repo
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "strings"
  9. "github.com/Unknwon/com"
  10. git "github.com/gogits/git-module"
  11. api "github.com/gogits/go-gogs-client"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/models/errors"
  14. "github.com/gogits/gogs/pkg/context"
  15. "github.com/gogits/gogs/pkg/form"
  16. "github.com/gogits/gogs/pkg/setting"
  17. )
  18. const (
  19. WEBHOOKS = "repo/settings/webhook/base"
  20. WEBHOOK_NEW = "repo/settings/webhook/new"
  21. ORG_WEBHOOK_NEW = "org/settings/webhook_new"
  22. )
  23. func Webhooks(c *context.Context) {
  24. c.Data["Title"] = c.Tr("repo.settings.hooks")
  25. c.Data["PageIsSettingsHooks"] = true
  26. c.Data["BaseLink"] = c.Repo.RepoLink
  27. c.Data["Description"] = c.Tr("repo.settings.hooks_desc", "https://github.com/gogits/go-gogs-client/wiki/Repositories-Webhooks")
  28. c.Data["Types"] = setting.Webhook.Types
  29. ws, err := models.GetWebhooksByRepoID(c.Repo.Repository.ID)
  30. if err != nil {
  31. c.Handle(500, "GetWebhooksByRepoID", err)
  32. return
  33. }
  34. c.Data["Webhooks"] = ws
  35. c.HTML(200, WEBHOOKS)
  36. }
  37. type OrgRepoCtx struct {
  38. OrgID int64
  39. RepoID int64
  40. Link string
  41. NewTemplate string
  42. }
  43. // getOrgRepoCtx determines whether this is a repo context or organization context.
  44. func getOrgRepoCtx(c *context.Context) (*OrgRepoCtx, error) {
  45. if len(c.Repo.RepoLink) > 0 {
  46. c.Data["PageIsRepositoryContext"] = true
  47. return &OrgRepoCtx{
  48. RepoID: c.Repo.Repository.ID,
  49. Link: c.Repo.RepoLink,
  50. NewTemplate: WEBHOOK_NEW,
  51. }, nil
  52. }
  53. if len(c.Org.OrgLink) > 0 {
  54. c.Data["PageIsOrganizationContext"] = true
  55. return &OrgRepoCtx{
  56. OrgID: c.Org.Organization.ID,
  57. Link: c.Org.OrgLink,
  58. NewTemplate: ORG_WEBHOOK_NEW,
  59. }, nil
  60. }
  61. return nil, errors.New("Unable to set OrgRepo context")
  62. }
  63. func checkHookType(c *context.Context) string {
  64. hookType := strings.ToLower(c.Params(":type"))
  65. if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
  66. c.Handle(404, "checkHookType", nil)
  67. return ""
  68. }
  69. return hookType
  70. }
  71. func WebhooksNew(c *context.Context) {
  72. c.Data["Title"] = c.Tr("repo.settings.add_webhook")
  73. c.Data["PageIsSettingsHooks"] = true
  74. c.Data["PageIsSettingsHooksNew"] = true
  75. c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  76. orCtx, err := getOrgRepoCtx(c)
  77. if err != nil {
  78. c.Handle(500, "getOrgRepoCtx", err)
  79. return
  80. }
  81. c.Data["HookType"] = checkHookType(c)
  82. if c.Written() {
  83. return
  84. }
  85. c.Data["BaseLink"] = orCtx.Link
  86. c.HTML(200, orCtx.NewTemplate)
  87. }
  88. func ParseHookEvent(f form.Webhook) *models.HookEvent {
  89. return &models.HookEvent{
  90. PushOnly: f.PushOnly(),
  91. SendEverything: f.SendEverything(),
  92. ChooseEvents: f.ChooseEvents(),
  93. HookEvents: models.HookEvents{
  94. Create: f.Create,
  95. Delete: f.Delete,
  96. Fork: f.Fork,
  97. Push: f.Push,
  98. Issues: f.Issues,
  99. IssueComment: f.IssueComment,
  100. PullRequest: f.PullRequest,
  101. Release: f.Release,
  102. },
  103. }
  104. }
  105. func WebHooksNewPost(c *context.Context, f form.NewWebhook) {
  106. c.Data["Title"] = c.Tr("repo.settings.add_webhook")
  107. c.Data["PageIsSettingsHooks"] = true
  108. c.Data["PageIsSettingsHooksNew"] = true
  109. c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  110. c.Data["HookType"] = "gogs"
  111. orCtx, err := getOrgRepoCtx(c)
  112. if err != nil {
  113. c.Handle(500, "getOrgRepoCtx", err)
  114. return
  115. }
  116. c.Data["BaseLink"] = orCtx.Link
  117. if c.HasError() {
  118. c.HTML(200, orCtx.NewTemplate)
  119. return
  120. }
  121. contentType := models.JSON
  122. if models.HookContentType(f.ContentType) == models.FORM {
  123. contentType = models.FORM
  124. }
  125. w := &models.Webhook{
  126. RepoID: orCtx.RepoID,
  127. URL: f.PayloadURL,
  128. ContentType: contentType,
  129. Secret: f.Secret,
  130. HookEvent: ParseHookEvent(f.Webhook),
  131. IsActive: f.Active,
  132. HookTaskType: models.GOGS,
  133. OrgID: orCtx.OrgID,
  134. }
  135. if err := w.UpdateEvent(); err != nil {
  136. c.Handle(500, "UpdateEvent", err)
  137. return
  138. } else if err := models.CreateWebhook(w); err != nil {
  139. c.Handle(500, "CreateWebhook", err)
  140. return
  141. }
  142. c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
  143. c.Redirect(orCtx.Link + "/settings/hooks")
  144. }
  145. func SlackHooksNewPost(c *context.Context, f form.NewSlackHook) {
  146. c.Data["Title"] = c.Tr("repo.settings")
  147. c.Data["PageIsSettingsHooks"] = true
  148. c.Data["PageIsSettingsHooksNew"] = true
  149. c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  150. orCtx, err := getOrgRepoCtx(c)
  151. if err != nil {
  152. c.Handle(500, "getOrgRepoCtx", err)
  153. return
  154. }
  155. if c.HasError() {
  156. c.HTML(200, orCtx.NewTemplate)
  157. return
  158. }
  159. meta, err := json.Marshal(&models.SlackMeta{
  160. Channel: f.Channel,
  161. Username: f.Username,
  162. IconURL: f.IconURL,
  163. Color: f.Color,
  164. })
  165. if err != nil {
  166. c.Handle(500, "Marshal", err)
  167. return
  168. }
  169. w := &models.Webhook{
  170. RepoID: orCtx.RepoID,
  171. URL: f.PayloadURL,
  172. ContentType: models.JSON,
  173. HookEvent: ParseHookEvent(f.Webhook),
  174. IsActive: f.Active,
  175. HookTaskType: models.SLACK,
  176. Meta: string(meta),
  177. OrgID: orCtx.OrgID,
  178. }
  179. if err := w.UpdateEvent(); err != nil {
  180. c.Handle(500, "UpdateEvent", err)
  181. return
  182. } else if err := models.CreateWebhook(w); err != nil {
  183. c.Handle(500, "CreateWebhook", err)
  184. return
  185. }
  186. c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
  187. c.Redirect(orCtx.Link + "/settings/hooks")
  188. }
  189. // FIXME: merge logic to Slack
  190. func DiscordHooksNewPost(c *context.Context, f form.NewDiscordHook) {
  191. c.Data["Title"] = c.Tr("repo.settings")
  192. c.Data["PageIsSettingsHooks"] = true
  193. c.Data["PageIsSettingsHooksNew"] = true
  194. c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  195. orCtx, err := getOrgRepoCtx(c)
  196. if err != nil {
  197. c.Handle(500, "getOrgRepoCtx", err)
  198. return
  199. }
  200. if c.HasError() {
  201. c.HTML(200, orCtx.NewTemplate)
  202. return
  203. }
  204. meta, err := json.Marshal(&models.SlackMeta{
  205. Username: f.Username,
  206. IconURL: f.IconURL,
  207. Color: f.Color,
  208. })
  209. if err != nil {
  210. c.Handle(500, "Marshal", err)
  211. return
  212. }
  213. w := &models.Webhook{
  214. RepoID: orCtx.RepoID,
  215. URL: f.PayloadURL,
  216. ContentType: models.JSON,
  217. HookEvent: ParseHookEvent(f.Webhook),
  218. IsActive: f.Active,
  219. HookTaskType: models.DISCORD,
  220. Meta: string(meta),
  221. OrgID: orCtx.OrgID,
  222. }
  223. if err := w.UpdateEvent(); err != nil {
  224. c.Handle(500, "UpdateEvent", err)
  225. return
  226. } else if err := models.CreateWebhook(w); err != nil {
  227. c.Handle(500, "CreateWebhook", err)
  228. return
  229. }
  230. c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
  231. c.Redirect(orCtx.Link + "/settings/hooks")
  232. }
  233. func DingtalkHooksNewPost(c *context.Context, f form.NewDingtalkHook) {
  234. c.Data["Title"] = c.Tr("repo.settings")
  235. c.Data["PageIsSettingsHooks"] = true
  236. c.Data["PageIsSettingsHooksNew"] = true
  237. c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  238. orCtx, err := getOrgRepoCtx(c)
  239. if err != nil {
  240. c.Handle(500, "getOrgRepoCtx", err)
  241. return
  242. }
  243. if c.HasError() {
  244. c.HTML(200, orCtx.NewTemplate)
  245. return
  246. }
  247. w := &models.Webhook{
  248. RepoID: orCtx.RepoID,
  249. URL: f.PayloadURL,
  250. ContentType: models.JSON,
  251. HookEvent: ParseHookEvent(f.Webhook),
  252. IsActive: f.Active,
  253. HookTaskType: models.DINGTALK,
  254. OrgID: orCtx.OrgID,
  255. }
  256. if err := w.UpdateEvent(); err != nil {
  257. c.Handle(500, "UpdateEvent", err)
  258. return
  259. } else if err := models.CreateWebhook(w); err != nil {
  260. c.Handle(500, "CreateWebhook", err)
  261. return
  262. }
  263. c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
  264. c.Redirect(orCtx.Link + "/settings/hooks")
  265. }
  266. func checkWebhook(c *context.Context) (*OrgRepoCtx, *models.Webhook) {
  267. c.Data["RequireHighlightJS"] = true
  268. orCtx, err := getOrgRepoCtx(c)
  269. if err != nil {
  270. c.Handle(500, "getOrgRepoCtx", err)
  271. return nil, nil
  272. }
  273. c.Data["BaseLink"] = orCtx.Link
  274. var w *models.Webhook
  275. if orCtx.RepoID > 0 {
  276. w, err = models.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  277. } else {
  278. w, err = models.GetWebhookByOrgID(c.Org.Organization.ID, c.ParamsInt64(":id"))
  279. }
  280. if err != nil {
  281. c.NotFoundOrServerError("GetWebhookOfRepoByID/GetWebhookByOrgID", errors.IsWebhookNotExist, err)
  282. return nil, nil
  283. }
  284. switch w.HookTaskType {
  285. case models.SLACK:
  286. c.Data["SlackHook"] = w.GetSlackHook()
  287. c.Data["HookType"] = "slack"
  288. case models.DISCORD:
  289. c.Data["SlackHook"] = w.GetSlackHook()
  290. c.Data["HookType"] = "discord"
  291. case models.DINGTALK:
  292. c.Data["HookType"] = "dingtalk"
  293. default:
  294. c.Data["HookType"] = "gogs"
  295. }
  296. c.Data["History"], err = w.History(1)
  297. if err != nil {
  298. c.Handle(500, "History", err)
  299. }
  300. return orCtx, w
  301. }
  302. func WebHooksEdit(c *context.Context) {
  303. c.Data["Title"] = c.Tr("repo.settings.update_webhook")
  304. c.Data["PageIsSettingsHooks"] = true
  305. c.Data["PageIsSettingsHooksEdit"] = true
  306. orCtx, w := checkWebhook(c)
  307. if c.Written() {
  308. return
  309. }
  310. c.Data["Webhook"] = w
  311. c.HTML(200, orCtx.NewTemplate)
  312. }
  313. func WebHooksEditPost(c *context.Context, f form.NewWebhook) {
  314. c.Data["Title"] = c.Tr("repo.settings.update_webhook")
  315. c.Data["PageIsSettingsHooks"] = true
  316. c.Data["PageIsSettingsHooksEdit"] = true
  317. orCtx, w := checkWebhook(c)
  318. if c.Written() {
  319. return
  320. }
  321. c.Data["Webhook"] = w
  322. if c.HasError() {
  323. c.HTML(200, orCtx.NewTemplate)
  324. return
  325. }
  326. contentType := models.JSON
  327. if models.HookContentType(f.ContentType) == models.FORM {
  328. contentType = models.FORM
  329. }
  330. w.URL = f.PayloadURL
  331. w.ContentType = contentType
  332. w.Secret = f.Secret
  333. w.HookEvent = ParseHookEvent(f.Webhook)
  334. w.IsActive = f.Active
  335. if err := w.UpdateEvent(); err != nil {
  336. c.Handle(500, "UpdateEvent", err)
  337. return
  338. } else if err := models.UpdateWebhook(w); err != nil {
  339. c.Handle(500, "WebHooksEditPost", err)
  340. return
  341. }
  342. c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
  343. c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  344. }
  345. func SlackHooksEditPost(c *context.Context, f form.NewSlackHook) {
  346. c.Data["Title"] = c.Tr("repo.settings")
  347. c.Data["PageIsSettingsHooks"] = true
  348. c.Data["PageIsSettingsHooksEdit"] = true
  349. orCtx, w := checkWebhook(c)
  350. if c.Written() {
  351. return
  352. }
  353. c.Data["Webhook"] = w
  354. if c.HasError() {
  355. c.HTML(200, orCtx.NewTemplate)
  356. return
  357. }
  358. meta, err := json.Marshal(&models.SlackMeta{
  359. Channel: f.Channel,
  360. Username: f.Username,
  361. IconURL: f.IconURL,
  362. Color: f.Color,
  363. })
  364. if err != nil {
  365. c.Handle(500, "Marshal", err)
  366. return
  367. }
  368. w.URL = f.PayloadURL
  369. w.Meta = string(meta)
  370. w.HookEvent = ParseHookEvent(f.Webhook)
  371. w.IsActive = f.Active
  372. if err := w.UpdateEvent(); err != nil {
  373. c.Handle(500, "UpdateEvent", err)
  374. return
  375. } else if err := models.UpdateWebhook(w); err != nil {
  376. c.Handle(500, "UpdateWebhook", err)
  377. return
  378. }
  379. c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
  380. c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  381. }
  382. // FIXME: merge logic to Slack
  383. func DiscordHooksEditPost(c *context.Context, f form.NewDiscordHook) {
  384. c.Data["Title"] = c.Tr("repo.settings")
  385. c.Data["PageIsSettingsHooks"] = true
  386. c.Data["PageIsSettingsHooksEdit"] = true
  387. orCtx, w := checkWebhook(c)
  388. if c.Written() {
  389. return
  390. }
  391. c.Data["Webhook"] = w
  392. if c.HasError() {
  393. c.HTML(200, orCtx.NewTemplate)
  394. return
  395. }
  396. meta, err := json.Marshal(&models.SlackMeta{
  397. Username: f.Username,
  398. IconURL: f.IconURL,
  399. Color: f.Color,
  400. })
  401. if err != nil {
  402. c.Handle(500, "Marshal", err)
  403. return
  404. }
  405. w.URL = f.PayloadURL
  406. w.Meta = string(meta)
  407. w.HookEvent = ParseHookEvent(f.Webhook)
  408. w.IsActive = f.Active
  409. if err := w.UpdateEvent(); err != nil {
  410. c.Handle(500, "UpdateEvent", err)
  411. return
  412. } else if err := models.UpdateWebhook(w); err != nil {
  413. c.Handle(500, "UpdateWebhook", err)
  414. return
  415. }
  416. c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
  417. c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  418. }
  419. func DingtalkHooksEditPost(c *context.Context, f form.NewDingtalkHook) {
  420. c.Data["Title"] = c.Tr("repo.settings")
  421. c.Data["PageIsSettingsHooks"] = true
  422. c.Data["PageIsSettingsHooksEdit"] = true
  423. orCtx, w := checkWebhook(c)
  424. if c.Written() {
  425. return
  426. }
  427. c.Data["Webhook"] = w
  428. if c.HasError() {
  429. c.HTML(200, orCtx.NewTemplate)
  430. return
  431. }
  432. w.URL = f.PayloadURL
  433. w.HookEvent = ParseHookEvent(f.Webhook)
  434. w.IsActive = f.Active
  435. if err := w.UpdateEvent(); err != nil {
  436. c.Handle(500, "UpdateEvent", err)
  437. return
  438. } else if err := models.UpdateWebhook(w); err != nil {
  439. c.Handle(500, "UpdateWebhook", err)
  440. return
  441. }
  442. c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
  443. c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  444. }
  445. func TestWebhook(c *context.Context) {
  446. var authorUsername, committerUsername string
  447. // Grab latest commit or fake one if it's empty repository.
  448. commit := c.Repo.Commit
  449. if commit == nil {
  450. ghost := models.NewGhostUser()
  451. commit = &git.Commit{
  452. ID: git.MustIDFromString(git.EMPTY_SHA),
  453. Author: ghost.NewGitSig(),
  454. Committer: ghost.NewGitSig(),
  455. CommitMessage: "This is a fake commit",
  456. }
  457. authorUsername = ghost.Name
  458. committerUsername = ghost.Name
  459. } else {
  460. // Try to match email with a real user.
  461. author, err := models.GetUserByEmail(commit.Author.Email)
  462. if err == nil {
  463. authorUsername = author.Name
  464. } else if !errors.IsUserNotExist(err) {
  465. c.Handle(500, "GetUserByEmail.(author)", err)
  466. return
  467. }
  468. committer, err := models.GetUserByEmail(commit.Committer.Email)
  469. if err == nil {
  470. committerUsername = committer.Name
  471. } else if !errors.IsUserNotExist(err) {
  472. c.Handle(500, "GetUserByEmail.(committer)", err)
  473. return
  474. }
  475. }
  476. fileStatus, err := commit.FileStatus()
  477. if err != nil {
  478. c.Handle(500, "FileStatus", err)
  479. return
  480. }
  481. apiUser := c.User.APIFormat()
  482. p := &api.PushPayload{
  483. Ref: git.BRANCH_PREFIX + c.Repo.Repository.DefaultBranch,
  484. Before: commit.ID.String(),
  485. After: commit.ID.String(),
  486. Commits: []*api.PayloadCommit{
  487. {
  488. ID: commit.ID.String(),
  489. Message: commit.Message(),
  490. URL: c.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  491. Author: &api.PayloadUser{
  492. Name: commit.Author.Name,
  493. Email: commit.Author.Email,
  494. UserName: authorUsername,
  495. },
  496. Committer: &api.PayloadUser{
  497. Name: commit.Committer.Name,
  498. Email: commit.Committer.Email,
  499. UserName: committerUsername,
  500. },
  501. Added: fileStatus.Added,
  502. Removed: fileStatus.Removed,
  503. Modified: fileStatus.Modified,
  504. },
  505. },
  506. Repo: c.Repo.Repository.APIFormat(nil),
  507. Pusher: apiUser,
  508. Sender: apiUser,
  509. }
  510. if err := models.TestWebhook(c.Repo.Repository, models.HOOK_EVENT_PUSH, p, c.ParamsInt64("id")); err != nil {
  511. c.Handle(500, "TestWebhook", err)
  512. } else {
  513. c.Flash.Info(c.Tr("repo.settings.webhook.test_delivery_success"))
  514. c.Status(200)
  515. }
  516. }
  517. func RedeliveryWebhook(c *context.Context) {
  518. webhook, err := models.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
  519. if err != nil {
  520. c.NotFoundOrServerError("GetWebhookOfRepoByID/GetWebhookByOrgID", errors.IsWebhookNotExist, err)
  521. return
  522. }
  523. hookTask, err := models.GetHookTaskOfWebhookByUUID(webhook.ID, c.Query("uuid"))
  524. if err != nil {
  525. c.NotFoundOrServerError("GetHookTaskOfWebhookByUUID/GetWebhookByOrgID", errors.IsHookTaskNotExist, err)
  526. return
  527. }
  528. hookTask.IsDelivered = false
  529. if err = models.UpdateHookTask(hookTask); err != nil {
  530. c.Handle(500, "UpdateHookTask", err)
  531. } else {
  532. go models.HookQueue.Add(c.Repo.Repository.ID)
  533. c.Flash.Info(c.Tr("repo.settings.webhook.redelivery_success", hookTask.UUID))
  534. c.Status(200)
  535. }
  536. }
  537. func DeleteWebhook(c *context.Context) {
  538. if err := models.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
  539. c.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  540. } else {
  541. c.Flash.Success(c.Tr("repo.settings.webhook_deletion_success"))
  542. }
  543. c.JSON(200, map[string]interface{}{
  544. "redirect": c.Repo.RepoLink + "/settings/hooks",
  545. })
  546. }