webhook.go 9.7 KB

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