webhook.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. var w *models.Webhook
  190. if orCtx.RepoID > 0 {
  191. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  192. } else {
  193. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  194. }
  195. if err != nil {
  196. if models.IsErrWebhookNotExist(err) {
  197. ctx.Handle(404, "GetWebhookByID", nil)
  198. } else {
  199. ctx.Handle(500, "GetWebhookByID", err)
  200. }
  201. return nil, nil
  202. }
  203. switch w.HookTaskType {
  204. case models.SLACK:
  205. ctx.Data["SlackHook"] = w.GetSlackHook()
  206. ctx.Data["HookType"] = "slack"
  207. default:
  208. ctx.Data["HookType"] = "gogs"
  209. }
  210. ctx.Data["History"], err = w.History(1)
  211. if err != nil {
  212. ctx.Handle(500, "History", err)
  213. }
  214. return orCtx, w
  215. }
  216. func WebHooksEdit(ctx *context.Context) {
  217. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  218. ctx.Data["PageIsSettingsHooks"] = true
  219. ctx.Data["PageIsSettingsHooksEdit"] = true
  220. orCtx, w := checkWebhook(ctx)
  221. if ctx.Written() {
  222. return
  223. }
  224. ctx.Data["Webhook"] = w
  225. ctx.HTML(200, orCtx.NewTemplate)
  226. }
  227. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  228. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  229. ctx.Data["PageIsSettingsHooks"] = true
  230. ctx.Data["PageIsSettingsHooksEdit"] = true
  231. orCtx, w := checkWebhook(ctx)
  232. if ctx.Written() {
  233. return
  234. }
  235. ctx.Data["Webhook"] = w
  236. if ctx.HasError() {
  237. ctx.HTML(200, orCtx.NewTemplate)
  238. return
  239. }
  240. contentType := models.JSON
  241. if models.HookContentType(form.ContentType) == models.FORM {
  242. contentType = models.FORM
  243. }
  244. w.URL = form.PayloadURL
  245. w.ContentType = contentType
  246. w.Secret = form.Secret
  247. w.HookEvent = ParseHookEvent(form.WebhookForm)
  248. w.IsActive = form.Active
  249. if err := w.UpdateEvent(); err != nil {
  250. ctx.Handle(500, "UpdateEvent", err)
  251. return
  252. } else if err := models.UpdateWebhook(w); err != nil {
  253. ctx.Handle(500, "WebHooksEditPost", err)
  254. return
  255. }
  256. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  257. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  258. }
  259. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  260. ctx.Data["Title"] = ctx.Tr("repo.settings")
  261. ctx.Data["PageIsSettingsHooks"] = true
  262. ctx.Data["PageIsSettingsHooksEdit"] = true
  263. orCtx, w := checkWebhook(ctx)
  264. if ctx.Written() {
  265. return
  266. }
  267. ctx.Data["Webhook"] = w
  268. if ctx.HasError() {
  269. ctx.HTML(200, orCtx.NewTemplate)
  270. return
  271. }
  272. meta, err := json.Marshal(&models.SlackMeta{
  273. Channel: form.Channel,
  274. Username: form.Username,
  275. IconURL: form.IconURL,
  276. Color: form.Color,
  277. })
  278. if err != nil {
  279. ctx.Handle(500, "Marshal", err)
  280. return
  281. }
  282. w.URL = form.PayloadURL
  283. w.Meta = string(meta)
  284. w.HookEvent = ParseHookEvent(form.WebhookForm)
  285. w.IsActive = form.Active
  286. if err := w.UpdateEvent(); err != nil {
  287. ctx.Handle(500, "UpdateEvent", err)
  288. return
  289. } else if err := models.UpdateWebhook(w); err != nil {
  290. ctx.Handle(500, "UpdateWebhook", err)
  291. return
  292. }
  293. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  294. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  295. }
  296. func TestWebhook(ctx *context.Context) {
  297. p := &api.PushPayload{
  298. Ref: git.BRANCH_PREFIX + ctx.Repo.Repository.DefaultBranch,
  299. Before: ctx.Repo.CommitID,
  300. After: ctx.Repo.CommitID,
  301. Commits: []*api.PayloadCommit{
  302. {
  303. ID: ctx.Repo.CommitID,
  304. Message: ctx.Repo.Commit.Message(),
  305. URL: ctx.Repo.Repository.FullLink() + "/commit/" + ctx.Repo.CommitID,
  306. Author: &api.PayloadAuthor{
  307. Name: ctx.Repo.Commit.Author.Name,
  308. Email: ctx.Repo.Commit.Author.Email,
  309. },
  310. Committer: &api.PayloadCommitter{
  311. Name: ctx.Repo.Commit.Committer.Name,
  312. Email: ctx.Repo.Commit.Committer.Email,
  313. },
  314. },
  315. },
  316. Repo: ctx.Repo.Repository.ComposePayload(),
  317. Pusher: &api.PayloadAuthor{
  318. Name: ctx.User.Name,
  319. Email: ctx.User.Email,
  320. UserName: ctx.User.Name,
  321. },
  322. Sender: &api.PayloadUser{
  323. UserName: ctx.User.Name,
  324. ID: ctx.User.ID,
  325. AvatarUrl: ctx.User.AvatarLink(),
  326. },
  327. }
  328. if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p); err != nil {
  329. ctx.Flash.Error("PrepareWebhooks: " + err.Error())
  330. ctx.Status(500)
  331. } else {
  332. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  333. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  334. ctx.Status(200)
  335. }
  336. }
  337. func DeleteWebhook(ctx *context.Context) {
  338. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  339. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  340. } else {
  341. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  342. }
  343. ctx.JSON(200, map[string]interface{}{
  344. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  345. })
  346. }