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

626 lines
17 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. // Copyright 2014 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. "time"
  11. "github.com/Unknwon/com"
  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/git"
  16. "github.com/gogits/gogs/modules/log"
  17. "github.com/gogits/gogs/modules/mailer"
  18. "github.com/gogits/gogs/modules/middleware"
  19. "github.com/gogits/gogs/modules/setting"
  20. )
  21. const (
  22. SETTINGS_OPTIONS base.TplName = "repo/settings/options"
  23. COLLABORATION base.TplName = "repo/settings/collaboration"
  24. HOOKS base.TplName = "repo/settings/hooks"
  25. GITHOOKS base.TplName = "repo/settings/githooks"
  26. GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  27. HOOK_NEW base.TplName = "repo/settings/hook_new"
  28. ORG_HOOK_NEW base.TplName = "org/settings/hook_new"
  29. )
  30. func Settings(ctx *middleware.Context) {
  31. ctx.Data["Title"] = ctx.Tr("repo.settings")
  32. ctx.Data["PageIsSettingsOptions"] = true
  33. ctx.HTML(200, SETTINGS_OPTIONS)
  34. }
  35. func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
  36. ctx.Data["Title"] = ctx.Tr("repo.settings")
  37. ctx.Data["PageIsSettingsOptions"] = true
  38. switch ctx.Query("action") {
  39. case "update":
  40. if ctx.HasError() {
  41. ctx.HTML(200, SETTINGS_OPTIONS)
  42. return
  43. }
  44. newRepoName := form.RepoName
  45. // Check if repository name has been changed.
  46. if ctx.Repo.Repository.Name != newRepoName {
  47. if models.IsRepositoryExist(ctx.Repo.Owner, newRepoName) {
  48. ctx.Data["Err_RepoName"] = true
  49. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, nil)
  50. return
  51. } else if err := models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
  52. if err == models.ErrRepoNameIllegal {
  53. ctx.Data["Err_RepoName"] = true
  54. ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), SETTINGS_OPTIONS, nil)
  55. } else {
  56. ctx.Handle(500, "ChangeRepositoryName", err)
  57. }
  58. return
  59. }
  60. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName)
  61. ctx.Repo.Repository.Name = newRepoName
  62. ctx.Repo.Repository.LowerName = strings.ToLower(newRepoName)
  63. }
  64. br := form.Branch
  65. if ctx.Repo.GitRepo.IsBranchExist(br) {
  66. ctx.Repo.Repository.DefaultBranch = br
  67. }
  68. ctx.Repo.Repository.Description = form.Description
  69. ctx.Repo.Repository.Website = form.Website
  70. visibilityChanged := ctx.Repo.Repository.IsPrivate != form.Private
  71. ctx.Repo.Repository.IsPrivate = form.Private
  72. if err := models.UpdateRepository(ctx.Repo.Repository, visibilityChanged); err != nil {
  73. ctx.Handle(404, "UpdateRepository", err)
  74. return
  75. }
  76. log.Trace("Repository updated: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  77. if ctx.Repo.Repository.IsMirror {
  78. if form.Interval > 0 {
  79. ctx.Repo.Mirror.Interval = form.Interval
  80. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  81. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  82. log.Error(4, "UpdateMirror: %v", err)
  83. }
  84. }
  85. }
  86. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  87. ctx.Redirect(fmt.Sprintf("%s/%s/%s/settings", setting.AppSubUrl, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name))
  88. case "transfer":
  89. if ctx.Repo.Repository.Name != form.RepoName {
  90. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  91. return
  92. }
  93. newOwner := ctx.Query("new_owner_name")
  94. isExist, err := models.IsUserExist(0, newOwner)
  95. if err != nil {
  96. ctx.Handle(500, "IsUserExist", err)
  97. return
  98. } else if !isExist {
  99. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  100. return
  101. }
  102. if _, err = models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  103. if err == models.ErrUserNotExist {
  104. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
  105. } else {
  106. ctx.Handle(500, "UserSignIn", err)
  107. }
  108. return
  109. }
  110. if err = models.TransferOwnership(ctx.User, newOwner, ctx.Repo.Repository); err != nil {
  111. if err == models.ErrRepoAlreadyExist {
  112. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  113. } else {
  114. ctx.Handle(500, "TransferOwnership", err)
  115. }
  116. return
  117. }
  118. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newOwner)
  119. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  120. ctx.Redirect(setting.AppSubUrl + "/")
  121. case "delete":
  122. if ctx.Repo.Repository.Name != form.RepoName {
  123. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  124. return
  125. }
  126. if ctx.Repo.Owner.IsOrganization() {
  127. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  128. ctx.Error(404)
  129. return
  130. }
  131. }
  132. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  133. if err == models.ErrUserNotExist {
  134. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_OPTIONS, nil)
  135. } else {
  136. ctx.Handle(500, "UserSignIn", err)
  137. }
  138. return
  139. }
  140. if err := models.DeleteRepository(ctx.Repo.Owner.Id, ctx.Repo.Repository.Id, ctx.Repo.Owner.Name); err != nil {
  141. ctx.Handle(500, "DeleteRepository", err)
  142. return
  143. }
  144. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
  145. if ctx.Repo.Owner.IsOrganization() {
  146. ctx.Redirect(setting.AppSubUrl + "/org/" + ctx.Repo.Owner.Name + "/dashboard")
  147. } else {
  148. ctx.Redirect(setting.AppSubUrl + "/")
  149. }
  150. }
  151. }
  152. func SettingsCollaboration(ctx *middleware.Context) {
  153. ctx.Data["Title"] = ctx.Tr("repo.settings")
  154. ctx.Data["PageIsSettingsCollaboration"] = true
  155. if ctx.Req.Method == "POST" {
  156. name := strings.ToLower(ctx.Query("collaborator"))
  157. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  158. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  159. return
  160. }
  161. u, err := models.GetUserByName(name)
  162. if err != nil {
  163. if err == models.ErrUserNotExist {
  164. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  165. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  166. } else {
  167. ctx.Handle(500, "GetUserByName", err)
  168. }
  169. return
  170. }
  171. // Check if user is organization member.
  172. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  173. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  174. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  175. return
  176. }
  177. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  178. ctx.Handle(500, "AddCollaborator", err)
  179. return
  180. }
  181. if setting.Service.EnableNotifyMail {
  182. if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
  183. ctx.Handle(500, "SendCollaboratorMail", err)
  184. return
  185. }
  186. }
  187. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  188. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  189. return
  190. }
  191. // Delete collaborator.
  192. remove := strings.ToLower(ctx.Query("remove"))
  193. if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
  194. u, err := models.GetUserByName(remove)
  195. if err != nil {
  196. ctx.Handle(500, "GetUserByName", err)
  197. return
  198. }
  199. if err := ctx.Repo.Repository.DeleteCollaborator(u); err != nil {
  200. ctx.Handle(500, "DeleteCollaborator", err)
  201. return
  202. }
  203. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  204. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  205. return
  206. }
  207. users, err := ctx.Repo.Repository.GetCollaborators()
  208. if err != nil {
  209. ctx.Handle(500, "GetCollaborators", err)
  210. return
  211. }
  212. ctx.Data["Collaborators"] = users
  213. ctx.HTML(200, COLLABORATION)
  214. }
  215. func Webhooks(ctx *middleware.Context) {
  216. ctx.Data["Title"] = ctx.Tr("repo.settings")
  217. ctx.Data["PageIsSettingsHooks"] = true
  218. // Delete web hook.
  219. remove := com.StrTo(ctx.Query("remove")).MustInt64()
  220. if remove > 0 {
  221. if err := models.DeleteWebhook(remove); err != nil {
  222. ctx.Handle(500, "DeleteWebhook", err)
  223. return
  224. }
  225. ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
  226. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
  227. return
  228. }
  229. ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.Id)
  230. if err != nil {
  231. ctx.Handle(500, "GetWebhooksByRepoId", err)
  232. return
  233. }
  234. ctx.Data["Webhooks"] = ws
  235. ctx.HTML(200, HOOKS)
  236. }
  237. func renderHookTypes(ctx *middleware.Context) {
  238. ctx.Data["HookTypes"] = []string{"Gogs", "Slack"}
  239. ctx.Data["HookType"] = "Gogs"
  240. }
  241. func WebHooksNew(ctx *middleware.Context) {
  242. ctx.Data["Title"] = ctx.Tr("repo.settings")
  243. ctx.Data["PageIsSettingsHooks"] = true
  244. ctx.Data["PageIsSettingsHooksNew"] = true
  245. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  246. renderHookTypes(ctx)
  247. orCtx, err := getOrgRepoCtx(ctx)
  248. if err != nil {
  249. ctx.Handle(500, "WebHooksNew(getOrgRepoCtx)", err)
  250. return
  251. }
  252. ctx.HTML(200, orCtx.NewTemplate)
  253. }
  254. func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  255. ctx.Data["Title"] = ctx.Tr("repo.settings")
  256. ctx.Data["PageIsSettingsHooks"] = true
  257. ctx.Data["PageIsSettingsHooksNew"] = true
  258. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  259. orCtx, err := getOrgRepoCtx(ctx)
  260. if err != nil {
  261. ctx.Handle(500, "WebHooksNewPost(getOrgRepoCtx)", err)
  262. return
  263. }
  264. if ctx.HasError() {
  265. ctx.HTML(200, orCtx.NewTemplate)
  266. return
  267. }
  268. // FIXME: code too old here, sync with APIs
  269. ct := models.JSON
  270. if form.ContentType == "2" {
  271. ct = models.FORM
  272. }
  273. w := &models.Webhook{
  274. RepoId: orCtx.RepoId,
  275. Url: form.PayloadUrl,
  276. ContentType: ct,
  277. Secret: form.Secret,
  278. HookEvent: &models.HookEvent{
  279. PushOnly: form.PushOnly,
  280. },
  281. IsActive: form.Active,
  282. HookTaskType: models.GOGS,
  283. Meta: "",
  284. OrgId: orCtx.OrgId,
  285. }
  286. if err := w.UpdateEvent(); err != nil {
  287. ctx.Handle(500, "UpdateEvent", err)
  288. return
  289. } else if err := models.CreateWebhook(w); err != nil {
  290. ctx.Handle(500, "CreateWebhook", err)
  291. return
  292. }
  293. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  294. ctx.Redirect(orCtx.Link + "/settings/hooks")
  295. }
  296. func WebHooksEdit(ctx *middleware.Context) {
  297. ctx.Data["Title"] = ctx.Tr("repo.settings")
  298. ctx.Data["PageIsSettingsHooks"] = true
  299. ctx.Data["PageIsSettingsHooksEdit"] = true
  300. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  301. if hookId == 0 {
  302. ctx.Handle(404, "setting.WebHooksEdit", nil)
  303. return
  304. }
  305. w, err := models.GetWebhookById(hookId)
  306. if err != nil {
  307. if err == models.ErrWebhookNotExist {
  308. ctx.Handle(404, "GetWebhookById", nil)
  309. } else {
  310. ctx.Handle(500, "GetWebhookById", err)
  311. }
  312. return
  313. }
  314. // set data per HookTaskType
  315. switch w.HookTaskType {
  316. case models.SLACK:
  317. {
  318. ctx.Data["SlackHook"] = w.GetSlackHook()
  319. ctx.Data["HookType"] = "Slack"
  320. }
  321. default:
  322. {
  323. ctx.Data["HookType"] = "Gogs"
  324. }
  325. }
  326. w.GetEvent()
  327. ctx.Data["Webhook"] = w
  328. orCtx, err := getOrgRepoCtx(ctx)
  329. if err != nil {
  330. ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err)
  331. return
  332. }
  333. ctx.HTML(200, orCtx.NewTemplate)
  334. }
  335. func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  336. ctx.Data["Title"] = ctx.Tr("repo.settings")
  337. ctx.Data["PageIsSettingsHooks"] = true
  338. ctx.Data["PageIsSettingsHooksEdit"] = true
  339. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  340. if hookId == 0 {
  341. ctx.Handle(404, "setting.WebHooksEditPost", nil)
  342. return
  343. }
  344. w, err := models.GetWebhookById(hookId)
  345. if err != nil {
  346. if err == models.ErrWebhookNotExist {
  347. ctx.Handle(404, "GetWebhookById", nil)
  348. } else {
  349. ctx.Handle(500, "GetWebhookById", err)
  350. }
  351. return
  352. }
  353. w.GetEvent()
  354. ctx.Data["Webhook"] = w
  355. orCtx, err := getOrgRepoCtx(ctx)
  356. if err != nil {
  357. ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err)
  358. return
  359. }
  360. if ctx.HasError() {
  361. ctx.HTML(200, orCtx.NewTemplate)
  362. return
  363. }
  364. ct := models.JSON
  365. if form.ContentType == "2" {
  366. ct = models.FORM
  367. }
  368. w.Url = form.PayloadUrl
  369. w.ContentType = ct
  370. w.Secret = form.Secret
  371. w.HookEvent = &models.HookEvent{
  372. PushOnly: form.PushOnly,
  373. }
  374. w.IsActive = form.Active
  375. if err := w.UpdateEvent(); err != nil {
  376. ctx.Handle(500, "UpdateEvent", err)
  377. return
  378. } else if err := models.UpdateWebhook(w); err != nil {
  379. ctx.Handle(500, "WebHooksEditPost", err)
  380. return
  381. }
  382. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  383. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  384. }
  385. func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  386. ctx.Data["Title"] = ctx.Tr("repo.settings")
  387. ctx.Data["PageIsSettingsHooks"] = true
  388. ctx.Data["PageIsSettingsHooksNew"] = true
  389. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  390. orCtx, err := getOrgRepoCtx(ctx)
  391. if err != nil {
  392. ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err)
  393. return
  394. }
  395. if ctx.HasError() {
  396. ctx.HTML(200, orCtx.NewTemplate)
  397. return
  398. }
  399. meta, err := json.Marshal(&models.Slack{
  400. Channel: form.Channel,
  401. })
  402. if err != nil {
  403. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  404. return
  405. }
  406. w := &models.Webhook{
  407. RepoId: orCtx.RepoId,
  408. Url: form.PayloadUrl,
  409. ContentType: models.JSON,
  410. Secret: "",
  411. HookEvent: &models.HookEvent{
  412. PushOnly: form.PushOnly,
  413. },
  414. IsActive: form.Active,
  415. HookTaskType: models.SLACK,
  416. Meta: string(meta),
  417. OrgId: orCtx.OrgId,
  418. }
  419. if err := w.UpdateEvent(); err != nil {
  420. ctx.Handle(500, "UpdateEvent", err)
  421. return
  422. } else if err := models.CreateWebhook(w); err != nil {
  423. ctx.Handle(500, "CreateWebhook", err)
  424. return
  425. }
  426. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  427. ctx.Redirect(orCtx.Link + "/settings/hooks")
  428. }
  429. func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  430. ctx.Data["Title"] = ctx.Tr("repo.settings")
  431. ctx.Data["PageIsSettingsHooks"] = true
  432. ctx.Data["PageIsSettingsHooksEdit"] = true
  433. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  434. if hookId == 0 {
  435. ctx.Handle(404, "SlackHooksEditPost(hookId)", nil)
  436. return
  437. }
  438. orCtx, err := getOrgRepoCtx(ctx)
  439. if err != nil {
  440. ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err)
  441. return
  442. }
  443. w, err := models.GetWebhookById(hookId)
  444. if err != nil {
  445. if err == models.ErrWebhookNotExist {
  446. ctx.Handle(404, "GetWebhookById", nil)
  447. } else {
  448. ctx.Handle(500, "GetWebhookById", err)
  449. }
  450. return
  451. }
  452. w.GetEvent()
  453. ctx.Data["Webhook"] = w
  454. if ctx.HasError() {
  455. ctx.HTML(200, orCtx.NewTemplate)
  456. return
  457. }
  458. meta, err := json.Marshal(&models.Slack{
  459. Channel: form.Channel,
  460. })
  461. if err != nil {
  462. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  463. return
  464. }
  465. w.Url = form.PayloadUrl
  466. w.Meta = string(meta)
  467. w.HookEvent = &models.HookEvent{
  468. PushOnly: form.PushOnly,
  469. }
  470. w.IsActive = form.Active
  471. if err := w.UpdateEvent(); err != nil {
  472. ctx.Handle(500, "UpdateEvent", err)
  473. return
  474. } else if err := models.UpdateWebhook(w); err != nil {
  475. ctx.Handle(500, "SlackHooksEditPost", err)
  476. return
  477. }
  478. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  479. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  480. }
  481. type OrgRepoCtx struct {
  482. OrgId int64
  483. RepoId int64
  484. Link string
  485. NewTemplate base.TplName
  486. }
  487. // determines whether this is a repo context or organization context
  488. func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
  489. if _, ok := ctx.Data["RepoLink"]; ok {
  490. return &OrgRepoCtx{
  491. OrgId: int64(0),
  492. RepoId: ctx.Repo.Repository.Id,
  493. Link: ctx.Repo.RepoLink,
  494. NewTemplate: HOOK_NEW,
  495. }, nil
  496. } else if _, ok := ctx.Data["OrgLink"]; ok {
  497. return &OrgRepoCtx{
  498. OrgId: ctx.Org.Organization.Id,
  499. RepoId: int64(0),
  500. Link: ctx.Org.OrgLink,
  501. NewTemplate: ORG_HOOK_NEW,
  502. }, nil
  503. } else {
  504. return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context")
  505. }
  506. }
  507. func GitHooks(ctx *middleware.Context) {
  508. ctx.Data["Title"] = ctx.Tr("repo.settings")
  509. ctx.Data["PageIsSettingsGitHooks"] = true
  510. hooks, err := ctx.Repo.GitRepo.Hooks()
  511. if err != nil {
  512. ctx.Handle(500, "Hooks", err)
  513. return
  514. }
  515. ctx.Data["Hooks"] = hooks
  516. ctx.HTML(200, GITHOOKS)
  517. }
  518. func GitHooksEdit(ctx *middleware.Context) {
  519. ctx.Data["Title"] = ctx.Tr("repo.settings")
  520. ctx.Data["PageIsSettingsGitHooks"] = true
  521. name := ctx.Params(":name")
  522. hook, err := ctx.Repo.GitRepo.GetHook(name)
  523. if err != nil {
  524. if err == git.ErrNotValidHook {
  525. ctx.Handle(404, "GetHook", err)
  526. } else {
  527. ctx.Handle(500, "GetHook", err)
  528. }
  529. return
  530. }
  531. ctx.Data["Hook"] = hook
  532. ctx.HTML(200, GITHOOK_EDIT)
  533. }
  534. func GitHooksEditPost(ctx *middleware.Context) {
  535. name := ctx.Params(":name")
  536. hook, err := ctx.Repo.GitRepo.GetHook(name)
  537. if err != nil {
  538. if err == git.ErrNotValidHook {
  539. ctx.Handle(404, "GetHook", err)
  540. } else {
  541. ctx.Handle(500, "GetHook", err)
  542. }
  543. return
  544. }
  545. hook.Content = ctx.Query("content")
  546. if err = hook.Update(); err != nil {
  547. ctx.Handle(500, "hook.Update", err)
  548. return
  549. }
  550. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  551. }