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.

698 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. if form.HasInvalidChannel() {
  291. ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
  292. ctx.Redirect(orCtx.Link + "/settings/hooks/slack/new")
  293. return
  294. }
  295. meta, err := json.Marshal(&models.SlackMeta{
  296. Channel: strings.TrimSpace(form.Channel),
  297. Username: form.Username,
  298. IconURL: form.IconURL,
  299. Color: form.Color,
  300. })
  301. if err != nil {
  302. ctx.ServerError("Marshal", err)
  303. return
  304. }
  305. w := &models.Webhook{
  306. RepoID: orCtx.RepoID,
  307. URL: form.PayloadURL,
  308. ContentType: models.ContentTypeJSON,
  309. HookEvent: ParseHookEvent(form.WebhookForm),
  310. IsActive: form.Active,
  311. HookTaskType: models.SLACK,
  312. Meta: string(meta),
  313. OrgID: orCtx.OrgID,
  314. }
  315. if err := w.UpdateEvent(); err != nil {
  316. ctx.ServerError("UpdateEvent", err)
  317. return
  318. } else if err := models.CreateWebhook(w); err != nil {
  319. ctx.ServerError("CreateWebhook", err)
  320. return
  321. }
  322. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  323. ctx.Redirect(orCtx.Link + "/settings/hooks")
  324. }
  325. func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
  326. ctx.Data["RequireHighlightJS"] = true
  327. orCtx, err := getOrgRepoCtx(ctx)
  328. if err != nil {
  329. ctx.ServerError("getOrgRepoCtx", err)
  330. return nil, nil
  331. }
  332. ctx.Data["BaseLink"] = orCtx.Link
  333. var w *models.Webhook
  334. if orCtx.RepoID > 0 {
  335. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  336. } else {
  337. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  338. }
  339. if err != nil {
  340. if models.IsErrWebhookNotExist(err) {
  341. ctx.NotFound("GetWebhookByID", nil)
  342. } else {
  343. ctx.ServerError("GetWebhookByID", err)
  344. }
  345. return nil, nil
  346. }
  347. ctx.Data["HookType"] = w.HookTaskType.Name()
  348. switch w.HookTaskType {
  349. case models.SLACK:
  350. ctx.Data["SlackHook"] = w.GetSlackHook()
  351. case models.DISCORD:
  352. ctx.Data["DiscordHook"] = w.GetDiscordHook()
  353. }
  354. ctx.Data["History"], err = w.History(1)
  355. if err != nil {
  356. ctx.ServerError("History", err)
  357. }
  358. return orCtx, w
  359. }
  360. // WebHooksEdit render editing web hook page
  361. func WebHooksEdit(ctx *context.Context) {
  362. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  363. ctx.Data["PageIsSettingsHooks"] = true
  364. ctx.Data["PageIsSettingsHooksEdit"] = true
  365. orCtx, w := checkWebhook(ctx)
  366. if ctx.Written() {
  367. return
  368. }
  369. ctx.Data["Webhook"] = w
  370. ctx.HTML(200, orCtx.NewTemplate)
  371. }
  372. // WebHooksEditPost response for editing web hook
  373. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  374. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  375. ctx.Data["PageIsSettingsHooks"] = true
  376. ctx.Data["PageIsSettingsHooksEdit"] = true
  377. orCtx, w := checkWebhook(ctx)
  378. if ctx.Written() {
  379. return
  380. }
  381. ctx.Data["Webhook"] = w
  382. if ctx.HasError() {
  383. ctx.HTML(200, orCtx.NewTemplate)
  384. return
  385. }
  386. contentType := models.ContentTypeJSON
  387. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  388. contentType = models.ContentTypeForm
  389. }
  390. w.URL = form.PayloadURL
  391. w.ContentType = contentType
  392. w.Secret = form.Secret
  393. w.HookEvent = ParseHookEvent(form.WebhookForm)
  394. w.IsActive = form.Active
  395. if err := w.UpdateEvent(); err != nil {
  396. ctx.ServerError("UpdateEvent", err)
  397. return
  398. } else if err := models.UpdateWebhook(w); err != nil {
  399. ctx.ServerError("WebHooksEditPost", err)
  400. return
  401. }
  402. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  403. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  404. }
  405. // GogsHooksEditPost response for editing gogs hook
  406. func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
  407. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  408. ctx.Data["PageIsSettingsHooks"] = true
  409. ctx.Data["PageIsSettingsHooksEdit"] = true
  410. orCtx, w := checkWebhook(ctx)
  411. if ctx.Written() {
  412. return
  413. }
  414. ctx.Data["Webhook"] = w
  415. if ctx.HasError() {
  416. ctx.HTML(200, orCtx.NewTemplate)
  417. return
  418. }
  419. contentType := models.ContentTypeJSON
  420. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  421. contentType = models.ContentTypeForm
  422. }
  423. w.URL = form.PayloadURL
  424. w.ContentType = contentType
  425. w.Secret = form.Secret
  426. w.HookEvent = ParseHookEvent(form.WebhookForm)
  427. w.IsActive = form.Active
  428. if err := w.UpdateEvent(); err != nil {
  429. ctx.ServerError("UpdateEvent", err)
  430. return
  431. } else if err := models.UpdateWebhook(w); err != nil {
  432. ctx.ServerError("GogsHooksEditPost", err)
  433. return
  434. }
  435. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  436. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  437. }
  438. // SlackHooksEditPost response for editing slack hook
  439. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  440. ctx.Data["Title"] = ctx.Tr("repo.settings")
  441. ctx.Data["PageIsSettingsHooks"] = true
  442. ctx.Data["PageIsSettingsHooksEdit"] = true
  443. orCtx, w := checkWebhook(ctx)
  444. if ctx.Written() {
  445. return
  446. }
  447. ctx.Data["Webhook"] = w
  448. if ctx.HasError() {
  449. ctx.HTML(200, orCtx.NewTemplate)
  450. return
  451. }
  452. if form.HasInvalidChannel() {
  453. ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
  454. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  455. return
  456. }
  457. meta, err := json.Marshal(&models.SlackMeta{
  458. Channel: strings.TrimSpace(form.Channel),
  459. Username: form.Username,
  460. IconURL: form.IconURL,
  461. Color: form.Color,
  462. })
  463. if err != nil {
  464. ctx.ServerError("Marshal", err)
  465. return
  466. }
  467. w.URL = form.PayloadURL
  468. w.Meta = string(meta)
  469. w.HookEvent = ParseHookEvent(form.WebhookForm)
  470. w.IsActive = form.Active
  471. if err := w.UpdateEvent(); err != nil {
  472. ctx.ServerError("UpdateEvent", err)
  473. return
  474. } else if err := models.UpdateWebhook(w); err != nil {
  475. ctx.ServerError("UpdateWebhook", err)
  476. return
  477. }
  478. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  479. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  480. }
  481. // DiscordHooksEditPost response for editing discord hook
  482. func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  483. ctx.Data["Title"] = ctx.Tr("repo.settings")
  484. ctx.Data["PageIsSettingsHooks"] = true
  485. ctx.Data["PageIsSettingsHooksEdit"] = true
  486. orCtx, w := checkWebhook(ctx)
  487. if ctx.Written() {
  488. return
  489. }
  490. ctx.Data["Webhook"] = w
  491. if ctx.HasError() {
  492. ctx.HTML(200, orCtx.NewTemplate)
  493. return
  494. }
  495. meta, err := json.Marshal(&models.DiscordMeta{
  496. Username: form.Username,
  497. IconURL: form.IconURL,
  498. })
  499. if err != nil {
  500. ctx.ServerError("Marshal", err)
  501. return
  502. }
  503. w.URL = form.PayloadURL
  504. w.Meta = string(meta)
  505. w.HookEvent = ParseHookEvent(form.WebhookForm)
  506. w.IsActive = form.Active
  507. if err := w.UpdateEvent(); err != nil {
  508. ctx.ServerError("UpdateEvent", err)
  509. return
  510. } else if err := models.UpdateWebhook(w); err != nil {
  511. ctx.ServerError("UpdateWebhook", err)
  512. return
  513. }
  514. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  515. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  516. }
  517. // DingtalkHooksEditPost response for editing discord hook
  518. func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  519. ctx.Data["Title"] = ctx.Tr("repo.settings")
  520. ctx.Data["PageIsSettingsHooks"] = true
  521. ctx.Data["PageIsSettingsHooksEdit"] = true
  522. orCtx, w := checkWebhook(ctx)
  523. if ctx.Written() {
  524. return
  525. }
  526. ctx.Data["Webhook"] = w
  527. if ctx.HasError() {
  528. ctx.HTML(200, orCtx.NewTemplate)
  529. return
  530. }
  531. w.URL = form.PayloadURL
  532. w.HookEvent = ParseHookEvent(form.WebhookForm)
  533. w.IsActive = form.Active
  534. if err := w.UpdateEvent(); err != nil {
  535. ctx.ServerError("UpdateEvent", err)
  536. return
  537. } else if err := models.UpdateWebhook(w); err != nil {
  538. ctx.ServerError("UpdateWebhook", err)
  539. return
  540. }
  541. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  542. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  543. }
  544. // TestWebhook test if web hook is work fine
  545. func TestWebhook(ctx *context.Context) {
  546. hookID := ctx.ParamsInt64(":id")
  547. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  548. if err != nil {
  549. ctx.Flash.Error("GetWebhookByID: " + err.Error())
  550. ctx.Status(500)
  551. return
  552. }
  553. // Grab latest commit or fake one if it's empty repository.
  554. commit := ctx.Repo.Commit
  555. if commit == nil {
  556. ghost := models.NewGhostUser()
  557. commit = &git.Commit{
  558. ID: git.MustIDFromString(git.EmptySHA),
  559. Author: ghost.NewGitSig(),
  560. Committer: ghost.NewGitSig(),
  561. CommitMessage: "This is a fake commit",
  562. }
  563. }
  564. apiUser := ctx.User.APIFormat()
  565. p := &api.PushPayload{
  566. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  567. Before: commit.ID.String(),
  568. After: commit.ID.String(),
  569. Commits: []*api.PayloadCommit{
  570. {
  571. ID: commit.ID.String(),
  572. Message: commit.Message(),
  573. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  574. Author: &api.PayloadUser{
  575. Name: commit.Author.Name,
  576. Email: commit.Author.Email,
  577. },
  578. Committer: &api.PayloadUser{
  579. Name: commit.Committer.Name,
  580. Email: commit.Committer.Email,
  581. },
  582. },
  583. },
  584. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  585. Pusher: apiUser,
  586. Sender: apiUser,
  587. }
  588. if err := models.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
  589. ctx.Flash.Error("PrepareWebhook: " + err.Error())
  590. ctx.Status(500)
  591. } else {
  592. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  593. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  594. ctx.Status(200)
  595. }
  596. }
  597. // DeleteWebhook delete a webhook
  598. func DeleteWebhook(ctx *context.Context) {
  599. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  600. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  601. } else {
  602. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  603. }
  604. ctx.JSON(200, map[string]interface{}{
  605. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  606. })
  607. }