webhook.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. PullRequest: form.PullRequest,
  95. },
  96. }
  97. }
  98. func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
  99. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  100. ctx.Data["PageIsSettingsHooks"] = true
  101. ctx.Data["PageIsSettingsHooksNew"] = true
  102. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  103. ctx.Data["HookType"] = "gogs"
  104. orCtx, err := getOrgRepoCtx(ctx)
  105. if err != nil {
  106. ctx.Handle(500, "getOrgRepoCtx", err)
  107. return
  108. }
  109. ctx.Data["BaseLink"] = orCtx.Link
  110. if ctx.HasError() {
  111. ctx.HTML(200, orCtx.NewTemplate)
  112. return
  113. }
  114. contentType := models.JSON
  115. if models.HookContentType(form.ContentType) == models.FORM {
  116. contentType = models.FORM
  117. }
  118. w := &models.Webhook{
  119. RepoID: orCtx.RepoID,
  120. URL: form.PayloadURL,
  121. ContentType: contentType,
  122. Secret: form.Secret,
  123. HookEvent: ParseHookEvent(form.WebhookForm),
  124. IsActive: form.Active,
  125. HookTaskType: models.GOGS,
  126. OrgID: orCtx.OrgID,
  127. }
  128. if err := w.UpdateEvent(); err != nil {
  129. ctx.Handle(500, "UpdateEvent", err)
  130. return
  131. } else if err := models.CreateWebhook(w); err != nil {
  132. ctx.Handle(500, "CreateWebhook", err)
  133. return
  134. }
  135. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  136. ctx.Redirect(orCtx.Link + "/settings/hooks")
  137. }
  138. func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
  139. ctx.Data["Title"] = ctx.Tr("repo.settings")
  140. ctx.Data["PageIsSettingsHooks"] = true
  141. ctx.Data["PageIsSettingsHooksNew"] = true
  142. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  143. orCtx, err := getOrgRepoCtx(ctx)
  144. if err != nil {
  145. ctx.Handle(500, "getOrgRepoCtx", err)
  146. return
  147. }
  148. if ctx.HasError() {
  149. ctx.HTML(200, orCtx.NewTemplate)
  150. return
  151. }
  152. meta, err := json.Marshal(&models.SlackMeta{
  153. Channel: form.Channel,
  154. Username: form.Username,
  155. IconURL: form.IconURL,
  156. Color: form.Color,
  157. })
  158. if err != nil {
  159. ctx.Handle(500, "Marshal", err)
  160. return
  161. }
  162. w := &models.Webhook{
  163. RepoID: orCtx.RepoID,
  164. URL: form.PayloadURL,
  165. ContentType: models.JSON,
  166. HookEvent: ParseHookEvent(form.WebhookForm),
  167. IsActive: form.Active,
  168. HookTaskType: models.SLACK,
  169. Meta: string(meta),
  170. OrgID: orCtx.OrgID,
  171. }
  172. if err := w.UpdateEvent(); err != nil {
  173. ctx.Handle(500, "UpdateEvent", err)
  174. return
  175. } else if err := models.CreateWebhook(w); err != nil {
  176. ctx.Handle(500, "CreateWebhook", err)
  177. return
  178. }
  179. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  180. ctx.Redirect(orCtx.Link + "/settings/hooks")
  181. }
  182. func checkWebhook(ctx *context.Context) (*OrgRepoCtx, *models.Webhook) {
  183. ctx.Data["RequireHighlightJS"] = true
  184. orCtx, err := getOrgRepoCtx(ctx)
  185. if err != nil {
  186. ctx.Handle(500, "getOrgRepoCtx", err)
  187. return nil, nil
  188. }
  189. ctx.Data["BaseLink"] = orCtx.Link
  190. var w *models.Webhook
  191. if orCtx.RepoID > 0 {
  192. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  193. } else {
  194. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  195. }
  196. if err != nil {
  197. if models.IsErrWebhookNotExist(err) {
  198. ctx.Handle(404, "GetWebhookByID", nil)
  199. } else {
  200. ctx.Handle(500, "GetWebhookByID", err)
  201. }
  202. return nil, nil
  203. }
  204. switch w.HookTaskType {
  205. case models.SLACK:
  206. ctx.Data["SlackHook"] = w.GetSlackHook()
  207. ctx.Data["HookType"] = "slack"
  208. default:
  209. ctx.Data["HookType"] = "gogs"
  210. }
  211. ctx.Data["History"], err = w.History(1)
  212. if err != nil {
  213. ctx.Handle(500, "History", err)
  214. }
  215. return orCtx, w
  216. }
  217. func WebHooksEdit(ctx *context.Context) {
  218. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  219. ctx.Data["PageIsSettingsHooks"] = true
  220. ctx.Data["PageIsSettingsHooksEdit"] = true
  221. orCtx, w := checkWebhook(ctx)
  222. if ctx.Written() {
  223. return
  224. }
  225. ctx.Data["Webhook"] = w
  226. ctx.HTML(200, orCtx.NewTemplate)
  227. }
  228. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  229. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  230. ctx.Data["PageIsSettingsHooks"] = true
  231. ctx.Data["PageIsSettingsHooksEdit"] = true
  232. orCtx, w := checkWebhook(ctx)
  233. if ctx.Written() {
  234. return
  235. }
  236. ctx.Data["Webhook"] = w
  237. if ctx.HasError() {
  238. ctx.HTML(200, orCtx.NewTemplate)
  239. return
  240. }
  241. contentType := models.JSON
  242. if models.HookContentType(form.ContentType) == models.FORM {
  243. contentType = models.FORM
  244. }
  245. w.URL = form.PayloadURL
  246. w.ContentType = contentType
  247. w.Secret = form.Secret
  248. w.HookEvent = ParseHookEvent(form.WebhookForm)
  249. w.IsActive = form.Active
  250. if err := w.UpdateEvent(); err != nil {
  251. ctx.Handle(500, "UpdateEvent", err)
  252. return
  253. } else if err := models.UpdateWebhook(w); err != nil {
  254. ctx.Handle(500, "WebHooksEditPost", err)
  255. return
  256. }
  257. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  258. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  259. }
  260. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  261. ctx.Data["Title"] = ctx.Tr("repo.settings")
  262. ctx.Data["PageIsSettingsHooks"] = true
  263. ctx.Data["PageIsSettingsHooksEdit"] = true
  264. orCtx, w := checkWebhook(ctx)
  265. if ctx.Written() {
  266. return
  267. }
  268. ctx.Data["Webhook"] = w
  269. if ctx.HasError() {
  270. ctx.HTML(200, orCtx.NewTemplate)
  271. return
  272. }
  273. meta, err := json.Marshal(&models.SlackMeta{
  274. Channel: form.Channel,
  275. Username: form.Username,
  276. IconURL: form.IconURL,
  277. Color: form.Color,
  278. })
  279. if err != nil {
  280. ctx.Handle(500, "Marshal", err)
  281. return
  282. }
  283. w.URL = form.PayloadURL
  284. w.Meta = string(meta)
  285. w.HookEvent = ParseHookEvent(form.WebhookForm)
  286. w.IsActive = form.Active
  287. if err := w.UpdateEvent(); err != nil {
  288. ctx.Handle(500, "UpdateEvent", err)
  289. return
  290. } else if err := models.UpdateWebhook(w); err != nil {
  291. ctx.Handle(500, "UpdateWebhook", err)
  292. return
  293. }
  294. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  295. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
  296. }
  297. func TestWebhook(ctx *context.Context) {
  298. // Grab latest commit or fake one if it's empty repository.
  299. commit := ctx.Repo.Commit
  300. if commit == nil {
  301. ghost := models.NewGhostUser()
  302. commit = &git.Commit{
  303. ID: git.MustIDFromString(git.EMPTY_SHA),
  304. Author: ghost.NewGitSig(),
  305. Committer: ghost.NewGitSig(),
  306. CommitMessage: "This is a fake commit",
  307. }
  308. }
  309. apiUser := ctx.User.APIFormat()
  310. p := &api.PushPayload{
  311. Ref: git.BRANCH_PREFIX + ctx.Repo.Repository.DefaultBranch,
  312. Before: commit.ID.String(),
  313. After: commit.ID.String(),
  314. Commits: []*api.PayloadCommit{
  315. {
  316. ID: commit.ID.String(),
  317. Message: commit.Message(),
  318. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  319. Author: &api.PayloadUser{
  320. Name: commit.Author.Name,
  321. Email: commit.Author.Email,
  322. },
  323. Committer: &api.PayloadUser{
  324. Name: commit.Committer.Name,
  325. Email: commit.Committer.Email,
  326. },
  327. },
  328. },
  329. Repo: ctx.Repo.Repository.APIFormat(nil),
  330. Pusher: apiUser,
  331. Sender: apiUser,
  332. }
  333. if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p); err != nil {
  334. ctx.Flash.Error("PrepareWebhooks: " + err.Error())
  335. ctx.Status(500)
  336. } else {
  337. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  338. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  339. ctx.Status(200)
  340. }
  341. }
  342. func DeleteWebhook(ctx *context.Context) {
  343. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  344. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  345. } else {
  346. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  347. }
  348. ctx.JSON(200, map[string]interface{}{
  349. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  350. })
  351. }