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.

686 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/webhook/base"
  22. tplHookNew base.TplName = "repo/settings/webhook/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. Delete: form.Delete,
  105. Fork: form.Fork,
  106. Issues: form.Issues,
  107. IssueComment: form.IssueComment,
  108. Release: form.Release,
  109. Push: form.Push,
  110. PullRequest: form.PullRequest,
  111. Repository: form.Repository,
  112. },
  113. }
  114. }
  115. // WebHooksNewPost response for creating webhook
  116. func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
  117. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  118. ctx.Data["PageIsSettingsHooks"] = true
  119. ctx.Data["PageIsSettingsHooksNew"] = true
  120. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  121. ctx.Data["HookType"] = "gitea"
  122. orCtx, err := getOrgRepoCtx(ctx)
  123. if err != nil {
  124. ctx.ServerError("getOrgRepoCtx", err)
  125. return
  126. }
  127. ctx.Data["BaseLink"] = orCtx.Link
  128. if ctx.HasError() {
  129. ctx.HTML(200, orCtx.NewTemplate)
  130. return
  131. }
  132. contentType := models.ContentTypeJSON
  133. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  134. contentType = models.ContentTypeForm
  135. }
  136. w := &models.Webhook{
  137. RepoID: orCtx.RepoID,
  138. URL: form.PayloadURL,
  139. ContentType: contentType,
  140. Secret: form.Secret,
  141. HookEvent: ParseHookEvent(form.WebhookForm),
  142. IsActive: form.Active,
  143. HookTaskType: models.GITEA,
  144. OrgID: orCtx.OrgID,
  145. }
  146. if err := w.UpdateEvent(); err != nil {
  147. ctx.ServerError("UpdateEvent", err)
  148. return
  149. } else if err := models.CreateWebhook(w); err != nil {
  150. ctx.ServerError("CreateWebhook", err)
  151. return
  152. }
  153. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  154. ctx.Redirect(orCtx.Link + "/settings/hooks")
  155. }
  156. // GogsHooksNewPost response for creating webhook
  157. func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) {
  158. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  159. ctx.Data["PageIsSettingsHooks"] = true
  160. ctx.Data["PageIsSettingsHooksNew"] = true
  161. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  162. ctx.Data["HookType"] = "gogs"
  163. orCtx, err := getOrgRepoCtx(ctx)
  164. if err != nil {
  165. ctx.ServerError("getOrgRepoCtx", err)
  166. return
  167. }
  168. ctx.Data["BaseLink"] = orCtx.Link
  169. if ctx.HasError() {
  170. ctx.HTML(200, orCtx.NewTemplate)
  171. return
  172. }
  173. contentType := models.ContentTypeJSON
  174. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  175. contentType = models.ContentTypeForm
  176. }
  177. w := &models.Webhook{
  178. RepoID: orCtx.RepoID,
  179. URL: form.PayloadURL,
  180. ContentType: contentType,
  181. Secret: form.Secret,
  182. HookEvent: ParseHookEvent(form.WebhookForm),
  183. IsActive: form.Active,
  184. HookTaskType: models.GOGS,
  185. OrgID: orCtx.OrgID,
  186. }
  187. if err := w.UpdateEvent(); err != nil {
  188. ctx.ServerError("UpdateEvent", err)
  189. return
  190. } else if err := models.CreateWebhook(w); err != nil {
  191. ctx.ServerError("CreateWebhook", err)
  192. return
  193. }
  194. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  195. ctx.Redirect(orCtx.Link + "/settings/hooks")
  196. }
  197. // DiscordHooksNewPost response for creating discord hook
  198. func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  199. ctx.Data["Title"] = ctx.Tr("repo.settings")
  200. ctx.Data["PageIsSettingsHooks"] = true
  201. ctx.Data["PageIsSettingsHooksNew"] = true
  202. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  203. orCtx, err := getOrgRepoCtx(ctx)
  204. if err != nil {
  205. ctx.ServerError("getOrgRepoCtx", err)
  206. return
  207. }
  208. if ctx.HasError() {
  209. ctx.HTML(200, orCtx.NewTemplate)
  210. return
  211. }
  212. meta, err := json.Marshal(&models.DiscordMeta{
  213. Username: form.Username,
  214. IconURL: form.IconURL,
  215. })
  216. if err != nil {
  217. ctx.ServerError("Marshal", err)
  218. return
  219. }
  220. w := &models.Webhook{
  221. RepoID: orCtx.RepoID,
  222. URL: form.PayloadURL,
  223. ContentType: models.ContentTypeJSON,
  224. HookEvent: ParseHookEvent(form.WebhookForm),
  225. IsActive: form.Active,
  226. HookTaskType: models.DISCORD,
  227. Meta: string(meta),
  228. OrgID: orCtx.OrgID,
  229. }
  230. if err := w.UpdateEvent(); err != nil {
  231. ctx.ServerError("UpdateEvent", err)
  232. return
  233. } else if err := models.CreateWebhook(w); err != nil {
  234. ctx.ServerError("CreateWebhook", err)
  235. return
  236. }
  237. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  238. ctx.Redirect(orCtx.Link + "/settings/hooks")
  239. }
  240. // DingtalkHooksNewPost response for creating dingtalk hook
  241. func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  242. ctx.Data["Title"] = ctx.Tr("repo.settings")
  243. ctx.Data["PageIsSettingsHooks"] = true
  244. ctx.Data["PageIsSettingsHooksNew"] = true
  245. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  246. orCtx, err := getOrgRepoCtx(ctx)
  247. if err != nil {
  248. ctx.ServerError("getOrgRepoCtx", err)
  249. return
  250. }
  251. if ctx.HasError() {
  252. ctx.HTML(200, orCtx.NewTemplate)
  253. return
  254. }
  255. w := &models.Webhook{
  256. RepoID: orCtx.RepoID,
  257. URL: form.PayloadURL,
  258. ContentType: models.ContentTypeJSON,
  259. HookEvent: ParseHookEvent(form.WebhookForm),
  260. IsActive: form.Active,
  261. HookTaskType: models.DINGTALK,
  262. Meta: "",
  263. OrgID: orCtx.OrgID,
  264. }
  265. if err := w.UpdateEvent(); err != nil {
  266. ctx.ServerError("UpdateEvent", err)
  267. return
  268. } else if err := models.CreateWebhook(w); err != nil {
  269. ctx.ServerError("CreateWebhook", err)
  270. return
  271. }
  272. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  273. ctx.Redirect(orCtx.Link + "/settings/hooks")
  274. }
  275. // SlackHooksNewPost response for creating slack hook
  276. func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
  277. ctx.Data["Title"] = ctx.Tr("repo.settings")
  278. ctx.Data["PageIsSettingsHooks"] = true
  279. ctx.Data["PageIsSettingsHooksNew"] = true
  280. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  281. orCtx, err := getOrgRepoCtx(ctx)
  282. if err != nil {
  283. ctx.ServerError("getOrgRepoCtx", err)
  284. return
  285. }
  286. if ctx.HasError() {
  287. ctx.HTML(200, orCtx.NewTemplate)
  288. return
  289. }
  290. meta, err := json.Marshal(&models.SlackMeta{
  291. Channel: form.Channel,
  292. Username: form.Username,
  293. IconURL: form.IconURL,
  294. Color: form.Color,
  295. })
  296. if err != nil {
  297. ctx.ServerError("Marshal", err)
  298. return
  299. }
  300. w := &models.Webhook{
  301. RepoID: orCtx.RepoID,
  302. URL: form.PayloadURL,
  303. ContentType: models.ContentTypeJSON,
  304. HookEvent: ParseHookEvent(form.WebhookForm),
  305. IsActive: form.Active,
  306. HookTaskType: models.SLACK,
  307. Meta: string(meta),
  308. OrgID: orCtx.OrgID,
  309. }
  310. if err := w.UpdateEvent(); err != nil {
  311. ctx.ServerError("UpdateEvent", err)
  312. return
  313. } else if err := models.CreateWebhook(w); err != nil {
  314. ctx.ServerError("CreateWebhook", err)
  315. return
  316. }
  317. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  318. ctx.Redirect(orCtx.Link + "/settings/hooks")
  319. }
  320. func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
  321. ctx.Data["RequireHighlightJS"] = true
  322. orCtx, err := getOrgRepoCtx(ctx)
  323. if err != nil {
  324. ctx.ServerError("getOrgRepoCtx", err)
  325. return nil, nil
  326. }
  327. ctx.Data["BaseLink"] = orCtx.Link
  328. var w *models.Webhook
  329. if orCtx.RepoID > 0 {
  330. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  331. } else {
  332. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  333. }
  334. if err != nil {
  335. if models.IsErrWebhookNotExist(err) {
  336. ctx.NotFound("GetWebhookByID", nil)
  337. } else {
  338. ctx.ServerError("GetWebhookByID", err)
  339. }
  340. return nil, nil
  341. }
  342. ctx.Data["HookType"] = w.HookTaskType.Name()
  343. switch w.HookTaskType {
  344. case models.SLACK:
  345. ctx.Data["SlackHook"] = w.GetSlackHook()
  346. case models.DISCORD:
  347. ctx.Data["DiscordHook"] = w.GetDiscordHook()
  348. }
  349. ctx.Data["History"], err = w.History(1)
  350. if err != nil {
  351. ctx.ServerError("History", err)
  352. }
  353. return orCtx, w
  354. }
  355. // WebHooksEdit render editing web hook page
  356. func WebHooksEdit(ctx *context.Context) {
  357. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  358. ctx.Data["PageIsSettingsHooks"] = true
  359. ctx.Data["PageIsSettingsHooksEdit"] = true
  360. orCtx, w := checkWebhook(ctx)
  361. if ctx.Written() {
  362. return
  363. }
  364. ctx.Data["Webhook"] = w
  365. ctx.HTML(200, orCtx.NewTemplate)
  366. }
  367. // WebHooksEditPost response for editing web hook
  368. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  369. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  370. ctx.Data["PageIsSettingsHooks"] = true
  371. ctx.Data["PageIsSettingsHooksEdit"] = true
  372. orCtx, w := checkWebhook(ctx)
  373. if ctx.Written() {
  374. return
  375. }
  376. ctx.Data["Webhook"] = w
  377. if ctx.HasError() {
  378. ctx.HTML(200, orCtx.NewTemplate)
  379. return
  380. }
  381. contentType := models.ContentTypeJSON
  382. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  383. contentType = models.ContentTypeForm
  384. }
  385. w.URL = form.PayloadURL
  386. w.ContentType = contentType
  387. w.Secret = form.Secret
  388. w.HookEvent = ParseHookEvent(form.WebhookForm)
  389. w.IsActive = form.Active
  390. if err := w.UpdateEvent(); err != nil {
  391. ctx.ServerError("UpdateEvent", err)
  392. return
  393. } else if err := models.UpdateWebhook(w); err != nil {
  394. ctx.ServerError("WebHooksEditPost", err)
  395. return
  396. }
  397. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  398. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  399. }
  400. // GogsHooksEditPost response for editing gogs hook
  401. func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
  402. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  403. ctx.Data["PageIsSettingsHooks"] = true
  404. ctx.Data["PageIsSettingsHooksEdit"] = true
  405. orCtx, w := checkWebhook(ctx)
  406. if ctx.Written() {
  407. return
  408. }
  409. ctx.Data["Webhook"] = w
  410. if ctx.HasError() {
  411. ctx.HTML(200, orCtx.NewTemplate)
  412. return
  413. }
  414. contentType := models.ContentTypeJSON
  415. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  416. contentType = models.ContentTypeForm
  417. }
  418. w.URL = form.PayloadURL
  419. w.ContentType = contentType
  420. w.Secret = form.Secret
  421. w.HookEvent = ParseHookEvent(form.WebhookForm)
  422. w.IsActive = form.Active
  423. if err := w.UpdateEvent(); err != nil {
  424. ctx.ServerError("UpdateEvent", err)
  425. return
  426. } else if err := models.UpdateWebhook(w); err != nil {
  427. ctx.ServerError("GogsHooksEditPost", err)
  428. return
  429. }
  430. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  431. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  432. }
  433. // SlackHooksEditPost response for editing slack hook
  434. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  435. ctx.Data["Title"] = ctx.Tr("repo.settings")
  436. ctx.Data["PageIsSettingsHooks"] = true
  437. ctx.Data["PageIsSettingsHooksEdit"] = true
  438. orCtx, w := checkWebhook(ctx)
  439. if ctx.Written() {
  440. return
  441. }
  442. ctx.Data["Webhook"] = w
  443. if ctx.HasError() {
  444. ctx.HTML(200, orCtx.NewTemplate)
  445. return
  446. }
  447. meta, err := json.Marshal(&models.SlackMeta{
  448. Channel: form.Channel,
  449. Username: form.Username,
  450. IconURL: form.IconURL,
  451. Color: form.Color,
  452. })
  453. if err != nil {
  454. ctx.ServerError("Marshal", err)
  455. return
  456. }
  457. w.URL = form.PayloadURL
  458. w.Meta = string(meta)
  459. w.HookEvent = ParseHookEvent(form.WebhookForm)
  460. w.IsActive = form.Active
  461. if err := w.UpdateEvent(); err != nil {
  462. ctx.ServerError("UpdateEvent", err)
  463. return
  464. } else if err := models.UpdateWebhook(w); err != nil {
  465. ctx.ServerError("UpdateWebhook", err)
  466. return
  467. }
  468. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  469. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  470. }
  471. // DiscordHooksEditPost response for editing discord hook
  472. func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  473. ctx.Data["Title"] = ctx.Tr("repo.settings")
  474. ctx.Data["PageIsSettingsHooks"] = true
  475. ctx.Data["PageIsSettingsHooksEdit"] = true
  476. orCtx, w := checkWebhook(ctx)
  477. if ctx.Written() {
  478. return
  479. }
  480. ctx.Data["Webhook"] = w
  481. if ctx.HasError() {
  482. ctx.HTML(200, orCtx.NewTemplate)
  483. return
  484. }
  485. meta, err := json.Marshal(&models.DiscordMeta{
  486. Username: form.Username,
  487. IconURL: form.IconURL,
  488. })
  489. if err != nil {
  490. ctx.ServerError("Marshal", err)
  491. return
  492. }
  493. w.URL = form.PayloadURL
  494. w.Meta = string(meta)
  495. w.HookEvent = ParseHookEvent(form.WebhookForm)
  496. w.IsActive = form.Active
  497. if err := w.UpdateEvent(); err != nil {
  498. ctx.ServerError("UpdateEvent", err)
  499. return
  500. } else if err := models.UpdateWebhook(w); err != nil {
  501. ctx.ServerError("UpdateWebhook", err)
  502. return
  503. }
  504. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  505. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  506. }
  507. // DingtalkHooksEditPost response for editing discord hook
  508. func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  509. ctx.Data["Title"] = ctx.Tr("repo.settings")
  510. ctx.Data["PageIsSettingsHooks"] = true
  511. ctx.Data["PageIsSettingsHooksEdit"] = true
  512. orCtx, w := checkWebhook(ctx)
  513. if ctx.Written() {
  514. return
  515. }
  516. ctx.Data["Webhook"] = w
  517. if ctx.HasError() {
  518. ctx.HTML(200, orCtx.NewTemplate)
  519. return
  520. }
  521. w.URL = form.PayloadURL
  522. w.HookEvent = ParseHookEvent(form.WebhookForm)
  523. w.IsActive = form.Active
  524. if err := w.UpdateEvent(); err != nil {
  525. ctx.ServerError("UpdateEvent", err)
  526. return
  527. } else if err := models.UpdateWebhook(w); err != nil {
  528. ctx.ServerError("UpdateWebhook", err)
  529. return
  530. }
  531. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  532. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  533. }
  534. // TestWebhook test if web hook is work fine
  535. func TestWebhook(ctx *context.Context) {
  536. hookID := ctx.ParamsInt64(":id")
  537. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  538. if err != nil {
  539. ctx.Flash.Error("GetWebhookByID: " + err.Error())
  540. ctx.Status(500)
  541. return
  542. }
  543. // Grab latest commit or fake one if it's empty repository.
  544. commit := ctx.Repo.Commit
  545. if commit == nil {
  546. ghost := models.NewGhostUser()
  547. commit = &git.Commit{
  548. ID: git.MustIDFromString(git.EmptySHA),
  549. Author: ghost.NewGitSig(),
  550. Committer: ghost.NewGitSig(),
  551. CommitMessage: "This is a fake commit",
  552. }
  553. }
  554. apiUser := ctx.User.APIFormat()
  555. p := &api.PushPayload{
  556. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  557. Before: commit.ID.String(),
  558. After: commit.ID.String(),
  559. Commits: []*api.PayloadCommit{
  560. {
  561. ID: commit.ID.String(),
  562. Message: commit.Message(),
  563. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  564. Author: &api.PayloadUser{
  565. Name: commit.Author.Name,
  566. Email: commit.Author.Email,
  567. },
  568. Committer: &api.PayloadUser{
  569. Name: commit.Committer.Name,
  570. Email: commit.Committer.Email,
  571. },
  572. },
  573. },
  574. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  575. Pusher: apiUser,
  576. Sender: apiUser,
  577. }
  578. if err := models.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
  579. ctx.Flash.Error("PrepareWebhook: " + err.Error())
  580. ctx.Status(500)
  581. } else {
  582. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  583. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  584. ctx.Status(200)
  585. }
  586. }
  587. // DeleteWebhook delete a webhook
  588. func DeleteWebhook(ctx *context.Context) {
  589. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  590. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  591. } else {
  592. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  593. }
  594. ctx.JSON(200, map[string]interface{}{
  595. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  596. })
  597. }