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.

614 lines
16 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://godoc.org/code.gitea.io/sdk/gitea")
  31. ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  32. if err != nil {
  33. ctx.Handle(500, "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.Handle(404, "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.Handle(500, "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.Handle(500, "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.Handle(500, "UpdateEvent", err)
  143. return
  144. } else if err := models.CreateWebhook(w); err != nil {
  145. ctx.Handle(500, "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.NewWebhookForm) {
  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.Handle(500, "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.Handle(500, "UpdateEvent", err)
  184. return
  185. } else if err := models.CreateWebhook(w); err != nil {
  186. ctx.Handle(500, "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.Handle(500, "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.Handle(500, "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.Handle(500, "UpdateEvent", err)
  227. return
  228. } else if err := models.CreateWebhook(w); err != nil {
  229. ctx.Handle(500, "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. // SlackHooksNewPost response for creating slack hook
  236. func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
  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.Handle(500, "getOrgRepoCtx", err)
  244. return
  245. }
  246. if ctx.HasError() {
  247. ctx.HTML(200, orCtx.NewTemplate)
  248. return
  249. }
  250. meta, err := json.Marshal(&models.SlackMeta{
  251. Channel: form.Channel,
  252. Username: form.Username,
  253. IconURL: form.IconURL,
  254. Color: form.Color,
  255. })
  256. if err != nil {
  257. ctx.Handle(500, "Marshal", err)
  258. return
  259. }
  260. w := &models.Webhook{
  261. RepoID: orCtx.RepoID,
  262. URL: form.PayloadURL,
  263. ContentType: models.ContentTypeJSON,
  264. HookEvent: ParseHookEvent(form.WebhookForm),
  265. IsActive: form.Active,
  266. HookTaskType: models.SLACK,
  267. Meta: string(meta),
  268. OrgID: orCtx.OrgID,
  269. }
  270. if err := w.UpdateEvent(); err != nil {
  271. ctx.Handle(500, "UpdateEvent", err)
  272. return
  273. } else if err := models.CreateWebhook(w); err != nil {
  274. ctx.Handle(500, "CreateWebhook", err)
  275. return
  276. }
  277. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  278. ctx.Redirect(orCtx.Link + "/settings/hooks")
  279. }
  280. func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
  281. ctx.Data["RequireHighlightJS"] = true
  282. orCtx, err := getOrgRepoCtx(ctx)
  283. if err != nil {
  284. ctx.Handle(500, "getOrgRepoCtx", err)
  285. return nil, nil
  286. }
  287. ctx.Data["BaseLink"] = orCtx.Link
  288. var w *models.Webhook
  289. if orCtx.RepoID > 0 {
  290. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  291. } else {
  292. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  293. }
  294. if err != nil {
  295. if models.IsErrWebhookNotExist(err) {
  296. ctx.Handle(404, "GetWebhookByID", nil)
  297. } else {
  298. ctx.Handle(500, "GetWebhookByID", err)
  299. }
  300. return nil, nil
  301. }
  302. switch w.HookTaskType {
  303. case models.SLACK:
  304. ctx.Data["SlackHook"] = w.GetSlackHook()
  305. ctx.Data["HookType"] = "slack"
  306. case models.GOGS:
  307. ctx.Data["HookType"] = "gogs"
  308. case models.DISCORD:
  309. ctx.Data["DiscordHook"] = w.GetDiscordHook()
  310. ctx.Data["HookType"] = "discord"
  311. default:
  312. ctx.Data["HookType"] = "gitea"
  313. }
  314. ctx.Data["History"], err = w.History(1)
  315. if err != nil {
  316. ctx.Handle(500, "History", err)
  317. }
  318. return orCtx, w
  319. }
  320. // WebHooksEdit render editing web hook page
  321. func WebHooksEdit(ctx *context.Context) {
  322. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  323. ctx.Data["PageIsSettingsHooks"] = true
  324. ctx.Data["PageIsSettingsHooksEdit"] = true
  325. orCtx, w := checkWebhook(ctx)
  326. if ctx.Written() {
  327. return
  328. }
  329. ctx.Data["Webhook"] = w
  330. ctx.HTML(200, orCtx.NewTemplate)
  331. }
  332. // WebHooksEditPost response for editing web hook
  333. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  334. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  335. ctx.Data["PageIsSettingsHooks"] = true
  336. ctx.Data["PageIsSettingsHooksEdit"] = true
  337. orCtx, w := checkWebhook(ctx)
  338. if ctx.Written() {
  339. return
  340. }
  341. ctx.Data["Webhook"] = w
  342. if ctx.HasError() {
  343. ctx.HTML(200, orCtx.NewTemplate)
  344. return
  345. }
  346. contentType := models.ContentTypeJSON
  347. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  348. contentType = models.ContentTypeForm
  349. }
  350. w.URL = form.PayloadURL
  351. w.ContentType = contentType
  352. w.Secret = form.Secret
  353. w.HookEvent = ParseHookEvent(form.WebhookForm)
  354. w.IsActive = form.Active
  355. if err := w.UpdateEvent(); err != nil {
  356. ctx.Handle(500, "UpdateEvent", err)
  357. return
  358. } else if err := models.UpdateWebhook(w); err != nil {
  359. ctx.Handle(500, "WebHooksEditPost", err)
  360. return
  361. }
  362. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  363. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  364. }
  365. // GogsHooksEditPost response for editing gogs hook
  366. func GogsHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  367. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  368. ctx.Data["PageIsSettingsHooks"] = true
  369. ctx.Data["PageIsSettingsHooksEdit"] = true
  370. orCtx, w := checkWebhook(ctx)
  371. if ctx.Written() {
  372. return
  373. }
  374. ctx.Data["Webhook"] = w
  375. if ctx.HasError() {
  376. ctx.HTML(200, orCtx.NewTemplate)
  377. return
  378. }
  379. contentType := models.ContentTypeJSON
  380. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  381. contentType = models.ContentTypeForm
  382. }
  383. w.URL = form.PayloadURL
  384. w.ContentType = contentType
  385. w.Secret = form.Secret
  386. w.HookEvent = ParseHookEvent(form.WebhookForm)
  387. w.IsActive = form.Active
  388. if err := w.UpdateEvent(); err != nil {
  389. ctx.Handle(500, "UpdateEvent", err)
  390. return
  391. } else if err := models.UpdateWebhook(w); err != nil {
  392. ctx.Handle(500, "GogsHooksEditPost", err)
  393. return
  394. }
  395. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  396. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  397. }
  398. // SlackHooksEditPost response for editing slack hook
  399. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  400. ctx.Data["Title"] = ctx.Tr("repo.settings")
  401. ctx.Data["PageIsSettingsHooks"] = true
  402. ctx.Data["PageIsSettingsHooksEdit"] = true
  403. orCtx, w := checkWebhook(ctx)
  404. if ctx.Written() {
  405. return
  406. }
  407. ctx.Data["Webhook"] = w
  408. if ctx.HasError() {
  409. ctx.HTML(200, orCtx.NewTemplate)
  410. return
  411. }
  412. meta, err := json.Marshal(&models.SlackMeta{
  413. Channel: form.Channel,
  414. Username: form.Username,
  415. IconURL: form.IconURL,
  416. Color: form.Color,
  417. })
  418. if err != nil {
  419. ctx.Handle(500, "Marshal", err)
  420. return
  421. }
  422. w.URL = form.PayloadURL
  423. w.Meta = string(meta)
  424. w.HookEvent = ParseHookEvent(form.WebhookForm)
  425. w.IsActive = form.Active
  426. if err := w.UpdateEvent(); err != nil {
  427. ctx.Handle(500, "UpdateEvent", err)
  428. return
  429. } else if err := models.UpdateWebhook(w); err != nil {
  430. ctx.Handle(500, "UpdateWebhook", err)
  431. return
  432. }
  433. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  434. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  435. }
  436. // DiscordHooksEditPost response for editing discord hook
  437. func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  438. ctx.Data["Title"] = ctx.Tr("repo.settings")
  439. ctx.Data["PageIsSettingsHooks"] = true
  440. ctx.Data["PageIsSettingsHooksEdit"] = true
  441. orCtx, w := checkWebhook(ctx)
  442. if ctx.Written() {
  443. return
  444. }
  445. ctx.Data["Webhook"] = w
  446. if ctx.HasError() {
  447. ctx.HTML(200, orCtx.NewTemplate)
  448. return
  449. }
  450. meta, err := json.Marshal(&models.DiscordMeta{
  451. Username: form.Username,
  452. IconURL: form.IconURL,
  453. })
  454. if err != nil {
  455. ctx.Handle(500, "Marshal", err)
  456. return
  457. }
  458. w.URL = form.PayloadURL
  459. w.Meta = string(meta)
  460. w.HookEvent = ParseHookEvent(form.WebhookForm)
  461. w.IsActive = form.Active
  462. if err := w.UpdateEvent(); err != nil {
  463. ctx.Handle(500, "UpdateEvent", err)
  464. return
  465. } else if err := models.UpdateWebhook(w); err != nil {
  466. ctx.Handle(500, "UpdateWebhook", err)
  467. return
  468. }
  469. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  470. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  471. }
  472. // TestWebhook test if web hook is work fine
  473. func TestWebhook(ctx *context.Context) {
  474. hookID := ctx.ParamsInt64(":id")
  475. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  476. if err != nil {
  477. ctx.Flash.Error("GetWebhookByID: " + err.Error())
  478. ctx.Status(500)
  479. return
  480. }
  481. // Grab latest commit or fake one if it's empty repository.
  482. commit := ctx.Repo.Commit
  483. if commit == nil {
  484. ghost := models.NewGhostUser()
  485. commit = &git.Commit{
  486. ID: git.MustIDFromString(git.EmptySHA),
  487. Author: ghost.NewGitSig(),
  488. Committer: ghost.NewGitSig(),
  489. CommitMessage: "This is a fake commit",
  490. }
  491. }
  492. apiUser := ctx.User.APIFormat()
  493. p := &api.PushPayload{
  494. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  495. Before: commit.ID.String(),
  496. After: commit.ID.String(),
  497. Commits: []*api.PayloadCommit{
  498. {
  499. ID: commit.ID.String(),
  500. Message: commit.Message(),
  501. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  502. Author: &api.PayloadUser{
  503. Name: commit.Author.Name,
  504. Email: commit.Author.Email,
  505. },
  506. Committer: &api.PayloadUser{
  507. Name: commit.Committer.Name,
  508. Email: commit.Committer.Email,
  509. },
  510. },
  511. },
  512. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  513. Pusher: apiUser,
  514. Sender: apiUser,
  515. }
  516. if err := models.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
  517. ctx.Flash.Error("PrepareWebhook: " + err.Error())
  518. ctx.Status(500)
  519. } else {
  520. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  521. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  522. ctx.Status(200)
  523. }
  524. }
  525. // DeleteWebhook delete a webhook
  526. func DeleteWebhook(ctx *context.Context) {
  527. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  528. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  529. } else {
  530. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  531. }
  532. ctx.JSON(200, map[string]interface{}{
  533. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  534. })
  535. }