setting.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. repo.PullsAllowRebase = f.PullsAllowRebase
  128. if err := models.UpdateRepository(repo, false); err != nil {
  129. c.ServerError("UpdateRepository", err)
  130. return
  131. }
  132. log.Trace("Repository advanced settings updated: %s/%s", c.Repo.Owner.Name, repo.Name)
  133. c.Flash.Success(c.Tr("repo.settings.update_settings_success"))
  134. c.Redirect(c.Repo.RepoLink + "/settings")
  135. case "convert":
  136. if !c.Repo.IsOwner() {
  137. c.NotFound()
  138. return
  139. }
  140. if repo.Name != f.RepoName {
  141. c.RenderWithErr(c.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  142. return
  143. }
  144. if c.Repo.Owner.IsOrganization() {
  145. if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
  146. c.NotFound()
  147. return
  148. }
  149. }
  150. if !repo.IsMirror {
  151. c.NotFound()
  152. return
  153. }
  154. repo.IsMirror = false
  155. if _, err := models.CleanUpMigrateInfo(repo); err != nil {
  156. c.ServerError("CleanUpMigrateInfo", err)
  157. return
  158. } else if err = models.DeleteMirrorByRepoID(c.Repo.Repository.ID); err != nil {
  159. c.ServerError("DeleteMirrorByRepoID", err)
  160. return
  161. }
  162. log.Trace("Repository converted from mirror to regular: %s/%s", c.Repo.Owner.Name, repo.Name)
  163. c.Flash.Success(c.Tr("repo.settings.convert_succeed"))
  164. c.Redirect(setting.AppSubURL + "/" + c.Repo.Owner.Name + "/" + repo.Name)
  165. case "transfer":
  166. if !c.Repo.IsOwner() {
  167. c.NotFound()
  168. return
  169. }
  170. if repo.Name != f.RepoName {
  171. c.RenderWithErr(c.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  172. return
  173. }
  174. if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin {
  175. if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
  176. c.NotFound()
  177. return
  178. }
  179. }
  180. newOwner := c.Query("new_owner_name")
  181. isExist, err := models.IsUserExist(0, newOwner)
  182. if err != nil {
  183. c.ServerError("IsUserExist", err)
  184. return
  185. } else if !isExist {
  186. c.RenderWithErr(c.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  187. return
  188. }
  189. if err = models.TransferOwnership(c.User, newOwner, repo); err != nil {
  190. if models.IsErrRepoAlreadyExist(err) {
  191. c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  192. } else {
  193. c.ServerError("TransferOwnership", err)
  194. }
  195. return
  196. }
  197. log.Trace("Repository transfered: %s/%s -> %s", c.Repo.Owner.Name, repo.Name, newOwner)
  198. c.Flash.Success(c.Tr("repo.settings.transfer_succeed"))
  199. c.Redirect(setting.AppSubURL + "/" + newOwner + "/" + repo.Name)
  200. case "delete":
  201. if !c.Repo.IsOwner() {
  202. c.NotFound()
  203. return
  204. }
  205. if repo.Name != f.RepoName {
  206. c.RenderWithErr(c.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  207. return
  208. }
  209. if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin {
  210. if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
  211. c.NotFound()
  212. return
  213. }
  214. }
  215. if err := models.DeleteRepository(c.Repo.Owner.ID, repo.ID); err != nil {
  216. c.ServerError("DeleteRepository", err)
  217. return
  218. }
  219. log.Trace("Repository deleted: %s/%s", c.Repo.Owner.Name, repo.Name)
  220. c.Flash.Success(c.Tr("repo.settings.deletion_success"))
  221. c.Redirect(c.Repo.Owner.DashboardLink())
  222. case "delete-wiki":
  223. if !c.Repo.IsOwner() {
  224. c.NotFound()
  225. return
  226. }
  227. if repo.Name != f.RepoName {
  228. c.RenderWithErr(c.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  229. return
  230. }
  231. if c.Repo.Owner.IsOrganization() && !c.User.IsAdmin {
  232. if !c.Repo.Owner.IsOwnedBy(c.User.ID) {
  233. c.NotFound()
  234. return
  235. }
  236. }
  237. repo.DeleteWiki()
  238. log.Trace("Repository wiki deleted: %s/%s", c.Repo.Owner.Name, repo.Name)
  239. repo.EnableWiki = false
  240. if err := models.UpdateRepository(repo, false); err != nil {
  241. c.ServerError("UpdateRepository", err)
  242. return
  243. }
  244. c.Flash.Success(c.Tr("repo.settings.wiki_deletion_success"))
  245. c.Redirect(c.Repo.RepoLink + "/settings")
  246. default:
  247. c.NotFound()
  248. }
  249. }
  250. func SettingsCollaboration(c *context.Context) {
  251. c.Data["Title"] = c.Tr("repo.settings")
  252. c.Data["PageIsSettingsCollaboration"] = true
  253. users, err := c.Repo.Repository.GetCollaborators()
  254. if err != nil {
  255. c.Handle(500, "GetCollaborators", err)
  256. return
  257. }
  258. c.Data["Collaborators"] = users
  259. c.HTML(200, SETTINGS_COLLABORATION)
  260. }
  261. func SettingsCollaborationPost(c *context.Context) {
  262. name := strings.ToLower(c.Query("collaborator"))
  263. if len(name) == 0 || c.Repo.Owner.LowerName == name {
  264. c.Redirect(setting.AppSubURL + c.Req.URL.Path)
  265. return
  266. }
  267. u, err := models.GetUserByName(name)
  268. if err != nil {
  269. if errors.IsUserNotExist(err) {
  270. c.Flash.Error(c.Tr("form.user_not_exist"))
  271. c.Redirect(setting.AppSubURL + c.Req.URL.Path)
  272. } else {
  273. c.Handle(500, "GetUserByName", err)
  274. }
  275. return
  276. }
  277. // Organization is not allowed to be added as a collaborator
  278. if u.IsOrganization() {
  279. c.Flash.Error(c.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  280. c.Redirect(setting.AppSubURL + c.Req.URL.Path)
  281. return
  282. }
  283. if err = c.Repo.Repository.AddCollaborator(u); err != nil {
  284. c.Handle(500, "AddCollaborator", err)
  285. return
  286. }
  287. if setting.Service.EnableNotifyMail {
  288. mailer.SendCollaboratorMail(models.NewMailerUser(u), models.NewMailerUser(c.User), models.NewMailerRepo(c.Repo.Repository))
  289. }
  290. c.Flash.Success(c.Tr("repo.settings.add_collaborator_success"))
  291. c.Redirect(setting.AppSubURL + c.Req.URL.Path)
  292. }
  293. func ChangeCollaborationAccessMode(c *context.Context) {
  294. if err := c.Repo.Repository.ChangeCollaborationAccessMode(
  295. c.QueryInt64("uid"),
  296. models.AccessMode(c.QueryInt("mode"))); err != nil {
  297. log.Error(2, "ChangeCollaborationAccessMode: %v", err)
  298. return
  299. }
  300. c.Status(204)
  301. }
  302. func DeleteCollaboration(c *context.Context) {
  303. if err := c.Repo.Repository.DeleteCollaboration(c.QueryInt64("id")); err != nil {
  304. c.Flash.Error("DeleteCollaboration: " + err.Error())
  305. } else {
  306. c.Flash.Success(c.Tr("repo.settings.remove_collaborator_success"))
  307. }
  308. c.JSON(200, map[string]interface{}{
  309. "redirect": c.Repo.RepoLink + "/settings/collaboration",
  310. })
  311. }
  312. func SettingsBranches(c *context.Context) {
  313. c.Data["Title"] = c.Tr("repo.settings.branches")
  314. c.Data["PageIsSettingsBranches"] = true
  315. if c.Repo.Repository.IsBare {
  316. c.Flash.Info(c.Tr("repo.settings.branches_bare"), true)
  317. c.HTML(200, SETTINGS_BRANCHES)
  318. return
  319. }
  320. protectBranches, err := models.GetProtectBranchesByRepoID(c.Repo.Repository.ID)
  321. if err != nil {
  322. c.Handle(500, "GetProtectBranchesByRepoID", err)
  323. return
  324. }
  325. // Filter out deleted branches
  326. branches := make([]string, 0, len(protectBranches))
  327. for i := range protectBranches {
  328. if c.Repo.GitRepo.IsBranchExist(protectBranches[i].Name) {
  329. branches = append(branches, protectBranches[i].Name)
  330. }
  331. }
  332. c.Data["ProtectBranches"] = branches
  333. c.HTML(200, SETTINGS_BRANCHES)
  334. }
  335. func UpdateDefaultBranch(c *context.Context) {
  336. branch := c.Query("branch")
  337. if c.Repo.GitRepo.IsBranchExist(branch) &&
  338. c.Repo.Repository.DefaultBranch != branch {
  339. c.Repo.Repository.DefaultBranch = branch
  340. if err := c.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  341. if !git.IsErrUnsupportedVersion(err) {
  342. c.Handle(500, "SetDefaultBranch", err)
  343. return
  344. }
  345. c.Flash.Warning(c.Tr("repo.settings.update_default_branch_unsupported"))
  346. c.Redirect(c.Repo.RepoLink + "/settings/branches")
  347. return
  348. }
  349. }
  350. if err := models.UpdateRepository(c.Repo.Repository, false); err != nil {
  351. c.Handle(500, "UpdateRepository", err)
  352. return
  353. }
  354. c.Flash.Success(c.Tr("repo.settings.update_default_branch_success"))
  355. c.Redirect(c.Repo.RepoLink + "/settings/branches")
  356. }
  357. func SettingsProtectedBranch(c *context.Context) {
  358. branch := c.Params("*")
  359. if !c.Repo.GitRepo.IsBranchExist(branch) {
  360. c.NotFound()
  361. return
  362. }
  363. c.Data["Title"] = c.Tr("repo.settings.protected_branches") + " - " + branch
  364. c.Data["PageIsSettingsBranches"] = true
  365. protectBranch, err := models.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch)
  366. if err != nil {
  367. if !models.IsErrBranchNotExist(err) {
  368. c.Handle(500, "GetProtectBranchOfRepoByName", err)
  369. return
  370. }
  371. // No options found, create defaults.
  372. protectBranch = &models.ProtectBranch{
  373. Name: branch,
  374. }
  375. }
  376. if c.Repo.Owner.IsOrganization() {
  377. users, err := c.Repo.Repository.GetWriters()
  378. if err != nil {
  379. c.Handle(500, "Repo.Repository.GetPushers", err)
  380. return
  381. }
  382. c.Data["Users"] = users
  383. c.Data["whitelist_users"] = protectBranch.WhitelistUserIDs
  384. teams, err := c.Repo.Owner.TeamsHaveAccessToRepo(c.Repo.Repository.ID, models.ACCESS_MODE_WRITE)
  385. if err != nil {
  386. c.Handle(500, "Repo.Owner.TeamsHaveAccessToRepo", err)
  387. return
  388. }
  389. c.Data["Teams"] = teams
  390. c.Data["whitelist_teams"] = protectBranch.WhitelistTeamIDs
  391. }
  392. c.Data["Branch"] = protectBranch
  393. c.HTML(200, SETTINGS_PROTECTED_BRANCH)
  394. }
  395. func SettingsProtectedBranchPost(c *context.Context, f form.ProtectBranch) {
  396. branch := c.Params("*")
  397. if !c.Repo.GitRepo.IsBranchExist(branch) {
  398. c.NotFound()
  399. return
  400. }
  401. protectBranch, err := models.GetProtectBranchOfRepoByName(c.Repo.Repository.ID, branch)
  402. if err != nil {
  403. if !models.IsErrBranchNotExist(err) {
  404. c.Handle(500, "GetProtectBranchOfRepoByName", err)
  405. return
  406. }
  407. // No options found, create defaults.
  408. protectBranch = &models.ProtectBranch{
  409. RepoID: c.Repo.Repository.ID,
  410. Name: branch,
  411. }
  412. }
  413. protectBranch.Protected = f.Protected
  414. protectBranch.RequirePullRequest = f.RequirePullRequest
  415. protectBranch.EnableWhitelist = f.EnableWhitelist
  416. if c.Repo.Owner.IsOrganization() {
  417. err = models.UpdateOrgProtectBranch(c.Repo.Repository, protectBranch, f.WhitelistUsers, f.WhitelistTeams)
  418. } else {
  419. err = models.UpdateProtectBranch(protectBranch)
  420. }
  421. if err != nil {
  422. c.Handle(500, "UpdateOrgProtectBranch/UpdateProtectBranch", err)
  423. return
  424. }
  425. c.Flash.Success(c.Tr("repo.settings.update_protect_branch_success"))
  426. c.Redirect(fmt.Sprintf("%s/settings/branches/%s", c.Repo.RepoLink, branch))
  427. }
  428. func SettingsGitHooks(c *context.Context) {
  429. c.Data["Title"] = c.Tr("repo.settings.githooks")
  430. c.Data["PageIsSettingsGitHooks"] = true
  431. hooks, err := c.Repo.GitRepo.Hooks()
  432. if err != nil {
  433. c.Handle(500, "Hooks", err)
  434. return
  435. }
  436. c.Data["Hooks"] = hooks
  437. c.HTML(200, SETTINGS_GITHOOKS)
  438. }
  439. func SettingsGitHooksEdit(c *context.Context) {
  440. c.Data["Title"] = c.Tr("repo.settings.githooks")
  441. c.Data["PageIsSettingsGitHooks"] = true
  442. c.Data["RequireSimpleMDE"] = true
  443. name := c.Params(":name")
  444. hook, err := c.Repo.GitRepo.GetHook(name)
  445. if err != nil {
  446. if err == git.ErrNotValidHook {
  447. c.Handle(404, "GetHook", err)
  448. } else {
  449. c.Handle(500, "GetHook", err)
  450. }
  451. return
  452. }
  453. c.Data["Hook"] = hook
  454. c.HTML(200, SETTINGS_GITHOOK_EDIT)
  455. }
  456. func SettingsGitHooksEditPost(c *context.Context) {
  457. name := c.Params(":name")
  458. hook, err := c.Repo.GitRepo.GetHook(name)
  459. if err != nil {
  460. if err == git.ErrNotValidHook {
  461. c.Handle(404, "GetHook", err)
  462. } else {
  463. c.Handle(500, "GetHook", err)
  464. }
  465. return
  466. }
  467. hook.Content = c.Query("content")
  468. if err = hook.Update(); err != nil {
  469. c.Handle(500, "hook.Update", err)
  470. return
  471. }
  472. c.Redirect(c.Data["Link"].(string))
  473. }
  474. func SettingsDeployKeys(c *context.Context) {
  475. c.Data["Title"] = c.Tr("repo.settings.deploy_keys")
  476. c.Data["PageIsSettingsKeys"] = true
  477. keys, err := models.ListDeployKeys(c.Repo.Repository.ID)
  478. if err != nil {
  479. c.Handle(500, "ListDeployKeys", err)
  480. return
  481. }
  482. c.Data["Deploykeys"] = keys
  483. c.HTML(200, SETTINGS_DEPLOY_KEYS)
  484. }
  485. func SettingsDeployKeysPost(c *context.Context, f form.AddSSHKey) {
  486. c.Data["Title"] = c.Tr("repo.settings.deploy_keys")
  487. c.Data["PageIsSettingsKeys"] = true
  488. keys, err := models.ListDeployKeys(c.Repo.Repository.ID)
  489. if err != nil {
  490. c.Handle(500, "ListDeployKeys", err)
  491. return
  492. }
  493. c.Data["Deploykeys"] = keys
  494. if c.HasError() {
  495. c.HTML(200, SETTINGS_DEPLOY_KEYS)
  496. return
  497. }
  498. content, err := models.CheckPublicKeyString(f.Content)
  499. if err != nil {
  500. if models.IsErrKeyUnableVerify(err) {
  501. c.Flash.Info(c.Tr("form.unable_verify_ssh_key"))
  502. } else {
  503. c.Data["HasError"] = true
  504. c.Data["Err_Content"] = true
  505. c.Flash.Error(c.Tr("form.invalid_ssh_key", err.Error()))
  506. c.Redirect(c.Repo.RepoLink + "/settings/keys")
  507. return
  508. }
  509. }
  510. key, err := models.AddDeployKey(c.Repo.Repository.ID, f.Title, content)
  511. if err != nil {
  512. c.Data["HasError"] = true
  513. switch {
  514. case models.IsErrKeyAlreadyExist(err):
  515. c.Data["Err_Content"] = true
  516. c.RenderWithErr(c.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &f)
  517. case models.IsErrKeyNameAlreadyUsed(err):
  518. c.Data["Err_Title"] = true
  519. c.RenderWithErr(c.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &f)
  520. default:
  521. c.Handle(500, "AddDeployKey", err)
  522. }
  523. return
  524. }
  525. log.Trace("Deploy key added: %d", c.Repo.Repository.ID)
  526. c.Flash.Success(c.Tr("repo.settings.add_key_success", key.Name))
  527. c.Redirect(c.Repo.RepoLink + "/settings/keys")
  528. }
  529. func DeleteDeployKey(c *context.Context) {
  530. if err := models.DeleteDeployKey(c.User, c.QueryInt64("id")); err != nil {
  531. c.Flash.Error("DeleteDeployKey: " + err.Error())
  532. } else {
  533. c.Flash.Success(c.Tr("repo.settings.deploy_key_deletion_success"))
  534. }
  535. c.JSON(200, map[string]interface{}{
  536. "redirect": c.Repo.RepoLink + "/settings/keys",
  537. })
  538. }