You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

webhook.go 30 kB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
8 years ago
8 years ago
8 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "path"
  11. "strings"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/auth"
  14. "code.gitea.io/gitea/modules/base"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/setting"
  18. api "code.gitea.io/gitea/modules/structs"
  19. "code.gitea.io/gitea/modules/webhook"
  20. "github.com/unknwon/com"
  21. )
  22. const (
  23. tplHooks base.TplName = "repo/settings/webhook/base"
  24. tplHookNew base.TplName = "repo/settings/webhook/new"
  25. tplOrgHookNew base.TplName = "org/settings/hook_new"
  26. tplAdminHookNew base.TplName = "admin/hook_new"
  27. )
  28. // Webhooks render web hooks list page
  29. func Webhooks(ctx *context.Context) {
  30. ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
  31. ctx.Data["PageIsSettingsHooks"] = true
  32. ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
  33. ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
  34. ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, models.ListOptions{})
  35. if err != nil {
  36. ctx.ServerError("GetWebhooksByRepoID", err)
  37. return
  38. }
  39. ctx.Data["Webhooks"] = ws
  40. ctx.HTML(200, tplHooks)
  41. }
  42. type orgRepoCtx struct {
  43. OrgID int64
  44. RepoID int64
  45. IsAdmin bool
  46. IsSystemWebhook bool
  47. Link string
  48. NewTemplate base.TplName
  49. }
  50. // getOrgRepoCtx determines whether this is a repo, organization, or admin (both default and system) context.
  51. func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
  52. if len(ctx.Repo.RepoLink) > 0 {
  53. return &orgRepoCtx{
  54. RepoID: ctx.Repo.Repository.ID,
  55. Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
  56. NewTemplate: tplHookNew,
  57. }, nil
  58. }
  59. if len(ctx.Org.OrgLink) > 0 {
  60. return &orgRepoCtx{
  61. OrgID: ctx.Org.Organization.ID,
  62. Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
  63. NewTemplate: tplOrgHookNew,
  64. }, nil
  65. }
  66. if ctx.User.IsAdmin {
  67. // Are we looking at default webhooks?
  68. if ctx.Params(":configType") == "hooks" {
  69. return &orgRepoCtx{
  70. IsAdmin: true,
  71. Link: path.Join(setting.AppSubURL, "/admin/hooks"),
  72. NewTemplate: tplAdminHookNew,
  73. }, nil
  74. }
  75. // Must be system webhooks instead
  76. return &orgRepoCtx{
  77. IsAdmin: true,
  78. IsSystemWebhook: true,
  79. Link: path.Join(setting.AppSubURL, "/admin/system-hooks"),
  80. NewTemplate: tplAdminHookNew,
  81. }, nil
  82. }
  83. return nil, errors.New("Unable to set OrgRepo context")
  84. }
  85. func checkHookType(ctx *context.Context) string {
  86. hookType := strings.ToLower(ctx.Params(":type"))
  87. if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
  88. ctx.NotFound("checkHookType", nil)
  89. return ""
  90. }
  91. return hookType
  92. }
  93. // WebhooksNew render creating webhook page
  94. func WebhooksNew(ctx *context.Context) {
  95. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  96. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  97. orCtx, err := getOrgRepoCtx(ctx)
  98. if err != nil {
  99. ctx.ServerError("getOrgRepoCtx", err)
  100. return
  101. }
  102. if orCtx.IsAdmin && orCtx.IsSystemWebhook {
  103. ctx.Data["PageIsAdminSystemHooks"] = true
  104. ctx.Data["PageIsAdminSystemHooksNew"] = true
  105. } else if orCtx.IsAdmin {
  106. ctx.Data["PageIsAdminHooks"] = true
  107. ctx.Data["PageIsAdminHooksNew"] = true
  108. } else {
  109. ctx.Data["PageIsSettingsHooks"] = true
  110. ctx.Data["PageIsSettingsHooksNew"] = true
  111. }
  112. hookType := checkHookType(ctx)
  113. ctx.Data["HookType"] = hookType
  114. if ctx.Written() {
  115. return
  116. }
  117. if hookType == "discord" {
  118. ctx.Data["DiscordHook"] = map[string]interface{}{
  119. "Username": "Gitea",
  120. "IconURL": setting.AppURL + "img/favicon.png",
  121. }
  122. }
  123. ctx.Data["BaseLink"] = orCtx.Link
  124. ctx.HTML(200, orCtx.NewTemplate)
  125. }
  126. // ParseHookEvent convert web form content to models.HookEvent
  127. func ParseHookEvent(form auth.WebhookForm) *models.HookEvent {
  128. return &models.HookEvent{
  129. PushOnly: form.PushOnly(),
  130. SendEverything: form.SendEverything(),
  131. ChooseEvents: form.ChooseEvents(),
  132. HookEvents: models.HookEvents{
  133. Create: form.Create,
  134. Delete: form.Delete,
  135. Fork: form.Fork,
  136. Issues: form.Issues,
  137. IssueAssign: form.IssueAssign,
  138. IssueLabel: form.IssueLabel,
  139. IssueMilestone: form.IssueMilestone,
  140. IssueComment: form.IssueComment,
  141. Release: form.Release,
  142. Push: form.Push,
  143. PullRequest: form.PullRequest,
  144. PullRequestAssign: form.PullRequestAssign,
  145. PullRequestLabel: form.PullRequestLabel,
  146. PullRequestMilestone: form.PullRequestMilestone,
  147. PullRequestComment: form.PullRequestComment,
  148. PullRequestReview: form.PullRequestReview,
  149. PullRequestSync: form.PullRequestSync,
  150. Repository: form.Repository,
  151. },
  152. BranchFilter: form.BranchFilter,
  153. }
  154. }
  155. // GiteaHooksNewPost response for creating Gitea webhook
  156. func GiteaHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
  157. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  158. ctx.Data["PageIsSettingsHooks"] = true
  159. ctx.Data["PageIsSettingsHooksNew"] = true
  160. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  161. ctx.Data["HookType"] = "gitea"
  162. orCtx, err := getOrgRepoCtx(ctx)
  163. if err != nil {
  164. ctx.ServerError("getOrgRepoCtx", err)
  165. return
  166. }
  167. ctx.Data["BaseLink"] = orCtx.Link
  168. if ctx.HasError() {
  169. ctx.HTML(200, orCtx.NewTemplate)
  170. return
  171. }
  172. contentType := models.ContentTypeJSON
  173. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  174. contentType = models.ContentTypeForm
  175. }
  176. w := &models.Webhook{
  177. RepoID: orCtx.RepoID,
  178. URL: form.PayloadURL,
  179. HTTPMethod: form.HTTPMethod,
  180. ContentType: contentType,
  181. Secret: form.Secret,
  182. HookEvent: ParseHookEvent(form.WebhookForm),
  183. IsActive: form.Active,
  184. HookTaskType: models.GITEA,
  185. OrgID: orCtx.OrgID,
  186. IsSystemWebhook: orCtx.IsSystemWebhook,
  187. }
  188. if err := w.UpdateEvent(); err != nil {
  189. ctx.ServerError("UpdateEvent", err)
  190. return
  191. } else if err := models.CreateWebhook(w); err != nil {
  192. ctx.ServerError("CreateWebhook", err)
  193. return
  194. }
  195. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  196. ctx.Redirect(orCtx.Link)
  197. }
  198. // GogsHooksNewPost response for creating webhook
  199. func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) {
  200. newGogsWebhookPost(ctx, form, models.GOGS)
  201. }
  202. // newGogsWebhookPost response for creating gogs hook
  203. func newGogsWebhookPost(ctx *context.Context, form auth.NewGogshookForm, kind models.HookTaskType) {
  204. ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
  205. ctx.Data["PageIsSettingsHooks"] = true
  206. ctx.Data["PageIsSettingsHooksNew"] = true
  207. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  208. ctx.Data["HookType"] = "gogs"
  209. orCtx, err := getOrgRepoCtx(ctx)
  210. if err != nil {
  211. ctx.ServerError("getOrgRepoCtx", err)
  212. return
  213. }
  214. ctx.Data["BaseLink"] = orCtx.Link
  215. if ctx.HasError() {
  216. ctx.HTML(200, orCtx.NewTemplate)
  217. return
  218. }
  219. contentType := models.ContentTypeJSON
  220. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  221. contentType = models.ContentTypeForm
  222. }
  223. w := &models.Webhook{
  224. RepoID: orCtx.RepoID,
  225. URL: form.PayloadURL,
  226. ContentType: contentType,
  227. Secret: form.Secret,
  228. HookEvent: ParseHookEvent(form.WebhookForm),
  229. IsActive: form.Active,
  230. HookTaskType: kind,
  231. OrgID: orCtx.OrgID,
  232. IsSystemWebhook: orCtx.IsSystemWebhook,
  233. }
  234. if err := w.UpdateEvent(); err != nil {
  235. ctx.ServerError("UpdateEvent", err)
  236. return
  237. } else if err := models.CreateWebhook(w); err != nil {
  238. ctx.ServerError("CreateWebhook", err)
  239. return
  240. }
  241. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  242. ctx.Redirect(orCtx.Link)
  243. }
  244. // DiscordHooksNewPost response for creating discord hook
  245. func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  246. ctx.Data["Title"] = ctx.Tr("repo.settings")
  247. ctx.Data["PageIsSettingsHooks"] = true
  248. ctx.Data["PageIsSettingsHooksNew"] = true
  249. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  250. orCtx, err := getOrgRepoCtx(ctx)
  251. if err != nil {
  252. ctx.ServerError("getOrgRepoCtx", err)
  253. return
  254. }
  255. if ctx.HasError() {
  256. ctx.HTML(200, orCtx.NewTemplate)
  257. return
  258. }
  259. meta, err := json.Marshal(&webhook.DiscordMeta{
  260. Username: form.Username,
  261. IconURL: form.IconURL,
  262. })
  263. if err != nil {
  264. ctx.ServerError("Marshal", err)
  265. return
  266. }
  267. w := &models.Webhook{
  268. RepoID: orCtx.RepoID,
  269. URL: form.PayloadURL,
  270. ContentType: models.ContentTypeJSON,
  271. HookEvent: ParseHookEvent(form.WebhookForm),
  272. IsActive: form.Active,
  273. HookTaskType: models.DISCORD,
  274. Meta: string(meta),
  275. OrgID: orCtx.OrgID,
  276. IsSystemWebhook: orCtx.IsSystemWebhook,
  277. }
  278. if err := w.UpdateEvent(); err != nil {
  279. ctx.ServerError("UpdateEvent", err)
  280. return
  281. } else if err := models.CreateWebhook(w); err != nil {
  282. ctx.ServerError("CreateWebhook", err)
  283. return
  284. }
  285. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  286. ctx.Redirect(orCtx.Link)
  287. }
  288. // DingtalkHooksNewPost response for creating dingtalk hook
  289. func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  290. ctx.Data["Title"] = ctx.Tr("repo.settings")
  291. ctx.Data["PageIsSettingsHooks"] = true
  292. ctx.Data["PageIsSettingsHooksNew"] = true
  293. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  294. orCtx, err := getOrgRepoCtx(ctx)
  295. if err != nil {
  296. ctx.ServerError("getOrgRepoCtx", err)
  297. return
  298. }
  299. if ctx.HasError() {
  300. ctx.HTML(200, orCtx.NewTemplate)
  301. return
  302. }
  303. w := &models.Webhook{
  304. RepoID: orCtx.RepoID,
  305. URL: form.PayloadURL,
  306. ContentType: models.ContentTypeJSON,
  307. HookEvent: ParseHookEvent(form.WebhookForm),
  308. IsActive: form.Active,
  309. HookTaskType: models.DINGTALK,
  310. Meta: "",
  311. OrgID: orCtx.OrgID,
  312. IsSystemWebhook: orCtx.IsSystemWebhook,
  313. }
  314. if err := w.UpdateEvent(); err != nil {
  315. ctx.ServerError("UpdateEvent", err)
  316. return
  317. } else if err := models.CreateWebhook(w); err != nil {
  318. ctx.ServerError("CreateWebhook", err)
  319. return
  320. }
  321. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  322. ctx.Redirect(orCtx.Link)
  323. }
  324. // TelegramHooksNewPost response for creating telegram hook
  325. func TelegramHooksNewPost(ctx *context.Context, form auth.NewTelegramHookForm) {
  326. ctx.Data["Title"] = ctx.Tr("repo.settings")
  327. ctx.Data["PageIsSettingsHooks"] = true
  328. ctx.Data["PageIsSettingsHooksNew"] = true
  329. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  330. orCtx, err := getOrgRepoCtx(ctx)
  331. if err != nil {
  332. ctx.ServerError("getOrgRepoCtx", err)
  333. return
  334. }
  335. if ctx.HasError() {
  336. ctx.HTML(200, orCtx.NewTemplate)
  337. return
  338. }
  339. meta, err := json.Marshal(&webhook.TelegramMeta{
  340. BotToken: form.BotToken,
  341. ChatID: form.ChatID,
  342. })
  343. if err != nil {
  344. ctx.ServerError("Marshal", err)
  345. return
  346. }
  347. w := &models.Webhook{
  348. RepoID: orCtx.RepoID,
  349. URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID),
  350. ContentType: models.ContentTypeJSON,
  351. HookEvent: ParseHookEvent(form.WebhookForm),
  352. IsActive: form.Active,
  353. HookTaskType: models.TELEGRAM,
  354. Meta: string(meta),
  355. OrgID: orCtx.OrgID,
  356. IsSystemWebhook: orCtx.IsSystemWebhook,
  357. }
  358. if err := w.UpdateEvent(); err != nil {
  359. ctx.ServerError("UpdateEvent", err)
  360. return
  361. } else if err := models.CreateWebhook(w); err != nil {
  362. ctx.ServerError("CreateWebhook", err)
  363. return
  364. }
  365. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  366. ctx.Redirect(orCtx.Link)
  367. }
  368. // MatrixHooksNewPost response for creating a Matrix hook
  369. func MatrixHooksNewPost(ctx *context.Context, form auth.NewMatrixHookForm) {
  370. ctx.Data["Title"] = ctx.Tr("repo.settings")
  371. ctx.Data["PageIsSettingsHooks"] = true
  372. ctx.Data["PageIsSettingsHooksNew"] = true
  373. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  374. orCtx, err := getOrgRepoCtx(ctx)
  375. if err != nil {
  376. ctx.ServerError("getOrgRepoCtx", err)
  377. return
  378. }
  379. if ctx.HasError() {
  380. ctx.HTML(200, orCtx.NewTemplate)
  381. return
  382. }
  383. meta, err := json.Marshal(&webhook.MatrixMeta{
  384. HomeserverURL: form.HomeserverURL,
  385. Room: form.RoomID,
  386. AccessToken: form.AccessToken,
  387. MessageType: form.MessageType,
  388. })
  389. if err != nil {
  390. ctx.ServerError("Marshal", err)
  391. return
  392. }
  393. w := &models.Webhook{
  394. RepoID: orCtx.RepoID,
  395. URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, form.RoomID),
  396. ContentType: models.ContentTypeJSON,
  397. HookEvent: ParseHookEvent(form.WebhookForm),
  398. IsActive: form.Active,
  399. HookTaskType: models.MATRIX,
  400. Meta: string(meta),
  401. OrgID: orCtx.OrgID,
  402. IsSystemWebhook: orCtx.IsSystemWebhook,
  403. }
  404. if err := w.UpdateEvent(); err != nil {
  405. ctx.ServerError("UpdateEvent", err)
  406. return
  407. } else if err := models.CreateWebhook(w); err != nil {
  408. ctx.ServerError("CreateWebhook", err)
  409. return
  410. }
  411. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  412. ctx.Redirect(orCtx.Link)
  413. }
  414. // MSTeamsHooksNewPost response for creating MS Teams hook
  415. func MSTeamsHooksNewPost(ctx *context.Context, form auth.NewMSTeamsHookForm) {
  416. ctx.Data["Title"] = ctx.Tr("repo.settings")
  417. ctx.Data["PageIsSettingsHooks"] = true
  418. ctx.Data["PageIsSettingsHooksNew"] = true
  419. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  420. orCtx, err := getOrgRepoCtx(ctx)
  421. if err != nil {
  422. ctx.ServerError("getOrgRepoCtx", err)
  423. return
  424. }
  425. if ctx.HasError() {
  426. ctx.HTML(200, orCtx.NewTemplate)
  427. return
  428. }
  429. w := &models.Webhook{
  430. RepoID: orCtx.RepoID,
  431. URL: form.PayloadURL,
  432. ContentType: models.ContentTypeJSON,
  433. HookEvent: ParseHookEvent(form.WebhookForm),
  434. IsActive: form.Active,
  435. HookTaskType: models.MSTEAMS,
  436. Meta: "",
  437. OrgID: orCtx.OrgID,
  438. IsSystemWebhook: orCtx.IsSystemWebhook,
  439. }
  440. if err := w.UpdateEvent(); err != nil {
  441. ctx.ServerError("UpdateEvent", err)
  442. return
  443. } else if err := models.CreateWebhook(w); err != nil {
  444. ctx.ServerError("CreateWebhook", err)
  445. return
  446. }
  447. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  448. ctx.Redirect(orCtx.Link)
  449. }
  450. // SlackHooksNewPost response for creating slack hook
  451. func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
  452. ctx.Data["Title"] = ctx.Tr("repo.settings")
  453. ctx.Data["PageIsSettingsHooks"] = true
  454. ctx.Data["PageIsSettingsHooksNew"] = true
  455. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  456. orCtx, err := getOrgRepoCtx(ctx)
  457. if err != nil {
  458. ctx.ServerError("getOrgRepoCtx", err)
  459. return
  460. }
  461. if ctx.HasError() {
  462. ctx.HTML(200, orCtx.NewTemplate)
  463. return
  464. }
  465. if form.HasInvalidChannel() {
  466. ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
  467. ctx.Redirect(orCtx.Link + "/slack/new")
  468. return
  469. }
  470. meta, err := json.Marshal(&webhook.SlackMeta{
  471. Channel: strings.TrimSpace(form.Channel),
  472. Username: form.Username,
  473. IconURL: form.IconURL,
  474. Color: form.Color,
  475. })
  476. if err != nil {
  477. ctx.ServerError("Marshal", err)
  478. return
  479. }
  480. w := &models.Webhook{
  481. RepoID: orCtx.RepoID,
  482. URL: form.PayloadURL,
  483. ContentType: models.ContentTypeJSON,
  484. HookEvent: ParseHookEvent(form.WebhookForm),
  485. IsActive: form.Active,
  486. HookTaskType: models.SLACK,
  487. Meta: string(meta),
  488. OrgID: orCtx.OrgID,
  489. IsSystemWebhook: orCtx.IsSystemWebhook,
  490. }
  491. if err := w.UpdateEvent(); err != nil {
  492. ctx.ServerError("UpdateEvent", err)
  493. return
  494. } else if err := models.CreateWebhook(w); err != nil {
  495. ctx.ServerError("CreateWebhook", err)
  496. return
  497. }
  498. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  499. ctx.Redirect(orCtx.Link)
  500. }
  501. // FeishuHooksNewPost response for creating feishu hook
  502. func FeishuHooksNewPost(ctx *context.Context, form auth.NewFeishuHookForm) {
  503. ctx.Data["Title"] = ctx.Tr("repo.settings")
  504. ctx.Data["PageIsSettingsHooks"] = true
  505. ctx.Data["PageIsSettingsHooksNew"] = true
  506. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  507. orCtx, err := getOrgRepoCtx(ctx)
  508. if err != nil {
  509. ctx.ServerError("getOrgRepoCtx", err)
  510. return
  511. }
  512. if ctx.HasError() {
  513. ctx.HTML(200, orCtx.NewTemplate)
  514. return
  515. }
  516. w := &models.Webhook{
  517. RepoID: orCtx.RepoID,
  518. URL: form.PayloadURL,
  519. ContentType: models.ContentTypeJSON,
  520. HookEvent: ParseHookEvent(form.WebhookForm),
  521. IsActive: form.Active,
  522. HookTaskType: models.FEISHU,
  523. Meta: "",
  524. OrgID: orCtx.OrgID,
  525. IsSystemWebhook: orCtx.IsSystemWebhook,
  526. }
  527. if err := w.UpdateEvent(); err != nil {
  528. ctx.ServerError("UpdateEvent", err)
  529. return
  530. } else if err := models.CreateWebhook(w); err != nil {
  531. ctx.ServerError("CreateWebhook", err)
  532. return
  533. }
  534. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  535. ctx.Redirect(orCtx.Link)
  536. }
  537. func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
  538. ctx.Data["RequireHighlightJS"] = true
  539. orCtx, err := getOrgRepoCtx(ctx)
  540. if err != nil {
  541. ctx.ServerError("getOrgRepoCtx", err)
  542. return nil, nil
  543. }
  544. ctx.Data["BaseLink"] = orCtx.Link
  545. var w *models.Webhook
  546. if orCtx.RepoID > 0 {
  547. w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
  548. } else if orCtx.OrgID > 0 {
  549. w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
  550. } else if orCtx.IsSystemWebhook {
  551. w, err = models.GetSystemWebhook(ctx.ParamsInt64(":id"))
  552. } else {
  553. w, err = models.GetDefaultWebhook(ctx.ParamsInt64(":id"))
  554. }
  555. if err != nil {
  556. if models.IsErrWebhookNotExist(err) {
  557. ctx.NotFound("GetWebhookByID", nil)
  558. } else {
  559. ctx.ServerError("GetWebhookByID", err)
  560. }
  561. return nil, nil
  562. }
  563. ctx.Data["HookType"] = w.HookTaskType.Name()
  564. switch w.HookTaskType {
  565. case models.SLACK:
  566. ctx.Data["SlackHook"] = webhook.GetSlackHook(w)
  567. case models.DISCORD:
  568. ctx.Data["DiscordHook"] = webhook.GetDiscordHook(w)
  569. case models.TELEGRAM:
  570. ctx.Data["TelegramHook"] = webhook.GetTelegramHook(w)
  571. case models.MATRIX:
  572. ctx.Data["MatrixHook"] = webhook.GetMatrixHook(w)
  573. }
  574. ctx.Data["History"], err = w.History(1)
  575. if err != nil {
  576. ctx.ServerError("History", err)
  577. }
  578. return orCtx, w
  579. }
  580. // WebHooksEdit render editing web hook page
  581. func WebHooksEdit(ctx *context.Context) {
  582. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  583. ctx.Data["PageIsSettingsHooks"] = true
  584. ctx.Data["PageIsSettingsHooksEdit"] = true
  585. orCtx, w := checkWebhook(ctx)
  586. if ctx.Written() {
  587. return
  588. }
  589. ctx.Data["Webhook"] = w
  590. ctx.HTML(200, orCtx.NewTemplate)
  591. }
  592. // WebHooksEditPost response for editing web hook
  593. func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
  594. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  595. ctx.Data["PageIsSettingsHooks"] = true
  596. ctx.Data["PageIsSettingsHooksEdit"] = true
  597. orCtx, w := checkWebhook(ctx)
  598. if ctx.Written() {
  599. return
  600. }
  601. ctx.Data["Webhook"] = w
  602. if ctx.HasError() {
  603. ctx.HTML(200, orCtx.NewTemplate)
  604. return
  605. }
  606. contentType := models.ContentTypeJSON
  607. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  608. contentType = models.ContentTypeForm
  609. }
  610. w.URL = form.PayloadURL
  611. w.ContentType = contentType
  612. w.Secret = form.Secret
  613. w.HookEvent = ParseHookEvent(form.WebhookForm)
  614. w.IsActive = form.Active
  615. w.HTTPMethod = form.HTTPMethod
  616. if err := w.UpdateEvent(); err != nil {
  617. ctx.ServerError("UpdateEvent", err)
  618. return
  619. } else if err := models.UpdateWebhook(w); err != nil {
  620. ctx.ServerError("WebHooksEditPost", err)
  621. return
  622. }
  623. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  624. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  625. }
  626. // GogsHooksEditPost response for editing gogs hook
  627. func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
  628. ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
  629. ctx.Data["PageIsSettingsHooks"] = true
  630. ctx.Data["PageIsSettingsHooksEdit"] = true
  631. orCtx, w := checkWebhook(ctx)
  632. if ctx.Written() {
  633. return
  634. }
  635. ctx.Data["Webhook"] = w
  636. if ctx.HasError() {
  637. ctx.HTML(200, orCtx.NewTemplate)
  638. return
  639. }
  640. contentType := models.ContentTypeJSON
  641. if models.HookContentType(form.ContentType) == models.ContentTypeForm {
  642. contentType = models.ContentTypeForm
  643. }
  644. w.URL = form.PayloadURL
  645. w.ContentType = contentType
  646. w.Secret = form.Secret
  647. w.HookEvent = ParseHookEvent(form.WebhookForm)
  648. w.IsActive = form.Active
  649. if err := w.UpdateEvent(); err != nil {
  650. ctx.ServerError("UpdateEvent", err)
  651. return
  652. } else if err := models.UpdateWebhook(w); err != nil {
  653. ctx.ServerError("GogsHooksEditPost", err)
  654. return
  655. }
  656. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  657. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  658. }
  659. // SlackHooksEditPost response for editing slack hook
  660. func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
  661. ctx.Data["Title"] = ctx.Tr("repo.settings")
  662. ctx.Data["PageIsSettingsHooks"] = true
  663. ctx.Data["PageIsSettingsHooksEdit"] = true
  664. orCtx, w := checkWebhook(ctx)
  665. if ctx.Written() {
  666. return
  667. }
  668. ctx.Data["Webhook"] = w
  669. if ctx.HasError() {
  670. ctx.HTML(200, orCtx.NewTemplate)
  671. return
  672. }
  673. if form.HasInvalidChannel() {
  674. ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
  675. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  676. return
  677. }
  678. meta, err := json.Marshal(&webhook.SlackMeta{
  679. Channel: strings.TrimSpace(form.Channel),
  680. Username: form.Username,
  681. IconURL: form.IconURL,
  682. Color: form.Color,
  683. })
  684. if err != nil {
  685. ctx.ServerError("Marshal", err)
  686. return
  687. }
  688. w.URL = form.PayloadURL
  689. w.Meta = string(meta)
  690. w.HookEvent = ParseHookEvent(form.WebhookForm)
  691. w.IsActive = form.Active
  692. if err := w.UpdateEvent(); err != nil {
  693. ctx.ServerError("UpdateEvent", err)
  694. return
  695. } else if err := models.UpdateWebhook(w); err != nil {
  696. ctx.ServerError("UpdateWebhook", err)
  697. return
  698. }
  699. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  700. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  701. }
  702. // DiscordHooksEditPost response for editing discord hook
  703. func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
  704. ctx.Data["Title"] = ctx.Tr("repo.settings")
  705. ctx.Data["PageIsSettingsHooks"] = true
  706. ctx.Data["PageIsSettingsHooksEdit"] = true
  707. orCtx, w := checkWebhook(ctx)
  708. if ctx.Written() {
  709. return
  710. }
  711. ctx.Data["Webhook"] = w
  712. if ctx.HasError() {
  713. ctx.HTML(200, orCtx.NewTemplate)
  714. return
  715. }
  716. meta, err := json.Marshal(&webhook.DiscordMeta{
  717. Username: form.Username,
  718. IconURL: form.IconURL,
  719. })
  720. if err != nil {
  721. ctx.ServerError("Marshal", err)
  722. return
  723. }
  724. w.URL = form.PayloadURL
  725. w.Meta = string(meta)
  726. w.HookEvent = ParseHookEvent(form.WebhookForm)
  727. w.IsActive = form.Active
  728. if err := w.UpdateEvent(); err != nil {
  729. ctx.ServerError("UpdateEvent", err)
  730. return
  731. } else if err := models.UpdateWebhook(w); err != nil {
  732. ctx.ServerError("UpdateWebhook", err)
  733. return
  734. }
  735. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  736. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  737. }
  738. // DingtalkHooksEditPost response for editing discord hook
  739. func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
  740. ctx.Data["Title"] = ctx.Tr("repo.settings")
  741. ctx.Data["PageIsSettingsHooks"] = true
  742. ctx.Data["PageIsSettingsHooksEdit"] = true
  743. orCtx, w := checkWebhook(ctx)
  744. if ctx.Written() {
  745. return
  746. }
  747. ctx.Data["Webhook"] = w
  748. if ctx.HasError() {
  749. ctx.HTML(200, orCtx.NewTemplate)
  750. return
  751. }
  752. w.URL = form.PayloadURL
  753. w.HookEvent = ParseHookEvent(form.WebhookForm)
  754. w.IsActive = form.Active
  755. if err := w.UpdateEvent(); err != nil {
  756. ctx.ServerError("UpdateEvent", err)
  757. return
  758. } else if err := models.UpdateWebhook(w); err != nil {
  759. ctx.ServerError("UpdateWebhook", err)
  760. return
  761. }
  762. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  763. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  764. }
  765. // TelegramHooksEditPost response for editing discord hook
  766. func TelegramHooksEditPost(ctx *context.Context, form auth.NewTelegramHookForm) {
  767. ctx.Data["Title"] = ctx.Tr("repo.settings")
  768. ctx.Data["PageIsSettingsHooks"] = true
  769. ctx.Data["PageIsSettingsHooksEdit"] = true
  770. orCtx, w := checkWebhook(ctx)
  771. if ctx.Written() {
  772. return
  773. }
  774. ctx.Data["Webhook"] = w
  775. if ctx.HasError() {
  776. ctx.HTML(200, orCtx.NewTemplate)
  777. return
  778. }
  779. meta, err := json.Marshal(&webhook.TelegramMeta{
  780. BotToken: form.BotToken,
  781. ChatID: form.ChatID,
  782. })
  783. if err != nil {
  784. ctx.ServerError("Marshal", err)
  785. return
  786. }
  787. w.Meta = string(meta)
  788. w.URL = fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID)
  789. w.HookEvent = ParseHookEvent(form.WebhookForm)
  790. w.IsActive = form.Active
  791. if err := w.UpdateEvent(); err != nil {
  792. ctx.ServerError("UpdateEvent", err)
  793. return
  794. } else if err := models.UpdateWebhook(w); err != nil {
  795. ctx.ServerError("UpdateWebhook", err)
  796. return
  797. }
  798. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  799. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  800. }
  801. // MatrixHooksEditPost response for editing a Matrix hook
  802. func MatrixHooksEditPost(ctx *context.Context, form auth.NewMatrixHookForm) {
  803. ctx.Data["Title"] = ctx.Tr("repo.settings")
  804. ctx.Data["PageIsSettingsHooks"] = true
  805. ctx.Data["PageIsSettingsHooksEdit"] = true
  806. orCtx, w := checkWebhook(ctx)
  807. if ctx.Written() {
  808. return
  809. }
  810. ctx.Data["Webhook"] = w
  811. if ctx.HasError() {
  812. ctx.HTML(200, orCtx.NewTemplate)
  813. return
  814. }
  815. meta, err := json.Marshal(&webhook.MatrixMeta{
  816. HomeserverURL: form.HomeserverURL,
  817. Room: form.RoomID,
  818. AccessToken: form.AccessToken,
  819. MessageType: form.MessageType,
  820. })
  821. if err != nil {
  822. ctx.ServerError("Marshal", err)
  823. return
  824. }
  825. w.Meta = string(meta)
  826. w.URL = fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, form.RoomID)
  827. w.HookEvent = ParseHookEvent(form.WebhookForm)
  828. w.IsActive = form.Active
  829. if err := w.UpdateEvent(); err != nil {
  830. ctx.ServerError("UpdateEvent", err)
  831. return
  832. } else if err := models.UpdateWebhook(w); err != nil {
  833. ctx.ServerError("UpdateWebhook", err)
  834. return
  835. }
  836. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  837. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  838. }
  839. // MSTeamsHooksEditPost response for editing MS Teams hook
  840. func MSTeamsHooksEditPost(ctx *context.Context, form auth.NewMSTeamsHookForm) {
  841. ctx.Data["Title"] = ctx.Tr("repo.settings")
  842. ctx.Data["PageIsSettingsHooks"] = true
  843. ctx.Data["PageIsSettingsHooksEdit"] = true
  844. orCtx, w := checkWebhook(ctx)
  845. if ctx.Written() {
  846. return
  847. }
  848. ctx.Data["Webhook"] = w
  849. if ctx.HasError() {
  850. ctx.HTML(200, orCtx.NewTemplate)
  851. return
  852. }
  853. w.URL = form.PayloadURL
  854. w.HookEvent = ParseHookEvent(form.WebhookForm)
  855. w.IsActive = form.Active
  856. if err := w.UpdateEvent(); err != nil {
  857. ctx.ServerError("UpdateEvent", err)
  858. return
  859. } else if err := models.UpdateWebhook(w); err != nil {
  860. ctx.ServerError("UpdateWebhook", err)
  861. return
  862. }
  863. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  864. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  865. }
  866. // FeishuHooksEditPost response for editing feishu hook
  867. func FeishuHooksEditPost(ctx *context.Context, form auth.NewFeishuHookForm) {
  868. ctx.Data["Title"] = ctx.Tr("repo.settings")
  869. ctx.Data["PageIsSettingsHooks"] = true
  870. ctx.Data["PageIsSettingsHooksEdit"] = true
  871. orCtx, w := checkWebhook(ctx)
  872. if ctx.Written() {
  873. return
  874. }
  875. ctx.Data["Webhook"] = w
  876. if ctx.HasError() {
  877. ctx.HTML(200, orCtx.NewTemplate)
  878. return
  879. }
  880. w.URL = form.PayloadURL
  881. w.HookEvent = ParseHookEvent(form.WebhookForm)
  882. w.IsActive = form.Active
  883. if err := w.UpdateEvent(); err != nil {
  884. ctx.ServerError("UpdateEvent", err)
  885. return
  886. } else if err := models.UpdateWebhook(w); err != nil {
  887. ctx.ServerError("UpdateWebhook", err)
  888. return
  889. }
  890. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  891. ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
  892. }
  893. // TestWebhook test if web hook is work fine
  894. func TestWebhook(ctx *context.Context) {
  895. hookID := ctx.ParamsInt64(":id")
  896. w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
  897. if err != nil {
  898. ctx.Flash.Error("GetWebhookByID: " + err.Error())
  899. ctx.Status(500)
  900. return
  901. }
  902. // Grab latest commit or fake one if it's empty repository.
  903. commit := ctx.Repo.Commit
  904. if commit == nil {
  905. ghost := models.NewGhostUser()
  906. commit = &git.Commit{
  907. ID: git.MustIDFromString(git.EmptySHA),
  908. Author: ghost.NewGitSig(),
  909. Committer: ghost.NewGitSig(),
  910. CommitMessage: "This is a fake commit",
  911. }
  912. }
  913. apiUser := ctx.User.APIFormat()
  914. p := &api.PushPayload{
  915. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  916. Before: commit.ID.String(),
  917. After: commit.ID.String(),
  918. Commits: []*api.PayloadCommit{
  919. {
  920. ID: commit.ID.String(),
  921. Message: commit.Message(),
  922. URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
  923. Author: &api.PayloadUser{
  924. Name: commit.Author.Name,
  925. Email: commit.Author.Email,
  926. },
  927. Committer: &api.PayloadUser{
  928. Name: commit.Committer.Name,
  929. Email: commit.Committer.Email,
  930. },
  931. },
  932. },
  933. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  934. Pusher: apiUser,
  935. Sender: apiUser,
  936. }
  937. if err := webhook.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
  938. ctx.Flash.Error("PrepareWebhook: " + err.Error())
  939. ctx.Status(500)
  940. } else {
  941. ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
  942. ctx.Status(200)
  943. }
  944. }
  945. // DeleteWebhook delete a webhook
  946. func DeleteWebhook(ctx *context.Context) {
  947. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
  948. ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
  949. } else {
  950. ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
  951. }
  952. ctx.JSON(200, map[string]interface{}{
  953. "redirect": ctx.Repo.RepoLink + "/settings/hooks",
  954. })
  955. }