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.

681 lines
18 KiB

8 years ago
8 years ago
8 years ago
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "strings"
  11. "code.gitea.io/git"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/auth"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/setting"
  17. api "code.gitea.io/sdk/gitea"
  18. "github.com/Unknwon/com"
  19. )
  20. const (
  21. tplHooks base.TplName = "repo/settings/hooks"
  22. tplHookNew base.TplName = "repo/settings/hook_new"
  23. tplOrgHookNew base.TplName = "org/settings/hook_new"
  24. )
  25. // Webhooks render web hooks list page
  26. func Webhooks(ctx *context.Context) {
  27. ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
  28. ctx.Data["PageIsSettingsHooks"] = true
  29. ctx.Data["BaseLink"] = ctx.Repo.RepoLink
  30. ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
  31. ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  32. if err != nil {
  33. ctx.ServerError("GetWebhooksByRepoID", err)
  34. return
  35. }
  36. ctx.Data["Webhooks"] = ws
  37. ctx.HTML(200, tplHooks)
  38. }
  39. type orgRepoCtx struct {
  40. OrgID int64
  41. RepoID int64
  42. Link string
  43. NewTemplate base.TplName
  44. }
  45. // getOrgRepoCtx determines whether this is a repo context or organization context.
  46. func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
  47. if len(ctx.Repo.RepoLink) > 0 {
  48. return &orgRepoCtx{
  49. RepoID: ctx.Repo.Repository.ID,
  50. Link: ctx.Repo.RepoLink,
  51. NewTemplate: tplHookNew,
  52. }, nil
  53. }
  54. if len(ctx.Org.OrgLink) > 0 {
  55. return &orgRepoCtx{
  56. OrgID: ctx.Org.Organization.ID,
  57. Link: ctx.Org.OrgLink,
  58. NewTemplate: tplOrgHookNew,
  59. }, nil
  60. }
  61. return nil, errors.New("Unable to set OrgRepo context")
  62. }
  63. func checkHookType(ctx *context.Context) string {
  64. hookType := strings.ToLower(ctx.Params(":type"))
  65. if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
  66. ctx.NotFound("checkHookType", nil)
  67. return ""
  68. }
  69. return hookType
  70. }
  71. // WebhooksNew render creating webhook page
  72. func WebhooksNew(ctx *context.Context) {
  73. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  74. ctx.Data["PageIsSettingsHooks"] = true
  75. ctx.Data["PageIsSettingsHooksNew"] = true
  76. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  77. orCtx, err := getOrgRepoCtx(ctx)
  78. if err != nil {
  79. ctx.ServerError("getOrgRepoCtx", err)
  80. return
  81. }
  82. hookType := checkHookType(ctx)
  83. ctx.Data["HookType"] = hookType
  84. if ctx.Written() {
  85. return
  86. }
  87. if hookType == "discord" {
  88. ctx.Data["DiscordHook"] = map[string]interface{}{
  89. "Username": "Gitea",
  90. "IconURL": setting.AppURL + "img/favicon.png",
  91. }
  92. }
  93. ctx.Data["BaseLink"] = orCtx.Link
  94. ctx.HTML(200, orCtx.NewTemplate)
  95. }
  96. // ParseHookEvent convert web form content to models.HookEvent
  97. func ParseHookEvent(form auth.WebhookForm) *models.HookEvent {
  98. return &models.HookEvent{
  99. PushOnly: form.PushOnly(),
  100. SendEverything: form.SendEverything(),
  101. ChooseEvents: form.ChooseEvents(),
  102. HookEvents: models.HookEvents{
  103. Create: form.Create,
  104. Push: form.Push,
  105. PullRequest: form.PullRequest,
  106. Repository: form.Repository,
  107. },
  108. }
  109. }
  110. // WebHooksNewPost response for creating webhook
  111. func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
  112. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  113. ctx.Data["PageIsSettingsHooks"] = true
  114. ctx.Data["PageIsSettingsHooksNew"] = true
  115. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  116. ctx.Data["HookType"] = "gitea"
  117. orCtx, err := getOrgRepoCtx(ctx)
  118. if err != nil {
  119. ctx.ServerError("getOrgRepoCtx", err)
  120. return
  121. }
  122. ctx.Data["BaseLink"] = orCtx.Link
  123. if ctx.HasError() {
  124. ctx.HTML(200, orCtx.NewTemplate)
  125. return
  126. }
  127. contentType := models.ContentTypeJSON
  128. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  129. contentType = models.ContentTypeForm
  130. }
  131. w := &models.Webhook{
  132. RepoID: orCtx.RepoID,
  133. URL: form.PayloadURL,
  134. ContentType: contentType,
  135. Secret: form.Secret,
  136. HookEvent: ParseHookEvent(form.WebhookForm),
  137. IsActive: form.Active,
  138. HookTaskType: models.GITEA,
  139. OrgID: orCtx.OrgID,
  140. }
  141. if err := w.UpdateEvent(); err != nil {
  142. ctx.ServerError("UpdateEvent", err)
  143. return
  144. } else if err := models.CreateWebhook(w); err != nil {
  145. ctx.ServerError("CreateWebhook", err)
  146. return
  147. }
  148. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  149. ctx.Redirect(orCtx.Link + "/settings/hooks")
  150. }
  151. // GogsHooksNewPost response for creating webhook
  152. func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) {
  153. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  154. ctx.Data["PageIsSettingsHooks"] = true
  155. ctx.Data["PageIsSettingsHooksNew"] = true
  156. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  157. ctx.Data["HookType"] = "gogs"
  158. orCtx, err := getOrgRepoCtx(ctx)
  159. if err != nil {
  160. ctx.ServerError("getOrgRepoCtx", err)
  161. return
  162. }
  163. ctx.Data["BaseLink"] = orCtx.Link
  164. if ctx.HasError() {
  165. ctx.HTML(200, orCtx.NewTemplate)
  166. return
  167. }
  168. contentType := models.ContentTypeJSON
  169. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  170. contentType = models.ContentTypeForm
  171. }
  172. w := &models.Webhook{
  173. RepoID: orCtx.RepoID,
  174. URL: form.PayloadURL,
  175. ContentType: contentType,
  176. Secret: form.Secret,
  177. HookEvent: ParseHookEvent(form.WebhookForm),
  178. IsActive: form.Active,
  179. HookTaskType: models.GITEA,
  180. OrgID: orCtx.OrgID,
  181. }
  182. if err := w.UpdateEvent(); err != nil {
  183. ctx.ServerError("UpdateEvent", err)
  184. return
  185. } else if err := models.CreateWebhook(w); err != nil {
  186. ctx.ServerError("CreateWebhook", err)
  187. return
  188. }
  189. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  190. ctx.Redirect(orCtx.Link + "/settings/hooks")
  191. }
  192. // DiscordHooksNewPost response for creating discord hook
  193. func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  194. ctx.Data["Title"] = ctx.Tr("repo.settings")
  195. ctx.Data["PageIsSettingsHooks"] = true
  196. ctx.Data["PageIsSettingsHooksNew"] = true
  197. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  198. orCtx, err := getOrgRepoCtx(ctx)
  199. if err != nil {
  200. ctx.ServerError("getOrgRepoCtx", err)
  201. return
  202. }
  203. if ctx.HasError() {
  204. ctx.HTML(200, orCtx.NewTemplate)
  205. return
  206. }
  207. meta, err := json.Marshal(&models.DiscordMeta{
  208. Username: form.Username,
  209. IconURL: form.IconURL,
  210. })
  211. if err != nil {
  212. ctx.ServerError("Marshal", err)
  213. return
  214. }
  215. w := &models.Webhook{
  216. RepoID: orCtx.RepoID,
  217. URL: form.PayloadURL,
  218. ContentType: models.ContentTypeJSON,
  219. HookEvent: ParseHookEvent(form.WebhookForm),
  220. IsActive: form.Active,
  221. HookTaskType: models.DISCORD,
  222. Meta: string(meta),
  223. OrgID: orCtx.OrgID,
  224. }
  225. if err := w.UpdateEvent(); err != nil {
  226. ctx.ServerError("UpdateEvent", err)
  227. return
  228. } else if err := models.CreateWebhook(w); err != nil {
  229. ctx.ServerError("CreateWebhook", err)
  230. return
  231. }
  232. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  233. ctx.Redirect(orCtx.Link + "/settings/hooks")
  234. }
  235. // DingtalkHooksNewPost response for creating dingtalk hook
  236. func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  237. ctx.Data["Title"] = ctx.Tr("repo.settings")
  238. ctx.Data["PageIsSettingsHooks"] = true
  239. ctx.Data["PageIsSettingsHooksNew"] = true
  240. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  241. orCtx, err := getOrgRepoCtx(ctx)
  242. if err != nil {
  243. ctx.ServerError("getOrgRepoCtx", err)
  244. return
  245. }
  246. if ctx.HasError() {
  247. ctx.HTML(200, orCtx.NewTemplate)
  248. return
  249. }
  250. w := &models.Webhook{
  251. RepoID: orCtx.RepoID,
  252. URL: form.PayloadURL,
  253. ContentType: models.ContentTypeJSON,
  254. HookEvent: ParseHookEvent(form.WebhookForm),
  255. IsActive: form.Active,
  256. HookTaskType: models.DINGTALK,
  257. Meta: "",
  258. OrgID: orCtx.OrgID,
  259. }
  260. if err := w.UpdateEvent(); err != nil {
  261. ctx.ServerError("UpdateEvent", err)
  262. return
  263. } else if err := models.CreateWebhook(w); err != nil {
  264. ctx.ServerError("CreateWebhook", err)
  265. return
  266. }
  267. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  268. ctx.Redirect(orCtx.Link + "/settings/hooks")
  269. }
  270. // SlackHooksNewPost response for creating slack hook
  271. func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
  272. ctx.Data["Title"] = ctx.Tr("repo.settings")
  273. ctx.Data["PageIsSettingsHooks"] = true
  274. ctx.Data["PageIsSettingsHooksNew"] = true
  275. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  276. orCtx, err := getOrgRepoCtx(ctx)
  277. if err != nil {
  278. ctx.ServerError("getOrgRepoCtx", err)
  279. return
  280. }
  281. if ctx.HasError() {
  282. ctx.HTML(200, orCtx.NewTemplate)
  283. return
  284. }
  285. meta, err := json.Marshal(&models.SlackMeta{
  286. Channel: form.Channel,
  287. Username: form.Username,
  288. IconURL: form.IconURL,
  289. Color: form.Color,
  290. })
  291. if err != nil {
  292. ctx.ServerError("Marshal", err)
  293. return
  294. }
  295. w := &models.Webhook{
  296. RepoID: orCtx.RepoID,
  297. URL: form.PayloadURL,
  298. ContentType: models.ContentTypeJSON,
  299. HookEvent: ParseHookEvent(form.WebhookForm),
  300. IsActive: form.Active,
  301. HookTaskType: models.SLACK,
  302. Meta: string(meta),
  303. OrgID: orCtx.OrgID,
  304. }
  305. if err := w.UpdateEvent(); err != nil {
  306. ctx.ServerError("UpdateEvent", err)
  307. return
  308. } else if err := models.CreateWebhook(w); err != nil {
  309. ctx.ServerError("CreateWebhook", err)
  310. return
  311. }
  312. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  313. ctx.Redirect(orCtx.Link + "/settings/hooks")
  314. }
  315. func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
  316. ctx.Data["RequireHighlightJS"] = true
  317. orCtx, err := getOrgRepoCtx(ctx)
  318. if err != nil {
  319. ctx.ServerError("getOrgRepoCtx", err)
  320. return nil, nil
  321. }
  322. ctx.Data["BaseLink"] = orCtx.Link
  323. var w *models.Webhook
  324. if orCtx.RepoID > 0 {
  325. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  326. } else {
  327. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  328. }
  329. if err != nil {
  330. if models.IsErrWebhookNotExist(err) {
  331. ctx.NotFound("GetWebhookByID", nil)
  332. } else {
  333. ctx.ServerError("GetWebhookByID", err)
  334. }
  335. return nil, nil
  336. }
  337. ctx.Data["HookType"] = w.HookTaskType.Name()
  338. switch w.HookTaskType {
  339. case models.SLACK:
  340. ctx.Data["SlackHook"] = w.GetSlackHook()
  341. case models.DISCORD:
  342. ctx.Data["DiscordHook"] = w.GetDiscordHook()
  343. }
  344. ctx.Data["History"], err = w.History(1)
  345. if err != nil {
  346. ctx.ServerError("History", err)
  347. }
  348. return orCtx, w
  349. }
  350. // WebHooksEdit render editing web hook page
  351. func WebHooksEdit(ctx *context.Context) {
  352. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  353. ctx.Data["PageIsSettingsHooks"] = true
  354. ctx.Data["PageIsSettingsHooksEdit"] = true
  355. orCtx, w := checkWebhook(ctx)
  356. if ctx.Written() {
  357. return
  358. }
  359. ctx.Data["Webhook"] = w
  360. ctx.HTML(200, orCtx.NewTemplate)
  361. }
  362. // WebHooksEditPost response for editing web hook
  363. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  364. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  365. ctx.Data["PageIsSettingsHooks"] = true
  366. ctx.Data["PageIsSettingsHooksEdit"] = true
  367. orCtx, w := checkWebhook(ctx)
  368. if ctx.Written() {
  369. return
  370. }
  371. ctx.Data["Webhook"] = w
  372. if ctx.HasError() {
  373. ctx.HTML(200, orCtx.NewTemplate)
  374. return
  375. }
  376. contentType := models.ContentTypeJSON
  377. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  378. contentType = models.ContentTypeForm
  379. }
  380. w.URL = form.PayloadURL
  381. w.ContentType = contentType
  382. w.Secret = form.Secret
  383. w.HookEvent = ParseHookEvent(form.WebhookForm)
  384. w.IsActive = form.Active
  385. if err := w.UpdateEvent(); err != nil {
  386. ctx.ServerError("UpdateEvent", err)
  387. return
  388. } else if err := models.UpdateWebhook(w); err != nil {
  389. ctx.ServerError("WebHooksEditPost", err)
  390. return
  391. }
  392. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  393. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  394. }
  395. // GogsHooksEditPost response for editing gogs hook
  396. func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
  397. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  398. ctx.Data["PageIsSettingsHooks"] = true
  399. ctx.Data["PageIsSettingsHooksEdit"] = true
  400. orCtx, w := checkWebhook(ctx)
  401. if ctx.Written() {
  402. return
  403. }
  404. ctx.Data["Webhook"] = w
  405. if ctx.HasError() {
  406. ctx.HTML(200, orCtx.NewTemplate)
  407. return
  408. }
  409. contentType := models.ContentTypeJSON
  410. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  411. contentType = models.ContentTypeForm
  412. }
  413. w.URL = form.PayloadURL
  414. w.ContentType = contentType
  415. w.Secret = form.Secret
  416. w.HookEvent = ParseHookEvent(form.WebhookForm)
  417. w.IsActive = form.Active
  418. if err := w.UpdateEvent(); err != nil {
  419. ctx.ServerError("UpdateEvent", err)
  420. return
  421. } else if err := models.UpdateWebhook(w); err != nil {
  422. ctx.ServerError("GogsHooksEditPost", err)
  423. return
  424. }
  425. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  426. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  427. }
  428. // SlackHooksEditPost response for editing slack hook
  429. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  430. ctx.Data["Title"] = ctx.Tr("repo.settings")
  431. ctx.Data["PageIsSettingsHooks"] = true
  432. ctx.Data["PageIsSettingsHooksEdit"] = true
  433. orCtx, w := checkWebhook(ctx)
  434. if ctx.Written() {
  435. return
  436. }
  437. ctx.Data["Webhook"] = w
  438. if ctx.HasError() {
  439. ctx.HTML(200, orCtx.NewTemplate)
  440. return
  441. }
  442. meta, err := json.Marshal(&models.SlackMeta{
  443. Channel: form.Channel,
  444. Username: form.Username,
  445. IconURL: form.IconURL,
  446. Color: form.Color,
  447. })
  448. if err != nil {
  449. ctx.ServerError("Marshal", err)
  450. return
  451. }
  452. w.URL = form.PayloadURL
  453. w.Meta = string(meta)
  454. w.HookEvent = ParseHookEvent(form.WebhookForm)
  455. w.IsActive = form.Active
  456. if err := w.UpdateEvent(); err != nil {
  457. ctx.ServerError("UpdateEvent", err)
  458. return
  459. } else if err := models.UpdateWebhook(w); err != nil {
  460. ctx.ServerError("UpdateWebhook", err)
  461. return
  462. }
  463. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  464. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  465. }
  466. // DiscordHooksEditPost response for editing discord hook
  467. func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  468. ctx.Data["Title"] = ctx.Tr("repo.settings")
  469. ctx.Data["PageIsSettingsHooks"] = true
  470. ctx.Data["PageIsSettingsHooksEdit"] = true
  471. orCtx, w := checkWebhook(ctx)
  472. if ctx.Written() {
  473. return
  474. }
  475. ctx.Data["Webhook"] = w
  476. if ctx.HasError() {
  477. ctx.HTML(200, orCtx.NewTemplate)
  478. return
  479. }
  480. meta, err := json.Marshal(&models.DiscordMeta{
  481. Username: form.Username,
  482. IconURL: form.IconURL,
  483. })
  484. if err != nil {
  485. ctx.ServerError("Marshal", err)
  486. return
  487. }
  488. w.URL = form.PayloadURL
  489. w.Meta = string(meta)
  490. w.HookEvent = ParseHookEvent(form.WebhookForm)
  491. w.IsActive = form.Active
  492. if err := w.UpdateEvent(); err != nil {
  493. ctx.ServerError("UpdateEvent", err)
  494. return
  495. } else if err := models.UpdateWebhook(w); err != nil {
  496. ctx.ServerError("UpdateWebhook", err)
  497. return
  498. }
  499. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  500. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  501. }
  502. // DingtalkHooksEditPost response for editing discord hook
  503. func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  504. ctx.Data["Title"] = ctx.Tr("repo.settings")
  505. ctx.Data["PageIsSettingsHooks"] = true
  506. ctx.Data["PageIsSettingsHooksEdit"] = true
  507. orCtx, w := checkWebhook(ctx)
  508. if ctx.Written() {
  509. return
  510. }
  511. ctx.Data["Webhook"] = w
  512. if ctx.HasError() {
  513. ctx.HTML(200, orCtx.NewTemplate)
  514. return
  515. }
  516. w.URL = form.PayloadURL
  517. w.HookEvent = ParseHookEvent(form.WebhookForm)
  518. w.IsActive = form.Active
  519. if err := w.UpdateEvent(); err != nil {
  520. ctx.ServerError("UpdateEvent", err)
  521. return
  522. } else if err := models.UpdateWebhook(w); err != nil {
  523. ctx.ServerError("UpdateWebhook", err)
  524. return
  525. }
  526. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  527. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  528. }
  529. // TestWebhook test if web hook is work fine
  530. func TestWebhook(ctx *context.Context) {
  531. hookID := ctx.ParamsInt64(":id")
  532. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  533. if err != nil {
  534. ctx.Flash.Error("GetWebhookByID: " + err.Error())
  535. ctx.Status(500)
  536. return
  537. }
  538. // Grab latest commit or fake one if it's empty repository.
  539. commit := ctx.Repo.Commit
  540. if commit == nil {
  541. ghost := models.NewGhostUser()
  542. commit = &git.Commit{
  543. ID: git.MustIDFromString(git.EmptySHA),
  544. Author: ghost.NewGitSig(),
  545. Committer: ghost.NewGitSig(),
  546. CommitMessage: "This is a fake commit",
  547. }
  548. }
  549. apiUser := ctx.User.APIFormat()
  550. p := &api.PushPayload{
  551. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  552. Before: commit.ID.String(),
  553. After: commit.ID.String(),
  554. Commits: []*api.PayloadCommit{
  555. {
  556. ID: commit.ID.String(),
  557. Message: commit.Message(),
  558. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  559. Author: &api.PayloadUser{
  560. Name: commit.Author.Name,
  561. Email: commit.Author.Email,
  562. },
  563. Committer: &api.PayloadUser{
  564. Name: commit.Committer.Name,
  565. Email: commit.Committer.Email,
  566. },
  567. },
  568. },
  569. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  570. Pusher: apiUser,
  571. Sender: apiUser,
  572. }
  573. if err := models.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
  574. ctx.Flash.Error("PrepareWebhook: " + err.Error())
  575. ctx.Status(500)
  576. } else {
  577. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  578. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  579. ctx.Status(200)
  580. }
  581. }
  582. // DeleteWebhook delete a webhook
  583. func DeleteWebhook(ctx *context.Context) {
  584. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  585. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  586. } else {
  587. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  588. }
  589. ctx.JSON(200, map[string]interface{}{
  590. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  591. })
  592. }