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.

713 lines
20 KiB

9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 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. "strings"
  7. "time"
  8. "code.gitea.io/git"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. const (
  17. tplSettingsOptions base.TplName = "repo/settings/options"
  18. tplCollaboration base.TplName = "repo/settings/collaboration"
  19. tplBranches base.TplName = "repo/settings/branches"
  20. tplGithooks base.TplName = "repo/settings/githooks"
  21. tplGithookEdit base.TplName = "repo/settings/githook_edit"
  22. tplDeployKeys base.TplName = "repo/settings/deploy_keys"
  23. )
  24. // Settings show a repository's settings page
  25. func Settings(ctx *context.Context) {
  26. ctx.Data["Title"] = ctx.Tr("repo.settings")
  27. ctx.Data["PageIsSettingsOptions"] = true
  28. ctx.HTML(200, tplSettingsOptions)
  29. }
  30. // SettingsPost response for changes of a repository
  31. func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
  32. ctx.Data["Title"] = ctx.Tr("repo.settings")
  33. ctx.Data["PageIsSettingsOptions"] = true
  34. repo := ctx.Repo.Repository
  35. switch ctx.Query("action") {
  36. case "update":
  37. if ctx.HasError() {
  38. ctx.HTML(200, tplSettingsOptions)
  39. return
  40. }
  41. isNameChanged := false
  42. oldRepoName := repo.Name
  43. newRepoName := form.RepoName
  44. // Check if repository name has been changed.
  45. if repo.LowerName != strings.ToLower(newRepoName) {
  46. isNameChanged = true
  47. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  48. ctx.Data["Err_RepoName"] = true
  49. switch {
  50. case models.IsErrRepoAlreadyExist(err):
  51. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), tplSettingsOptions, &form)
  52. case models.IsErrNameReserved(err):
  53. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), tplSettingsOptions, &form)
  54. case models.IsErrNamePatternNotAllowed(err):
  55. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form)
  56. default:
  57. ctx.Handle(500, "ChangeRepositoryName", err)
  58. }
  59. return
  60. }
  61. err := models.NewRepoRedirect(ctx.Repo.Owner.ID, repo.ID, repo.Name, newRepoName)
  62. if err != nil {
  63. ctx.Handle(500, "NewRepoRedirect", err)
  64. }
  65. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  66. }
  67. // In case it's just a case change.
  68. repo.Name = newRepoName
  69. repo.LowerName = strings.ToLower(newRepoName)
  70. repo.Description = form.Description
  71. repo.Website = form.Website
  72. // Visibility of forked repository is forced sync with base repository.
  73. if repo.IsFork {
  74. form.Private = repo.BaseRepo.IsPrivate
  75. }
  76. visibilityChanged := repo.IsPrivate != form.Private
  77. repo.IsPrivate = form.Private
  78. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  79. ctx.Handle(500, "UpdateRepository", err)
  80. return
  81. }
  82. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  83. if isNameChanged {
  84. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  85. log.Error(4, "RenameRepoAction: %v", err)
  86. }
  87. }
  88. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  89. ctx.Redirect(repo.Link() + "/settings")
  90. case "mirror":
  91. if !repo.IsMirror {
  92. ctx.Handle(404, "", nil)
  93. return
  94. }
  95. if form.Interval > 0 {
  96. ctx.Repo.Mirror.EnablePrune = form.EnablePrune
  97. ctx.Repo.Mirror.Interval = form.Interval
  98. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  99. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  100. ctx.Handle(500, "UpdateMirror", err)
  101. return
  102. }
  103. }
  104. if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
  105. ctx.Handle(500, "SaveAddress", err)
  106. return
  107. }
  108. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  109. ctx.Redirect(repo.Link() + "/settings")
  110. case "mirror-sync":
  111. if !repo.IsMirror {
  112. ctx.Handle(404, "", nil)
  113. return
  114. }
  115. go models.MirrorQueue.Add(repo.ID)
  116. ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
  117. ctx.Redirect(repo.Link() + "/settings")
  118. case "advanced":
  119. var units []models.RepoUnit
  120. for _, tp := range models.MustRepoUnits {
  121. units = append(units, models.RepoUnit{
  122. RepoID: repo.ID,
  123. Type: tp,
  124. Index: int(tp),
  125. Config: new(models.UnitConfig),
  126. })
  127. }
  128. if form.EnableWiki {
  129. if form.EnableExternalWiki {
  130. units = append(units, models.RepoUnit{
  131. RepoID: repo.ID,
  132. Type: models.UnitTypeExternalWiki,
  133. Index: int(models.UnitTypeExternalWiki),
  134. Config: &models.ExternalWikiConfig{
  135. ExternalWikiURL: form.ExternalWikiURL,
  136. },
  137. })
  138. } else {
  139. units = append(units, models.RepoUnit{
  140. RepoID: repo.ID,
  141. Type: models.UnitTypeWiki,
  142. Index: int(models.UnitTypeWiki),
  143. Config: new(models.UnitConfig),
  144. })
  145. }
  146. }
  147. if form.EnableIssues {
  148. if form.EnableExternalTracker {
  149. units = append(units, models.RepoUnit{
  150. RepoID: repo.ID,
  151. Type: models.UnitTypeExternalWiki,
  152. Index: int(models.UnitTypeExternalWiki),
  153. Config: &models.ExternalTrackerConfig{
  154. ExternalTrackerURL: form.ExternalTrackerURL,
  155. ExternalTrackerFormat: form.TrackerURLFormat,
  156. ExternalTrackerStyle: form.TrackerIssueStyle,
  157. },
  158. })
  159. } else {
  160. units = append(units, models.RepoUnit{
  161. RepoID: repo.ID,
  162. Type: models.UnitTypeIssues,
  163. Index: int(models.UnitTypeIssues),
  164. Config: new(models.UnitConfig),
  165. })
  166. }
  167. }
  168. if form.EnablePulls {
  169. units = append(units, models.RepoUnit{
  170. RepoID: repo.ID,
  171. Type: models.UnitTypePullRequests,
  172. Index: int(models.UnitTypePullRequests),
  173. Config: new(models.UnitConfig),
  174. })
  175. }
  176. if err := models.UpdateRepositoryUnits(repo, units); err != nil {
  177. ctx.Handle(500, "UpdateRepositoryUnits", err)
  178. return
  179. }
  180. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  181. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  182. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  183. case "convert":
  184. if !ctx.Repo.IsOwner() {
  185. ctx.Error(404)
  186. return
  187. }
  188. if repo.Name != form.RepoName {
  189. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  190. return
  191. }
  192. if ctx.Repo.Owner.IsOrganization() {
  193. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  194. ctx.Error(404)
  195. return
  196. }
  197. }
  198. if !repo.IsMirror {
  199. ctx.Error(404)
  200. return
  201. }
  202. repo.IsMirror = false
  203. if _, err := models.CleanUpMigrateInfo(repo); err != nil {
  204. ctx.Handle(500, "CleanUpMigrateInfo", err)
  205. return
  206. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  207. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  208. return
  209. }
  210. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  211. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  212. ctx.Redirect(setting.AppSubURL + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  213. case "transfer":
  214. if !ctx.Repo.IsOwner() {
  215. ctx.Error(404)
  216. return
  217. }
  218. if repo.Name != form.RepoName {
  219. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  220. return
  221. }
  222. if ctx.Repo.Owner.IsOrganization() {
  223. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  224. ctx.Error(404)
  225. return
  226. }
  227. }
  228. newOwner := ctx.Query("new_owner_name")
  229. isExist, err := models.IsUserExist(0, newOwner)
  230. if err != nil {
  231. ctx.Handle(500, "IsUserExist", err)
  232. return
  233. } else if !isExist {
  234. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), tplSettingsOptions, nil)
  235. return
  236. }
  237. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  238. if models.IsErrRepoAlreadyExist(err) {
  239. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), tplSettingsOptions, nil)
  240. } else {
  241. ctx.Handle(500, "TransferOwnership", err)
  242. }
  243. return
  244. }
  245. log.Trace("Repository transferred: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  246. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  247. ctx.Redirect(setting.AppSubURL + "/" + newOwner + "/" + repo.Name)
  248. case "delete":
  249. if !ctx.Repo.IsOwner() {
  250. ctx.Error(404)
  251. return
  252. }
  253. if repo.Name != form.RepoName {
  254. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  255. return
  256. }
  257. if ctx.Repo.Owner.IsOrganization() {
  258. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  259. ctx.Error(404)
  260. return
  261. }
  262. }
  263. if err := models.DeleteRepository(ctx.Repo.Owner.ID, repo.ID); err != nil {
  264. ctx.Handle(500, "DeleteRepository", err)
  265. return
  266. }
  267. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  268. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  269. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  270. case "delete-wiki":
  271. if !ctx.Repo.IsOwner() {
  272. ctx.Error(404)
  273. return
  274. }
  275. if repo.Name != form.RepoName {
  276. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), tplSettingsOptions, nil)
  277. return
  278. }
  279. if ctx.Repo.Owner.IsOrganization() {
  280. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  281. ctx.Error(404)
  282. return
  283. }
  284. }
  285. repo.DeleteWiki()
  286. log.Trace("Repository wiki deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  287. ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
  288. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  289. default:
  290. ctx.Handle(404, "", nil)
  291. }
  292. }
  293. // Collaboration render a repository's collaboration page
  294. func Collaboration(ctx *context.Context) {
  295. ctx.Data["Title"] = ctx.Tr("repo.settings")
  296. ctx.Data["PageIsSettingsCollaboration"] = true
  297. users, err := ctx.Repo.Repository.GetCollaborators()
  298. if err != nil {
  299. ctx.Handle(500, "GetCollaborators", err)
  300. return
  301. }
  302. ctx.Data["Collaborators"] = users
  303. ctx.HTML(200, tplCollaboration)
  304. }
  305. // CollaborationPost response for actions for a collaboration of a repository
  306. func CollaborationPost(ctx *context.Context) {
  307. name := strings.ToLower(ctx.Query("collaborator"))
  308. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  309. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  310. return
  311. }
  312. u, err := models.GetUserByName(name)
  313. if err != nil {
  314. if models.IsErrUserNotExist(err) {
  315. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  316. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  317. } else {
  318. ctx.Handle(500, "GetUserByName", err)
  319. }
  320. return
  321. }
  322. // Organization is not allowed to be added as a collaborator.
  323. if u.IsOrganization() {
  324. ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  325. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  326. return
  327. }
  328. // Check if user is organization member.
  329. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.ID) {
  330. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  331. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  332. return
  333. }
  334. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  335. ctx.Handle(500, "AddCollaborator", err)
  336. return
  337. }
  338. if setting.Service.EnableNotifyMail {
  339. models.SendCollaboratorMail(u, ctx.User, ctx.Repo.Repository)
  340. }
  341. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  342. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  343. }
  344. // ChangeCollaborationAccessMode response for changing access of a collaboration
  345. func ChangeCollaborationAccessMode(ctx *context.Context) {
  346. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  347. ctx.QueryInt64("uid"),
  348. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  349. log.Error(4, "ChangeCollaborationAccessMode: %v", err)
  350. }
  351. }
  352. // DeleteCollaboration delete a collaboration for a repository
  353. func DeleteCollaboration(ctx *context.Context) {
  354. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  355. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  356. } else {
  357. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  358. }
  359. ctx.JSON(200, map[string]interface{}{
  360. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  361. })
  362. }
  363. // ProtectedBranch render the page to protect the repository
  364. func ProtectedBranch(ctx *context.Context) {
  365. ctx.Data["Title"] = ctx.Tr("repo.settings")
  366. ctx.Data["PageIsSettingsBranches"] = true
  367. protectedBranches, err := ctx.Repo.Repository.GetProtectedBranches()
  368. if err != nil {
  369. ctx.Handle(500, "GetProtectedBranches", err)
  370. return
  371. }
  372. ctx.Data["ProtectedBranches"] = protectedBranches
  373. branches := ctx.Data["Branches"].([]string)
  374. leftBranches := make([]string, 0, len(branches)-len(protectedBranches))
  375. for _, b := range branches {
  376. var protected bool
  377. for _, pb := range protectedBranches {
  378. if b == pb.BranchName {
  379. protected = true
  380. break
  381. }
  382. }
  383. if !protected {
  384. leftBranches = append(leftBranches, b)
  385. }
  386. }
  387. ctx.Data["LeftBranches"] = leftBranches
  388. ctx.HTML(200, tplBranches)
  389. }
  390. // ProtectedBranchPost response for protect for a branch of a repository
  391. func ProtectedBranchPost(ctx *context.Context) {
  392. ctx.Data["Title"] = ctx.Tr("repo.settings")
  393. ctx.Data["PageIsSettingsBranches"] = true
  394. repo := ctx.Repo.Repository
  395. switch ctx.Query("action") {
  396. case "default_branch":
  397. if ctx.HasError() {
  398. ctx.HTML(200, tplBranches)
  399. return
  400. }
  401. branch := strings.ToLower(ctx.Query("branch"))
  402. if ctx.Repo.GitRepo.IsBranchExist(branch) &&
  403. repo.DefaultBranch != branch {
  404. repo.DefaultBranch = branch
  405. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  406. if !git.IsErrUnsupportedVersion(err) {
  407. ctx.Handle(500, "SetDefaultBranch", err)
  408. return
  409. }
  410. }
  411. if err := repo.UpdateDefaultBranch(); err != nil {
  412. ctx.Handle(500, "SetDefaultBranch", err)
  413. return
  414. }
  415. }
  416. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  417. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  418. ctx.Redirect(setting.AppSubURL + ctx.Req.URL.Path)
  419. case "protected_branch":
  420. if ctx.HasError() {
  421. ctx.JSON(200, map[string]string{
  422. "redirect": setting.AppSubURL + ctx.Req.URL.Path,
  423. })
  424. return
  425. }
  426. branchName := strings.ToLower(ctx.Query("branchName"))
  427. if len(branchName) == 0 || !ctx.Repo.GitRepo.IsBranchExist(branchName) {
  428. ctx.JSON(200, map[string]string{
  429. "redirect": setting.AppSubURL + ctx.Req.URL.Path,
  430. })
  431. return
  432. }
  433. canPush := ctx.QueryBool("canPush")
  434. if canPush {
  435. if err := ctx.Repo.Repository.AddProtectedBranch(branchName, canPush); err != nil {
  436. ctx.Flash.Error(ctx.Tr("repo.settings.add_protected_branch_failed", branchName))
  437. ctx.JSON(200, map[string]string{
  438. "status": "ok",
  439. })
  440. return
  441. }
  442. ctx.Flash.Success(ctx.Tr("repo.settings.add_protected_branch_success", branchName))
  443. ctx.JSON(200, map[string]string{
  444. "redirect": setting.AppSubURL + ctx.Req.URL.Path,
  445. })
  446. } else {
  447. if err := ctx.Repo.Repository.DeleteProtectedBranch(ctx.QueryInt64("id")); err != nil {
  448. ctx.Flash.Error("DeleteProtectedBranch: " + err.Error())
  449. } else {
  450. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success", branchName))
  451. }
  452. ctx.JSON(200, map[string]interface{}{
  453. "status": "ok",
  454. })
  455. }
  456. default:
  457. ctx.Handle(404, "", nil)
  458. }
  459. }
  460. // ChangeProtectedBranch response for changing access of a protect branch
  461. func ChangeProtectedBranch(ctx *context.Context) {
  462. if err := ctx.Repo.Repository.ChangeProtectedBranch(
  463. ctx.QueryInt64("id"),
  464. ctx.QueryBool("canPush")); err != nil {
  465. log.Error(4, "ChangeProtectedBranch: %v", err)
  466. }
  467. }
  468. // DeleteProtectedBranch delete a protection for a branch of a repository
  469. func DeleteProtectedBranch(ctx *context.Context) {
  470. if err := ctx.Repo.Repository.DeleteProtectedBranch(ctx.QueryInt64("id")); err != nil {
  471. ctx.Flash.Error("DeleteProtectedBranch: " + err.Error())
  472. } else {
  473. ctx.Flash.Success(ctx.Tr("repo.settings.remove_protected_branch_success"))
  474. }
  475. ctx.JSON(200, map[string]interface{}{
  476. "redirect": ctx.Repo.RepoLink + "/settings/branches",
  477. })
  478. }
  479. // parseOwnerAndRepo get repos by owner
  480. func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
  481. owner, err := models.GetUserByName(ctx.Params(":username"))
  482. if err != nil {
  483. if models.IsErrUserNotExist(err) {
  484. ctx.Handle(404, "GetUserByName", err)
  485. } else {
  486. ctx.Handle(500, "GetUserByName", err)
  487. }
  488. return nil, nil
  489. }
  490. repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame"))
  491. if err != nil {
  492. if models.IsErrRepoNotExist(err) {
  493. ctx.Handle(404, "GetRepositoryByName", err)
  494. } else {
  495. ctx.Handle(500, "GetRepositoryByName", err)
  496. }
  497. return nil, nil
  498. }
  499. return owner, repo
  500. }
  501. // GitHooks hooks of a repository
  502. func GitHooks(ctx *context.Context) {
  503. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  504. ctx.Data["PageIsSettingsGitHooks"] = true
  505. hooks, err := ctx.Repo.GitRepo.Hooks()
  506. if err != nil {
  507. ctx.Handle(500, "Hooks", err)
  508. return
  509. }
  510. ctx.Data["Hooks"] = hooks
  511. ctx.HTML(200, tplGithooks)
  512. }
  513. // GitHooksEdit render for editing a hook of repository page
  514. func GitHooksEdit(ctx *context.Context) {
  515. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  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, tplGithookEdit)
  529. }
  530. // GitHooksEditPost response for editing a git hook of a repository
  531. func GitHooksEditPost(ctx *context.Context) {
  532. name := ctx.Params(":name")
  533. hook, err := ctx.Repo.GitRepo.GetHook(name)
  534. if err != nil {
  535. if err == git.ErrNotValidHook {
  536. ctx.Handle(404, "GetHook", err)
  537. } else {
  538. ctx.Handle(500, "GetHook", err)
  539. }
  540. return
  541. }
  542. hook.Content = ctx.Query("content")
  543. if err = hook.Update(); err != nil {
  544. ctx.Handle(500, "hook.Update", err)
  545. return
  546. }
  547. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  548. }
  549. // DeployKeys render the deploy keys list of a repository page
  550. func DeployKeys(ctx *context.Context) {
  551. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  552. ctx.Data["PageIsSettingsKeys"] = true
  553. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  554. if err != nil {
  555. ctx.Handle(500, "ListDeployKeys", err)
  556. return
  557. }
  558. ctx.Data["Deploykeys"] = keys
  559. ctx.HTML(200, tplDeployKeys)
  560. }
  561. // DeployKeysPost response for adding a deploy key of a repository
  562. func DeployKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  563. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  564. ctx.Data["PageIsSettingsKeys"] = true
  565. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  566. if err != nil {
  567. ctx.Handle(500, "ListDeployKeys", err)
  568. return
  569. }
  570. ctx.Data["Deploykeys"] = keys
  571. if ctx.HasError() {
  572. ctx.HTML(200, tplDeployKeys)
  573. return
  574. }
  575. content, err := models.CheckPublicKeyString(form.Content)
  576. if err != nil {
  577. if models.IsErrKeyUnableVerify(err) {
  578. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  579. } else {
  580. ctx.Data["HasError"] = true
  581. ctx.Data["Err_Content"] = true
  582. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  583. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  584. return
  585. }
  586. }
  587. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  588. if err != nil {
  589. ctx.Data["HasError"] = true
  590. switch {
  591. case models.IsErrKeyAlreadyExist(err):
  592. ctx.Data["Err_Content"] = true
  593. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), tplDeployKeys, &form)
  594. case models.IsErrKeyNameAlreadyUsed(err):
  595. ctx.Data["Err_Title"] = true
  596. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), tplDeployKeys, &form)
  597. default:
  598. ctx.Handle(500, "AddDeployKey", err)
  599. }
  600. return
  601. }
  602. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  603. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  604. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  605. }
  606. // DeleteDeployKey response for deleting a deploy key
  607. func DeleteDeployKey(ctx *context.Context) {
  608. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  609. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  610. } else {
  611. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  612. }
  613. ctx.JSON(200, map[string]interface{}{
  614. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  615. })
  616. }