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.

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