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.

416 lines
11 KiB

8 years ago
8 years ago
  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. "errors"
  8. "fmt"
  9. "strings"
  10. "github.com/Unknwon/com"
  11. "code.gitea.io/git"
  12. api "code.gitea.io/sdk/gitea"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/auth"
  15. "code.gitea.io/gitea/modules/base"
  16. "code.gitea.io/gitea/modules/context"
  17. "code.gitea.io/gitea/modules/setting"
  18. )
  19. const (
  20. tplHooks base.TplName = "repo/settings/hooks"
  21. tplHookNew base.TplName = "repo/settings/hook_new"
  22. tplOrgHookNew base.TplName = "org/settings/hook_new"
  23. )
  24. // Webhooks render web hooks list page
  25. func Webhooks(ctx *context.Context) {
  26. ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
  27. ctx.Data["PageIsSettingsHooks"] = true
  28. ctx.Data["BaseLink"] = ctx.Repo.RepoLink
  29. ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://godoc.org/code.gitea.io/sdk/gitea")
  30. ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  31. if err != nil {
  32. ctx.Handle(500, "GetWebhooksByRepoID", err)
  33. return
  34. }
  35. ctx.Data["Webhooks"] = ws
  36. ctx.HTML(200, tplHooks)
  37. }
  38. type orgRepoCtx struct {
  39. OrgID int64
  40. RepoID int64
  41. Link string
  42. NewTemplate base.TplName
  43. }
  44. // getOrgRepoCtx determines whether this is a repo context or organization context.
  45. func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
  46. if len(ctx.Repo.RepoLink) > 0 {
  47. return &orgRepoCtx{
  48. RepoID: ctx.Repo.Repository.ID,
  49. Link: ctx.Repo.RepoLink,
  50. NewTemplate: tplHookNew,
  51. }, nil
  52. }
  53. if len(ctx.Org.OrgLink) > 0 {
  54. return &orgRepoCtx{
  55. OrgID: ctx.Org.Organization.ID,
  56. Link: ctx.Org.OrgLink,
  57. NewTemplate: tplOrgHookNew,
  58. }, nil
  59. }
  60. return nil, errors.New("Unable to set OrgRepo context")
  61. }
  62. func checkHookType(ctx *context.Context) string {
  63. hookType := strings.ToLower(ctx.Params(":type"))
  64. if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
  65. ctx.Handle(404, "checkHookType", nil)
  66. return ""
  67. }
  68. return hookType
  69. }
  70. // WebhooksNew render creating webhook page
  71. func WebhooksNew(ctx *context.Context) {
  72. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  73. ctx.Data["PageIsSettingsHooks"] = true
  74. ctx.Data["PageIsSettingsHooksNew"] = true
  75. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  76. orCtx, err := getOrgRepoCtx(ctx)
  77. if err != nil {
  78. ctx.Handle(500, "getOrgRepoCtx", err)
  79. return
  80. }
  81. ctx.Data["HookType"] = checkHookType(ctx)
  82. if ctx.Written() {
  83. return
  84. }
  85. ctx.Data["BaseLink"] = orCtx.Link
  86. ctx.HTML(200, orCtx.NewTemplate)
  87. }
  88. // ParseHookEvent convert web form content to models.HookEvent
  89. func ParseHookEvent(form auth.WebhookForm) *models.HookEvent {
  90. return &models.HookEvent{
  91. PushOnly: form.PushOnly(),
  92. SendEverything: form.SendEverything(),
  93. ChooseEvents: form.ChooseEvents(),
  94. HookEvents: models.HookEvents{
  95. Create: form.Create,
  96. Push: form.Push,
  97. PullRequest: form.PullRequest,
  98. },
  99. }
  100. }
  101. // WebHooksNewPost response for creating webhook
  102. func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
  103. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  104. ctx.Data["PageIsSettingsHooks"] = true
  105. ctx.Data["PageIsSettingsHooksNew"] = true
  106. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  107. ctx.Data["HookType"] = "gogs"
  108. orCtx, err := getOrgRepoCtx(ctx)
  109. if err != nil {
  110. ctx.Handle(500, "getOrgRepoCtx", err)
  111. return
  112. }
  113. ctx.Data["BaseLink"] = orCtx.Link
  114. if ctx.HasError() {
  115. ctx.HTML(200, orCtx.NewTemplate)
  116. return
  117. }
  118. contentType := models.ContentTypeJSON
  119. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  120. contentType = models.ContentTypeForm
  121. }
  122. w := &models.Webhook{
  123. RepoID: orCtx.RepoID,
  124. URL: form.PayloadURL,
  125. ContentType: contentType,
  126. Secret: form.Secret,
  127. HookEvent: ParseHookEvent(form.WebhookForm),
  128. IsActive: form.Active,
  129. HookTaskType: models.GOGS,
  130. OrgID: orCtx.OrgID,
  131. }
  132. if err := w.UpdateEvent(); err != nil {
  133. ctx.Handle(500, "UpdateEvent", err)
  134. return
  135. } else if err := models.CreateWebhook(w); err != nil {
  136. ctx.Handle(500, "CreateWebhook", err)
  137. return
  138. }
  139. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  140. ctx.Redirect(orCtx.Link + "/settings/hooks")
  141. }
  142. // SlackHooksNewPost response for creating slack hook
  143. func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
  144. ctx.Data["Title"] = ctx.Tr("repo.settings")
  145. ctx.Data["PageIsSettingsHooks"] = true
  146. ctx.Data["PageIsSettingsHooksNew"] = true
  147. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  148. orCtx, err := getOrgRepoCtx(ctx)
  149. if err != nil {
  150. ctx.Handle(500, "getOrgRepoCtx", err)
  151. return
  152. }
  153. if ctx.HasError() {
  154. ctx.HTML(200, orCtx.NewTemplate)
  155. return
  156. }
  157. meta, err := json.Marshal(&models.SlackMeta{
  158. Channel: form.Channel,
  159. Username: form.Username,
  160. IconURL: form.IconURL,
  161. Color: form.Color,
  162. })
  163. if err != nil {
  164. ctx.Handle(500, "Marshal", err)
  165. return
  166. }
  167. w := &models.Webhook{
  168. RepoID: orCtx.RepoID,
  169. URL: form.PayloadURL,
  170. ContentType: models.ContentTypeJSON,
  171. HookEvent: ParseHookEvent(form.WebhookForm),
  172. IsActive: form.Active,
  173. HookTaskType: models.SLACK,
  174. Meta: string(meta),
  175. OrgID: orCtx.OrgID,
  176. }
  177. if err := w.UpdateEvent(); err != nil {
  178. ctx.Handle(500, "UpdateEvent", err)
  179. return
  180. } else if err := models.CreateWebhook(w); err != nil {
  181. ctx.Handle(500, "CreateWebhook", err)
  182. return
  183. }
  184. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  185. ctx.Redirect(orCtx.Link + "/settings/hooks")
  186. }
  187. func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
  188. ctx.Data["RequireHighlightJS"] = true
  189. orCtx, err := getOrgRepoCtx(ctx)
  190. if err != nil {
  191. ctx.Handle(500, "getOrgRepoCtx", err)
  192. return nil, nil
  193. }
  194. ctx.Data["BaseLink"] = orCtx.Link
  195. var w *models.Webhook
  196. if orCtx.RepoID > 0 {
  197. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  198. } else {
  199. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  200. }
  201. if err != nil {
  202. if models.IsErrWebhookNotExist(err) {
  203. ctx.Handle(404, "GetWebhookByID", nil)
  204. } else {
  205. ctx.Handle(500, "GetWebhookByID", err)
  206. }
  207. return nil, nil
  208. }
  209. switch w.HookTaskType {
  210. case models.SLACK:
  211. ctx.Data["SlackHook"] = w.GetSlackHook()
  212. ctx.Data["HookType"] = "slack"
  213. default:
  214. ctx.Data["HookType"] = "gogs"
  215. }
  216. ctx.Data["History"], err = w.History(1)
  217. if err != nil {
  218. ctx.Handle(500, "History", err)
  219. }
  220. return orCtx, w
  221. }
  222. // WebHooksEdit render editing web hook page
  223. func WebHooksEdit(ctx *context.Context) {
  224. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  225. ctx.Data["PageIsSettingsHooks"] = true
  226. ctx.Data["PageIsSettingsHooksEdit"] = true
  227. orCtx, w := checkWebhook(ctx)
  228. if ctx.Written() {
  229. return
  230. }
  231. ctx.Data["Webhook"] = w
  232. ctx.HTML(200, orCtx.NewTemplate)
  233. }
  234. // WebHooksEditPost response for editing web hook
  235. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  236. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  237. ctx.Data["PageIsSettingsHooks"] = true
  238. ctx.Data["PageIsSettingsHooksEdit"] = true
  239. orCtx, w := checkWebhook(ctx)
  240. if ctx.Written() {
  241. return
  242. }
  243. ctx.Data["Webhook"] = w
  244. if ctx.HasError() {
  245. ctx.HTML(200, orCtx.NewTemplate)
  246. return
  247. }
  248. contentType := models.ContentTypeJSON
  249. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  250. contentType = models.ContentTypeForm
  251. }
  252. w.URL = form.PayloadURL
  253. w.ContentType = contentType
  254. w.Secret = form.Secret
  255. w.HookEvent = ParseHookEvent(form.WebhookForm)
  256. w.IsActive = form.Active
  257. if err := w.UpdateEvent(); err != nil {
  258. ctx.Handle(500, "UpdateEvent", err)
  259. return
  260. } else if err := models.UpdateWebhook(w); err != nil {
  261. ctx.Handle(500, "WebHooksEditPost", err)
  262. return
  263. }
  264. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  265. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  266. }
  267. // SlackHooksEditPost reponse for editing slack hook
  268. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  269. ctx.Data["Title"] = ctx.Tr("repo.settings")
  270. ctx.Data["PageIsSettingsHooks"] = true
  271. ctx.Data["PageIsSettingsHooksEdit"] = true
  272. orCtx, w := checkWebhook(ctx)
  273. if ctx.Written() {
  274. return
  275. }
  276. ctx.Data["Webhook"] = w
  277. if ctx.HasError() {
  278. ctx.HTML(200, orCtx.NewTemplate)
  279. return
  280. }
  281. meta, err := json.Marshal(&models.SlackMeta{
  282. Channel: form.Channel,
  283. Username: form.Username,
  284. IconURL: form.IconURL,
  285. Color: form.Color,
  286. })
  287. if err != nil {
  288. ctx.Handle(500, "Marshal", err)
  289. return
  290. }
  291. w.URL = form.PayloadURL
  292. w.Meta = string(meta)
  293. w.HookEvent = ParseHookEvent(form.WebhookForm)
  294. w.IsActive = form.Active
  295. if err := w.UpdateEvent(); err != nil {
  296. ctx.Handle(500, "UpdateEvent", err)
  297. return
  298. } else if err := models.UpdateWebhook(w); err != nil {
  299. ctx.Handle(500, "UpdateWebhook", err)
  300. return
  301. }
  302. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  303. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  304. }
  305. // TestWebhook test if web hook is work fine
  306. func TestWebhook(ctx *context.Context) {
  307. // Grab latest commit or fake one if it's empty repository.
  308. commit := ctx.Repo.Commit
  309. if commit == nil {
  310. ghost := models.NewGhostUser()
  311. commit = &git.Commit{
  312. ID: git.MustIDFromString(git.EMPTY_SHA),
  313. Author: ghost.NewGitSig(),
  314. Committer: ghost.NewGitSig(),
  315. CommitMessage: "This is a fake commit",
  316. }
  317. }
  318. apiUser := ctx.User.APIFormat()
  319. p := &api.PushPayload{
  320. Ref: git.BRANCH_PREFIX + ctx.Repo.Repository.DefaultBranch,
  321. Before: commit.ID.String(),
  322. After: commit.ID.String(),
  323. Commits: []*api.PayloadCommit{
  324. {
  325. ID: commit.ID.String(),
  326. Message: commit.Message(),
  327. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  328. Author: &api.PayloadUser{
  329. Name: commit.Author.Name,
  330. Email: commit.Author.Email,
  331. },
  332. Committer: &api.PayloadUser{
  333. Name: commit.Committer.Name,
  334. Email: commit.Committer.Email,
  335. },
  336. },
  337. },
  338. Repo: ctx.Repo.Repository.APIFormat(nil),
  339. Pusher: apiUser,
  340. Sender: apiUser,
  341. }
  342. if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HookEventPush, p); err != nil {
  343. ctx.Flash.Error("PrepareWebhooks: " + err.Error())
  344. ctx.Status(500)
  345. } else {
  346. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  347. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  348. ctx.Status(200)
  349. }
  350. }
  351. // DeleteWebhook delete a webhook
  352. func DeleteWebhook(ctx *context.Context) {
  353. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  354. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  355. } else {
  356. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  357. }
  358. ctx.JSON(200, map[string]interface{}{
  359. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  360. })
  361. }