webhook.go 9.7 KB

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