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.

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