setting.go 17 KB

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