setting.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 WebHooksNew(ctx *middleware.Context) {
  238. ctx.Data["Title"] = ctx.Tr("repo.settings")
  239. ctx.Data["PageIsSettingsHooks"] = true
  240. ctx.Data["PageIsSettingsHooksNew"] = true
  241. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  242. ctx.Data["HookType"] = "Gogs"
  243. orCtx, err := getOrgRepoCtx(ctx)
  244. if err != nil {
  245. ctx.Handle(500, "WebHooksNew(getOrgRepoCtx)", err)
  246. return
  247. }
  248. ctx.HTML(200, orCtx.NewTemplate)
  249. }
  250. func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  251. ctx.Data["Title"] = ctx.Tr("repo.settings")
  252. ctx.Data["PageIsSettingsHooks"] = true
  253. ctx.Data["PageIsSettingsHooksNew"] = true
  254. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  255. orCtx, err := getOrgRepoCtx(ctx)
  256. if err != nil {
  257. ctx.Handle(500, "WebHooksNewPost(getOrgRepoCtx)", err)
  258. return
  259. }
  260. if ctx.HasError() {
  261. ctx.HTML(200, orCtx.NewTemplate)
  262. return
  263. }
  264. // FIXME: code too old here, sync with APIs
  265. ct := models.JSON
  266. if form.ContentType == "2" {
  267. ct = models.FORM
  268. }
  269. w := &models.Webhook{
  270. RepoId: orCtx.RepoId,
  271. Url: form.PayloadUrl,
  272. ContentType: ct,
  273. Secret: form.Secret,
  274. HookEvent: &models.HookEvent{
  275. PushOnly: form.PushOnly,
  276. },
  277. IsActive: form.Active,
  278. HookTaskType: models.GOGS,
  279. Meta: "",
  280. OrgId: orCtx.OrgId,
  281. }
  282. if err := w.UpdateEvent(); err != nil {
  283. ctx.Handle(500, "UpdateEvent", err)
  284. return
  285. } else if err := models.CreateWebhook(w); err != nil {
  286. ctx.Handle(500, "CreateWebhook", err)
  287. return
  288. }
  289. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  290. ctx.Redirect(orCtx.Link + "/settings/hooks")
  291. }
  292. func WebHooksEdit(ctx *middleware.Context) {
  293. ctx.Data["Title"] = ctx.Tr("repo.settings")
  294. ctx.Data["PageIsSettingsHooks"] = true
  295. ctx.Data["PageIsSettingsHooksEdit"] = true
  296. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  297. if hookId == 0 {
  298. ctx.Handle(404, "setting.WebHooksEdit", nil)
  299. return
  300. }
  301. w, err := models.GetWebhookById(hookId)
  302. if err != nil {
  303. if err == models.ErrWebhookNotExist {
  304. ctx.Handle(404, "GetWebhookById", nil)
  305. } else {
  306. ctx.Handle(500, "GetWebhookById", err)
  307. }
  308. return
  309. }
  310. // set data per HookTaskType
  311. switch w.HookTaskType {
  312. case models.SLACK:
  313. {
  314. ctx.Data["SlackHook"] = w.GetSlackHook()
  315. ctx.Data["HookType"] = "Slack"
  316. }
  317. default:
  318. {
  319. ctx.Data["HookType"] = "Gogs"
  320. }
  321. }
  322. w.GetEvent()
  323. ctx.Data["Webhook"] = w
  324. orCtx, err := getOrgRepoCtx(ctx)
  325. if err != nil {
  326. ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err)
  327. return
  328. }
  329. ctx.HTML(200, orCtx.NewTemplate)
  330. }
  331. func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
  332. ctx.Data["Title"] = ctx.Tr("repo.settings")
  333. ctx.Data["PageIsSettingsHooks"] = true
  334. ctx.Data["PageIsSettingsHooksEdit"] = true
  335. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  336. if hookId == 0 {
  337. ctx.Handle(404, "setting.WebHooksEditPost", nil)
  338. return
  339. }
  340. w, err := models.GetWebhookById(hookId)
  341. if err != nil {
  342. if err == models.ErrWebhookNotExist {
  343. ctx.Handle(404, "GetWebhookById", nil)
  344. } else {
  345. ctx.Handle(500, "GetWebhookById", err)
  346. }
  347. return
  348. }
  349. w.GetEvent()
  350. ctx.Data["Webhook"] = w
  351. orCtx, err := getOrgRepoCtx(ctx)
  352. if err != nil {
  353. ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err)
  354. return
  355. }
  356. if ctx.HasError() {
  357. ctx.HTML(200, orCtx.NewTemplate)
  358. return
  359. }
  360. ct := models.JSON
  361. if form.ContentType == "2" {
  362. ct = models.FORM
  363. }
  364. w.Url = form.PayloadUrl
  365. w.ContentType = ct
  366. w.Secret = form.Secret
  367. w.HookEvent = &models.HookEvent{
  368. PushOnly: form.PushOnly,
  369. }
  370. w.IsActive = form.Active
  371. if err := w.UpdateEvent(); err != nil {
  372. ctx.Handle(500, "UpdateEvent", err)
  373. return
  374. } else if err := models.UpdateWebhook(w); err != nil {
  375. ctx.Handle(500, "WebHooksEditPost", err)
  376. return
  377. }
  378. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  379. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  380. }
  381. func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  382. ctx.Data["Title"] = ctx.Tr("repo.settings")
  383. ctx.Data["PageIsSettingsHooks"] = true
  384. ctx.Data["PageIsSettingsHooksNew"] = true
  385. ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
  386. orCtx, err := getOrgRepoCtx(ctx)
  387. if err != nil {
  388. ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err)
  389. return
  390. }
  391. if ctx.HasError() {
  392. ctx.HTML(200, orCtx.NewTemplate)
  393. return
  394. }
  395. meta, err := json.Marshal(&models.Slack{
  396. Channel: form.Channel,
  397. })
  398. if err != nil {
  399. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  400. return
  401. }
  402. w := &models.Webhook{
  403. RepoId: orCtx.RepoId,
  404. Url: form.PayloadUrl,
  405. ContentType: models.JSON,
  406. Secret: "",
  407. HookEvent: &models.HookEvent{
  408. PushOnly: form.PushOnly,
  409. },
  410. IsActive: form.Active,
  411. HookTaskType: models.SLACK,
  412. Meta: string(meta),
  413. OrgId: orCtx.OrgId,
  414. }
  415. if err := w.UpdateEvent(); err != nil {
  416. ctx.Handle(500, "UpdateEvent", err)
  417. return
  418. } else if err := models.CreateWebhook(w); err != nil {
  419. ctx.Handle(500, "CreateWebhook", err)
  420. return
  421. }
  422. ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
  423. ctx.Redirect(orCtx.Link + "/settings/hooks")
  424. }
  425. func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
  426. ctx.Data["Title"] = ctx.Tr("repo.settings")
  427. ctx.Data["PageIsSettingsHooks"] = true
  428. ctx.Data["PageIsSettingsHooksEdit"] = true
  429. hookId := com.StrTo(ctx.Params(":id")).MustInt64()
  430. if hookId == 0 {
  431. ctx.Handle(404, "SlackHooksEditPost(hookId)", nil)
  432. return
  433. }
  434. orCtx, err := getOrgRepoCtx(ctx)
  435. if err != nil {
  436. ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err)
  437. return
  438. }
  439. w, err := models.GetWebhookById(hookId)
  440. if err != nil {
  441. if err == models.ErrWebhookNotExist {
  442. ctx.Handle(404, "GetWebhookById", nil)
  443. } else {
  444. ctx.Handle(500, "GetWebhookById", err)
  445. }
  446. return
  447. }
  448. w.GetEvent()
  449. ctx.Data["Webhook"] = w
  450. if ctx.HasError() {
  451. ctx.HTML(200, orCtx.NewTemplate)
  452. return
  453. }
  454. meta, err := json.Marshal(&models.Slack{
  455. Channel: form.Channel,
  456. })
  457. if err != nil {
  458. ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err)
  459. return
  460. }
  461. w.Url = form.PayloadUrl
  462. w.Meta = string(meta)
  463. w.HookEvent = &models.HookEvent{
  464. PushOnly: form.PushOnly,
  465. }
  466. w.IsActive = form.Active
  467. if err := w.UpdateEvent(); err != nil {
  468. ctx.Handle(500, "UpdateEvent", err)
  469. return
  470. } else if err := models.UpdateWebhook(w); err != nil {
  471. ctx.Handle(500, "SlackHooksEditPost", err)
  472. return
  473. }
  474. ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
  475. ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
  476. }
  477. type OrgRepoCtx struct {
  478. OrgId int64
  479. RepoId int64
  480. Link string
  481. NewTemplate base.TplName
  482. }
  483. // determines whether this is a repo context or organization context
  484. func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
  485. if _, ok := ctx.Data["RepoLink"]; ok {
  486. return &OrgRepoCtx{
  487. OrgId: int64(0),
  488. RepoId: ctx.Repo.Repository.Id,
  489. Link: ctx.Repo.RepoLink,
  490. NewTemplate: HOOK_NEW,
  491. }, nil
  492. } else if _, ok := ctx.Data["OrgLink"]; ok {
  493. return &OrgRepoCtx{
  494. OrgId: ctx.Org.Organization.Id,
  495. RepoId: int64(0),
  496. Link: ctx.Org.OrgLink,
  497. NewTemplate: ORG_HOOK_NEW,
  498. }, nil
  499. } else {
  500. return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context")
  501. }
  502. }
  503. func GitHooks(ctx *middleware.Context) {
  504. ctx.Data["Title"] = ctx.Tr("repo.settings")
  505. ctx.Data["PageIsSettingsGitHooks"] = true
  506. hooks, err := ctx.Repo.GitRepo.Hooks()
  507. if err != nil {
  508. ctx.Handle(500, "Hooks", err)
  509. return
  510. }
  511. ctx.Data["Hooks"] = hooks
  512. ctx.HTML(200, GITHOOKS)
  513. }
  514. func GitHooksEdit(ctx *middleware.Context) {
  515. ctx.Data["Title"] = ctx.Tr("repo.settings")
  516. ctx.Data["PageIsSettingsGitHooks"] = true
  517. name := ctx.Params(":name")
  518. hook, err := ctx.Repo.GitRepo.GetHook(name)
  519. if err != nil {
  520. if err == git.ErrNotValidHook {
  521. ctx.Handle(404, "GetHook", err)
  522. } else {
  523. ctx.Handle(500, "GetHook", err)
  524. }
  525. return
  526. }
  527. ctx.Data["Hook"] = hook
  528. ctx.HTML(200, GITHOOK_EDIT)
  529. }
  530. func GitHooksEditPost(ctx *middleware.Context) {
  531. name := ctx.Params(":name")
  532. hook, err := ctx.Repo.GitRepo.GetHook(name)
  533. if err != nil {
  534. if err == git.ErrNotValidHook {
  535. ctx.Handle(404, "GetHook", err)
  536. } else {
  537. ctx.Handle(500, "GetHook", err)
  538. }
  539. return
  540. }
  541. hook.Content = ctx.Query("content")
  542. if err = hook.Update(); err != nil {
  543. ctx.Handle(500, "hook.Update", err)
  544. return
  545. }
  546. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  547. }