setting.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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() && !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 != 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. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  354. if err != nil {
  355. if !models.IsErrBranchNotExist(err) {
  356. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  357. return
  358. }
  359. // No options found, create defaults.
  360. protectBranch = &models.ProtectBranch{
  361. Name: branch,
  362. }
  363. }
  364. if ctx.Repo.Owner.IsOrganization() {
  365. users, err := ctx.Repo.Repository.GetWriters()
  366. if err != nil {
  367. ctx.Handle(500, "Repo.Repository.GetPushers", err)
  368. return
  369. }
  370. ctx.Data["Users"] = users
  371. ctx.Data["whitelist_users"] = protectBranch.WhitelistUserIDs
  372. if err = ctx.Repo.Owner.GetTeams(); err != nil {
  373. ctx.Handle(500, "Repo.Owner.GetTeams", err)
  374. return
  375. }
  376. ctx.Data["Teams"] = ctx.Repo.Owner.Teams
  377. ctx.Data["whitelist_teams"] = protectBranch.WhitelistTeamIDs
  378. }
  379. ctx.Data["Branch"] = protectBranch
  380. ctx.HTML(200, SETTINGS_PROTECTED_BRANCH)
  381. }
  382. func SettingsProtectedBranchPost(ctx *context.Context, form auth.ProtectBranchForm) {
  383. branch := ctx.Params("*")
  384. if !ctx.Repo.GitRepo.IsBranchExist(branch) {
  385. ctx.NotFound()
  386. return
  387. }
  388. protectBranch, err := models.GetProtectBranchOfRepoByName(ctx.Repo.Repository.ID, branch)
  389. if err != nil {
  390. if !models.IsErrBranchNotExist(err) {
  391. ctx.Handle(500, "GetProtectBranchOfRepoByName", err)
  392. return
  393. }
  394. // No options found, create defaults.
  395. protectBranch = &models.ProtectBranch{
  396. RepoID: ctx.Repo.Repository.ID,
  397. Name: branch,
  398. }
  399. }
  400. protectBranch.Protected = form.Protected
  401. protectBranch.RequirePullRequest = form.RequirePullRequest
  402. protectBranch.EnableWhitelist = form.EnableWhitelist
  403. if ctx.Repo.Owner.IsOrganization() {
  404. err = models.UpdateOrgProtectBranch(ctx.Repo.Repository, protectBranch, form.WhitelistUsers, form.WhitelistTeams)
  405. } else {
  406. err = models.UpdateProtectBranch(protectBranch)
  407. }
  408. if err != nil {
  409. ctx.Handle(500, "UpdateOrgProtectBranch/UpdateProtectBranch", err)
  410. return
  411. }
  412. ctx.Flash.Success(ctx.Tr("repo.settings.update_protect_branch_success"))
  413. ctx.Redirect(fmt.Sprintf("%s/settings/branches/%s", ctx.Repo.RepoLink, branch))
  414. }
  415. func SettingsGitHooks(ctx *context.Context) {
  416. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  417. ctx.Data["PageIsSettingsGitHooks"] = true
  418. hooks, err := ctx.Repo.GitRepo.Hooks()
  419. if err != nil {
  420. ctx.Handle(500, "Hooks", err)
  421. return
  422. }
  423. ctx.Data["Hooks"] = hooks
  424. ctx.HTML(200, SETTINGS_GITHOOKS)
  425. }
  426. func SettingsGitHooksEdit(ctx *context.Context) {
  427. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  428. ctx.Data["PageIsSettingsGitHooks"] = true
  429. name := ctx.Params(":name")
  430. hook, err := ctx.Repo.GitRepo.GetHook(name)
  431. if err != nil {
  432. if err == git.ErrNotValidHook {
  433. ctx.Handle(404, "GetHook", err)
  434. } else {
  435. ctx.Handle(500, "GetHook", err)
  436. }
  437. return
  438. }
  439. ctx.Data["Hook"] = hook
  440. ctx.HTML(200, SETTINGS_GITHOOK_EDIT)
  441. }
  442. func SettingsGitHooksEditPost(ctx *context.Context) {
  443. name := ctx.Params(":name")
  444. hook, err := ctx.Repo.GitRepo.GetHook(name)
  445. if err != nil {
  446. if err == git.ErrNotValidHook {
  447. ctx.Handle(404, "GetHook", err)
  448. } else {
  449. ctx.Handle(500, "GetHook", err)
  450. }
  451. return
  452. }
  453. hook.Content = ctx.Query("content")
  454. if err = hook.Update(); err != nil {
  455. ctx.Handle(500, "hook.Update", err)
  456. return
  457. }
  458. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  459. }
  460. func SettingsDeployKeys(ctx *context.Context) {
  461. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  462. ctx.Data["PageIsSettingsKeys"] = true
  463. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  464. if err != nil {
  465. ctx.Handle(500, "ListDeployKeys", err)
  466. return
  467. }
  468. ctx.Data["Deploykeys"] = keys
  469. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  470. }
  471. func SettingsDeployKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  472. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  473. ctx.Data["PageIsSettingsKeys"] = true
  474. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  475. if err != nil {
  476. ctx.Handle(500, "ListDeployKeys", err)
  477. return
  478. }
  479. ctx.Data["Deploykeys"] = keys
  480. if ctx.HasError() {
  481. ctx.HTML(200, SETTINGS_DEPLOY_KEYS)
  482. return
  483. }
  484. content, err := models.CheckPublicKeyString(form.Content)
  485. if err != nil {
  486. if models.IsErrKeyUnableVerify(err) {
  487. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  488. } else {
  489. ctx.Data["HasError"] = true
  490. ctx.Data["Err_Content"] = true
  491. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  492. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  493. return
  494. }
  495. }
  496. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  497. if err != nil {
  498. ctx.Data["HasError"] = true
  499. switch {
  500. case models.IsErrKeyAlreadyExist(err):
  501. ctx.Data["Err_Content"] = true
  502. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), SETTINGS_DEPLOY_KEYS, &form)
  503. case models.IsErrKeyNameAlreadyUsed(err):
  504. ctx.Data["Err_Title"] = true
  505. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), SETTINGS_DEPLOY_KEYS, &form)
  506. default:
  507. ctx.Handle(500, "AddDeployKey", err)
  508. }
  509. return
  510. }
  511. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  512. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  513. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  514. }
  515. func DeleteDeployKey(ctx *context.Context) {
  516. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  517. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  518. } else {
  519. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  520. }
  521. ctx.JSON(200, map[string]interface{}{
  522. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  523. })
  524. }