setting.go 17 KB

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