setting.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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/base"
  13. "github.com/gogits/gogs/modules/context"
  14. "github.com/gogits/gogs/modules/form"
  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, f form.RepoSetting) {
  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 := 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(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, &f)
  53. case models.IsErrNameReserved(err):
  54. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &f)
  55. case models.IsErrNamePatternNotAllowed(err):
  56. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &f)
  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 = 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. 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 f.Interval > 0 {
  93. ctx.Repo.Mirror.EnablePrune = f.EnablePrune
  94. ctx.Repo.Mirror.Interval = f.Interval
  95. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(f.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(f.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 = f.EnableWiki
  117. repo.EnableExternalWiki = f.EnableExternalWiki
  118. repo.ExternalWikiURL = f.ExternalWikiURL
  119. repo.EnableIssues = f.EnableIssues
  120. repo.EnableExternalTracker = f.EnableExternalTracker
  121. repo.ExternalTrackerURL = f.ExternalTrackerURL
  122. repo.ExternalTrackerFormat = f.TrackerURLFormat
  123. repo.ExternalTrackerStyle = f.TrackerIssueStyle
  124. repo.EnablePulls = f.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 != f.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 != f.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 != f.RepoName {
  203. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  204. return
  205. }
  206. if ctx.Repo.Owner.IsOrganization() && !ctx.User.IsAdmin {
  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 != f.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. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  281. ctx.Handle(500, "AddCollaborator", err)
  282. return
  283. }
  284. if setting.Service.EnableNotifyMail {
  285. mailer.SendCollaboratorMail(models.NewMailerUser(u), models.NewMailerUser(ctx.User), models.NewMailerRepo(ctx.Repo.Repository))
  286. }
  287. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  288. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  289. }
  290. func ChangeCollaborationAccessMode(ctx *context.Context) {
  291. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  292. ctx.QueryInt64("uid"),
  293. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  294. log.Error(2, "ChangeCollaborationAccessMode: %v", err)
  295. return
  296. }
  297. ctx.Status(204)
  298. }
  299. func DeleteCollaboration(ctx *context.Context) {
  300. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  301. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  302. } else {
  303. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  304. }
  305. ctx.JSON(200, map[string]interface{}{
  306. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  307. })
  308. }
  309. func SettingsBranches(ctx *context.Context) {
  310. ctx.Data["Title"] = ctx.Tr("repo.settings.branches")
  311. ctx.Data["PageIsSettingsBranches"] = true
  312. protectBranches, err := models.GetProtectBranchesByRepoID(ctx.Repo.Repository.ID)
  313. if err != nil {
  314. ctx.Handle(500, "GetProtectBranchesByRepoID", err)
  315. return
  316. }
  317. ctx.Data["ProtectBranches"] = protectBranches
  318. ctx.HTML(200, SETTINGS_BRANCHES)
  319. }
  320. func UpdateDefaultBranch(ctx *context.Context) {
  321. branch := ctx.Query("branch")
  322. if ctx.Repo.GitRepo.IsBranchExist(branch) &&
  323. ctx.Repo.Repository.DefaultBranch != branch {
  324. ctx.Repo.Repository.DefaultBranch = branch
  325. if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
  326. if !git.IsErrUnsupportedVersion(err) {
  327. ctx.Handle(500, "SetDefaultBranch", err)
  328. return
  329. }
  330. }
  331. }
  332. if err := models.UpdateRepository(ctx.Repo.Repository, false); err != nil {
  333. ctx.Handle(500, "UpdateRepository", err)
  334. return
  335. }
  336. ctx.Flash.Success(ctx.Tr("repo.settings.update_default_branch_success"))
  337. ctx.Redirect(ctx.Repo.RepoLink + "/settings/branches")
  338. }
  339. func SettingsProtectedBranch(ctx *context.Context) {
  340. branch := ctx.Params("*")
  341. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  342. ctx.NotFound()
  343. return
  344. }
  345. ctx.Data["Title"] = ctx.Tr("repo.settings.protected_branches") + " - " + branch
  346. ctx.Data["PageIsSettingsBranches"] = true
  347. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  348. if err != nil {
  349. if !models.IsErrBranchNotExist(err) {
  350. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  351. return
  352. }
  353. // No options found, create defaults.
  354. protectBranch = &models.ProtectBranch{
  355. Name: branch,
  356. }
  357. }
  358. if ctx.Repo.Owner.IsOrganization() {
  359. users, err := ctx.Repo.Repository.GetWriters()
  360. if err != nil {
  361. ctx.Handle(500, "Repo.Repository.GetPushers", err)
  362. return
  363. }
  364. ctx.Data["Users"] = users
  365. ctx.Data["whitelist_users"] = protectBranch.WhitelistUserIDs
  366. teams, err := ctx.Repo.Owner.TeamsHaveAccessToRepo(ctx.Repo.Repository.ID, models.ACCESS_MODE_WRITE)
  367. if err != nil {
  368. ctx.Handle(500, "Repo.Owner.TeamsHaveAccessToRepo", err)
  369. return
  370. }
  371. ctx.Data["Teams"] = teams
  372. ctx.Data["whitelist_teams"] = protectBranch.WhitelistTeamIDs
  373. }
  374. ctx.Data["Branch"] = protectBranch
  375. ctx.HTML(200, SETTINGS_PROTECTED_BRANCH)
  376. }
  377. func SettingsProtectedBranchPost(ctx *context.Context, f form.ProtectBranch) {
  378. branch := ctx.Params("*")
  379. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  380. ctx.NotFound()
  381. return
  382. }
  383. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  384. if err != nil {
  385. if !models.IsErrBranchNotExist(err) {
  386. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  387. return
  388. }
  389. // No options found, create defaults.
  390. protectBranch = &models.ProtectBranch{
  391. RepoID: ctx.Repo.Repository.ID,
  392. Name: branch,
  393. }
  394. }
  395. protectBranch.Protected = f.Protected
  396. protectBranch.RequirePullRequest = f.RequirePullRequest
  397. protectBranch.EnableWhitelist = f.EnableWhitelist
  398. if ctx.Repo.Owner.IsOrganization() {
  399. err = models.UpdateOrgProtectBranch(ctx.Repo.Repository, protectBranch, f.WhitelistUsers, f.WhitelistTeams)
  400. } else {
  401. err = models.UpdateProtectBranch(protectBranch)
  402. }
  403. if err != nil {
  404. ctx.Handle(500, "UpdateOrgProtectBranch/UpdateProtectBranch", err)
  405. return
  406. }
  407. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success"))
  408. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  409. }
  410. func SettingsGitHooks(ctx *context.Context) {
  411. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  412. ctx.Data["PageIsSettingsGitHooks"] = true
  413. hooks, err := ctx.Repo.GitRepo.Hooks()
  414. if err != nil {
  415. ctx.Handle(500, "Hooks", err)
  416. return
  417. }
  418. ctx.Data["Hooks"] = hooks
  419. ctx.HTML(200, SETTINGS_GITHOOKS)
  420. }
  421. func SettingsGitHooksEdit(ctx *context.Context) {
  422. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  423. ctx.Data["PageIsSettingsGitHooks"] = true
  424. name := ctx.Params(":name")
  425. hook, err := ctx.Repo.GitRepo.GetHook(name)
  426. if err != nil {
  427. if err == git.ErrNotValidHook {
  428. ctx.Handle(404, "GetHook", err)
  429. } else {
  430. ctx.Handle(500, "GetHook", err)
  431. }
  432. return
  433. }
  434. ctx.Data["Hook"] = hook
  435. ctx.HTML(200, SETTINGS_GITHOOK_EDIT)
  436. }
  437. func SettingsGitHooksEditPost(ctx *context.Context) {
  438. name := ctx.Params(":name")
  439. hook, err := ctx.Repo.GitRepo.GetHook(name)
  440. if err != nil {
  441. if err == git.ErrNotValidHook {
  442. ctx.Handle(404, "GetHook", err)
  443. } else {
  444. ctx.Handle(500, "GetHook", err)
  445. }
  446. return
  447. }
  448. hook.Content = ctx.Query("content")
  449. if err = hook.Update(); err != nil {
  450. ctx.Handle(500, "hook.Update", err)
  451. return
  452. }
  453. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  454. }
  455. func SettingsDeployKeys(ctx *context.Context) {
  456. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  457. ctx.Data["PageIsSettingsKeys"] = true
  458. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  459. if err != nil {
  460. ctx.Handle(500, "ListDeployKeys", err)
  461. return
  462. }
  463. ctx.Data["Deploykeys"] = keys
  464. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  465. }
  466. func SettingsDeployKeysPost(ctx *context.Context, f form.AddSSHKey) {
  467. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  468. ctx.Data["PageIsSettingsKeys"] = true
  469. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  470. if err != nil {
  471. ctx.Handle(500, "ListDeployKeys", err)
  472. return
  473. }
  474. ctx.Data["Deploykeys"] = keys
  475. if ctx.HasError() {
  476. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  477. return
  478. }
  479. content, err := models.CheckPublicKeyString(f.Content)
  480. if err != nil {
  481. if models.IsErrKeyUnableVerify(err) {
  482. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  483. } else {
  484. ctx.Data["HasError"] = true
  485. ctx.Data["Err_Content"] = true
  486. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  487. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  488. return
  489. }
  490. }
  491. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, f.Title, content)
  492. if err != nil {
  493. ctx.Data["HasError"] = true
  494. switch {
  495. case models.IsErrKeyAlreadyExist(err):
  496. ctx.Data["Err_Content"] = true
  497. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &f)
  498. case models.IsErrKeyNameAlreadyUsed(err):
  499. ctx.Data["Err_Title"] = true
  500. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &f)
  501. default:
  502. ctx.Handle(500, "AddDeployKey", err)
  503. }
  504. return
  505. }
  506. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  507. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  508. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  509. }
  510. func DeleteDeployKey(ctx *context.Context) {
  511. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  512. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  513. } else {
  514. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  515. }
  516. ctx.JSON(200, map[string]interface{}{
  517. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  518. })
  519. }