setting.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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. "fmt"
  7. "strings"
  8. "time"
  9. log "gopkg.in/clog.v1"
  10. "github.com/gogits/git-module"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/context"
  15. "github.com/gogits/gogs/modules/mailer"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. const (
  19. SETTINGS_OPTIONS base.TplName = "repo/settings/options"
  20. SETTINGS_COLLABORATION base.TplName = "repo/settings/collaboration"
  21. SETTINGS_BRANCHES base.TplName = "repo/settings/branches"
  22. SETTINGS_PROTECTED_BRANCH base.TplName = "repo/settings/protected_branch"
  23. SETTINGS_GITHOOKS base.TplName = "repo/settings/githooks"
  24. SETTINGS_GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  25. SETTINGS_DEPLOY_KEYS base.TplName = "repo/settings/deploy_keys"
  26. )
  27. func Settings(ctx *context.Context) {
  28. ctx.Data["Title"] = ctx.Tr("repo.settings")
  29. ctx.Data["PageIsSettingsOptions"] = true
  30. ctx.HTML(200, SETTINGS_OPTIONS)
  31. }
  32. func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
  33. ctx.Data["Title"] = ctx.Tr("repo.settings")
  34. ctx.Data["PageIsSettingsOptions"] = true
  35. repo := ctx.Repo.Repository
  36. switch ctx.Query("action") {
  37. case "update":
  38. if ctx.HasError() {
  39. ctx.HTML(200, SETTINGS_OPTIONS)
  40. return
  41. }
  42. isNameChanged := false
  43. oldRepoName := repo.Name
  44. newRepoName := form.RepoName
  45. // Check if repository name has been changed.
  46. if repo.LowerName != strings.ToLower(newRepoName) {
  47. isNameChanged = true
  48. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  49. ctx.Data["Err_RepoName"] = true
  50. switch {
  51. case models.IsErrRepoAlreadyExist(err):
  52. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form)
  53. case models.IsErrNameReserved(err):
  54. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &form)
  55. case models.IsErrNamePatternNotAllowed(err):
  56. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &form)
  57. default:
  58. ctx.Handle(500, "ChangeRepositoryName", err)
  59. }
  60. return
  61. }
  62. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  63. }
  64. // In case it's just a case change.
  65. repo.Name = newRepoName
  66. repo.LowerName = strings.ToLower(newRepoName)
  67. repo.Description = form.Description
  68. repo.Website = form.Website
  69. // Visibility of forked repository is forced sync with base repository.
  70. if repo.IsFork {
  71. form.Private = repo.BaseRepo.IsPrivate
  72. }
  73. visibilityChanged := repo.IsPrivate != form.Private
  74. repo.IsPrivate = form.Private
  75. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  76. ctx.Handle(500, "UpdateRepository", err)
  77. return
  78. }
  79. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  80. if isNameChanged {
  81. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  82. log.Error(4, "RenameRepoAction: %v", err)
  83. }
  84. }
  85. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  86. ctx.Redirect(repo.Link() + "/settings")
  87. case "mirror":
  88. if !repo.IsMirror {
  89. ctx.Handle(404, "", nil)
  90. return
  91. }
  92. if form.Interval > 0 {
  93. ctx.Repo.Mirror.EnablePrune = form.EnablePrune
  94. ctx.Repo.Mirror.Interval = form.Interval
  95. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  96. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  97. ctx.Handle(500, "UpdateMirror", err)
  98. return
  99. }
  100. }
  101. if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
  102. ctx.Handle(500, "SaveAddress", err)
  103. return
  104. }
  105. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  106. ctx.Redirect(repo.Link() + "/settings")
  107. case "mirror-sync":
  108. if !repo.IsMirror {
  109. ctx.Handle(404, "", nil)
  110. return
  111. }
  112. go models.MirrorQueue.Add(repo.ID)
  113. ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress"))
  114. ctx.Redirect(repo.Link() + "/settings")
  115. case "advanced":
  116. repo.EnableWiki = form.EnableWiki
  117. repo.EnableExternalWiki = form.EnableExternalWiki
  118. repo.ExternalWikiURL = form.ExternalWikiURL
  119. repo.EnableIssues = form.EnableIssues
  120. repo.EnableExternalTracker = form.EnableExternalTracker
  121. repo.ExternalTrackerURL = form.ExternalTrackerURL
  122. repo.ExternalTrackerFormat = form.TrackerURLFormat
  123. repo.ExternalTrackerStyle = form.TrackerIssueStyle
  124. repo.EnablePulls = form.EnablePulls
  125. if err := models.UpdateRepository(repo, false); err != nil {
  126. ctx.Handle(500, "UpdateRepository", err)
  127. return
  128. }
  129. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  130. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  131. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  132. case "convert":
  133. if !ctx.Repo.IsOwner() {
  134. ctx.Error(404)
  135. return
  136. }
  137. if repo.Name != form.RepoName {
  138. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  139. return
  140. }
  141. if ctx.Repo.Owner.IsOrganization() {
  142. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  143. ctx.Error(404)
  144. return
  145. }
  146. }
  147. if !repo.IsMirror {
  148. ctx.Error(404)
  149. return
  150. }
  151. repo.IsMirror = false
  152. if _, err := models.CleanUpMigrateInfo(repo); err != nil {
  153. ctx.Handle(500, "CleanUpMigrateInfo", err)
  154. return
  155. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  156. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  157. return
  158. }
  159. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  160. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  161. ctx.Redirect(setting.AppSubUrl + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  162. case "transfer":
  163. if !ctx.Repo.IsOwner() {
  164. ctx.Error(404)
  165. return
  166. }
  167. if repo.Name != form.RepoName {
  168. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  169. return
  170. }
  171. if ctx.Repo.Owner.IsOrganization() {
  172. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  173. ctx.Error(404)
  174. return
  175. }
  176. }
  177. newOwner := ctx.Query("new_owner_name")
  178. isExist, err := models.IsUserExist(0, newOwner)
  179. if err != nil {
  180. ctx.Handle(500, "IsUserExist", err)
  181. return
  182. } else if !isExist {
  183. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  184. return
  185. }
  186. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  187. if models.IsErrRepoAlreadyExist(err) {
  188. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  189. } else {
  190. ctx.Handle(500, "TransferOwnership", err)
  191. }
  192. return
  193. }
  194. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  195. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  196. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  197. case "delete":
  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"), SETTINGS_OPTIONS, 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 err := models.DeleteRepository(ctx.Repo.Owner.ID, repo.ID); err != nil {
  213. ctx.Handle(500, "DeleteRepository", err)
  214. return
  215. }
  216. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  217. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  218. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  219. case "delete-wiki":
  220. if !ctx.Repo.IsOwner() {
  221. ctx.Error(404)
  222. return
  223. }
  224. if repo.Name != form.RepoName {
  225. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  226. return
  227. }
  228. if ctx.Repo.Owner.IsOrganization() {
  229. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.ID) {
  230. ctx.Error(404)
  231. return
  232. }
  233. }
  234. repo.DeleteWiki()
  235. log.Trace("Repository wiki deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  236. repo.EnableWiki = false
  237. if err := models.UpdateRepository(repo, false); err != nil {
  238. ctx.Handle(500, "UpdateRepository", err)
  239. return
  240. }
  241. ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
  242. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  243. default:
  244. ctx.Handle(404, "", nil)
  245. }
  246. }
  247. func SettingsCollaboration(ctx *context.Context) {
  248. ctx.Data["Title"] = ctx.Tr("repo.settings")
  249. ctx.Data["PageIsSettingsCollaboration"] = true
  250. users, err := ctx.Repo.Repository.GetCollaborators()
  251. if err != nil {
  252. ctx.Handle(500, "GetCollaborators", err)
  253. return
  254. }
  255. ctx.Data["Collaborators"] = users
  256. ctx.HTML(200, SETTINGS_COLLABORATION)
  257. }
  258. func SettingsCollaborationPost(ctx *context.Context) {
  259. name := strings.ToLower(ctx.Query("collaborator"))
  260. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  261. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  262. return
  263. }
  264. u, err := models.GetUserByName(name)
  265. if err != nil {
  266. if models.IsErrUserNotExist(err) {
  267. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  268. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  269. } else {
  270. ctx.Handle(500, "GetUserByName", err)
  271. }
  272. return
  273. }
  274. // Organization is not allowed to be added as a collaborator.
  275. if u.IsOrganization() {
  276. ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  277. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  278. return
  279. }
  280. // Check if user is organization member.
  281. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.ID) {
  282. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  283. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  284. return
  285. }
  286. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  287. ctx.Handle(500, "AddCollaborator", err)
  288. return
  289. }
  290. if setting.Service.EnableNotifyMail {
  291. mailer.SendCollaboratorMail(models.NewMailerUser(u), models.NewMailerUser(ctx.User), models.NewMailerRepo(ctx.Repo.Repository))
  292. }
  293. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  294. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  295. }
  296. func ChangeCollaborationAccessMode(ctx *context.Context) {
  297. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  298. ctx.QueryInt64("uid"),
  299. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  300. log.Error(2, "ChangeCollaborationAccessMode: %v", err)
  301. return
  302. }
  303. ctx.Status(204)
  304. }
  305. func DeleteCollaboration(ctx *context.Context) {
  306. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  307. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  308. } else {
  309. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  310. }
  311. ctx.JSON(200, map[string]interface{}{
  312. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  313. })
  314. }
  315. func SettingsBranches(ctx *context.Context) {
  316. ctx.Data["Title"] = ctx.Tr("repo.settings.branches")
  317. ctx.Data["PageIsSettingsBranches"] = true
  318. protectBranches, err := models.GetProtectBranchesByRepoID(ctx.Repo.Repository.ID)
  319. if err != nil {
  320. ctx.Handle(500, "GetProtectBranchesByRepoID", err)
  321. return
  322. }
  323. ctx.Data["ProtectBranches"] = protectBranches
  324. ctx.HTML(200, SETTINGS_BRANCHES)
  325. }
  326. func UpdateDefaultBranch(ctx *context.Context) {
  327. branch := ctx.Query("branch")
  328. if ctx.Repo.GitRepo.IsBranchExist(branch) &&
  329. ctx.Repo.Repository.DefaultBranch != branch {
  330. ctx.Repo.Repository.DefaultBranch = branch
  331. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  332. if !git.IsErrUnsupportedVersion(err) {
  333. ctx.Handle(500, "SetDefaultBranch", err)
  334. return
  335. }
  336. }
  337. }
  338. if err := models.UpdateRepository(ctx.Repo.Repository, false); err != nil {
  339. ctx.Handle(500, "UpdateRepository", err)
  340. return
  341. }
  342. ctx.Flash.Success(ctx.Tr("repo.settings.update_default_branch_success"))
  343. ctx.Redirect(ctx.Repo.RepoLink + "/settings/branches")
  344. }
  345. func SettingsProtectedBranch(ctx *context.Context) {
  346. branch := ctx.Params("*")
  347. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  348. ctx.NotFound()
  349. return
  350. }
  351. ctx.Data["Title"] = ctx.Tr("repo.settings.protected_branches") + " - " + branch
  352. ctx.Data["PageIsSettingsBranches"] = true
  353. ctx.Data["IsOrgRepo"] = ctx.Repo.Owner.IsOrganization()
  354. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  355. if err != nil {
  356. if !models.IsErrBranchNotExist(err) {
  357. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  358. return
  359. }
  360. // No options found, create defaults.
  361. protectBranch = &models.ProtectBranch{
  362. Name: branch,
  363. }
  364. }
  365. ctx.Data["Branch"] = protectBranch
  366. ctx.HTML(200, SETTINGS_PROTECTED_BRANCH)
  367. }
  368. func SettingsProtectedBranchPost(ctx *context.Context, form auth.ProtectBranchForm) {
  369. branch := ctx.Params("*")
  370. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  371. ctx.NotFound()
  372. return
  373. }
  374. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  375. if err != nil {
  376. if !models.IsErrBranchNotExist(err) {
  377. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  378. return
  379. }
  380. // No options found, create defaults.
  381. protectBranch = &models.ProtectBranch{
  382. RepoID: ctx.Repo.Repository.ID,
  383. Name: branch,
  384. }
  385. }
  386. protectBranch.Protected = form.Protected
  387. protectBranch.RequirePullRequest = form.RequirePullRequest
  388. if err = models.UpdateProtectBranch(protectBranch); err != nil {
  389. ctx.Handle(500, "UpdateProtectBranch", err)
  390. return
  391. }
  392. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success"))
  393. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  394. }
  395. func SettingsGitHooks(ctx *context.Context) {
  396. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  397. ctx.Data["PageIsSettingsGitHooks"] = true
  398. hooks, err := ctx.Repo.GitRepo.Hooks()
  399. if err != nil {
  400. ctx.Handle(500, "Hooks", err)
  401. return
  402. }
  403. ctx.Data["Hooks"] = hooks
  404. ctx.HTML(200, SETTINGS_GITHOOKS)
  405. }
  406. func SettingsGitHooksEdit(ctx *context.Context) {
  407. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  408. ctx.Data["PageIsSettingsGitHooks"] = true
  409. name := ctx.Params(":name")
  410. hook, err := ctx.Repo.GitRepo.GetHook(name)
  411. if err != nil {
  412. if err == git.ErrNotValidHook {
  413. ctx.Handle(404, "GetHook", err)
  414. } else {
  415. ctx.Handle(500, "GetHook", err)
  416. }
  417. return
  418. }
  419. ctx.Data["Hook"] = hook
  420. ctx.HTML(200, SETTINGS_GITHOOK_EDIT)
  421. }
  422. func SettingsGitHooksEditPost(ctx *context.Context) {
  423. name := ctx.Params(":name")
  424. hook, err := ctx.Repo.GitRepo.GetHook(name)
  425. if err != nil {
  426. if err == git.ErrNotValidHook {
  427. ctx.Handle(404, "GetHook", err)
  428. } else {
  429. ctx.Handle(500, "GetHook", err)
  430. }
  431. return
  432. }
  433. hook.Content = ctx.Query("content")
  434. if err = hook.Update(); err != nil {
  435. ctx.Handle(500, "hook.Update", err)
  436. return
  437. }
  438. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  439. }
  440. func SettingsDeployKeys(ctx *context.Context) {
  441. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  442. ctx.Data["PageIsSettingsKeys"] = true
  443. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  444. if err != nil {
  445. ctx.Handle(500, "ListDeployKeys", err)
  446. return
  447. }
  448. ctx.Data["Deploykeys"] = keys
  449. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  450. }
  451. func SettingsDeployKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  452. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  453. ctx.Data["PageIsSettingsKeys"] = true
  454. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  455. if err != nil {
  456. ctx.Handle(500, "ListDeployKeys", err)
  457. return
  458. }
  459. ctx.Data["Deploykeys"] = keys
  460. if ctx.HasError() {
  461. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  462. return
  463. }
  464. content, err := models.CheckPublicKeyString(form.Content)
  465. if err != nil {
  466. if models.IsErrKeyUnableVerify(err) {
  467. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  468. } else {
  469. ctx.Data["HasError"] = true
  470. ctx.Data["Err_Content"] = true
  471. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  472. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  473. return
  474. }
  475. }
  476. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  477. if err != nil {
  478. ctx.Data["HasError"] = true
  479. switch {
  480. case models.IsErrKeyAlreadyExist(err):
  481. ctx.Data["Err_Content"] = true
  482. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &form)
  483. case models.IsErrKeyNameAlreadyUsed(err):
  484. ctx.Data["Err_Title"] = true
  485. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &form)
  486. default:
  487. ctx.Handle(500, "AddDeployKey", err)
  488. }
  489. return
  490. }
  491. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  492. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  493. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  494. }
  495. func DeleteDeployKey(ctx *context.Context) {
  496. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  497. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  498. } else {
  499. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  500. }
  501. ctx.JSON(200, map[string]interface{}{
  502. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  503. })
  504. }