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.

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